format.go raw

   1  package lattice
   2  
   3  import "fmt"
   4  
   5  // Format constants for .iskr version 5, as iskradb/lattice/io.mx defines them.
   6  const (
   7  	Magic       = "ISKR"
   8  	Version     = 5
   9  	NodeSize    = 136
  10  	RecordSize  = 48
  11  	HeaderSize  = 72
  12  	PendingSize = 4
  13  )
  14  
  15  // Version4 is the previous C=8 layout: 128-bit SipHash keys in 200-byte blocks.
  16  // Records, flags and header are otherwise identical.
  17  //
  18  // Version 4 is the only older version this package reads. Version 3 and earlier
  19  // were C=3 with 64-byte blocks, a 3-bit branch flag and a 6-byte record header,
  20  // and claiming to read them would be false. Version 4 is not a header-only
  21  // difference either; see RegionCaps.
  22  const (
  23  	Version4  = 4
  24  	NodeSize4 = 200
  25  )
  26  
  27  // CompatibleVersion reports whether this package can parse version v. Version 4
  28  // is not read by the page cache: ConvertToV5 translates it first.
  29  func CompatibleVersion(v uint16) bool { return v == Version }
  30  
  31  // Version-5 header offsets, as iskradb/lattice/io.mx writeHeaderBuf lays them out.
  32  const (
  33  	OffMagic      = 0
  34  	OffVersion    = 4
  35  	OffNCount     = 6
  36  	OffRCount     = 10
  37  	OffPendCount  = 14
  38  	OffRoots      = 18
  39  	OffPendingEnd = 50
  40  	OffFreeHead   = 50
  41  	OffFreeCount  = 54
  42  	OffNodeCap    = 58
  43  	OffRecCap     = 62
  44  )
  45  
  46  // FileHeader is the 72-byte .iskr header. NodeCap and RecCap are the region
  47  // capacities the page cache was built with, not the in-use counts; Flush writes
  48  // them as the in-use counts, the same as iskradb's writeHeader(w, t) does.
  49  type FileHeader struct {
  50  	Version   uint16
  51  	NCount    uint32
  52  	RCount    uint32
  53  	PendCount uint32
  54  	Roots     [C]uint32
  55  	FreeHead  uint32
  56  	FreeCount uint32
  57  	NodeCap   uint32
  58  	RecCap    uint32
  59  }
  60  
  61  // RegionCaps is what a version-5 reader derives from a header:
  62  // headerSize + nodeCap*nodeSize is where the page cache looks for records.
  63  type RegionCaps struct {
  64  	NodeCap uint32
  65  	RecCap  uint32
  66  	RecOff  int64
  67  }
  68  
  69  type versionError struct {
  70  	got  uint16
  71  	want string
  72  }
  73  
  74  func (e versionError) Error() string {
  75  	return fmt.Sprintf("lattice: file is version %d, this reader supports %s", e.got, e.want)
  76  }
  77  
  78  // ReadRegionCaps derives the record region offset from a raw header, exactly as
  79  // iskradb/lattice/store.mx newPageCache does: NodeCap at OffNodeCap, RecCap at
  80  // OffRecCap, records at HeaderSize + NodeCap*NodeSize.
  81  //
  82  // It does not verify the version first, because the point of the function is
  83  // that a version-5 reader applies version-5 offsets to whatever file it is
  84  // given. On a version-4 header those offsets land on roots 6 and 7 and on the
  85  // middle word of the free pointer. EmptyLattice is that failure in 72 bytes.
  86  func ReadRegionCaps(raw []byte) (RegionCaps, error) {
  87  	if len(raw) < OffRecCap+4 {
  88  		return RegionCaps{}, fmt.Errorf("lattice: header is %d bytes, need %d", len(raw), OffRecCap+4)
  89  	}
  90  	if string(raw[OffMagic:OffMagic+4]) != Magic {
  91  		return RegionCaps{}, fmt.Errorf("lattice: bad magic %q", raw[OffMagic:OffMagic+4])
  92  	}
  93  	rc := RegionCaps{
  94  		NodeCap: le.Uint32(raw[OffNodeCap:]),
  95  		RecCap:  le.Uint32(raw[OffRecCap:]),
  96  	}
  97  	rc.RecOff = int64(HeaderSize) + int64(rc.NodeCap)*int64(NodeSize)
  98  	return rc, nil
  99  }
 100