structs.go 6.1 KB

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