structs.go raw

   1  /*
   2   * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
   3   * SPDX-License-Identifier: Apache-2.0
   4   */
   5  
   6  package badger
   7  
   8  import (
   9  	"encoding/binary"
  10  	"fmt"
  11  	"time"
  12  	"unsafe"
  13  )
  14  
  15  type valuePointer struct {
  16  	Fid    uint32
  17  	Len    uint32
  18  	Offset uint32
  19  }
  20  
  21  const vptrSize = unsafe.Sizeof(valuePointer{})
  22  
  23  func (p valuePointer) Less(o valuePointer) bool {
  24  	if p.Fid != o.Fid {
  25  		return p.Fid < o.Fid
  26  	}
  27  	if p.Offset != o.Offset {
  28  		return p.Offset < o.Offset
  29  	}
  30  	return p.Len < o.Len
  31  }
  32  
  33  func (p valuePointer) IsZero() bool {
  34  	return p.Fid == 0 && p.Offset == 0 && p.Len == 0
  35  }
  36  
  37  // Encode encodes Pointer into byte buffer.
  38  func (p valuePointer) Encode() []byte {
  39  	b := make([]byte, vptrSize)
  40  	// Copy over the content from p to b.
  41  	*(*valuePointer)(unsafe.Pointer(&b[0])) = p
  42  	return b
  43  }
  44  
  45  // Decode decodes the value pointer into the provided byte buffer.
  46  func (p *valuePointer) Decode(b []byte) {
  47  	// Copy over data from b into p. Using *p=unsafe.pointer(...) leads to
  48  	// pointer alignment issues. See https://github.com/hypermodeinc/badger/issues/1096
  49  	// and comment https://github.com/hypermodeinc/badger/pull/1097#pullrequestreview-307361714
  50  	copy(((*[vptrSize]byte)(unsafe.Pointer(p))[:]), b[:vptrSize])
  51  }
  52  
  53  // header is used in value log as a header before Entry.
  54  type header struct {
  55  	klen      uint32
  56  	vlen      uint32
  57  	expiresAt uint64
  58  	meta      byte
  59  	userMeta  byte
  60  }
  61  
  62  const (
  63  	// Maximum possible size of the header. The maximum size of header struct will be 18 but the
  64  	// maximum size of varint encoded header will be 22.
  65  	maxHeaderSize = 22
  66  )
  67  
  68  // Encode encodes the header into []byte. The provided []byte should be atleast 5 bytes. The
  69  // function will panic if out []byte isn't large enough to hold all the values.
  70  // The encoded header looks like
  71  // +------+----------+------------+--------------+-----------+
  72  // | Meta | UserMeta | Key Length | Value Length | ExpiresAt |
  73  // +------+----------+------------+--------------+-----------+
  74  func (h header) Encode(out []byte) int {
  75  	out[0], out[1] = h.meta, h.userMeta
  76  	index := 2
  77  	index += binary.PutUvarint(out[index:], uint64(h.klen))
  78  	index += binary.PutUvarint(out[index:], uint64(h.vlen))
  79  	index += binary.PutUvarint(out[index:], h.expiresAt)
  80  	return index
  81  }
  82  
  83  // Decode decodes the given header from the provided byte slice.
  84  // Returns the number of bytes read.
  85  func (h *header) Decode(buf []byte) int {
  86  	h.meta, h.userMeta = buf[0], buf[1]
  87  	index := 2
  88  	klen, count := binary.Uvarint(buf[index:])
  89  	h.klen = uint32(klen)
  90  	index += count
  91  	vlen, count := binary.Uvarint(buf[index:])
  92  	h.vlen = uint32(vlen)
  93  	index += count
  94  	h.expiresAt, count = binary.Uvarint(buf[index:])
  95  	return index + count
  96  }
  97  
  98  // DecodeFrom reads the header from the hashReader.
  99  // Returns the number of bytes read.
 100  func (h *header) DecodeFrom(reader *hashReader) (int, error) {
 101  	var err error
 102  	h.meta, err = reader.ReadByte()
 103  	if err != nil {
 104  		return 0, err
 105  	}
 106  	h.userMeta, err = reader.ReadByte()
 107  	if err != nil {
 108  		return 0, err
 109  	}
 110  	klen, err := binary.ReadUvarint(reader)
 111  	if err != nil {
 112  		return 0, err
 113  	}
 114  	h.klen = uint32(klen)
 115  	vlen, err := binary.ReadUvarint(reader)
 116  	if err != nil {
 117  		return 0, err
 118  	}
 119  	h.vlen = uint32(vlen)
 120  	h.expiresAt, err = binary.ReadUvarint(reader)
 121  	if err != nil {
 122  		return 0, err
 123  	}
 124  	return reader.bytesRead, nil
 125  }
 126  
 127  // Entry provides Key, Value, UserMeta and ExpiresAt. This struct can be used by
 128  // the user to set data.
 129  type Entry struct {
 130  	Key       []byte
 131  	Value     []byte
 132  	ExpiresAt uint64 // time.Unix
 133  	version   uint64
 134  	offset    uint32 // offset is an internal field.
 135  	UserMeta  byte
 136  	meta      byte
 137  
 138  	// Fields maintained internally.
 139  	hlen         int // Length of the header.
 140  	valThreshold int64
 141  }
 142  
 143  func (e *Entry) isZero() bool {
 144  	return len(e.Key) == 0
 145  }
 146  
 147  func (e *Entry) estimateSizeAndSetThreshold(threshold int64) int64 {
 148  	if e.valThreshold == 0 {
 149  		e.valThreshold = threshold
 150  	}
 151  	k := int64(len(e.Key))
 152  	v := int64(len(e.Value))
 153  	if v < e.valThreshold {
 154  		return k + v + 2 // Meta, UserMeta
 155  	}
 156  	return k + 12 + 2 // 12 for ValuePointer, 2 for metas.
 157  }
 158  
 159  func (e *Entry) skipVlogAndSetThreshold(threshold int64) bool {
 160  	if e.valThreshold == 0 {
 161  		e.valThreshold = threshold
 162  	}
 163  	return int64(len(e.Value)) < e.valThreshold
 164  }
 165  
 166  //nolint:unused
 167  func (e Entry) print(prefix string) {
 168  	fmt.Printf("%s Key: %s Meta: %d UserMeta: %d Offset: %d len(val)=%d",
 169  		prefix, e.Key, e.meta, e.UserMeta, e.offset, len(e.Value))
 170  }
 171  
 172  // NewEntry creates a new entry with key and value passed in args. This newly created entry can be
 173  // set in a transaction by calling txn.SetEntry(). All other properties of Entry can be set by
 174  // calling WithMeta, WithDiscard, WithTTL methods on it.
 175  // This function uses key and value reference, hence users must
 176  // not modify key and value until the end of transaction.
 177  func NewEntry(key, value []byte) *Entry {
 178  	return &Entry{
 179  		Key:   key,
 180  		Value: value,
 181  	}
 182  }
 183  
 184  // WithMeta adds meta data to Entry e. This byte is stored alongside the key
 185  // and can be used as an aid to interpret the value or store other contextual
 186  // bits corresponding to the key-value pair of entry.
 187  func (e *Entry) WithMeta(meta byte) *Entry {
 188  	e.UserMeta = meta
 189  	return e
 190  }
 191  
 192  // WithDiscard adds a marker to Entry e. This means all the previous versions of the key (of the
 193  // Entry) will be eligible for garbage collection.
 194  // This method is only useful if you have set a higher limit for options.NumVersionsToKeep. The
 195  // default setting is 1, in which case, this function doesn't add any more benefit. If however, you
 196  // have a higher setting for NumVersionsToKeep (in Dgraph, we set it to infinity), you can use this
 197  // method to indicate that all the older versions can be discarded and removed during compactions.
 198  func (e *Entry) WithDiscard() *Entry {
 199  	e.meta = bitDiscardEarlierVersions
 200  	return e
 201  }
 202  
 203  // WithTTL adds time to live duration to Entry e. Entry stored with a TTL would automatically expire
 204  // after the time has elapsed, and will be eligible for garbage collection.
 205  func (e *Entry) WithTTL(dur time.Duration) *Entry {
 206  	e.ExpiresAt = uint64(time.Now().Add(dur).Unix())
 207  	return e
 208  }
 209  
 210  // withMergeBit sets merge bit in entry's metadata. This
 211  // function is called by MergeOperator's Add method.
 212  func (e *Entry) withMergeBit() *Entry {
 213  	e.meta = bitMergeEntry
 214  	return e
 215  }
 216