package lattice import ( "fmt" "os" "strings" ) // TranslatedSuffix is what ConvertToV5 appends to a version-4 base path. const TranslatedSuffix = ".iskr5" // ReadV4File reads a version-4 .iskr into memory, keeping block indices, free // list, records and the journal. Only the low 64 bits of each 128-bit key are // kept, which is the same rule Migrate4to5 applies. // // The count returned is how many keys had a nonzero high half, the half that // truncation discards. ja-test.iskr has one in every slot it checked, so the // count is reported and not refused: the source was written by iskradb v0.2.3 // (version 4, 128-bit SipHash keys) months after v0.2.4 declared version 5, and // the low half of each key is all the writer that follows v0.2.4 would keep // anyway. Whether those keys still identify anything is the caller's call; see // ConvertToV5Strict. func ReadV4File(path string) (*Tree, uint64, error) { data, err := os.ReadFile(path) if err != nil { return nil, 0, err } if len(data) < HeaderSize { return nil, 0, fmt.Errorf("lattice: %s is %d bytes, shorter than the %d-byte header", path, len(data), HeaderSize) } if string(data[0:4]) != Magic { return nil, 0, fmt.Errorf("lattice: %s: bad magic %q", path, data[0:4]) } if v := le.Uint16(data[4:6]); v != Version4 { return nil, 0, fmt.Errorf("lattice: %s is version %d, this reads version %d", path, v, Version4) } nCount := le.Uint32(data[6:10]) rCount := le.Uint32(data[10:14]) pendCount := le.Uint32(data[14:18]) var roots [C]uint32 for i := 0; i < C; i++ { roots[i] = le.Uint32(data[18+i*4:]) } need := int64(HeaderSize) + int64(nCount)*NodeSize4 + int64(rCount)*RecordSize + int64(pendCount)*PendingSize if int64(len(data)) < need { return nil, 0, fmt.Errorf("lattice: %s is %d bytes; %d blocks and %d records need %d", path, len(data), nCount, rCount, need) } t := AllocTree(nCount, rCount, roots) t.SourceVersion = Version4 t.FreeHead = le.Uint32(data[50:54]) t.FreeCount = le.Uint32(data[54:58]) if t.FreeHead != NullIndex && t.FreeHead >= nCount { return nil, 0, fmt.Errorf("lattice: %s free head %d beyond %d blocks", path, t.FreeHead, nCount) } var zeroHigh uint64 off := HeaderSize for i := uint32(0); i < nCount; i++ { t.Nodes[i] = decodeNode4(data[off : off+NodeSize4]) off += NodeSize4 } for i := uint32(0); i < rCount; i++ { t.Records[i] = DecodeRecord(data[off : off+RecordSize]) off += RecordSize } if pendCount > 0 { t.Pending = make([]PendingExp, pendCount) for i := uint32(0); i < pendCount; i++ { e := data[off : off+PendingSize] t.Pending[i] = PendingExp{Branch: Branch(e[0]), Depth: le.Uint16(e[1:3]), Factor: e[3]} off += PendingSize } } // The high half of each 128-bit key is not in memory; it can only be checked // here, from the file bytes. for i := uint32(0); i < nCount; i++ { base := HeaderSize + int(i)*NodeSize4 for k := 0; k < C; k++ { hi := le.Uint64(data[base+k*16+8:]) if hi != 0 { zeroHigh++ } } } t.rebuildRecKey() return t, zeroHigh, nil } // ConvertToV5 base path reads base+".iskr" if it is version 4, writes // base+TranslatedSuffix+".iskr" as version 5 with the same block indices, // records and journal, and returns the path it wrote. A base path already at // version 5 is returned with an error, not converted. func ConvertToV5(base string) (string, error) { t, _, err := convert(base) return t, err } // ConvertToV5Strict is ConvertToV5 that refuses a file any of whose keys had a // nonzero high half. func ConvertToV5Strict(base string) (string, error) { t, high, err := convertFacts(base) if err != nil { return "", err } if high > 0 { 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) } return t, nil } func convertFacts(base string) (string, uint64, error) { t, high, err := ReadV4File(base + ".iskr") if err != nil { return "", 0, err } if err := writeConverted(base, t); err != nil { return "", 0, err } return base + TranslatedSuffix + ".iskr", high, nil } func convert(base string) (string, uint64, error) { path, high, err := convertFacts(base) return path, high, err } func writeConverted(base string, t *Tree) error { // The .meta companion is version-independent and is moved with the tree so // the pair stays in step. It is copied after the .iskr exists, so a crash // between the two leaves a tree with no companion rather than a companion // with no tree. out := base + TranslatedSuffix if err := WriteISKRMemory(out, t, nil); err != nil { return err } if src, err := os.ReadFile(base + ".meta"); err == nil { if err := os.WriteFile(out+".meta", src, 0o644); err != nil { return err } } return nil } // IsVersion4Path reports whether path names a file whose header is version 4. func IsVersion4Path(path string) (bool, error) { data, err := os.ReadFile(path) if err != nil { return false, err } if len(data) < HeaderSize || !strings.HasPrefix(string(data[0:4]), Magic) { return false, nil } return le.Uint16(data[4:6]) == Version4, nil }