github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/object/metadata.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package object
     4  
     5  import (
     6  	etime "../earth-time"
     7  	"../protocol"
     8  	"../syllab"
     9  )
    10  
    11  const MetadataLength uint32 = 64
    12  
    13  // Metadata is the header structure of an object!
    14  type Metadata []byte
    15  
    16  // methods to implements protocol.ObjectMetadata interface
    17  func (md Metadata) ID() (id [32]byte)        { copy(id[:], md[0:]); return }
    18  func (md Metadata) WriteTime() protocol.Time { return etime.Time(syllab.GetInt64(md, 32)) }
    19  func (md Metadata) MediaTypeID() uint64      { return syllab.GetUInt64(md, 40) }
    20  func (md Metadata) CompressTypeID() uint64   { return syllab.GetUInt64(md, 48) }
    21  func (md Metadata) DataLength() uint64       { return syllab.GetUInt64(md, 56) }
    22  
    23  func (md Metadata) setID(id [32]byte)                       { copy(md[0:], id[:]) }
    24  func (md Metadata) setWriteTime(writeTime etime.Time)       { syllab.SetInt64(md, 32, int64(writeTime)) }
    25  func (md Metadata) setMediaTypeID(mediaTypeID uint64)       { syllab.SetUInt64(md, 40, mediaTypeID) }
    26  func (md Metadata) setCompressTypeID(compressTypeID uint64) { syllab.SetUInt64(md, 48, compressTypeID) }
    27  func (md Metadata) setDataLength(dataLength int)            { syllab.SetUInt64(md, 56, uint64(dataLength)) }
    28  
    29  // methods to implements protocol.Syllab interface
    30  func (md Metadata) CheckSyllab(payload []byte) (err protocol.Error) {
    31  	if len(payload) < int(md.LenOfSyllabStack()) {
    32  		err = syllab.ErrShortArrayDecode
    33  	}
    34  	return
    35  }
    36  func (md Metadata) FromSyllab(payload []byte, stackIndex uint32) {
    37  	// err = ErrSourceNotChangeable
    38  }
    39  func (md Metadata) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
    40  	freeHeapIndex = syllab.SetByteArray(payload, md, stackIndex, heapIndex)
    41  	return
    42  }
    43  func (md Metadata) LenAsSyllab() uint64          { return uint64(md.LenOfSyllabStack() + md.LenOfSyllabHeap()) }
    44  func (md Metadata) LenOfSyllabStack() uint32     { return 8 }
    45  func (md Metadata) LenOfSyllabHeap() (ln uint32) { ln = uint32(len(md)); return }
    46  
    47  /*
    48  ********** metadata structure **********
    49   */
    50  
    51  type metadata struct {
    52  	// Describe the object
    53  	id        [32]byte
    54  	writeTime etime.Time
    55  
    56  	// Describe the object data
    57  	mediaTypeID    uint64
    58  	compressTypeID uint64
    59  	dataLength     uint64
    60  }
    61  
    62  // methods to implements protocol.ObjectMetadata interface
    63  func (md *metadata) ID() [32]byte             { return md.id }
    64  func (md *metadata) WriteTime() protocol.Time { return md.writeTime }
    65  func (md *metadata) MediaTypeID() uint64      { return md.mediaTypeID }
    66  func (md *metadata) CompressTypeID() uint64   { return md.compressTypeID }
    67  func (md *metadata) DataLength() uint64       { return md.dataLength }
    68  
    69  // methods to implements protocol.Syllab interface
    70  func (md *metadata) CheckSyllab(payload []byte) (err protocol.Error) {
    71  	if len(payload) < int(md.LenOfSyllabStack()) {
    72  		err = syllab.ErrShortArrayDecode
    73  	}
    74  	return
    75  }
    76  func (md *metadata) FromSyllab(buf []byte, stackIndex uint32) {
    77  	copy(md.id[:], buf[0:])
    78  	md.writeTime = etime.Time(syllab.GetInt64(buf, 32))
    79  
    80  	md.mediaTypeID = syllab.GetUInt64(buf, 40)
    81  	md.compressTypeID = syllab.GetUInt64(buf, 48)
    82  	md.dataLength = syllab.GetUInt64(buf, 56)
    83  }
    84  func (md *metadata) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
    85  	copy(payload[0:], md.id[:])
    86  	syllab.SetInt64(payload, 32, int64(md.writeTime))
    87  
    88  	syllab.SetUInt64(payload, 40, md.mediaTypeID)
    89  	syllab.SetUInt64(payload, 48, md.compressTypeID)
    90  	syllab.SetUInt64(payload, 56, md.dataLength)
    91  	return heapIndex
    92  }
    93  func (md *metadata) LenOfSyllabStack() uint32     { return MetadataLength }
    94  func (md *metadata) LenOfSyllabHeap() (ln uint32) { return }
    95  func (md *metadata) LenAsSyllab() uint64          { return uint64(MetadataLength) }