package lattice import "fmt" // Format constants for .iskr version 5, as iskradb/lattice/io.mx defines them. const ( Magic = "ISKR" Version = 5 NodeSize = 136 RecordSize = 48 HeaderSize = 72 PendingSize = 4 ) // Version4 is the previous C=8 layout: 128-bit SipHash keys in 200-byte blocks. // Records, flags and header are otherwise identical. // // Version 4 is the only older version this package reads. Version 3 and earlier // were C=3 with 64-byte blocks, a 3-bit branch flag and a 6-byte record header, // and claiming to read them would be false. Version 4 is not a header-only // difference either; see RegionCaps. const ( Version4 = 4 NodeSize4 = 200 ) // CompatibleVersion reports whether this package can parse version v. Version 4 // is not read by the page cache: ConvertToV5 translates it first. func CompatibleVersion(v uint16) bool { return v == Version } // Version-5 header offsets, as iskradb/lattice/io.mx writeHeaderBuf lays them out. const ( OffMagic = 0 OffVersion = 4 OffNCount = 6 OffRCount = 10 OffPendCount = 14 OffRoots = 18 OffPendingEnd = 50 OffFreeHead = 50 OffFreeCount = 54 OffNodeCap = 58 OffRecCap = 62 ) // FileHeader is the 72-byte .iskr header. NodeCap and RecCap are the region // capacities the page cache was built with, not the in-use counts; Flush writes // them as the in-use counts, the same as iskradb's writeHeader(w, t) does. type FileHeader struct { Version uint16 NCount uint32 RCount uint32 PendCount uint32 Roots [C]uint32 FreeHead uint32 FreeCount uint32 NodeCap uint32 RecCap uint32 } // RegionCaps is what a version-5 reader derives from a header: // headerSize + nodeCap*nodeSize is where the page cache looks for records. type RegionCaps struct { NodeCap uint32 RecCap uint32 RecOff int64 } type versionError struct { got uint16 want string } func (e versionError) Error() string { return fmt.Sprintf("lattice: file is version %d, this reader supports %s", e.got, e.want) } // ReadRegionCaps derives the record region offset from a raw header, exactly as // iskradb/lattice/store.mx newPageCache does: NodeCap at OffNodeCap, RecCap at // OffRecCap, records at HeaderSize + NodeCap*NodeSize. // // It does not verify the version first, because the point of the function is // that a version-5 reader applies version-5 offsets to whatever file it is // given. On a version-4 header those offsets land on roots 6 and 7 and on the // middle word of the free pointer. EmptyLattice is that failure in 72 bytes. func ReadRegionCaps(raw []byte) (RegionCaps, error) { if len(raw) < OffRecCap+4 { return RegionCaps{}, fmt.Errorf("lattice: header is %d bytes, need %d", len(raw), OffRecCap+4) } if string(raw[OffMagic:OffMagic+4]) != Magic { return RegionCaps{}, fmt.Errorf("lattice: bad magic %q", raw[OffMagic:OffMagic+4]) } rc := RegionCaps{ NodeCap: le.Uint32(raw[OffNodeCap:]), RecCap: le.Uint32(raw[OffRecCap:]), } rc.RecOff = int64(HeaderSize) + int64(rc.NodeCap)*int64(NodeSize) return rc, nil }