Register a new sorted index. Returns the number of entries backfilled. `existing_entries` is an iterator of `(primary_key_bytes, value_bytes)` pairs from the KV hash table, used to populate the index from existing data.
(
&mut self,
tenant_id: u64,
def: SortedIndexDef,
existing_entries: impl Iterator<Item = (Vec<u8>, Vec<u8>)>,
)
| 73 | /// `existing_entries` is an iterator of `(primary_key_bytes, value_bytes)` pairs |
| 74 | /// from the KV hash table, used to populate the index from existing data. |
| 75 | pub fn register( |
| 76 | &mut self, |
| 77 | tenant_id: u64, |
| 78 | def: SortedIndexDef, |
| 79 | existing_entries: impl Iterator<Item = (Vec<u8>, Vec<u8>)>, |
| 80 | ) -> u32 { |
| 81 | let idx_key = index_key(tenant_id, &def.name); |
| 82 | let tbl_key = super::super::engine_helpers::table_key(tenant_id, &def.collection); |
| 83 | |
| 84 | let mut tree = OrderStatTree::new(); |
| 85 | let mut backfilled = 0u32; |
| 86 | |
| 87 | // Backfill from existing data. |
| 88 | for (pk_bytes, value_bytes) in existing_entries { |
| 89 | if let Some(sort_key) = extract_sort_key_from_value(&def, &value_bytes) { |
| 90 | tree.insert(sort_key, pk_bytes); |
| 91 | backfilled += 1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | self.collection_indexes |
| 96 | .entry(tbl_key) |
| 97 | .or_default() |
| 98 | .push(idx_key.clone()); |
| 99 | |
| 100 | self.indexes.insert(idx_key, SortedIndex { def, tree }); |
| 101 | backfilled |
| 102 | } |
| 103 | |
| 104 | /// Drop every sorted index belonging to `(tenant_id, collection)`. |
| 105 | /// Returns the number of indexes removed. |