iterator.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package y
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. )
  21. // ValueStruct represents the value info that can be associated with a key, but also the internal
  22. // Meta field.
  23. type ValueStruct struct {
  24. Meta byte
  25. UserMeta byte
  26. ExpiresAt uint64
  27. Value []byte
  28. Version uint64 // This field is not serialized. Only for internal usage.
  29. }
  30. func sizeVarint(x uint64) (n int) {
  31. for {
  32. n++
  33. x >>= 7
  34. if x == 0 {
  35. break
  36. }
  37. }
  38. return n
  39. }
  40. // EncodedSize is the size of the ValueStruct when encoded
  41. func (v *ValueStruct) EncodedSize() uint32 {
  42. sz := len(v.Value) + 2 // meta, usermeta.
  43. enc := sizeVarint(v.ExpiresAt)
  44. return uint32(sz + enc)
  45. }
  46. // Decode uses the length of the slice to infer the length of the Value field.
  47. func (v *ValueStruct) Decode(b []byte) {
  48. v.Meta = b[0]
  49. v.UserMeta = b[1]
  50. var sz int
  51. v.ExpiresAt, sz = binary.Uvarint(b[2:])
  52. v.Value = b[2+sz:]
  53. }
  54. // Encode expects a slice of length at least v.EncodedSize().
  55. func (v *ValueStruct) Encode(b []byte) uint32 {
  56. b[0] = v.Meta
  57. b[1] = v.UserMeta
  58. sz := binary.PutUvarint(b[2:], v.ExpiresAt)
  59. n := copy(b[2+sz:], v.Value)
  60. return uint32(2 + sz + n)
  61. }
  62. // EncodeTo should be kept in sync with the Encode function above. The reason
  63. // this function exists is to avoid creating byte arrays per key-value pair in
  64. // table/builder.go.
  65. func (v *ValueStruct) EncodeTo(buf *bytes.Buffer) {
  66. buf.WriteByte(v.Meta)
  67. buf.WriteByte(v.UserMeta)
  68. var enc [binary.MaxVarintLen64]byte
  69. sz := binary.PutUvarint(enc[:], v.ExpiresAt)
  70. buf.Write(enc[:sz])
  71. buf.Write(v.Value)
  72. }
  73. // Iterator is an interface for a basic iterator.
  74. type Iterator interface {
  75. Next()
  76. Rewind()
  77. Seek(key []byte)
  78. Key() []byte
  79. Value() ValueStruct
  80. Valid() bool
  81. // All iterators should be closed so that file garbage collection works.
  82. Close() error
  83. }