extra.go raw

   1  package lattice
   2  
   3  // SetFormOnRecord stores form on a record the way iskra/storage.mx does: up to
   4  // 23 bytes inline with the length in Inline[23], or the first 23 bytes inline
   5  // with Inline[23]=0 and the whole form appended to pool, referenced by
   6  // DataFile=1. This is a different encoding from Record.SetInline, which is the
   7  // 24-byte payload form of iskradb's own InsertRec.
   8  func SetFormOnRecord(r *Record, form string, pool *[]byte) {
   9  	b := []byte(form)
  10  	if len(b) <= 23 {
  11  		copy(r.Inline[:], b)
  12  		for i := len(b); i < 23; i++ {
  13  			r.Inline[i] = 0
  14  		}
  15  		r.Inline[23] = byte(len(b))
  16  		r.DataFile = 0
  17  		r.DataOff = 0
  18  		r.DataLen = 0
  19  		return
  20  	}
  21  	copy(r.Inline[:23], b[:23])
  22  	r.Inline[23] = 0
  23  	r.DataFile = 1
  24  	r.DataOff = uint32(len(*pool))
  25  	r.DataLen = uint32(len(b))
  26  	*pool = append(*pool, b...)
  27  }
  28  
  29  // FormFromRecord reads the form SetFormOnRecord stored. An overflow form whose
  30  // run is outside the pool yields "".
  31  func FormFromRecord(r *Record, pool []byte) string {
  32  	if r.Inline[23] != 0 {
  33  		return string(r.Inline[:r.Inline[23]])
  34  	}
  35  	if r.DataFile == 1 && r.DataLen > 0 {
  36  		end := uint64(r.DataOff) + uint64(r.DataLen)
  37  		if end <= uint64(len(pool)) {
  38  			return string(pool[r.DataOff:end])
  39  		}
  40  	}
  41  	return ""
  42  }
  43  
  44  // FileBytes is the size of the backing file, or 0 for an in-memory tree.
  45  func (pc *Store) FileBytes() int64 {
  46  	st, err := pc.F.Stat()
  47  	if err != nil {
  48  		return -1
  49  	}
  50  	return st.Size()
  51  }
  52