Back to Seaweedfs

Needle Layout Format

weed/storage/needle/README.md

4.238.7 KB
Original Source

Needle Layout Format

This document describes the binary layout of the Needle structure as used in SeaweedFS storage, for all supported versions (v1, v2, v3).

A Needle represents a file or data blob stored in a volume file. The layout determines how the Needle is serialized to disk for efficient storage and retrieval.


Common Field Sizes

FieldSize (bytes)
Cookie4
NeedleId8
Size4
DataSize4
Flags1
NameSize1
MimeSize1
LastModified5
Ttl2
PairsSize2
Checksum4
Timestamp8

Needle Layouts by Version

Version 1

OffsetFieldSize (bytes)Description
0Cookie4Random number to mitigate brute force lookups
4Id8Needle ID
12Size4Length of Data
16DataNFile data (N = Size)
16+NChecksum4CRC32 of Data
20+NPadding0-7To align to 8 bytes

Version 2

OffsetFieldSize (bytes)Description
0Cookie4Random number
4Id8Needle ID
12Size4Total size of the following fields
16DataSize4Length of Data (N)
20DataNFile data
20+NFlags1Bit flags
21+NNameSize1 (opt)Optional, if present
22+NNameM (opt)Optional, if present (M = NameSize)
...MimeSize1 (opt)Optional, if present
...MimeK (opt)Optional, if present (K = MimeSize)
...LastModified5 (opt)Optional, if present
...Ttl2 (opt)Optional, if present
...PairsSize2 (opt)Optional, if present
...PairsP (opt)Optional, if present (P = PairsSize)
...Checksum4CRC32
...Padding0-7To align to 8 bytes

Version 3

OffsetFieldSize (bytes)Description
0Cookie4Random number
4Id8Needle ID
12Size4Total size of the following fields
16DataSize4Length of Data (N)
20DataNFile data
20+NFlags1Bit flags
21+NNameSize1 (opt)Optional, if present
22+NNameM (opt)Optional, if present (M = NameSize)
...MimeSize1 (opt)Optional, if present
...MimeK (opt)Optional, if present (K = MimeSize)
...LastModified5 (opt)Optional, if present
...Ttl2 (opt)Optional, if present
...PairsSize2 (opt)Optional, if present
...PairsP (opt)Optional, if present (P = PairsSize)
...Checksum4CRC32
...Timestamp8Append time in nanoseconds
...Padding0-7To align to 8 bytes
  • Offsets marked with ... depend on the presence and size of previous optional fields.
  • Fields marked (opt) are optional and only present if the corresponding size or flag is non-zero.
  • N = DataSize, M = NameSize, K = MimeSize, P = PairsSize.

Field Explanations

  • Cookie: 4 bytes, random value for security.
  • Id: 8 bytes, unique identifier for the Needle.
  • Size: 4 bytes, total size of the Needle data section (not including header, checksum, timestamp, or padding).
  • DataSize: 4 bytes, length of the Data field.
  • Data: File data (variable length).
  • Flags: 1 byte, bit flags for Needle properties.
  • NameSize/Name: 1 byte + variable, optional file name.
  • MimeSize/Mime: 1 byte + variable, optional MIME type.
  • LastModified: 5 bytes, optional last modified timestamp.
  • Ttl: 2 bytes, optional time-to-live.
  • PairsSize/Pairs: 2 bytes + variable, optional key-value pairs.
  • Checksum: 4 bytes, CRC32 checksum of the Needle data.
  • Timestamp: 8 bytes, append time (only in v3).
  • Padding: 0-7 bytes, to align the total Needle size to 8 bytes.

Version Comparison Table

Fieldv1v2v3
Cookie
Id
Size
DataSize
Data
Flags
NameSize/Name
MimeSize/Mime
LastModified
Ttl
PairsSize/Pairs
Checksum
Timestamp
Padding

Flags Field Details

The Flags field (present in v2 and v3) is a bitmask that encodes several boolean properties of the Needle. Each bit has a specific meaning:

Bit ValueNameMeaning
0x01FlagIsCompressedData is compressed (isCompressed)
0x02FlagHasNameName field is present (NameSize/Name)
0x04FlagHasMimeMime field is present (MimeSize/Mime)
0x08FlagHasLastModifiedDateLastModified field is present
0x10FlagHasTtlTtl field is present
0x20FlagHasPairsPairs field is present (PairsSize/Pairs)
0x80FlagIsChunkManifestData is a chunk manifest (for large files)
  • If a flag is set, the corresponding field(s) will appear in the Needle layout at the appropriate position.
  • The Flags field is always present in v2 and v3, immediately after the Data field.

Optional Fields

  • Fields marked as optional in the layout tables are only present if the corresponding flag in the Flags field is set (except for Name/Mime/Pairs, which also depend on their size fields being non-zero).
  • The order of optional fields is fixed and matches the order of their flags.

Special Notes

  • isCompressed: If set, the Data field is compressed (typically using gzip). This is indicated by the lowest bit (0x01) in the Flags byte.
  • isChunkManifest: If set, the Data field contains a manifest describing chunks of a large file, not raw file data.
  • All multi-byte fields are stored in big-endian order.
  • Padding is always added at the end to align the total Needle size to 8 bytes.
  • N = DataSize, M = NameSize, K = MimeSize, P = PairsSize in the layout tables above.

For more details, see the implementation in the corresponding Go files in this directory.