package lattice // SetFormOnRecord stores form on a record the way iskra/storage.mx does: up to // 23 bytes inline with the length in Inline[23], or the first 23 bytes inline // with Inline[23]=0 and the whole form appended to pool, referenced by // DataFile=1. This is a different encoding from Record.SetInline, which is the // 24-byte payload form of iskradb's own InsertRec. func SetFormOnRecord(r *Record, form string, pool *[]byte) { b := []byte(form) if len(b) <= 23 { copy(r.Inline[:], b) for i := len(b); i < 23; i++ { r.Inline[i] = 0 } r.Inline[23] = byte(len(b)) r.DataFile = 0 r.DataOff = 0 r.DataLen = 0 return } copy(r.Inline[:23], b[:23]) r.Inline[23] = 0 r.DataFile = 1 r.DataOff = uint32(len(*pool)) r.DataLen = uint32(len(b)) *pool = append(*pool, b...) } // FormFromRecord reads the form SetFormOnRecord stored. An overflow form whose // run is outside the pool yields "". func FormFromRecord(r *Record, pool []byte) string { if r.Inline[23] != 0 { return string(r.Inline[:r.Inline[23]]) } if r.DataFile == 1 && r.DataLen > 0 { end := uint64(r.DataOff) + uint64(r.DataLen) if end <= uint64(len(pool)) { return string(pool[r.DataOff:end]) } } return "" } // FileBytes is the size of the backing file, or 0 for an in-memory tree. func (pc *Store) FileBytes() int64 { st, err := pc.F.Stat() if err != nil { return -1 } return st.Size() }