BTreeIndex class

B-tree index implementation using a self-balancing tree structure.

Provides ordered storage of indexed values, enabling both exact-match lookups and efficient range queries.

Performance Characteristics

Operation Time Complexity
Insert O(log n)
Remove O(log n)
Search O(log n)
Range O(log n + k)

Where n is the number of unique keys and k is the number of results.

Use Cases

Best suited for fields that require:

  • Range queries (e.g., age > 18 AND age < 65)
  • Ordered iteration (e.g., sorting by date)
  • Min/max lookups

Example

final index = BTreeIndex('createdAt');

// Insert entities
index.insert('entity-1', {'createdAt': DateTime(2024, 1, 1)});
index.insert('entity-2', {'createdAt': DateTime(2024, 6, 15)});

// Range query for first half of 2024
final results = index.rangeSearch(
  DateTime(2024, 1, 1),
  DateTime(2024, 7, 1),
);
Implemented types

Constructors

BTreeIndex(String field)
Creates a new B-tree index on the specified field.

Properties

entryCount → int
Returns the total number of indexed entity references.
no setter
field → String
The field name on which this index is built.
final
hashCode → int
The hash code for this object.
no setterinherited
keyCount → int
Returns the number of unique keys in the index.
no setter
maxKey → dynamic
Returns the maximum key in the index, or null if empty.
no setter
minKey → dynamic
Returns the minimum key in the index, or null if empty.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

clear() → void
Clears all entries from the index.
override
countEquals(dynamic value) → int
Counts entities where the indexed value equals value.
countGreaterThan(dynamic value) → int
Counts entities where the indexed value is greater than value.
countGreaterThanOrEqual(dynamic value) → int
Counts entities where the indexed value is greater than or equal to value.
countLessThan(dynamic value) → int
Counts entities where the indexed value is less than value.
countLessThanOrEqual(dynamic value) → int
Counts entities where the indexed value is less than or equal to value.
countRange(dynamic lowerBound, dynamic upperBound, {bool includeLower = true, bool includeUpper = false}) → int
Counts entities where the indexed value is between lowerBound and upperBound.
existsEquals(dynamic value) → bool
Checks if any entity exists where the indexed value equals value.
existsGreaterThan(dynamic value) → bool
Checks if any entity exists where the indexed value is greater than value.
existsLessThan(dynamic value) → bool
Checks if any entity exists where the indexed value is less than value.
greaterThan(dynamic value) → List<String>
Finds all entity IDs where the indexed value is greater than value.
greaterThanOrEqual(dynamic value) → List<String>
Finds all entity IDs where the indexed value is greater than or equal to value.
insert(String entityId, Map<String, dynamic> data) → void
Inserts an entity into the index.
override
lessThan(dynamic value) → List<String>
Finds all entity IDs where the indexed value is less than value.
lessThanOrEqual(dynamic value) → List<String>
Finds all entity IDs where the indexed value is less than or equal to value.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rangeSearch(dynamic lowerBound, dynamic upperBound, {bool includeLower = true, bool includeUpper = false}) → List<String>
Performs a range search between lowerBound and upperBound.
remove(String entityId, Map<String, dynamic> data) → void
Removes an entity from the index.
override
restoreFromMap(Map<dynamic, Set<String>> data) → void
Restores the index state from a serialized map.
Searches for entities matching the given value.
override
toMap() → Map<dynamic, Set<String>>
Exports the index state as a map for persistence.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited