|
|
@ -28,7 +28,7 @@ func (n *Needle) DiskSize(version Version) int64 { |
|
|
|
return GetActualSize(n.Size, version) |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, error) { |
|
|
|
func (n *Needle) prepareWriteBuffer(version Version) ([]byte, Size, int64, error) { |
|
|
|
|
|
|
|
writeBytes := make([]byte, 0) |
|
|
|
|
|
|
@ -37,8 +37,8 @@ func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, err |
|
|
|
header := make([]byte, NeedleHeaderSize) |
|
|
|
CookieToBytes(header[0:CookieSize], n.Cookie) |
|
|
|
NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id) |
|
|
|
n.Size = uint32(len(n.Data)) |
|
|
|
util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size) |
|
|
|
n.Size = Size(len(n.Data)) |
|
|
|
SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size) |
|
|
|
size := n.Size |
|
|
|
actualSize := NeedleHeaderSize + int64(n.Size) |
|
|
|
writeBytes = append(writeBytes, header...) |
|
|
@ -58,12 +58,12 @@ func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, err |
|
|
|
} |
|
|
|
n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime)) |
|
|
|
if n.DataSize > 0 { |
|
|
|
n.Size = 4 + n.DataSize + 1 |
|
|
|
n.Size = 4 + Size(n.DataSize) + 1 |
|
|
|
if n.HasName() { |
|
|
|
n.Size = n.Size + 1 + uint32(n.NameSize) |
|
|
|
n.Size = n.Size + 1 + Size(n.NameSize) |
|
|
|
} |
|
|
|
if n.HasMime() { |
|
|
|
n.Size = n.Size + 1 + uint32(n.MimeSize) |
|
|
|
n.Size = n.Size + 1 + Size(n.MimeSize) |
|
|
|
} |
|
|
|
if n.HasLastModifiedDate() { |
|
|
|
n.Size = n.Size + LastModifiedBytesLength |
|
|
@ -72,12 +72,12 @@ func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, err |
|
|
|
n.Size = n.Size + TtlBytesLength |
|
|
|
} |
|
|
|
if n.HasPairs() { |
|
|
|
n.Size += 2 + uint32(n.PairsSize) |
|
|
|
n.Size += 2 + Size(n.PairsSize) |
|
|
|
} |
|
|
|
} else { |
|
|
|
n.Size = 0 |
|
|
|
} |
|
|
|
util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size) |
|
|
|
SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size) |
|
|
|
writeBytes = append(writeBytes, header[0:NeedleHeaderSize]...) |
|
|
|
if n.DataSize > 0 { |
|
|
|
util.Uint32toBytes(header[0:4], n.DataSize) |
|
|
@ -119,13 +119,13 @@ func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, err |
|
|
|
writeBytes = append(writeBytes, header[0:NeedleChecksumSize+TimestampSize+padding]...) |
|
|
|
} |
|
|
|
|
|
|
|
return writeBytes, n.DataSize, GetActualSize(n.Size, version), nil |
|
|
|
return writeBytes, Size(n.DataSize), GetActualSize(n.Size, version), nil |
|
|
|
} |
|
|
|
|
|
|
|
return writeBytes, 0, 0, fmt.Errorf("Unsupported Version! (%d)", version) |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size uint32, actualSize int64, err error) { |
|
|
|
func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size Size, actualSize int64, err error) { |
|
|
|
|
|
|
|
if end, _, e := w.GetStat(); e == nil { |
|
|
|
defer func(w backend.BackendStorageFile, off int64) { |
|
|
@ -154,7 +154,7 @@ func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset u |
|
|
|
return offset, size, actualSize, err |
|
|
|
} |
|
|
|
|
|
|
|
func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size uint32, version Version) (dataSlice []byte, err error) { |
|
|
|
func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size Size, version Version) (dataSlice []byte, err error) { |
|
|
|
|
|
|
|
dataSize := GetActualSize(size, version) |
|
|
|
dataSlice = make([]byte, int(dataSize)) |
|
|
@ -165,7 +165,7 @@ func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size uint32, ver |
|
|
|
} |
|
|
|
|
|
|
|
// ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
|
|
|
|
func (n *Needle) ReadBytes(bytes []byte, offset int64, size uint32, version Version) (err error) { |
|
|
|
func (n *Needle) ReadBytes(bytes []byte, offset int64, size Size, version Version) (err error) { |
|
|
|
n.ParseNeedleHeader(bytes) |
|
|
|
if n.Size != size { |
|
|
|
return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size) |
|
|
@ -195,7 +195,7 @@ func (n *Needle) ReadBytes(bytes []byte, offset int64, size uint32, version Vers |
|
|
|
} |
|
|
|
|
|
|
|
// ReadData hydrates the needle from the file, with only n.Id is set.
|
|
|
|
func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size uint32, version Version) (err error) { |
|
|
|
func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) { |
|
|
|
bytes, err := ReadNeedleBlob(r, offset, size, version) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
@ -206,7 +206,7 @@ func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size uint3 |
|
|
|
func (n *Needle) ParseNeedleHeader(bytes []byte) { |
|
|
|
n.Cookie = BytesToCookie(bytes[0:CookieSize]) |
|
|
|
n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize]) |
|
|
|
n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize]) |
|
|
|
n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize]) |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) { |
|
|
@ -288,7 +288,7 @@ func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int6 |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func PaddingLength(needleSize uint32, version Version) uint32 { |
|
|
|
func PaddingLength(needleSize Size, version Version) Size { |
|
|
|
if version == Version3 { |
|
|
|
// this is same value as version2, but just listed here for clarity
|
|
|
|
return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize) |
|
|
@ -296,7 +296,7 @@ func PaddingLength(needleSize uint32, version Version) uint32 { |
|
|
|
return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize) |
|
|
|
} |
|
|
|
|
|
|
|
func NeedleBodyLength(needleSize uint32, version Version) int64 { |
|
|
|
func NeedleBodyLength(needleSize Size, version Version) int64 { |
|
|
|
if version == Version3 { |
|
|
|
return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version)) |
|
|
|
} |
|
|
@ -390,6 +390,6 @@ func (n *Needle) SetHasPairs() { |
|
|
|
n.Flags = n.Flags | FlagHasPairs |
|
|
|
} |
|
|
|
|
|
|
|
func GetActualSize(size uint32, version Version) int64 { |
|
|
|
func GetActualSize(size Size, version Version) int64 { |
|
|
|
return NeedleHeaderSize + NeedleBodyLength(size, version) |
|
|
|
} |
xxxxxxxxxx