Skip to main content

iterator.go

iterator.go - Overview

This file defines iterators for traversing data within a table, including block-level iteration and table-level concatenation. It includes structures and functions for seeking, moving forward/backward, and accessing key-value pairs.

Detailed Documentation

blockIterator

Purpose: Iterates over entries within a block of a table.

Fields:

  • data: []byte - Raw data of the block.
  • idx: int - Index of the current entry.
  • err: error - Any error encountered during iteration.
  • baseKey: []byte - Base key for the block.
  • key: []byte - Current key.
  • val: []byte - Current value.
  • entryOffsets: []uint32 - Offsets of each entry within the block.
  • block: *Block - Pointer to the block being iterated.
  • tableID: uint64 - ID of the table the block belongs to.
  • blockID: int - ID of the block.
  • prevOverlap: uint16 - Overlap of the previous key with the base key.

setBlock(b *Block)

Purpose: Sets the block to iterate over, resetting the iterator's state.

Parameters:

  • b: *Block - The block to iterate over.

Returns: None

setIdx(i int)

Purpose: Sets the iterator to a specific index within the block and sets the key and value.

Parameters:

  • i: int - The index to set the iterator to.

Returns: None

Valid() bool

Purpose: Checks if the iterator is valid (no error and within bounds).

Parameters: None

Returns: bool - True if the iterator is valid, false otherwise.

Error() error

Purpose: Returns the current error of the iterator.

Parameters: None

Returns: error - The current error, or nil if no error.

Close()

Purpose: Decrements the reference count of the associated block.

Parameters: None

Returns: None

seek(key []byte, whence int)

Purpose: Moves the iterator to the first entry that is greater than or equal to the given key.

Parameters:

  • key: []byte - The key to seek to.
  • whence: int - Start index for search.

Returns: None

seekToFirst()

Purpose: Moves the iterator to the first element in the block.

Parameters: None

Returns: None

seekToLast()

Purpose: Moves the iterator to the last element in the block.

Parameters: None

Returns: None

next()

Purpose: Moves the iterator to the next element in the block.

Parameters: None

Returns: None

prev()

Purpose: Moves the iterator to the previous element in the block.

Parameters: None

Returns: None

Iterator

Purpose: An iterator for a Table.

Fields:

  • t: *Table - The table being iterated.
  • bpos: int - The current block position.
  • bi: blockIterator - The block iterator.
  • err: error - Any error encountered during iteration.
  • opt: int - Options for the iterator (e.g., reversed, no cache).

NewIterator(opt int) *Iterator

Purpose: Creates a new iterator for the table.

Parameters:

  • opt: int - Options for the iterator. Valid options are REVERSED and NOCACHE.

Returns: *Iterator - A new iterator for the table.

Close() error

Purpose: Closes the iterator, decrementing the table's reference count.

Parameters: None

Returns: error - Any error encountered during closing.

reset()

Purpose: Resets the iterator to its initial state.

Parameters: None

Returns: None

Valid() bool

Purpose: Checks if the iterator is valid (no error).

Parameters: None

Returns: bool - True if the iterator is valid, false otherwise.

useCache() bool

Purpose: Determines whether to use the cache.

Parameters: None

Returns: bool - true if the cache should be used, false otherwise.

seekToFirst()

Purpose: Seeks to the first key in the table.

Parameters: None

Returns: None

seekToLast()

Purpose: Seeks to the last key in the table.

Parameters: None

Returns: None

seekHelper(blockIdx int, key []byte)

Purpose: Seeks to a specific key within a specific block.

Parameters:

  • blockIdx: int - The index of the block to seek within.
  • key: []byte - The key to seek to.

Returns: None

seekFrom(key []byte, whence int)

Purpose: Seeks to the first key that is greater than or equal to the given key, starting from a specific position.

Parameters:

  • key: []byte - The key to seek to.
  • whence: int - Origin of the search.

Returns: None

seek(key []byte)

Purpose: Seeks to the first key that is greater than or equal to the given key.

Parameters:

  • key: []byte - The key to seek to.

Returns: None

seekForPrev(key []byte)

Purpose: Seeks to the last key that is less than or equal to the given key.

Parameters:

  • key: []byte - The key to seek to.

Returns: None

next()

Purpose: Moves the iterator to the next key in the table.

Parameters: None

Returns: None

prev()

Purpose: Moves the iterator to the previous key in the table.

Parameters: None

Returns: None

Key() []byte

Purpose: Returns the current key.

Parameters: None

Returns: []byte - The current key.

Value() y.ValueStruct

Purpose: Returns the current value as a y.ValueStruct.

Parameters: None

Returns: y.ValueStruct - The current value.

ValueCopy() y.ValueStruct

Purpose: Copies the current value and returns it as a decoded y.ValueStruct.

Parameters: None

Returns: y.ValueStruct - A copy of the current value.

Next()

Purpose: Moves the iterator to the next key, respecting the iteration direction.

Parameters: None

Returns: None

Rewind()

Purpose: Resets the iterator to the beginning or end, depending on the iteration direction.

Parameters: None

Returns: None

Seek(key []byte)

Purpose: Seeks to a specific key, respecting the iteration direction.

Parameters:

  • key: []byte - The key to seek to.

Returns: None

ConcatIterator

Purpose: Concatenates multiple Table iterators into a single iterator.

Fields:

  • idx: int - Index of the currently active iterator.
  • cur: *Iterator - The current iterator.
  • iters: []*Iterator - Slice of iterators to concatenate.
  • tables: []*Table - Slice of tables corresponding to the iterators.
  • options: int - Options for the iterator (e.g., reversed, no cache).

NewConcatIterator(tbls []*Table, opt int) *ConcatIterator

Purpose: Creates a new concatenated iterator.

Parameters:

  • tbls: []*Table - The tables to iterate over.
  • opt: int - Options for the iterator. Valid options are REVERSED and NOCACHE.

Returns: *ConcatIterator - A new concatenated iterator.

setIdx(idx int)

Purpose: Sets the current iterator index.

Parameters:

  • idx: int - The index of the iterator to set.

Returns: None

Rewind()

Purpose: Resets the concatenated iterator to the beginning or end, depending on the iteration direction.

Parameters: None

Returns: None

Valid() bool

Purpose: Checks if the concatenated iterator is valid.

Parameters: None

Returns: bool - True if the iterator is valid, false otherwise.

Key() []byte

Purpose: Returns the current key.

Parameters: None

Returns: []byte - The current key.

Value() y.ValueStruct

Purpose: Returns the current value as a y.ValueStruct.

Parameters: None

Returns: y.ValueStruct - The current value.

Seek(key []byte)

Purpose: Seeks to a specific key within the concatenated iterators, respecting the iteration direction.

Parameters:

  • key: []byte - The key to seek to.

Returns: None

Next()

Purpose: Advances the concatenated iterator to the next key.

Parameters: None

Returns: None

Close() error

Purpose: Closes the concatenated iterator and all underlying iterators, decrementing the table's reference count.

Parameters: None

Returns: error - Any error encountered during closing.

Getting Started Relevance