convert.go raw

   1  package lattice
   2  
   3  import (
   4  	"fmt"
   5  	"os"
   6  	"strings"
   7  )
   8  
   9  // TranslatedSuffix is what ConvertToV5 appends to a version-4 base path.
  10  const TranslatedSuffix = ".iskr5"
  11  
  12  // ReadV4File reads a version-4 .iskr into memory, keeping block indices, free
  13  // list, records and the journal. Only the low 64 bits of each 128-bit key are
  14  // kept, which is the same rule Migrate4to5 applies.
  15  //
  16  // The count returned is how many keys had a nonzero high half, the half that
  17  // truncation discards. ja-test.iskr has one in every slot it checked, so the
  18  // count is reported and not refused: the source was written by iskradb v0.2.3
  19  // (version 4, 128-bit SipHash keys) months after v0.2.4 declared version 5, and
  20  // the low half of each key is all the writer that follows v0.2.4 would keep
  21  // anyway. Whether those keys still identify anything is the caller's call; see
  22  // ConvertToV5Strict.
  23  func ReadV4File(path string) (*Tree, uint64, error) {
  24  	data, err := os.ReadFile(path)
  25  	if err != nil {
  26  		return nil, 0, err
  27  	}
  28  	if len(data) < HeaderSize {
  29  		return nil, 0, fmt.Errorf("lattice: %s is %d bytes, shorter than the %d-byte header", path, len(data), HeaderSize)
  30  	}
  31  	if string(data[0:4]) != Magic {
  32  		return nil, 0, fmt.Errorf("lattice: %s: bad magic %q", path, data[0:4])
  33  	}
  34  	if v := le.Uint16(data[4:6]); v != Version4 {
  35  		return nil, 0, fmt.Errorf("lattice: %s is version %d, this reads version %d", path, v, Version4)
  36  	}
  37  	nCount := le.Uint32(data[6:10])
  38  	rCount := le.Uint32(data[10:14])
  39  	pendCount := le.Uint32(data[14:18])
  40  	var roots [C]uint32
  41  	for i := 0; i < C; i++ {
  42  		roots[i] = le.Uint32(data[18+i*4:])
  43  	}
  44  	need := int64(HeaderSize) + int64(nCount)*NodeSize4 + int64(rCount)*RecordSize + int64(pendCount)*PendingSize
  45  	if int64(len(data)) < need {
  46  		return nil, 0, fmt.Errorf("lattice: %s is %d bytes; %d blocks and %d records need %d", path, len(data), nCount, rCount, need)
  47  	}
  48  	t := AllocTree(nCount, rCount, roots)
  49  	t.SourceVersion = Version4
  50  	t.FreeHead = le.Uint32(data[50:54])
  51  	t.FreeCount = le.Uint32(data[54:58])
  52  	if t.FreeHead != NullIndex && t.FreeHead >= nCount {
  53  		return nil, 0, fmt.Errorf("lattice: %s free head %d beyond %d blocks", path, t.FreeHead, nCount)
  54  	}
  55  
  56  	var zeroHigh uint64
  57  	off := HeaderSize
  58  	for i := uint32(0); i < nCount; i++ {
  59  		t.Nodes[i] = decodeNode4(data[off : off+NodeSize4])
  60  		off += NodeSize4
  61  	}
  62  	for i := uint32(0); i < rCount; i++ {
  63  		t.Records[i] = DecodeRecord(data[off : off+RecordSize])
  64  		off += RecordSize
  65  	}
  66  	if pendCount > 0 {
  67  		t.Pending = make([]PendingExp, pendCount)
  68  		for i := uint32(0); i < pendCount; i++ {
  69  			e := data[off : off+PendingSize]
  70  			t.Pending[i] = PendingExp{Branch: Branch(e[0]), Depth: le.Uint16(e[1:3]), Factor: e[3]}
  71  			off += PendingSize
  72  		}
  73  	}
  74  	// The high half of each 128-bit key is not in memory; it can only be checked
  75  	// here, from the file bytes.
  76  	for i := uint32(0); i < nCount; i++ {
  77  		base := HeaderSize + int(i)*NodeSize4
  78  		for k := 0; k < C; k++ {
  79  			hi := le.Uint64(data[base+k*16+8:])
  80  			if hi != 0 {
  81  				zeroHigh++
  82  			}
  83  		}
  84  	}
  85  	t.rebuildRecKey()
  86  	return t, zeroHigh, nil
  87  }
  88  
  89  // ConvertToV5 base path reads base+".iskr" if it is version 4, writes
  90  // base+TranslatedSuffix+".iskr" as version 5 with the same block indices,
  91  // records and journal, and returns the path it wrote. A base path already at
  92  // version 5 is returned with an error, not converted.
  93  func ConvertToV5(base string) (string, error) {
  94  	t, _, err := convert(base)
  95  	return t, err
  96  }
  97  
  98  // ConvertToV5Strict is ConvertToV5 that refuses a file any of whose keys had a
  99  // nonzero high half.
 100  func ConvertToV5Strict(base string) (string, error) {
 101  	t, high, err := convertFacts(base)
 102  	if err != nil {
 103  		return "", err
 104  	}
 105  	if high > 0 {
 106  		return "", fmt.Errorf("lattice: %s.iskr has %d keys whose high 64 bits are not zero; translating them would keep only the low half of a real hash", base, high)
 107  	}
 108  	return t, nil
 109  }
 110  
 111  func convertFacts(base string) (string, uint64, error) {
 112  	t, high, err := ReadV4File(base + ".iskr")
 113  	if err != nil {
 114  		return "", 0, err
 115  	}
 116  	if err := writeConverted(base, t); err != nil {
 117  		return "", 0, err
 118  	}
 119  	return base + TranslatedSuffix + ".iskr", high, nil
 120  }
 121  
 122  func convert(base string) (string, uint64, error) {
 123  	path, high, err := convertFacts(base)
 124  	return path, high, err
 125  }
 126  
 127  func writeConverted(base string, t *Tree) error {
 128  	// The .meta companion is version-independent and is moved with the tree so
 129  	// the pair stays in step. It is copied after the .iskr exists, so a crash
 130  	// between the two leaves a tree with no companion rather than a companion
 131  	// with no tree.
 132  	out := base + TranslatedSuffix
 133  	if err := WriteISKRMemory(out, t, nil); err != nil {
 134  		return err
 135  	}
 136  	if src, err := os.ReadFile(base + ".meta"); err == nil {
 137  		if err := os.WriteFile(out+".meta", src, 0o644); err != nil {
 138  			return err
 139  		}
 140  	}
 141  	return nil
 142  }
 143  
 144  // IsVersion4Path reports whether path names a file whose header is version 4.
 145  func IsVersion4Path(path string) (bool, error) {
 146  	data, err := os.ReadFile(path)
 147  	if err != nil {
 148  		return false, err
 149  	}
 150  	if len(data) < HeaderSize || !strings.HasPrefix(string(data[0:4]), Magic) {
 151  		return false, nil
 152  	}
 153  	return le.Uint16(data[4:6]) == Version4, nil
 154  }
 155