Skip to content

Commit

Permalink
fstree: unify some combined-related constants
Browse files Browse the repository at this point in the history
Yeah, lengthy names, but it's better for future extensions.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Nov 26, 2024
1 parent 8ba4b50 commit b243841
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 16 additions & 13 deletions pkg/local_object_storage/blobstor/fstree/fstree.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ const (
// the system (and we usually don't have 15 fields). ZSTD magic is also
// different.
combinedPrefix = 0x7f

// combinedLenSize is sizeof(uint32), length is a serialized 32-bit BE integer.
combinedLenSize = 4

// combinedIDOff is the offset from the start of the combined prefix to OID.
combinedIDOff = 1

// combinedLengthOff is the offset from the start of the combined prefix to object length.
combinedLengthOff = combinedIDOff + oid.Size

// combinedDataOff is the offset from the start of the combined prefix to object data.
// It's also the length of the prefix in total.
combinedDataOff = combinedLengthOff + combinedLenSize
)

var _ common.Storage = (*FSTree)(nil)
Expand Down Expand Up @@ -353,18 +366,8 @@ func getRawObjectBytes(id oid.ID, p string) ([]byte, error) {
}

func extractCombinedObject(id oid.ID, f *os.File) ([]byte, error) {
const (
prefixSize = 1
idSize = sha256.Size
lengthSize = 4

idOff = prefixSize
lengthOff = idOff + idSize
dataOff = lengthOff + lengthSize
)

var (
comBuf [dataOff]byte
comBuf [combinedDataOff]byte
data []byte
isCombined bool
)
Expand Down Expand Up @@ -398,8 +401,8 @@ func extractCombinedObject(id oid.ID, f *os.File) ([]byte, error) {
return data, nil
}
isCombined = true
var l = binary.BigEndian.Uint32(comBuf[lengthOff:dataOff])
if bytes.Equal(comBuf[idOff:lengthOff], id[:]) {
var l = binary.BigEndian.Uint32(comBuf[combinedLengthOff:combinedDataOff])
if bytes.Equal(comBuf[combinedIDOff:combinedLengthOff], id[:]) {
data = make([]byte, l)
_, err = io.ReadFull(f, data)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func (b *syncBatch) wait() error {
func (b *syncBatch) write(id oid.ID, p string, data []byte) error {
var (
err error
pref [1 + len(id) + 4]byte
pref [combinedDataOff]byte
)
pref[0] = combinedPrefix
copy(pref[1:], id[:])
binary.BigEndian.PutUint32(pref[1+len(id):], uint32(len(data)))
copy(pref[combinedIDOff:], id[:])
binary.BigEndian.PutUint32(pref[combinedLengthOff:], uint32(len(data)))

n, err := unix.Writev(b.fd, [][]byte{pref[:], data})
if err != nil {
Expand Down

0 comments on commit b243841

Please sign in to comment.