surrealdb-core 3.2.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
//! SurrealDB KV implementation of DiskANN's provider traits.
//!
//! The upstream DiskANN graph owns search and mutation algorithms, but delegates persistence and
//! distance/computer access through provider traits. This module maps those trait calls onto
//! SurrealDB transactions, keeps the persisted `!de`/`!dn`/`!ds` keys authoritative, and batches
//! cache misses so RocksDB-backed lookups avoid long chains of individual point reads.
//!
//! # Cache coherency invariant
//!
//! The shared [`DiskAnnCache`] is consulted by both committed and in-flight transactions, so a
//! single rule keeps cache and KV from diverging:
//!
//! 1. **Read-only transactions** are the only path allowed to publish KV-sourced state into the
//!    cache. The `read_state` / `read_elements` / `read_nodes` paths gate their cache writes on
//!    `!context.tx.writeable()` for exactly this reason — a writable tx's `tx.get` returns its own
//!    buffered writes, so caching that view would leak uncommitted state to other transactions.
//! 2. **Writable transactions** may write through the cache *only* from within a
//!    `DiskAnnIndex::apply_compaction` frame, because that frame holds the graph write lock across
//!    the commit and clears the per-index cache via [`DiskAnnCache::remove_index`] on apply or
//!    commit failure. The mutating call sites are `set_element`, `delete`, `set_neighbors`,
//!    `append_vector`, and `write_state`.
//!
//! Adding a new cache write from a writable tx outside an `apply_compaction` frame, or relaxing
//! the read-only-tx guard, reintroduces the race described in
//! [issue #7318](https://github.com/surrealdb/surrealdb/issues/7318).

use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;

use anyhow::Result;
use diskann::graph::glue::{
	CopyIds, DefaultPostProcessor, HybridPredicate, InsertStrategy, PruneAccessor, PruneStrategy,
	SearchAccessor, SearchStrategy,
};
use diskann::graph::{AdjacencyList, workingset};
use diskann::provider::{
	DataProvider, Delete, ElementStatus, ExecutionContext, HasId, NeighborAccessor,
	NeighborAccessorMut, NoopGuard, SetElement,
};
use diskann::utils::VectorRepr;
use diskann::{ANNError, ANNResult, default_post_processor};
use diskann_vector::distance::Metric;
use diskann_vector::{Half, PreprocessedDistanceFunction};
use tracing::warn;

use crate::catalog::{DatabaseId, IndexId, NamespaceId, TableId};
use crate::idx::IndexKeyBase;
use crate::idx::trees::diskann::cache::DiskAnnCache;
use crate::idx::trees::diskann::{DiskAnnElement, DiskAnnNode, DiskAnnState, ElementId};
use crate::idx::trees::vector::SerializedVector;
#[cfg(test)]
use crate::idx::trees::vector::Vector;
use crate::kvs::{KVValue, Transaction};

/// Provider execution context passed through the upstream DiskANN trait calls.
#[derive(Clone)]
pub(super) struct DiskAnnProviderContext {
	/// Transaction used for all KV reads and writes in the current graph operation.
	pub(super) tx: Arc<Transaction>,
	/// Index key builder for the graph key families.
	pub(super) ikb: IndexKeyBase,
}

impl ExecutionContext for DiskAnnProviderContext {}

/// Vector element types that DiskANN can read from and write to persisted SurrealDB vectors.
pub(super) trait DiskAnnVectorElement:
	VectorRepr + Copy + Default + Send + Sync + 'static
{
	/// Converts a typed DiskANN slice into SurrealDB's revisioned vector representation.
	fn serialized_from_slice(slice: &[Self]) -> SerializedVector;
	/// Copies a persisted vector into a typed scratch buffer for DiskANN distance computation.
	fn copy_from_serialized(vector: &SerializedVector, buffer: &mut [Self]) -> ANNResult<()>;
}

fn vector_type_mismatch(expected: &str, vector: &SerializedVector) -> ANNError {
	ANNError::log_index_error(format!("DiskANN expected {expected} vector, got {vector:?}"))
}

fn vector_len_mismatch(current: usize, expected: usize) -> ANNError {
	ANNError::log_index_error(format!(
		"DiskANN vector dimension mismatch: got {current}, expected {expected}"
	))
}

impl DiskAnnVectorElement for f32 {
	fn serialized_from_slice(slice: &[Self]) -> SerializedVector {
		SerializedVector::F32(slice.to_vec())
	}

	fn copy_from_serialized(vector: &SerializedVector, buffer: &mut [Self]) -> ANNResult<()> {
		let SerializedVector::F32(values) = vector else {
			return Err(vector_type_mismatch("F32", vector));
		};
		if values.len() != buffer.len() {
			return Err(vector_len_mismatch(values.len(), buffer.len()));
		}
		buffer.copy_from_slice(values);
		Ok(())
	}
}

impl DiskAnnVectorElement for Half {
	fn serialized_from_slice(slice: &[Self]) -> SerializedVector {
		SerializedVector::F16(slice.iter().map(|v| v.to_bits()).collect())
	}

	fn copy_from_serialized(vector: &SerializedVector, buffer: &mut [Self]) -> ANNResult<()> {
		let SerializedVector::F16(values) = vector else {
			return Err(vector_type_mismatch("F16", vector));
		};
		if values.len() != buffer.len() {
			return Err(vector_len_mismatch(values.len(), buffer.len()));
		}
		for (dst, bits) in buffer.iter_mut().zip(values.iter()) {
			*dst = Half::from_bits(*bits);
		}
		Ok(())
	}
}

impl DiskAnnVectorElement for i8 {
	fn serialized_from_slice(slice: &[Self]) -> SerializedVector {
		SerializedVector::I8(slice.to_vec())
	}

	fn copy_from_serialized(vector: &SerializedVector, buffer: &mut [Self]) -> ANNResult<()> {
		let SerializedVector::I8(values) = vector else {
			return Err(vector_type_mismatch("I8", vector));
		};
		if values.len() != buffer.len() {
			return Err(vector_len_mismatch(values.len(), buffer.len()));
		}
		buffer.copy_from_slice(values);
		Ok(())
	}
}

impl DiskAnnVectorElement for u8 {
	fn serialized_from_slice(slice: &[Self]) -> SerializedVector {
		SerializedVector::U8(slice.to_vec())
	}

	fn copy_from_serialized(vector: &SerializedVector, buffer: &mut [Self]) -> ANNResult<()> {
		let SerializedVector::U8(values) = vector else {
			return Err(vector_type_mismatch("U8", vector));
		};
		if values.len() != buffer.len() {
			return Err(vector_len_mismatch(values.len(), buffer.len()));
		}
		buffer.copy_from_slice(values);
		Ok(())
	}
}

/// DiskANN data provider backed by SurrealDB's KV transaction API.
///
/// The upstream DiskANN graph code asks the provider for state, vectors, and adjacency lists during
/// graph search and mutation. This implementation keeps KV as the source of truth, while the shared
/// [`DiskAnnCache`] absorbs hot reads and batches cache misses with `getm`.
pub(super) struct DiskAnnProvider {
	pub(super) ikb: IndexKeyBase,
	table_id: TableId,
	cache: DiskAnnCache,
	dim: usize,
	metric: Metric,
}

impl DiskAnnProvider {
	/// Creates a provider for one DiskANN index and vector type/metric pair.
	pub(super) fn new(
		ikb: IndexKeyBase,
		table_id: TableId,
		cache: DiskAnnCache,
		dim: usize,
		metric: Metric,
	) -> Self {
		Self {
			ikb,
			table_id,
			cache,
			dim,
			metric,
		}
	}

	/// Returns the cache scope tuple for this provider's index.
	fn cache_index(&self) -> (NamespaceId, DatabaseId, TableId, IndexId) {
		(self.ikb.ns(), self.ikb.db(), self.table_id, self.ikb.index())
	}

	/// Builds the provider context for a specific transaction.
	pub(super) fn context(&self, tx: Arc<Transaction>) -> DiskAnnProviderContext {
		DiskAnnProviderContext {
			tx,
			ikb: self.ikb.clone(),
		}
	}

	/// Reads persisted graph state, using the cache for the entry point and next element id.
	///
	/// The `writeable()` guard around `insert_state` enforces rule (1) of the module-level
	/// [cache coherency invariant](self): only read-only transactions publish KV state into the
	/// shared cache. Same rule the doc-id path enforces in `DiskAnnDocs::get_things_batch`
	/// (`docs.rs`).
	async fn read_state(&self, context: &DiskAnnProviderContext) -> Result<DiskAnnState> {
		let index = self.cache_index();
		if let Some(state) = self.cache.get_state(index) {
			return Ok(state);
		}
		let state = context.tx.get(&context.ikb.new_ds_key(), None).await?.unwrap_or_default();
		if !context.tx.writeable() {
			self.cache.insert_state(index, state.clone());
		}
		Ok(state)
	}

	/// Persists graph state and refreshes the cached copy atomically from the caller's view.
	async fn write_state(
		&self,
		context: &DiskAnnProviderContext,
		state: &DiskAnnState,
	) -> Result<()> {
		context.tx.set(&context.ikb.new_ds_key(), state).await?;
		self.cache.insert_state(self.cache_index(), state.clone());
		Ok(())
	}

	/// Reads element vector/status pairs in input order.
	///
	/// Cached entries are returned immediately; only misses are sent to KV through `getm`. Missing
	/// elements remain `None` so callers can preserve the previous single-read behavior.
	async fn read_elements(
		&self,
		context: &DiskAnnProviderContext,
		element_ids: &[ElementId],
	) -> Result<Vec<Option<Arc<DiskAnnElement>>>> {
		let index = self.cache_index();
		let mut elements = vec![None; element_ids.len()];
		let mut miss_positions = Vec::new();
		let mut miss_keys = Vec::new();
		for (pos, element_id) in element_ids.iter().copied().enumerate() {
			if let Some(element) = self.cache.get_element(index, element_id) {
				elements[pos] = Some(element);
			} else {
				miss_positions.push((pos, element_id));
				miss_keys.push(context.ikb.new_de_key(element_id));
			}
		}
		if !miss_keys.is_empty() {
			let fetched: Vec<Option<DiskAnnElement>> = context.tx.getm(miss_keys, None).await?;
			let cache_misses = !context.tx.writeable();
			for ((pos, element_id), element) in miss_positions.into_iter().zip(fetched) {
				if let Some(element) = element {
					elements[pos] = Some(if cache_misses {
						self.cache.insert_element(index, element_id, element)
					} else {
						Arc::new(element)
					});
				}
			}
		}
		Ok(elements)
	}

	/// Reads adjacency lists in input order.
	///
	/// Graph search fans out through adjacency lists, so batching cache misses here removes a
	/// large number of sequential point reads on KV backends that support efficient multi-get.
	async fn read_nodes(
		&self,
		context: &DiskAnnProviderContext,
		element_ids: &[ElementId],
	) -> Result<Vec<Option<Arc<DiskAnnNode>>>> {
		let index = self.cache_index();
		let mut nodes = vec![None; element_ids.len()];
		let mut miss_positions = Vec::new();
		let mut miss_keys = Vec::new();
		for (pos, element_id) in element_ids.iter().copied().enumerate() {
			if let Some(node) = self.cache.get_node(index, element_id) {
				nodes[pos] = Some(node);
			} else {
				miss_positions.push((pos, element_id));
				miss_keys.push(context.ikb.new_dn_key(element_id));
			}
		}
		if !miss_keys.is_empty() {
			let fetched: Vec<Option<DiskAnnNode>> = context.tx.getm(miss_keys, None).await?;
			let cache_misses = !context.tx.writeable();
			for ((pos, element_id), node) in miss_positions.into_iter().zip(fetched) {
				if let Some(node) = node {
					nodes[pos] = Some(if cache_misses {
						self.cache.insert_node(index, element_id, node)
					} else {
						Arc::new(node)
					});
				}
			}
		}
		Ok(nodes)
	}

	/// Allocates and persists the next graph element ID.
	pub(super) async fn allocate_element_id(
		&self,
		context: &DiskAnnProviderContext,
	) -> Result<ElementId> {
		let mut state = self.read_state(context).await?;
		let element_id = state.next_element_id;
		state.next_element_id = state.next_element_id.saturating_add(1);
		self.write_state(context, &state).await?;
		Ok(element_id)
	}

	/// Sets the entry point only when the graph has none.
	pub(super) async fn ensure_entry_point(
		&self,
		context: &DiskAnnProviderContext,
		element_id: ElementId,
	) -> Result<()> {
		let mut state = self.read_state(context).await?;
		if state.enter_point.is_none() {
			state.enter_point = Some(element_id);
			self.write_state(context, &state).await?;
		}
		Ok(())
	}

	/// Replaces the persisted graph entry point.
	pub(super) async fn set_entry_point(
		&self,
		context: &DiskAnnProviderContext,
		element_id: Option<ElementId>,
	) -> Result<()> {
		let mut state = self.read_state(context).await?;
		state.enter_point = element_id;
		self.write_state(context, &state).await
	}

	/// Returns usable graph starting points, repairing deleted entry points with a fallback scan.
	pub(super) async fn valid_starting_points(
		&self,
		context: &DiskAnnProviderContext,
	) -> ANNResult<Vec<ElementId>> {
		let state =
			self.read_state(context).await.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		if let Some(element_id) = state.enter_point
			&& self
				.status_by_internal_id(context, element_id)
				.await
				.is_ok_and(ElementStatus::is_valid)
		{
			return Ok(vec![element_id]);
		}
		// The stored entry point may have been deleted. Scan for a replacement and populate the
		// element cache with any entries touched during the fallback scan.
		let rng =
			context.ikb.new_de_range().map_err(|e| ANNError::log_index_error(e.to_string()))?;
		let mut cursor = context
			.tx
			.open_vals_cursor(rng, crate::idx::planner::ScanDirection::Forward, 0, None)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		let cache_misses = !context.tx.writeable();
		loop {
			let batch = cursor
				.next_batch(crate::kvs::NORMAL_BATCH_SIZE)
				.await
				.map_err(|e| ANNError::log_index_error(e.to_string()))?;
			if batch.is_empty() {
				break;
			}
			for (key, value) in &batch {
				let key: crate::key::index::de::De<'_> = storekey::decode_borrow(key)
					.map_err(|e| ANNError::log_index_error(e.to_string()))?;
				let element = DiskAnnElement::kv_decode_value(value, ())
					.map_err(|e| ANNError::log_index_error(e.to_string()))?;
				let deleted = element.deleted;
				// Only publish non-tombstoned entries into the shared cache, and only from a
				// read-only tx (writable txs may have buffered uncommitted writes; see
				// `read_state`). Tombstones aren't useful for any subsequent reader.
				if !deleted && cache_misses {
					self.cache.insert_element(self.cache_index(), key.element_id, element);
				}
				if !deleted {
					// On a writable tx (i.e. compaction or a record-write path), persist
					// the discovered entry point so subsequent searches don't have to
					// re-scan `!de:*` from the dangling stored value. On a read-only tx
					// (KNN search) we can't write — the next compaction will fix it.
					//
					// Persistence is a pure hint: a transient failure here (e.g. a
					// concurrent `delc` on the state key) shouldn't fail the whole
					// search, since the discovered entry point is already correct in
					// memory and the next compaction will rediscover it.
					if context.tx.writeable()
						&& let Err(e) = self.set_entry_point(context, Some(key.element_id)).await
					{
						warn!(
							error = %e,
							element_id = key.element_id,
							"Failed to persist DiskANN entry-point hint; continuing with in-memory value",
						);
					}
					return Ok(vec![key.element_id]);
				}
			}
		}
		Ok(Vec::new())
	}

	/// Returns present, non-deleted vectors for the requested element ids.
	///
	/// Deleted and missing elements are reported as `None`, matching the pre-cache behavior used by
	/// post-search result construction.
	#[cfg(test)]
	async fn get_vectors(
		&self,
		context: &DiskAnnProviderContext,
		element_ids: &[ElementId],
	) -> Result<Vec<Option<Vector>>> {
		let elements = self.read_elements(context, element_ids).await?;
		Ok(elements
			.into_iter()
			.map(|element| {
				element.and_then(|element| {
					if element.deleted {
						None
					} else {
						Some(Vector::from(element.vector.clone()))
					}
				})
			})
			.collect())
	}
}

impl DataProvider for DiskAnnProvider {
	type Context = DiskAnnProviderContext;
	type InternalId = ElementId;
	type ExternalId = ElementId;
	type Error = ANNError;
	type Guard = NoopGuard<ElementId>;

	fn to_internal_id(
		&self,
		_: &Self::Context,
		gid: &Self::ExternalId,
	) -> Result<Self::InternalId, Self::Error> {
		Ok(*gid)
	}

	fn to_external_id(
		&self,
		_: &Self::Context,
		id: Self::InternalId,
	) -> Result<Self::ExternalId, Self::Error> {
		Ok(id)
	}
}

impl<T> SetElement<&[T]> for DiskAnnProvider
where
	T: DiskAnnVectorElement,
{
	type SetError = ANNError;

	async fn set_element(
		&self,
		context: &Self::Context,
		id: &Self::ExternalId,
		element: &[T],
	) -> Result<Self::Guard, Self::SetError> {
		if element.len() != self.dim {
			return Err(vector_len_mismatch(element.len(), self.dim));
		}
		let key = context.ikb.new_de_key(*id);
		let element = DiskAnnElement {
			vector: T::serialized_from_slice(element),
			deleted: false,
		};
		context
			.tx
			.set(&key, &element)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// Writable-tx cache write-through: only sound because the caller is inside an
		// `apply_compaction` frame that clears the per-index cache on commit/apply failure.
		// See the module-level `Cache coherency invariant` (rule 2).
		self.cache.insert_element(self.cache_index(), *id, element);
		Ok(NoopGuard::new(*id))
	}
}

impl Delete for DiskAnnProvider {
	async fn delete(
		&self,
		context: &Self::Context,
		gid: &Self::ExternalId,
	) -> Result<(), Self::Error> {
		let key = context.ikb.new_de_key(*gid);
		let Some(element) = self
			.read_elements(context, std::slice::from_ref(gid))
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?
			.pop()
			.flatten()
		else {
			return Ok(());
		};
		let mut element = (*element).clone();
		element.deleted = true;
		context
			.tx
			.set(&key, &element)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// Keep the deleted marker hot and visible to subsequent status checks.
		self.cache.insert_element(self.cache_index(), *gid, element);
		Ok(())
	}

	async fn release(
		&self,
		context: &Self::Context,
		id: Self::InternalId,
	) -> Result<(), Self::Error> {
		context
			.tx
			.del(&context.ikb.new_de_key(id))
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// Release removes the element record entirely, so remove any cached vector/status too.
		self.cache.remove_element(self.cache_index(), id);
		Ok(())
	}

	async fn status_by_internal_id(
		&self,
		context: &Self::Context,
		id: Self::InternalId,
	) -> Result<ElementStatus, Self::Error> {
		let Some(element) = self
			.read_elements(context, std::slice::from_ref(&id))
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?
			.pop()
			.flatten()
		else {
			return Err(ANNError::log_index_error(format!("DiskANN element {id} is missing")));
		};
		if element.deleted {
			Ok(ElementStatus::Deleted)
		} else {
			Ok(ElementStatus::Valid)
		}
	}

	fn status_by_external_id(
		&self,
		context: &Self::Context,
		gid: &Self::ExternalId,
	) -> impl std::future::Future<Output = Result<ElementStatus, Self::Error>> + Send {
		self.status_by_internal_id(context, *gid)
	}
}

/// Search accessor used by DiskANN greedy graph search.
///
/// Holds the query distance computer, preprocessed once from the query vector at
/// construction, plus a reusable scratch buffer for decoding persisted vectors into typed
/// slices during distance evaluation.
pub(super) struct DiskAnnSearchAccessor<'a, T: DiskAnnVectorElement> {
	/// Provider that owns KV/cache access.
	provider: &'a DiskAnnProvider,
	/// Transaction/key context for the current DiskANN operation.
	context: &'a DiskAnnProviderContext,
	/// Query distance computer, preprocessed from the search/insert query vector.
	query: <T as VectorRepr>::QueryDistance,
	/// Reusable scratch buffer for converting persisted vectors into typed slices.
	buffer: Box<[T]>,
}

impl<'a, T> DiskAnnSearchAccessor<'a, T>
where
	T: DiskAnnVectorElement,
{
	fn new(
		provider: &'a DiskAnnProvider,
		context: &'a DiskAnnProviderContext,
		query: &[T],
	) -> Result<Self, ANNError> {
		if query.len() != provider.dim {
			return Err(vector_len_mismatch(query.len(), provider.dim));
		}
		Ok(Self {
			provider,
			context,
			query: T::query_distance(query, provider.metric),
			buffer: vec![T::default(); provider.dim].into_boxed_slice(),
		})
	}

	/// Decodes `element` into the scratch buffer and returns its distance to the query.
	fn distance_to(&mut self, element: &DiskAnnElement) -> ANNResult<f32> {
		T::copy_from_serialized(&element.vector, &mut self.buffer)?;
		Ok(self.query.evaluate_similarity(&self.buffer))
	}
}

impl<T: DiskAnnVectorElement> HasId for DiskAnnSearchAccessor<'_, T> {
	type Id = ElementId;
}

impl<T> SearchAccessor for DiskAnnSearchAccessor<'_, T>
where
	T: DiskAnnVectorElement,
{
	async fn starting_points(&self) -> ANNResult<Vec<ElementId>> {
		self.provider.valid_starting_points(self.context).await
	}

	async fn start_point_distances<F>(&mut self, mut f: F) -> ANNResult<()>
	where
		F: FnMut(ElementId, f32) + Send,
	{
		let ids = self.provider.valid_starting_points(self.context).await?;
		let elements = self
			.provider
			.read_elements(self.context, &ids)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		for (id, element) in ids.into_iter().zip(elements) {
			let Some(element) = element else {
				return Err(ANNError::log_index_error(format!(
					"DiskANN start point {id} is missing"
				)));
			};
			let distance = self.distance_to(&element)?;
			f(id, distance);
		}
		Ok(())
	}

	/// Expands one search beam using batched adjacency-list reads and batched candidate reads.
	///
	/// The default DiskANN implementation walks neighbors and fetches vectors one node at a
	/// time. This override reads every adjacency list for `ids` in a single `getm`, keeps the
	/// candidates the predicate admits (`eval_mut` both filters and deduplicates), then reads
	/// their vectors in a second `getm` before computing query distances.
	async fn expand_beam<Itr, P, F>(
		&mut self,
		ids: Itr,
		mut pred: P,
		mut on_neighbors: F,
	) -> ANNResult<()>
	where
		Itr: Iterator<Item = ElementId> + Send,
		P: HybridPredicate<ElementId> + Send + Sync,
		F: FnMut(ElementId, f32) + Send,
	{
		let ids: Vec<_> = ids.collect();
		let nodes = self
			.provider
			.read_nodes(self.context, &ids)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// A well-behaved predicate never re-admits an id it has already yielded, so filtering
		// the neighbors with `eval_mut` also deduplicates the surviving candidates.
		let mut candidates = Vec::new();
		for node in nodes.iter().flatten() {
			for &id in &node.neighbors {
				if pred.eval_mut(&id) {
					candidates.push(id);
				}
			}
		}
		let elements = self
			.provider
			.read_elements(self.context, &candidates)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		for (id, element) in candidates.into_iter().zip(elements) {
			// A candidate whose element was compacted away mid-search is dropped: the
			// `expand_beam` contract tolerates skipping candidates in exceptional cases.
			if let Some(element) = element {
				let distance = self.distance_to(&element)?;
				on_neighbors(id, distance);
			}
		}
		Ok(())
	}
}

/// Working-set cache of decoded vectors, reused across a prune operation's rounds.
type DiskAnnWorkingSet<T> = workingset::Map<ElementId, Box<[T]>, workingset::map::Ref<[T]>>;
/// Read-only view over a [`DiskAnnWorkingSet`], returned to the prune algorithm by `fill`.
type DiskAnnWorkingSetView<'a, T> =
	workingset::map::View<'a, ElementId, Box<[T]>, workingset::map::Ref<[T]>>;

/// Prune accessor used by DiskANN index construction (insert and in-place delete).
///
/// Owns a bounded working-set cache that [`PruneAccessor::fill`] populates from batched KV
/// reads, and hands the prune algorithm a view over it together with an element-to-element
/// distance computer.
pub(super) struct DiskAnnPruneAccessor<'a, T: DiskAnnVectorElement> {
	/// Provider that owns KV/cache access.
	provider: &'a DiskAnnProvider,
	/// Transaction/key context for the current DiskANN operation.
	context: &'a DiskAnnProviderContext,
	/// Bounded cache of decoded vectors, reused across the operation's prune rounds.
	set: DiskAnnWorkingSet<T>,
}

impl<'a, T> DiskAnnPruneAccessor<'a, T>
where
	T: DiskAnnVectorElement,
{
	fn new(
		provider: &'a DiskAnnProvider,
		context: &'a DiskAnnProviderContext,
		set: DiskAnnWorkingSet<T>,
	) -> Self {
		Self {
			provider,
			context,
			set,
		}
	}
}

impl<T: DiskAnnVectorElement> HasId for DiskAnnPruneAccessor<'_, T> {
	type Id = ElementId;
}

impl<T> PruneAccessor for DiskAnnPruneAccessor<'_, T>
where
	T: DiskAnnVectorElement,
{
	type Neighbors<'b>
		= DiskAnnNeighborAccessor<'b>
	where
		Self: 'b;
	type ElementRef<'b> = &'b [T];
	type View<'b>
		= DiskAnnWorkingSetView<'b, T>
	where
		Self: 'b;
	type Distance<'b>
		= <T as VectorRepr>::Distance
	where
		Self: 'b;

	fn neighbors(&mut self) -> Self::Neighbors<'_> {
		DiskAnnNeighborAccessor {
			provider: self.provider,
			context: self.context,
		}
	}

	/// Makes the vectors for `itr` available in the returned view alongside a distance computer.
	///
	/// The upstream `Map::fill` closure is synchronous, but SurrealDB reads vectors from the
	/// transaction asynchronously. So this first batches the ids the working set does not
	/// already cache into a single `getm`, decodes them, then feeds the prefetched vectors to
	/// `Map::fill` through a synchronous closure. Deleted and missing elements are skipped,
	/// which the prune algorithm tolerates.
	async fn fill<Itr>(&mut self, itr: Itr) -> ANNResult<(Self::View<'_>, Self::Distance<'_>)>
	where
		Itr: ExactSizeIterator<Item = ElementId> + Clone + Send + Sync,
	{
		let missing: Vec<ElementId> = itr.clone().filter(|id| !self.set.contains_key(id)).collect();
		let fetched = self
			.provider
			.read_elements(self.context, &missing)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		let mut decoded: HashMap<ElementId, Box<[T]>> = HashMap::with_capacity(missing.len());
		for (id, element) in missing.into_iter().zip(fetched) {
			if let Some(element) = element
				&& !element.deleted
			{
				let mut buffer = vec![T::default(); self.provider.dim].into_boxed_slice();
				T::copy_from_serialized(&element.vector, &mut buffer)?;
				decoded.insert(id, buffer);
			}
		}
		let distance = T::distance(self.provider.metric, Some(self.provider.dim));
		let view =
			self.set.fill(itr, |id| -> ANNResult<Option<Box<[T]>>> { Ok(decoded.remove(&id)) })?;
		Ok((view, distance))
	}
}

/// Accessor for reading and mutating DiskANN adjacency lists.
#[derive(Clone, Copy)]
pub(super) struct DiskAnnNeighborAccessor<'a> {
	/// Provider that owns adjacency-list KV/cache access.
	provider: &'a DiskAnnProvider,
	/// Transaction/key context for the current DiskANN operation.
	context: &'a DiskAnnProviderContext,
}

impl HasId for DiskAnnNeighborAccessor<'_> {
	type Id = ElementId;
}

impl NeighborAccessor for DiskAnnNeighborAccessor<'_> {
	async fn get_neighbors(
		&mut self,
		id: ElementId,
		neighbors: &mut AdjacencyList<ElementId>,
	) -> ANNResult<()> {
		if let Some(node) = self
			.provider
			.read_nodes(self.context, &[id])
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?
			.into_iter()
			.next()
			.flatten()
		{
			neighbors.overwrite_trusted(&node.neighbors);
		} else {
			neighbors.clear();
		}
		Ok(())
	}
}

impl NeighborAccessorMut for DiskAnnNeighborAccessor<'_> {
	async fn set_neighbors(&mut self, id: ElementId, neighbors: &[ElementId]) -> ANNResult<()> {
		let node = DiskAnnNode {
			neighbors: neighbors.to_vec(),
		};
		self.context
			.tx
			.set(&self.context.ikb.new_dn_key(id), &node)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// Keep cached adjacency coherent with the persisted graph mutation.
		self.provider.cache.insert_node(self.provider.cache_index(), id, node);
		Ok(())
	}

	async fn append_vector(&mut self, id: ElementId, neighbors: &[ElementId]) -> ANNResult<()> {
		let key = self.context.ikb.new_dn_key(id);
		let mut node: DiskAnnNode = self
			.provider
			.read_nodes(self.context, &[id])
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?
			.into_iter()
			.next()
			.flatten()
			.map(|node| (*node).clone())
			.unwrap_or_default();
		for neighbor in neighbors {
			if !node.neighbors.contains(neighbor) {
				node.neighbors.push(*neighbor);
			}
		}
		self.context
			.tx
			.set(&key, &node)
			.await
			.map_err(|e| ANNError::log_index_error(e.to_string()))?;
		// Keep cached adjacency coherent with the persisted graph mutation.
		self.provider.cache.insert_node(self.provider.cache_index(), id, node);
		Ok(())
	}
}

/// DiskANN search/insert/prune strategy using SurrealDB's provider accessors.
#[derive(Debug)]
pub(super) struct DiskAnnStrategy<T>(PhantomData<fn() -> T>);

impl<T> Default for DiskAnnStrategy<T> {
	fn default() -> Self {
		Self(PhantomData)
	}
}

impl<T> Clone for DiskAnnStrategy<T> {
	fn clone(&self) -> Self {
		Self::default()
	}
}

impl<'a, T> SearchStrategy<'a, DiskAnnProvider, &'a [T]> for DiskAnnStrategy<T>
where
	T: DiskAnnVectorElement,
{
	type SearchAccessorError = ANNError;
	type SearchAccessor = DiskAnnSearchAccessor<'a, T>;

	fn search_accessor(
		&'a self,
		provider: &'a DiskAnnProvider,
		context: &'a DiskAnnProviderContext,
		query: &'a [T],
	) -> Result<DiskAnnSearchAccessor<'a, T>, ANNError> {
		DiskAnnSearchAccessor::new(provider, context, query)
	}
}

impl<'a, T> DefaultPostProcessor<'a, DiskAnnProvider, &'a [T]> for DiskAnnStrategy<T>
where
	T: DiskAnnVectorElement,
{
	default_post_processor!(CopyIds);
}

impl<T> PruneStrategy<DiskAnnProvider> for DiskAnnStrategy<T>
where
	T: DiskAnnVectorElement,
{
	type PruneAccessor<'a> = DiskAnnPruneAccessor<'a, T>;
	type PruneAccessorError = ANNError;

	fn prune_accessor<'a>(
		&'a self,
		provider: &'a DiskAnnProvider,
		context: &'a DiskAnnProviderContext,
		capacity: usize,
	) -> Result<Self::PruneAccessor<'a>, ANNError> {
		let set = workingset::map::Builder::new(workingset::map::Capacity::Default).build(capacity);
		Ok(DiskAnnPruneAccessor::new(provider, context, set))
	}
}

impl<'a, T> InsertStrategy<'a, DiskAnnProvider, &'a [T]> for DiskAnnStrategy<T>
where
	T: DiskAnnVectorElement,
{
	type PruneStrategy = Self;

	fn prune_strategy(&self) -> Self::PruneStrategy {
		self.clone()
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::catalog::{DatabaseId, IndexId, NamespaceId, TableId};
	use crate::idx::trees::diskann::cache::DiskAnnCache;
	use crate::kvs::{Datastore, LockType, TransactionType};

	fn ikb() -> IndexKeyBase {
		IndexKeyBase::new(NamespaceId(1), DatabaseId(2), "tb".into(), IndexId(3))
	}

	async fn provider_and_context() -> Result<(DiskAnnProvider, DiskAnnProviderContext)> {
		let ds = Datastore::new("memory").await?;
		let tx = Arc::new(ds.transaction(TransactionType::Write, LockType::Optimistic).await?);
		let ikb = ikb();
		let provider =
			DiskAnnProvider::new(ikb, TableId(4), DiskAnnCache::new(1024 * 1024), 2, Metric::L2);
		let context = provider.context(tx);
		Ok((provider, context))
	}

	#[tokio::test]
	async fn diskann_provider_batches_element_reads_and_reports_missing() -> Result<()> {
		let (provider, context) = provider_and_context().await?;
		context
			.tx
			.set(
				&context.ikb.new_de_key(1),
				&DiskAnnElement {
					vector: SerializedVector::F32(vec![1.0, 2.0]),
					deleted: false,
				},
			)
			.await?;
		context
			.tx
			.set(
				&context.ikb.new_de_key(2),
				&DiskAnnElement {
					vector: SerializedVector::F32(vec![3.0, 4.0]),
					deleted: false,
				},
			)
			.await?;

		// A single `getm`-backed read returns present elements in input order and `None` for a
		// missing id.
		let elements = provider.read_elements(&context, &[1, 2, 3]).await?;
		assert_eq!(
			elements[0].as_ref().map(|e| e.vector.clone()),
			Some(SerializedVector::F32(vec![1.0, 2.0]))
		);
		assert_eq!(
			elements[1].as_ref().map(|e| e.vector.clone()),
			Some(SerializedVector::F32(vec![3.0, 4.0]))
		);
		assert!(elements[2].is_none());
		// Provider reads through a writable tx must not publish into the shared cache —
		// `tx.get` returns the writer's own buffered (possibly uncommitted) writes, and the
		// cache is process-wide, so leaking that view to other txs is the failure mode
		// #7318 surfaced.
		assert!(provider.cache.get_element(provider.cache_index(), 1).is_none());
		assert!(provider.cache.get_element(provider.cache_index(), 2).is_none());
		Ok(())
	}

	#[tokio::test]
	async fn diskann_provider_batches_nodes_and_updates_cache_on_mutation() -> Result<()> {
		let (provider, context) = provider_and_context().await?;
		context
			.tx
			.set(
				&context.ikb.new_dn_key(1),
				&DiskAnnNode {
					neighbors: vec![2, 3],
				},
			)
			.await?;
		context
			.tx
			.set(
				&context.ikb.new_dn_key(2),
				&DiskAnnNode {
					neighbors: vec![4],
				},
			)
			.await?;

		let nodes = provider.read_nodes(&context, &[1, 2]).await?;
		assert_eq!(nodes[0].as_ref().unwrap().neighbors, vec![2, 3]);
		assert_eq!(nodes[1].as_ref().unwrap().neighbors, vec![4]);

		let mut neighbor_accessor = DiskAnnNeighborAccessor {
			provider: &provider,
			context: &context,
		};
		neighbor_accessor.set_neighbors(1, &[8, 9]).await.unwrap();

		let cached = provider.cache.get_node(provider.cache_index(), 1).unwrap();
		assert_eq!(cached.neighbors, vec![8, 9]);
		Ok(())
	}

	#[tokio::test]
	async fn diskann_provider_batch_vectors_omits_deleted_elements() -> Result<()> {
		let (provider, context) = provider_and_context().await?;
		context
			.tx
			.set(
				&context.ikb.new_de_key(1),
				&DiskAnnElement {
					vector: SerializedVector::F32(vec![1.0, 2.0]),
					deleted: false,
				},
			)
			.await?;
		context
			.tx
			.set(
				&context.ikb.new_de_key(2),
				&DiskAnnElement {
					vector: SerializedVector::F32(vec![3.0, 4.0]),
					deleted: true,
				},
			)
			.await?;

		let vectors = provider.get_vectors(&context, &[1, 2, 3]).await?;
		assert!(vectors[0].is_some());
		assert!(vectors[1].is_none());
		assert!(vectors[2].is_none());
		Ok(())
	}
}