surrealdb-core-nightly 2.1.20250210

A nightly release of the surrealdb-core crate
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
use crate::cnf::NORMAL_FETCH_SIZE;
use crate::ctx::{Context, MutableContext};
use crate::dbs::distinct::SyncDistinct;
use crate::dbs::{Iterable, Iterator, Operable, Options, Processed, Statement};
use crate::err::Error;
use crate::idx::planner::iterators::{IndexItemRecord, IteratorRef, ThingIterator};
use crate::idx::planner::{IterationStage, RecordStrategy};
use crate::key::{graph, thing};
use crate::kvs::{Key, KeyDecode, KeyEncode, Transaction, Val};
use crate::sql::dir::Dir;
use crate::sql::id::range::IdRange;
use crate::sql::{Edges, Table, Thing, Value};
use futures::StreamExt;
use reblessive::tree::Stk;
use std::borrow::Cow;
use std::ops::{Bound, Range};
use std::sync::Arc;
use std::vec;

impl Iterable {
	pub(super) async fn iterate(
		self,
		stk: &mut Stk,
		ctx: &Context,
		opt: &Options,
		stm: &Statement<'_>,
		ite: &mut Iterator,
		dis: Option<&mut SyncDistinct>,
	) -> Result<(), Error> {
		if self.iteration_stage_check(ctx) {
			let txn = ctx.tx();
			let mut coll = ConcurrentCollector {
				stk,
				ctx,
				opt,
				txn: &txn,
				stm,
				ite,
			};
			if let Some(dis) = dis {
				let mut coll = ConcurrentDistinctCollector {
					coll,
					dis,
				};
				coll.collect_iterable(ctx, opt, self).await?;
			} else {
				coll.collect_iterable(ctx, opt, self).await?;
			}
		}
		Ok(())
	}

	fn iteration_stage_check(&self, ctx: &Context) -> bool {
		match self {
			Iterable::Table(tb, _) | Iterable::Index(tb, _, _) => {
				if let Some(IterationStage::BuildKnn) = ctx.get_iteration_stage() {
					if let Some(qp) = ctx.get_query_planner() {
						if let Some(exe) = qp.get_query_executor(tb) {
							return exe.has_bruteforce_knn();
						}
					}
				}
			}
			_ => {}
		}
		true
	}
}

pub(super) enum Collected {
	Edge(Key),
	RangeKey(Key),
	TableKey(Key),
	Relatable {
		f: Thing,
		v: Thing,
		w: Thing,
		o: Option<Value>,
	},
	Thing(Thing),
	Yield(Table),
	Value(Value),
	Defer(Thing),
	Mergeable(Thing, Value),
	KeyVal(Key, Val),
	Count(usize),
	IndexItem(IndexItemRecord),
	IndexItemKey(IndexItemRecord),
}

impl Collected {
	pub(super) async fn process(
		self,
		opt: &Options,
		txn: &Transaction,
		rid_only: bool,
	) -> Result<Processed, Error> {
		match self {
			Self::Edge(key) => Self::process_edge(opt, txn, key, rid_only).await,
			Self::RangeKey(key) => Self::process_range_key(key).await,
			Self::TableKey(key) => Self::process_table_key(key).await,
			Self::Relatable {
				f,
				v,
				w,
				o,
			} => Self::process_relatable(opt, txn, f, v, w, o, rid_only).await,
			Self::Thing(thing) => Self::process_thing(opt, txn, thing, rid_only).await,
			Self::Yield(table) => Self::process_yield(opt, txn, table, rid_only).await,
			Self::Value(value) => Ok(Self::process_value(value)),
			Self::Defer(key) => Self::process_defer(opt, txn, key, rid_only).await,
			Self::Mergeable(v, o) => Self::process_mergeable(opt, txn, v, o, rid_only).await,
			Self::KeyVal(key, val) => Ok(Self::process_key_val(key, val)?),
			Self::Count(c) => Ok(Self::process_count(c)),
			Self::IndexItem(i) => Self::process_index_item(opt, txn, i, rid_only).await,
			Self::IndexItemKey(i) => Ok(Self::process_index_item_key(i)),
		}
	}

	async fn process_edge(
		opt: &Options,
		txn: &Transaction,
		key: Key,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// Parse the data from the store
		let gra: graph::Graph = graph::Graph::decode(&key)?;
		// Fetch the data from the store
		let val = if rid_only {
			Arc::new(Value::Null)
		} else {
			let (ns, db) = opt.ns_db()?;
			txn.get_record(ns, db, gra.ft, &gra.fk, None).await?
		};
		let rid = Thing::from((gra.ft, gra.fk));
		// Parse the data from the store
		let val = Operable::Value(val);
		// Process the record
		Ok(Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(rid.into()),
			ir: None,
			val,
		})
	}

	async fn process_range_key(key: Key) -> Result<Processed, Error> {
		let key = thing::Thing::decode(&key)?;
		let val = Value::Null;
		let rid = Thing::from((key.tb, key.id));
		// Create a new operable value
		let val = Operable::Value(val.into());
		// Process the record
		let pro = Processed {
			rs: RecordStrategy::KeysOnly,
			generate: None,
			rid: Some(rid.into()),
			ir: None,
			val,
		};
		Ok(pro)
	}

	async fn process_table_key(key: Key) -> Result<Processed, Error> {
		let key = thing::Thing::decode(&key)?;
		let rid = Thing::from((key.tb, key.id));
		// Process the record
		let pro = Processed {
			rs: RecordStrategy::KeysOnly,
			generate: None,
			rid: Some(rid.into()),
			ir: None,
			val: Operable::Value(Value::Null.into()),
		};
		Ok(pro)
	}

	async fn process_relatable(
		opt: &Options,
		txn: &Transaction,
		f: Thing,
		v: Thing,
		w: Thing,
		o: Option<Value>,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// if it is skippable we only need the record id
		let val = if rid_only {
			Operable::Value(Arc::new(Value::Null))
		} else {
			// Check that the table exists
			let (ns, db) = opt.ns_db()?;
			txn.check_ns_db_tb(ns, db, &v.tb, opt.strict).await?;
			// Fetch the data from the store
			let val = txn.get_record(ns, db, &v.tb, &v.id, None).await?;
			// Create a new operable value
			Operable::Relate(f, val, w, o.map(|v| v.into()))
		};
		// Process the document record
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(v.into()),
			ir: None,
			val,
		};
		Ok(pro)
	}

	async fn process_thing(
		opt: &Options,
		txn: &Transaction,
		v: Thing,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// if it is skippable we only need the record id
		let val = if rid_only {
			Arc::new(Value::Null)
		} else {
			// Check that the table exists
			let (ns, db) = opt.ns_db()?;
			txn.check_ns_db_tb(ns, db, &v.tb, opt.strict).await?;
			// Fetch the data from the store
			txn.get_record(ns, db, &v.tb, &v.id, opt.version).await?
		};
		// Parse the data from the store
		let val = Operable::Value(val);
		// Process the document record
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(v.into()),
			ir: None,
			val,
		};
		// Everything ok
		Ok(pro)
	}

	async fn process_yield(
		opt: &Options,
		txn: &Transaction,
		v: Table,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// if it is skippable we only need the record id
		if !rid_only {
			// Check that the table exists
			let (ns, db) = opt.ns_db()?;
			txn.check_ns_db_tb(ns, db, &v, opt.strict).await?;
		}
		// Pass the value through
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: Some(v),
			rid: None,
			ir: None,
			val: Operable::Value(Value::None.into()),
		};
		Ok(pro)
	}

	fn process_value(v: Value) -> Processed {
		// Pass the value through
		Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: None,
			ir: None,
			val: Operable::Value(v.into()),
		}
	}

	async fn process_defer(
		opt: &Options,
		txn: &Transaction,
		v: Thing,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// if it is skippable we only need the record id
		if !rid_only {
			// Check that the table exists
			let (ns, db) = opt.ns_db()?;
			txn.check_ns_db_tb(ns, db, &v.tb, opt.strict).await?;
		}
		// Process the document record
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(v.into()),
			ir: None,
			val: Operable::Value(Value::None.into()),
		};
		Ok(pro)
	}

	async fn process_mergeable(
		opt: &Options,
		txn: &Transaction,
		v: Thing,
		o: Value,
		rid_only: bool,
	) -> Result<Processed, Error> {
		// if it is skippable we only need the record id
		if !rid_only {
			// Check that the table exists
			let (ns, db) = opt.ns_db()?;
			txn.check_ns_db_tb(ns, db, &v.tb, opt.strict).await?;
		}
		// Process the document record
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(v.into()),
			ir: None,
			val: Operable::Insert(Value::None.into(), o.into()),
		};
		// Everything ok
		Ok(pro)
	}

	fn process_key_val(key: Key, val: Val) -> Result<Processed, Error> {
		let key = thing::Thing::decode(&key)?;
		let val: Value = revision::from_slice(&val)?;
		let rid = Thing::from((key.tb, key.id));
		// Create a new operable value
		let val = Operable::Value(val.into());
		// Process the record
		Ok(Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(rid.into()),
			ir: None,
			val,
		})
	}

	fn process_count(count: usize) -> Processed {
		Processed {
			rs: RecordStrategy::Count,
			generate: None,
			rid: None,
			ir: None,
			val: Operable::Count(count),
		}
	}

	fn process_index_item_key(i: IndexItemRecord) -> Processed {
		let (t, v, ir) = i.consume();
		Processed {
			rs: RecordStrategy::KeysOnly,
			generate: None,
			rid: Some(t),
			ir: Some(Arc::new(ir)),
			val: Operable::Value(v.unwrap_or_else(|| Value::Null.into())),
		}
	}

	async fn process_index_item(
		opt: &Options,
		txn: &Transaction,
		i: IndexItemRecord,
		rid_only: bool,
	) -> Result<Processed, Error> {
		let (t, v, ir) = i.consume();
		let v = if let Some(v) = v {
			// The value may already be fetched by the KNN iterator to evaluate the condition
			v
		} else if rid_only {
			// if it is skippable we only need the record id
			Value::Null.into()
		} else {
			Iterable::fetch_thing(txn, opt, &t).await?
		};
		let pro = Processed {
			rs: RecordStrategy::KeysAndValues,
			generate: None,
			rid: Some(t),
			ir: Some(ir.into()),
			val: Operable::Value(v),
		};
		Ok(pro)
	}
}

pub(super) struct ConcurrentCollector<'a> {
	stk: &'a mut Stk,
	ctx: &'a Context,
	opt: &'a Options,
	txn: &'a Transaction,
	stm: &'a Statement<'a>,
	ite: &'a mut Iterator,
}
impl Collector for ConcurrentCollector<'_> {
	async fn collect(&mut self, collected: Collected) -> Result<(), Error> {
		// if it is skippable don't need to process the document
		if self.ite.skippable() == 0 {
			let pro = collected.process(self.opt, self.txn, false).await?;
			self.ite.process(self.stk, self.ctx, self.opt, self.stm, pro).await?;
		} else {
			self.ite.skipped(1);
		}
		Ok(())
	}

	fn iterator(&mut self) -> &mut Iterator {
		self.ite
	}
}

pub(super) struct ConcurrentDistinctCollector<'a> {
	coll: ConcurrentCollector<'a>,
	dis: &'a mut SyncDistinct,
}

impl Collector for ConcurrentDistinctCollector<'_> {
	async fn collect(&mut self, collected: Collected) -> Result<(), Error> {
		let skippable = self.coll.ite.skippable() > 0;
		// If it is skippable, we just need to collect the record id (if any)
		// to ensure that distinct can be checked.
		let pro = collected.process(self.coll.opt, self.coll.txn, skippable).await?;
		if !self.dis.check_already_processed(&pro) {
			if !skippable {
				self.coll
					.ite
					.process(self.coll.stk, self.coll.ctx, self.coll.opt, self.coll.stm, pro)
					.await?;
			} else {
				self.coll.ite.skipped(1);
			}
		}
		Ok(())
	}

	fn iterator(&mut self) -> &mut Iterator {
		self.coll.ite
	}
}

pub(super) trait Collector {
	async fn collect(&mut self, collected: Collected) -> Result<(), Error>;

	fn iterator(&mut self) -> &mut Iterator;

	fn check_query_planner_context<'b>(ctx: &'b Context, table: &'b Table) -> Cow<'b, Context> {
		if let Some(qp) = ctx.get_query_planner() {
			if let Some(exe) = qp.get_query_executor(&table.0) {
				// We set the query executor matching the current table in the Context
				// Avoiding search in the hashmap of the query planner for each doc
				let mut ctx = MutableContext::new(ctx);
				ctx.set_query_executor(exe.clone());
				return Cow::Owned(ctx.freeze());
			}
		}
		Cow::Borrowed(ctx)
	}

	async fn collect_iterable(
		&mut self,
		ctx: &Context,
		opt: &Options,
		iterable: Iterable,
	) -> Result<(), Error> {
		if ctx.is_ok(true) {
			match iterable {
				Iterable::Value(v) => {
					if v.is_some() {
						return self.collect(Collected::Value(v)).await;
					}
				}
				Iterable::Yield(v) => self.collect(Collected::Yield(v)).await?,
				Iterable::Thing(v) => self.collect(Collected::Thing(v)).await?,
				Iterable::Defer(v) => self.collect(Collected::Defer(v)).await?,
				Iterable::Edges(e) => self.collect_edges(ctx, opt, e).await?,
				Iterable::Range(tb, v, rs) => match rs {
					RecordStrategy::Count => self.collect_range_count(ctx, opt, &tb, v).await?,
					RecordStrategy::KeysOnly => self.collect_range_keys(ctx, opt, &tb, v).await?,
					RecordStrategy::KeysAndValues => self.collect_range(ctx, opt, &tb, v).await?,
				},
				Iterable::Table(v, rs) => {
					let ctx = Self::check_query_planner_context(ctx, &v);
					match rs {
						RecordStrategy::Count => self.collect_table_count(&ctx, opt, &v).await?,
						RecordStrategy::KeysOnly => self.collect_table_keys(&ctx, opt, &v).await?,
						RecordStrategy::KeysAndValues => self.collect_table(&ctx, opt, &v).await?,
					}
				}
				Iterable::Index(v, irf, rs) => {
					if let Some(qp) = ctx.get_query_planner() {
						if let Some(exe) = qp.get_query_executor(&v.0) {
							// We set the query executor matching the current table in the Context
							// Avoiding search in the hashmap of the query planner for each doc
							let mut ctx = MutableContext::new(ctx);
							ctx.set_query_executor(exe.clone());
							let ctx = ctx.freeze();
							return self.collect_index_items(&ctx, opt, &v, irf, rs).await;
						}
					}
					self.collect_index_items(ctx, opt, &v, irf, rs).await?
				}
				Iterable::Mergeable(v, o) => self.collect(Collected::Mergeable(v, o)).await?,
				Iterable::Relatable(f, v, w, o) => {
					self.collect(Collected::Relatable {
						f,
						v,
						w,
						o,
					})
					.await?
				}
			}
		}
		Ok(())
	}

	async fn start_skip(
		&mut self,
		ctx: &Context,
		txn: &Transaction,
		mut rng: Range<Key>,
	) -> Result<Option<Range<Key>>, Error> {
		let ite = self.iterator();
		let skippable = ite.skippable();
		if skippable == 0 {
			// There is nothing to skip, we return the original range.
			return Ok(Some(rng));
		}
		// We only need to iterate over keys.
		let mut stream = txn.stream_keys(rng.clone(), Some(skippable));
		let mut skipped = 0;
		let mut last_key = vec![];
		while let Some(res) = stream.next().await {
			if ctx.is_done(skipped % 100 == 0) {
				break;
			}
			last_key = res?;
			skipped += 1;
		}
		// Update the iterator about the number of skipped keys
		ite.skipped(skipped);
		// If we don't have a last key we're done
		if last_key.is_empty() {
			return Ok(None);
		}
		// We set the range for the next iteration
		last_key.push(0xFF);
		rng.start = last_key;
		Ok(Some(rng))
	}

	async fn collect_table(
		&mut self,
		ctx: &Context,
		opt: &Options,
		v: &Table,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Check that the table exists
		let (ns, db) = opt.ns_db()?;
		txn.check_ns_db_tb(ns, db, v, opt.strict).await?;
		// Prepare the start and end keys
		let beg = thing::prefix(ns, db, v)?;
		let end = thing::suffix(ns, db, v)?;
		// Optionally skip keys
		let rng = if let Some(r) = self.start_skip(ctx, &txn, beg..end).await? {
			r
		} else {
			return Ok(());
		};
		// Create a new iterable range
		let mut stream = txn.stream(rng, opt.version, None);

		// Loop until no more entries
		let mut count = 0;
		while let Some(res) = stream.next().await {
			// Check if the context is finished
			if ctx.is_done(count % 100 == 0) {
				break;
			}
			// Parse the data from the store
			let (k, v) = res?;
			self.collect(Collected::KeyVal(k, v)).await?;
			count += 1;
		}
		// Everything ok
		Ok(())
	}

	async fn collect_table_keys(
		&mut self,
		ctx: &Context,
		opt: &Options,
		v: &Table,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Check that the table exists
		let (ns, db) = opt.ns_db()?;
		txn.check_ns_db_tb(ns, db, v, opt.strict).await?;
		// Prepare the start and end keys
		let beg = thing::prefix(ns, db, v)?;
		let end = thing::suffix(ns, db, v)?;
		// Optionally skip keys
		let rng = if let Some(rng) = self.start_skip(ctx, &txn, beg..end).await? {
			// Returns the next range of keys
			rng
		} else {
			// There is nothing left to iterate
			return Ok(());
		};
		// Create a new iterable range
		let mut stream = txn.stream_keys(rng, None);
		// Loop until no more entries
		let mut count = 0;
		while let Some(res) = stream.next().await {
			// Check if the context is finished
			if ctx.is_done(count % 100 == 0) {
				break;
			}
			// Parse the data from the store
			let k = res?;
			// Collect the key
			self.collect(Collected::TableKey(k)).await?;
			count += 1;
		}
		// Everything ok
		Ok(())
	}

	async fn collect_table_count(
		&mut self,
		ctx: &Context,
		opt: &Options,
		v: &Table,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Check that the table exists
		let (ns, db) = opt.ns_db()?;
		txn.check_ns_db_tb(ns, db, v, opt.strict).await?;
		// Prepare the start and end keys
		let beg = thing::prefix(ns, db, v)?;
		let end = thing::suffix(ns, db, v)?;
		// Create a new iterable range
		let count = txn.count(beg..end).await?;
		// Collect the count
		self.collect(Collected::Count(count)).await?;
		// Everything ok
		Ok(())
	}

	async fn range_prepare(
		txn: &Transaction,
		opt: &Options,
		tb: &str,
		r: IdRange,
	) -> Result<(Vec<u8>, Vec<u8>), Error> {
		// Check that the table exists
		let (ns, db) = opt.ns_db()?;
		txn.check_ns_db_tb(ns, db, tb, opt.strict).await?;
		// Prepare the range start key
		let beg = match &r.beg {
			Bound::Unbounded => thing::prefix(ns, db, tb)?,
			Bound::Included(v) => thing::new(ns, db, tb, v).encode()?,
			Bound::Excluded(v) => {
				let mut key = thing::new(ns, db, tb, v).encode()?;
				key.push(0x00);
				key
			}
		};
		// Prepare the range end key
		let end = match &r.end {
			Bound::Unbounded => thing::suffix(ns, db, tb)?,
			Bound::Excluded(v) => thing::new(ns, db, tb, v).encode()?,
			Bound::Included(v) => {
				let mut key = thing::new(ns, db, tb, v).encode()?;
				key.push(0x00);
				key
			}
		};
		Ok((beg, end))
	}

	async fn collect_range(
		&mut self,
		ctx: &Context,
		opt: &Options,
		tb: &str,
		r: IdRange,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Prepare
		let (beg, end) = Self::range_prepare(&txn, opt, tb, r).await?;
		// Optionally skip keys
		let rng = if let Some(rng) = self.start_skip(ctx, &txn, beg..end).await? {
			// Returns the next range of keys
			rng
		} else {
			// There is nothing left to iterate
			return Ok(());
		};
		// Create a new iterable range
		let mut stream = txn.stream(rng, None, None);
		// Loop until no more entries
		let mut count = 0;
		while let Some(res) = stream.next().await {
			// Check if the context is finished
			if ctx.is_done(count % 100 == 0) {
				break;
			}
			// Parse the data from the store
			let (k, v) = res?;
			// Collect
			self.collect(Collected::KeyVal(k, v)).await?;
			count += 1;
		}
		// Everything ok
		Ok(())
	}

	async fn collect_range_keys(
		&mut self,
		ctx: &Context,
		opt: &Options,
		tb: &str,
		r: IdRange,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Prepare
		let (beg, end) = Self::range_prepare(&txn, opt, tb, r).await?;
		// Optionally skip keys
		let rng = if let Some(rng) = self.start_skip(ctx, &txn, beg..end).await? {
			// Returns the next range of keys
			rng
		} else {
			// There is nothing left to iterate
			return Ok(());
		};
		// Create a new iterable range
		let mut stream = txn.stream_keys(rng, None);
		// Loop until no more entries
		let mut count = 0;
		while let Some(res) = stream.next().await {
			// Check if the context is finished
			if ctx.is_done(count % 100 == 0) {
				break;
			}
			// Parse the data from the store
			let k = res?;
			self.collect(Collected::RangeKey(k)).await?;
			count += 1;
		}
		// Everything ok
		Ok(())
	}

	async fn collect_range_count(
		&mut self,
		ctx: &Context,
		opt: &Options,
		tb: &str,
		r: IdRange,
	) -> Result<(), Error> {
		// Get the transaction
		let txn = ctx.tx();
		// Prepare
		let (beg, end) = Self::range_prepare(&txn, opt, tb, r).await?;
		// Create a new iterable range
		let count = txn.count(beg..end).await?;
		// Collect the count
		self.collect(Collected::Count(count)).await?;
		// Everything ok
		Ok(())
	}

	async fn collect_edges(&mut self, ctx: &Context, opt: &Options, e: Edges) -> Result<(), Error> {
		// Pull out options
		let (ns, db) = opt.ns_db()?;
		let tb = &e.from.tb;
		let id = &e.from.id;
		// Fetch start and end key pairs
		let keys = match e.what.len() {
			0 => match e.dir {
				// /ns/db/tb/id
				Dir::Both => {
					vec![(graph::prefix(ns, db, tb, id), graph::suffix(ns, db, tb, id))]
				}
				// /ns/db/tb/id/IN
				Dir::In => vec![(
					graph::egprefix(ns, db, tb, id, &e.dir),
					graph::egsuffix(ns, db, tb, id, &e.dir),
				)],
				// /ns/db/tb/id/OUT
				Dir::Out => vec![(
					graph::egprefix(ns, db, tb, id, &e.dir),
					graph::egsuffix(ns, db, tb, id, &e.dir),
				)],
			},
			_ => match e.dir {
				// /ns/db/tb/id/IN/TB
				Dir::In => e
					.what
					.iter()
					.map(|v| v.0.to_owned())
					.map(|v| {
						(
							graph::ftprefix(ns, db, tb, id, &e.dir, &v),
							graph::ftsuffix(ns, db, tb, id, &e.dir, &v),
						)
					})
					.collect::<Vec<_>>(),
				// /ns/db/tb/id/OUT/TB
				Dir::Out => e
					.what
					.iter()
					.map(|v| v.0.to_owned())
					.map(|v| {
						(
							graph::ftprefix(ns, db, tb, id, &e.dir, &v),
							graph::ftsuffix(ns, db, tb, id, &e.dir, &v),
						)
					})
					.collect::<Vec<_>>(),
				// /ns/db/tb/id/IN/TB, /ns/db/tb/id/OUT/TB
				Dir::Both => e
					.what
					.iter()
					.map(|v| v.0.to_owned())
					.flat_map(|v| {
						[
							(
								graph::ftprefix(ns, db, tb, id, &Dir::In, &v),
								graph::ftsuffix(ns, db, tb, id, &Dir::In, &v),
							),
							(
								graph::ftprefix(ns, db, tb, id, &Dir::Out, &v),
								graph::ftsuffix(ns, db, tb, id, &Dir::Out, &v),
							),
						]
					})
					.collect::<Vec<_>>(),
			},
		};
		// Get the transaction
		let txn = ctx.tx();
		// Check that the table exists
		txn.check_ns_db_tb(ns, db, tb, opt.strict).await?;
		// Loop over the chosen edge types
		for (beg, end) in keys.into_iter() {
			// Create a new iterable range
			let mut stream = txn.stream(beg?..end?, None, None);
			// Loop until no more entries
			let mut count = 0;
			while let Some(res) = stream.next().await {
				// Check if the context is finished
				if ctx.is_done(count % 100 == 0) {
					break;
				}
				// Parse the key from the result
				let key = res?.0;
				// Collector the key
				self.collect(Collected::Edge(key)).await?;
				count += 1;
			}
		}
		// Everything ok
		Ok(())
	}

	async fn collect_index_items(
		&mut self,
		ctx: &Context,
		opt: &Options,
		table: &Table,
		irf: IteratorRef,
		rs: RecordStrategy,
	) -> Result<(), Error> {
		// Check that the table exists
		let (ns, db) = opt.ns_db()?;
		ctx.tx().check_ns_db_tb(ns, db, &table.0, opt.strict).await?;
		if let Some(exe) = ctx.get_query_executor() {
			if let Some(iterator) = exe.new_iterator(opt, irf).await? {
				let txn = ctx.tx();
				match rs {
					RecordStrategy::Count => {
						self.collect_index_item_count(ctx, &txn, iterator).await?
					}
					RecordStrategy::KeysOnly => {
						self.collect_index_item_key(ctx, &txn, iterator).await?
					}
					RecordStrategy::KeysAndValues => {
						self.collect_index_item_key_value(ctx, &txn, iterator).await?
					}
				}
				// Everything ok
				return Ok(());
			} else {
				return Err(Error::QueryNotExecutedDetail {
					message: "No iterator has been found.".to_string(),
				});
			}
		}
		Err(Error::QueryNotExecutedDetail {
			message: "No QueryExecutor has been found.".to_string(),
		})
	}

	async fn collect_index_item_key(
		&mut self,
		ctx: &Context,
		txn: &Transaction,
		mut iterator: ThingIterator,
	) -> Result<(), Error> {
		while !ctx.is_done(true) {
			let records: Vec<IndexItemRecord> =
				iterator.next_batch(ctx, txn, *NORMAL_FETCH_SIZE).await?;
			if records.is_empty() {
				break;
			}
			for (c, r) in records.into_iter().enumerate() {
				if ctx.is_done(c % 100 == 0) {
					break;
				}
				self.collect(Collected::IndexItemKey(r)).await?;
			}
		}
		Ok(())
	}

	async fn collect_index_item_key_value(
		&mut self,
		ctx: &Context,
		txn: &Transaction,
		mut iterator: ThingIterator,
	) -> Result<(), Error> {
		while !ctx.is_done(true) {
			let records: Vec<IndexItemRecord> =
				iterator.next_batch(ctx, txn, *NORMAL_FETCH_SIZE).await?;
			if records.is_empty() {
				break;
			}
			for (c, r) in records.into_iter().enumerate() {
				if ctx.is_done(c % 100 == 0) {
					break;
				}
				self.collect(Collected::IndexItem(r)).await?;
			}
		}
		Ok(())
	}

	async fn collect_index_item_count(
		&mut self,
		ctx: &Context,
		txn: &Transaction,
		mut iterator: ThingIterator,
	) -> Result<(), Error> {
		let mut total_count = 0;
		while !ctx.is_done(true) {
			let count = iterator.next_count(ctx, txn, *NORMAL_FETCH_SIZE).await?;
			if count == 0 {
				break;
			}
			total_count += count;
		}
		self.collect(Collected::Count(total_count)).await
	}
}

impl Iterable {
	/// Returns the value from the store, or Value::None it the value does not exist.
	pub(crate) async fn fetch_thing(
		txn: &Transaction,
		opt: &Options,
		thg: &Thing,
	) -> Result<Arc<Value>, Error> {
		// Fetch and parse the data from the store
		let (ns, db) = opt.ns_db()?;
		let val = txn.get_record(ns, db, &thg.tb, &thg.id, None).await?;
		// Return the result
		Ok(val)
	}
}