Skip to main content

iterator.go

iterator.go - Overview

This file defines the ValueStruct and Iterator interface, along with related helper functions for encoding and decoding ValueStruct instances.

Detailed Documentation

ValueStruct

type ValueStruct struct {
Meta byte
UserMeta byte
ExpiresAt uint64
Value []byte

Version uint64 // This field is not serialized. Only for internal usage.
}
  • Purpose: Represents the value and metadata associated with a key. Includes internal metadata.
    • Meta: Internal metadata.
    • UserMeta: User-defined metadata.
    • ExpiresAt: Unix timestamp indicating when the value expires.
    • Value: The actual value (byte array).
    • Version: Internal versioning field (not serialized).

sizeVarint

func sizeVarint(x uint64) (n int)
  • Purpose: Calculates the number of bytes required to represent a uint64 value when encoded as a varint.
    • Parameters:
      • x: The uint64 value.
    • Returns: The number of bytes required for varint encoding.

ValueStruct.EncodedSize

func (v *ValueStruct) EncodedSize() uint32
  • Purpose: Calculates the size in bytes required to encode the ValueStruct.
    • Returns: The encoded size of the ValueStruct as a uint32.

ValueStruct.Decode

func (v *ValueStruct) Decode(b []byte)
  • Purpose: Decodes a byte slice into the ValueStruct. The length of the slice is used to determine the length of the Value field.
    • Parameters:
      • b: The byte slice to decode.

ValueStruct.Encode

func (v *ValueStruct) Encode(b []byte) uint32
  • Purpose: Encodes the ValueStruct into a byte slice. Expects the slice to be large enough to hold the encoded data (at least v.EncodedSize() bytes).
    • Parameters:
      • b: The byte slice to encode into.
    • Returns: The number of bytes written to the slice.

ValueStruct.EncodeTo

func (v *ValueStruct) EncodeTo(buf *bytes.Buffer)
  • Purpose: Encodes the ValueStruct into a bytes.Buffer. Avoids creating byte arrays per key-value pair.
    • Parameters:
      • buf: The bytes.Buffer to encode into.

Iterator

type Iterator interface {
Next()
Rewind()
Seek(key []byte)
Key() []byte
Value() ValueStruct
Valid() bool
Close() error
}
  • Purpose: Defines a basic iterator interface for key-value pairs.
    • Next(): Moves the iterator to the next key-value pair.
    • Rewind(): Resets the iterator to the beginning.
    • Seek(key []byte): Moves the iterator to the first key-value pair with a key equal to or greater than the provided key.
    • Key(): Returns the current key.
    • Value(): Returns the current ValueStruct.
    • Valid(): Indicates if the iterator is currently pointing to a valid key-value pair.
    • Close(): Closes the iterator and releases any associated resources.

Getting Started Relevance