surrealdb-core 3.2.3

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
//! Graph adjacency keys.
//!
//! Each [`RELATE`](crate::expr::statements::RelateStatement) writes four KV
//! keys that together model a single relation between an `in` vertex `l`,
//! an edge record `rid`, and an `out` vertex `r`:
//!
//! ```text
//!              ltr (target = r)         pointer
//!          ┌─────────────────────┬─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
//!          │                     ▼                  ▼
//!     ┌────┴─────┐   etl  ┌────────────┐  etr   ┌──────────┐
//!     │   left   │───────▶│ rid (edge) │───────▶│  right   │
//!     └──────────┘   in   └────────────┘  out   └────┬─────┘
//!           ▼                    ▼                   │
//!           └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┴───────────────────┘
//!                  pointer         rtl (source = l)
//! ```
//!
//! Two roles share the same wire format ([`Graph`] / [`GraphWithTarget`])
//! but carry different meaning:
//!
//! - **Pointer keys** (`ltr`, `rtl`) live on the IN / OUT vertex's adjacency. They embed the
//!   opposite endpoint vertex in `(tt, tk)` so a `->edge->vertex` (or `<-edge<-vertex`) range scan
//!   can resolve the far vertex without reading the edge record. Constructed with [`new_pointer`].
//! - **Inner keys** (`etl`, `etr`) live on the edge record's own adjacency. Their `(ft, fk)` slot
//!   already names the endpoint vertex, so they keep the legacy layout with no embedded target.
//!   Constructed with [`new`].
//!
//! Pointer keys are the only ones that benefit from the target embedding;
//! inner keys are unchanged from the pre-target-vertex layout. Pointer-key
//! bytes are a strict prefix of legacy-key bytes (legacy = `Graph`,
//! pointer = `Graph` + `(tt, tk)`), so a single range scan transparently
//! returns both formats and the decoder handles each per-row.
//!
//! Writers (`doc::edges::store_edges_data`) and the cascade delete path
//! (`doc::purge::purge_pointers`) both refer to the keys by these names.

use std::borrow::Cow;

use anyhow::Result;
use storekey::{BorrowDecode, BorrowReader, Encode};

use crate::catalog::{DatabaseId, NamespaceId};
use crate::expr::dir::Dir;
use crate::key::category::{Categorise, Category};
use crate::kvs::{KVKey, impl_kv_key_storekey};
use crate::val::{RecordId, RecordIdKey, TableName};

#[derive(Clone, Debug, Eq, PartialEq, Encode, BorrowDecode)]
#[storekey(format = "()")]
struct Prefix<'a> {
	__: u8,
	_a: u8,
	pub ns: NamespaceId,
	_b: u8,
	pub db: DatabaseId,
	_c: u8,
	pub tb: Cow<'a, TableName>,
	_d: u8,
	pub id: RecordIdKey,
}

impl_kv_key_storekey!(Prefix<'_> => Vec<u8>);

impl<'a> Prefix<'a> {
	fn new(ns: NamespaceId, db: DatabaseId, tb: &'a TableName, id: &RecordIdKey) -> Self {
		Self {
			__: b'/',
			_a: b'*',
			ns,
			_b: b'*',
			db,
			_c: b'*',
			tb: Cow::Borrowed(tb),
			_d: b'~',
			id: id.to_owned(),
		}
	}
}

#[derive(Clone, Debug, Eq, PartialEq, Encode, BorrowDecode)]
#[storekey(format = "()")]
struct PrefixEg<'a> {
	__: u8,
	_a: u8,
	pub ns: NamespaceId,
	_b: u8,
	pub db: DatabaseId,
	_c: u8,
	pub tb: Cow<'a, TableName>,
	_d: u8,
	pub id: RecordIdKey,
	pub eg: Dir,
}

impl_kv_key_storekey!(PrefixEg<'_> => Vec<u8>);

impl<'a> PrefixEg<'a> {
	fn new(ns: NamespaceId, db: DatabaseId, tb: &'a TableName, id: &RecordIdKey, eg: Dir) -> Self {
		Self {
			__: b'/',
			_a: b'*',
			ns,
			_b: b'*',
			db,
			_c: b'*',
			tb: Cow::Borrowed(tb),
			_d: b'~',
			id: id.clone(),
			eg,
		}
	}
}

#[derive(Clone, Debug, Eq, PartialEq, Encode, BorrowDecode)]
#[storekey(format = "()")]
struct PrefixFt<'a> {
	__: u8,
	_a: u8,
	pub ns: NamespaceId,
	_b: u8,
	pub db: DatabaseId,
	_c: u8,
	pub tb: Cow<'a, TableName>,
	_d: u8,
	pub id: RecordIdKey,
	pub eg: Dir,
	pub ft: Cow<'a, str>,
}

impl_kv_key_storekey!(PrefixFt<'_> => Vec<u8>);

impl<'a> PrefixFt<'a> {
	fn new(
		ns: NamespaceId,
		db: DatabaseId,
		tb: &'a TableName,
		id: &RecordIdKey,
		eg: Dir,
		ft: &'a str,
	) -> Self {
		Self {
			__: b'/',
			_a: b'*',
			ns,
			_b: b'*',
			db,
			_c: b'*',
			tb: Cow::Borrowed(tb),
			_d: b'~',
			id: id.to_owned(),
			eg,
			ft: Cow::Borrowed(ft),
		}
	}
}

#[derive(Clone, Debug, Eq, PartialEq, Encode, BorrowDecode)]
#[storekey(format = "()")]
pub(crate) struct Graph<'a> {
	__: u8,
	_a: u8,
	pub ns: NamespaceId,
	_b: u8,
	pub db: DatabaseId,
	_c: u8,
	pub tb: Cow<'a, TableName>,
	_d: u8,
	pub id: RecordIdKey,
	pub eg: Dir,
	pub ft: Cow<'a, TableName>,
	pub fk: Cow<'a, RecordIdKey>,
}

impl_kv_key_storekey!(Graph<'_> => ());

/// Pointer-key wire format: the [`Graph`] layout followed by the target
/// vertex `(tt, tk)`.
///
/// See the module-level docs for the role pointer keys play in a relation
/// and why their bytes are a strict superset of the legacy [`Graph`]
/// encoding. Construct via [`new_pointer`].
#[derive(Clone, Debug, Eq, PartialEq, Encode, BorrowDecode)]
#[storekey(format = "()")]
pub(crate) struct GraphWithTarget<'a> {
	__: u8,
	_a: u8,
	pub ns: NamespaceId,
	_b: u8,
	pub db: DatabaseId,
	_c: u8,
	pub tb: Cow<'a, TableName>,
	_d: u8,
	pub id: RecordIdKey,
	pub eg: Dir,
	pub ft: Cow<'a, TableName>,
	pub fk: Cow<'a, RecordIdKey>,
	pub tt: Cow<'a, TableName>,
	pub tk: Cow<'a, RecordIdKey>,
}

impl_kv_key_storekey!(GraphWithTarget<'_> => ());

/// Result of decoding a graph adjacency key.
///
/// `edge` is the record sitting in the key's `(ft, fk)` slot. Its meaning
/// depends on which side of the relation the key came from (see the
/// module-level diagram): for **inner keys** this is the endpoint vertex,
/// for **pointer keys** this is the edge record itself.
///
/// `target` is `Some` only for **pointer keys** written in the new layout
/// — those embed the far endpoint vertex in `(tt, tk)`. It is `None` for
/// inner keys and for any pointer key that pre-dates the new layout.
#[derive(Debug, Clone)]
pub(crate) struct DecodedGraph {
	pub edge: RecordId,
	pub target: Option<RecordId>,
}

impl Graph<'_> {
	/// Decode a graph adjacency key, transparently handling legacy and
	/// pointer layouts.
	///
	/// Forward-compat contract: once the pointer-key tail `(tt, tk)` has
	/// been consumed, any further trailing bytes are ignored. A future
	/// field appended after `(tt, tk)` therefore stays readable by this
	/// decoder, just without the extra information.
	///
	/// Trailing bytes appended directly after a legacy (no-target) key are
	/// **not** tolerated: the decoder eagerly tries to consume them as
	/// `(tt, tk)` and bubbles up whatever decode error storekey produces.
	/// This is intentional — new fields are always layered after the
	/// target tail (preserving the prefix property the range scans rely
	/// on), never appended to the legacy form. A legacy key with garbage
	/// trailing bytes is malformed data, not a forward-compat scenario.
	pub fn decode_key(k: &[u8]) -> Result<DecodedGraph> {
		let mut reader = BorrowReader::new(k);
		let g = <Graph as BorrowDecode>::borrow_decode(&mut reader)?;
		let edge = RecordId {
			table: g.ft.into_owned(),
			key: g.fk.into_owned(),
		};
		let target = if reader.is_empty() {
			None
		} else {
			let tt = <Cow<TableName> as BorrowDecode>::borrow_decode(&mut reader)?;
			let tk = <Cow<RecordIdKey> as BorrowDecode>::borrow_decode(&mut reader)?;
			Some(RecordId {
				table: tt.into_owned(),
				key: tk.into_owned(),
			})
		};
		Ok(DecodedGraph {
			edge,
			target,
		})
	}
}

pub fn new<'a>(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &'a TableName,
	id: &RecordIdKey,
	eg: Dir,
	fk: &'a RecordId,
) -> Graph<'a> {
	Graph::new(ns, db, tb, id.to_owned(), eg, fk)
}

/// Construct a **pointer key** (vertex-side adjacency) embedding the far
/// endpoint of the relation.
///
/// `fk` identifies the edge record; `target` is the vertex reached by
/// traversing through that edge in the given direction. Used by
/// `doc::edges::store_edges_data` to write the two pointer keys of a
/// `RELATE` so that subsequent `->edge->vertex` (and mirror) range scans
/// can resolve the far vertex from the adjacency alone, without reading
/// the edge record. See the module-level docs for the four-key layout.
pub fn new_pointer<'a>(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &'a TableName,
	id: &RecordIdKey,
	eg: Dir,
	fk: &'a RecordId,
	target: &'a RecordId,
) -> GraphWithTarget<'a> {
	GraphWithTarget {
		__: b'/',
		_a: b'*',
		ns,
		_b: b'*',
		db,
		_c: b'*',
		tb: Cow::Borrowed(tb),
		_d: b'~',
		id: id.to_owned(),
		eg,
		ft: Cow::Borrowed(&fk.table),
		fk: Cow::Borrowed(&fk.key),
		tt: Cow::Borrowed(&target.table),
		tk: Cow::Borrowed(&target.key),
	}
}

pub fn prefix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
) -> Result<Vec<u8>> {
	let mut k = Prefix::new(ns, db, tb, id).encode_key()?;
	k.extend_from_slice(&[0x00]);
	Ok(k)
}

pub fn suffix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
) -> Result<Vec<u8>> {
	let mut k = Prefix::new(ns, db, tb, id).encode_key()?;
	k.extend_from_slice(&[0xff]);
	Ok(k)
}

pub fn egprefix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
	eg: Dir,
) -> Result<Vec<u8>> {
	let mut k = PrefixEg::new(ns, db, tb, id, eg).encode_key()?;
	k.extend_from_slice(&[0x00]);
	Ok(k)
}

pub fn egsuffix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
	eg: Dir,
) -> Result<Vec<u8>> {
	let mut k = PrefixEg::new(ns, db, tb, id, eg).encode_key()?;
	k.extend_from_slice(&[0xff]);
	Ok(k)
}

pub fn ftprefix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
	eg: Dir,
	ft: &str,
) -> Result<Vec<u8>> {
	let mut k = PrefixFt::new(ns, db, tb, id, eg, ft).encode_key()?;
	k.extend_from_slice(&[0x00]);
	Ok(k)
}

pub fn ftsuffix(
	ns: NamespaceId,
	db: DatabaseId,
	tb: &TableName,
	id: &RecordIdKey,
	eg: Dir,
	ft: &str,
) -> Result<Vec<u8>> {
	let mut k = PrefixFt::new(ns, db, tb, id, eg, ft).encode_key()?;
	k.extend_from_slice(&[0xff]);
	Ok(k)
}

impl Categorise for Graph<'_> {
	fn categorise(&self) -> Category {
		Category::Graph
	}
}

impl Categorise for GraphWithTarget<'_> {
	fn categorise(&self) -> Category {
		Category::Graph
	}
}

impl<'a> Graph<'a> {
	pub fn new(
		ns: NamespaceId,
		db: DatabaseId,
		tb: &'a TableName,
		id: RecordIdKey,
		eg: Dir,
		fk: &'a RecordId,
	) -> Self {
		Self {
			__: b'/',
			_a: b'*',
			ns,
			_b: b'*',
			db,
			_c: b'*',
			tb: Cow::Borrowed(tb),
			_d: b'~',
			id,
			eg,
			ft: Cow::Borrowed(&fk.table),
			fk: Cow::Borrowed(&fk.key),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::syn;
	use crate::types::PublicValue;

	#[test]
	fn key() {
		let Ok(PublicValue::RecordId(fk)) = syn::value("other:test") else {
			panic!()
		};
		let fk = fk.into();
		let tb: TableName = "testtb".into();
		let val = Graph::new(
			NamespaceId(1),
			DatabaseId(2),
			&tb,
			"testid".to_owned().into(),
			Dir::Out,
			&fk,
		);
		let enc = Graph::encode_key(&val).unwrap();
		assert_eq!(
			enc,
			b"/*\x00\x00\x00\x01*\x00\x00\x00\x02*testtb\0~\x03testid\0\x03other\0\x03test\0"
		);
		// Legacy keys decode to no target.
		let dec = Graph::decode_key(&enc).unwrap();
		assert_eq!(dec.edge.table.as_str(), "other");
		assert!(dec.target.is_none());
	}

	#[test]
	fn key_with_target() {
		let Ok(PublicValue::RecordId(edge)) = syn::value("likes:abc") else {
			panic!()
		};
		let Ok(PublicValue::RecordId(target)) = syn::value("person:bob") else {
			panic!()
		};
		let edge: RecordId = edge.into();
		let target: RecordId = target.into();
		let tb: TableName = "person".into();
		let id: RecordIdKey = "alice".to_owned().into();
		// Build the new-format key.
		let new_key =
			new_pointer(NamespaceId(1), DatabaseId(2), &tb, &id, Dir::Out, &edge, &target);
		let new_bytes = GraphWithTarget::encode_key(&new_key).unwrap();
		// Build the legacy-format key on the same vertex/edge for comparison.
		let legacy = Graph::new(NamespaceId(1), DatabaseId(2), &tb, id.clone(), Dir::Out, &edge);
		let legacy_bytes = Graph::encode_key(&legacy).unwrap();
		// The legacy encoding must be a strict prefix of the new encoding so
		// the same range scan returns both formats.
		assert!(new_bytes.starts_with(&legacy_bytes));
		assert!(new_bytes.len() > legacy_bytes.len());
		// The byte immediately after the legacy prefix is the first byte of
		// the target table's storekey encoding. The range-bound construction
		// in `exec::operators::scan::graph::eval_graph_bound` and
		// `expr::lookup::ComputedLookupSubject::presuf` appends `0xff` to a
		// legacy key to "skip past every new-format variant of the same fk"
		// without spilling into the next fk's keyspace. That trick only
		// holds while this boundary byte is strictly less than `0xff`; if a
		// future storekey change makes it `0xff`, both call sites need to
		// be revisited.
		let boundary_byte = new_bytes[legacy_bytes.len()];
		assert!(
			boundary_byte < 0xff,
			"first byte after legacy graph-key prefix must be < 0xff but was 0x{:02x}; \
			 the `0xff` sentinel used by graph range bounds is no longer safe",
			boundary_byte,
		);
		// The unified decoder must round-trip the target on the new format.
		let dec = Graph::decode_key(&new_bytes).unwrap();
		assert_eq!(dec.edge.table.as_str(), "likes");
		assert_eq!(dec.edge.key, RecordIdKey::from("abc".to_owned()));
		let tgt = dec.target.expect("new-format key must carry a target");
		assert_eq!(tgt.table.as_str(), "person");
		assert_eq!(tgt.key, RecordIdKey::from("bob".to_owned()));
	}

	#[test]
	fn legacy_key_with_trailing_bytes_errors() {
		// Negative half of the forward-compat contract: trailing bytes on
		// a *legacy* (no-target) key are not tolerated. The decoder
		// eagerly tries to read them as the pointer-key `(tt, tk)` tail
		// and bubbles up the decode error. New fields always land after
		// the target tail, not directly after legacy bytes, so a legacy
		// key with garbage trailing bytes is malformed data and must
		// surface as an error rather than silently decode.
		let Ok(PublicValue::RecordId(fk)) = syn::value("other:test") else {
			panic!()
		};
		let fk: RecordId = fk.into();
		let tb: TableName = "person".into();
		let legacy = Graph::new(
			NamespaceId(1),
			DatabaseId(2),
			&tb,
			"alice".to_owned().into(),
			Dir::Out,
			&fk,
		);
		let mut bytes = Graph::encode_key(&legacy).unwrap();
		bytes.extend_from_slice(b"trailing-garbage");
		Graph::decode_key(&bytes)
			.expect_err("trailing bytes on a legacy key must surface as a decode error");
	}

	#[test]
	fn key_with_trailing_bytes_decodes() {
		// Positive half of the forward-compat contract: trailing bytes
		// appended *after* the pointer-key tail are silently ignored, so
		// a future field layered on top of the new format stays readable
		// by the current decoder.
		let Ok(PublicValue::RecordId(edge)) = syn::value("likes:abc") else {
			panic!()
		};
		let Ok(PublicValue::RecordId(target)) = syn::value("person:bob") else {
			panic!()
		};
		let edge: RecordId = edge.into();
		let target: RecordId = target.into();
		let tb: TableName = "person".into();
		let id: RecordIdKey = "alice".to_owned().into();
		let key = new_pointer(NamespaceId(1), DatabaseId(2), &tb, &id, Dir::Out, &edge, &target);
		let mut bytes = GraphWithTarget::encode_key(&key).unwrap();
		// Append arbitrary trailing bytes mimicking a future field.
		bytes.extend_from_slice(b"future-extension");
		let dec = Graph::decode_key(&bytes).expect("trailing bytes must not fail decode");
		assert_eq!(dec.edge.table.as_str(), "likes");
		let tgt = dec.target.expect("target still decoded from prefix");
		assert_eq!(tgt.table.as_str(), "person");
	}
}