Skip to main content

SortedMap

Standard library collection that may be larger than memory, and which is efficiently mapped to Reboot's underlying storage.

from reboot.std.collections.sorted_map import SortedMap

Keys are strings and values are bytes. SortedMaps are frequently used to create indexes of other state types, but can also be used to efficiently store large collections of primitive values.

For example:

  • To index a data type in its natural state-ID order, you can create a SortedMap with the type's ID as keys and empty values. This is effectively a sorted set.
  • To index a data type by creation time, you can use time-ordered UUIDs (such as UUIDv1 or UUIDv7) as keys and the data type's state-ID as values.
  • To index a large collection of Protobuf message types, you can use a key of your choice and the serialized value of each Protobuf message as a value.

To use a SortedMap in your application, you typically want to choose an ID for the map and store it somewhere (e.g., as a field in the appropriate state data type). To use the map, first lookup the map using its ID and then invoke the appropriate operations. The map will be implicitly constructed when the first writer method is invoked (e.g., Insert()).

You should also include the SortedMap servicer when starting your application, as described in the overview.

SortedMap

A larger than memory map, sorted by its keys. Supports batch insertion, batch removal, sorted range queries, and point-lookup queries.

Insert

rpc Insert(InsertRequest) InsertResponse

Insert a batch of entries into the SortedMap.

Keys which are already in the map will be overwritten.

Remove

rpc Remove(RemoveRequest) RemoveResponse

Remove a batch of keys from the SortedMap.

Any keys in the batch that are not present in the map will be ignored.

Get

rpc Get(GetRequest) GetResponse

Fetch a single key's value.

If the key does not exist, the GetResponse leaves the value field unset; the client can use proto's generated field-presence check to determine whether the key existed.

Range

rpc Range(RangeRequest) RangeResponse

Read a range of data from the SortedMap.

The start_key must be less than the end_key (if both are specified), and the results are returned in ascending key order.

The start_key is inclusive and the end_key is exclusive. If the start or end are unset, then the range is unbounded in that direction. If both the start and end keys are unset, the limit smallest keys in the map are returned.

The limit argument is required, to avoid exhausting memory in the client or server.

ReverseRange

rpc ReverseRange(ReverseRangeRequest) ReverseRangeResponse

Read a range of data in descending order from the SortedMap.

This is similar to Range but reads data in reverse order: the start_key must be greater than the end_key (if both are specified), and the results are returned in descending key order.

The start_key is inclusive and the end_key is exclusive. If the start or end are unset, then the range is unbounded in that direction. If both the start and end keys are unset, the limit largest keys in the map are returned.

The limit argument is required, to avoid exhausting memory in the client or server

Messages

Entry

See Range and ReverseRange.

FieldTypeDescription
key stringThe key of an entry.
value bytesThe value of an entry.

GetRequest

See Get.

FieldTypeDescription
key stringnone

GetResponse

See Get.

FieldTypeDescription
oneof _value.valueoptional bytesnone

InsertRequest

See Insert.

FieldTypeDescription
entriesmap InsertRequest.EntriesEntryThe entries to insert.

InsertRequest.EntriesEntry

FieldTypeDescription
key stringnone
value bytesnone

InsertResponse

See Insert.

InvalidRangeError

See Range and ReverseRange. Raised when the requested range is not valid; contains an error string explaining why the range is not valid.

FieldTypeDescription
message stringnone

RangeRequest

See Range.

FieldTypeDescription
oneof _start_key.start_keyoptional stringThe start key to scan from (inclusive). Must be smaller than end_key. If not specified, the range is unbounded at the start.
oneof _end_key.end_keyoptional stringThe end key to scan to (exclusive). If not specified, the range is unbounded at the end.
limit uint32The maximum number of entries to return. Must be specified and non-zero.

RangeResponse

See Range.

FieldTypeDescription
entriesrepeated EntryEntries are in (ascending) key order. We cannot use protobuf's map because it does not preserve insertion order.

RemoveRequest

See Remove.

FieldTypeDescription
keysrepeated stringThe keys to remove.

RemoveResponse

See Remove.

ReverseRangeRequest

See ReverseRange.

FieldTypeDescription
oneof _start_key.start_keyoptional stringThe start key to scan from (inclusive). Must be greater than end_key. If not specified, the range is unbounded at the start.
oneof _end_key.end_keyoptional stringThe end key to scan to (exclusive). If not specified, the range is unbounded at the end.
limit uint32The maximum number of entries to return. Must be specified and non-zero.

ReverseRangeResponse

See ReverseRange.

FieldTypeDescription
entriesrepeated EntryEntries are in (descending) key order. We cannot use protobuf's map because it does not preserve insertion order.

See the protobuf documentation for more information on scalar value types.