surrealdb-engine-local 3.3.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
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
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! The engine the SDK drives when it embeds the database.

use std::borrow::Cow;
#[cfg(not(target_family = "wasm"))]
use std::pin::pin;
use std::sync::Arc;
#[cfg(not(target_family = "wasm"))]
use std::task::{Poll, ready};
#[cfg(not(target_family = "wasm"))]
use std::{future::Future, path::PathBuf};

use async_channel::Sender;
#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
use futures::StreamExt;
#[cfg(not(target_family = "wasm"))]
use futures::stream::poll_fn;
use surrealdb_core::dbs::{AuthPrincipalSnapshot, Session};
use surrealdb_core::iam;
#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
use surrealdb_core::iam::check::check_ns_db;
use surrealdb_core::kvs::{Datastore, QueryRequest};
#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
use surrealdb_engine_api::MlExportConfig;
use surrealdb_engine_api::{EngineContext, EngineFuture, SurrealEngine, single_result};
#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
use surrealdb_iam::{Action, ResourceKind};
use surrealdb_kvs::TransactionType;
#[cfg(not(target_family = "wasm"))]
use surrealdb_rpc::export::Config as DbExportConfig;
use surrealdb_rpc::{QueryResult, QueryResultBuilder, QueryStreamItem, QueryType, Token};
use surrealdb_types::{Array, Error, Notification, Object, ToSql, Value, Variables};
#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
use surrealml_core::storage::surml_file::SurMlFile;
#[cfg(not(target_family = "wasm"))]
use tokio::{
	fs::OpenOptions,
	io::{self, AsyncReadExt, AsyncWriteExt},
};
#[cfg(not(target_family = "wasm"))]
use tokio_util::bytes::BytesMut;
use uuid::Uuid;

use crate::session::{self, SessionRegistry, SessionState};
use crate::std_error_to_types_error;

/// A datastore this process owns, served through the SDK's engine interface.
///
/// Values cross this boundary as themselves: the SDK asked for a query result,
/// and the datastore produced one, so there is no wire format in between and
/// nothing to encode a result into purely for the SDK to take it apart again.
pub struct LocalEngine {
	pub(crate) kvs: Arc<Datastore>,
	pub(crate) sessions: Arc<SessionRegistry>,
}

impl std::fmt::Debug for LocalEngine {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("LocalEngine").finish_non_exhaustive()
	}
}

impl LocalEngine {
	/// The state the request's session owns, once that session is registered.
	async fn state(&self, ctx: EngineContext) -> Result<Arc<SessionState>, Error> {
		session::resolve(&self.sessions, ctx.session).await
	}
}

/// The failure of naming a transaction the session does not hold.
fn transaction_not_found() -> Error {
	Error::not_found(
		"Transaction not found".to_string(),
		Some(surrealdb_types::NotFoundError::Transaction),
	)
}

/// The failure of committing a transaction whose query was abandoned part-way.
fn transaction_abandoned() -> Error {
	Error::internal(
		"The transaction was rolled back: a query running in it was abandoned before it finished"
			.to_string(),
	)
}

/// The parameter [`kill_live_query`] binds the subscription's id to.
const KILL_ID_PARAM: &str = "__kill_id";

/// Ends a live query by running the `KILL` statement for it.
///
/// The id is bound rather than written into the statement text: `KILL` accepts
/// a UUID literal or a parameter, and a `Uuid`'s own rendering is neither --
/// bare hex lexes as a number. Binding also leaves no escaping question behind.
pub(crate) async fn kill_live_query(
	kvs: &Datastore,
	id: Uuid,
	session: &Session,
	mut vars: Variables,
) -> Result<Vec<QueryResult>, Error> {
	// The session's variables are the caller's to name, so the binding goes in
	// last: this statement kills the id it was given.
	vars.insert(KILL_ID_PARAM.to_string(), Value::Uuid(id.into()));

	let results = kvs.execute(&format!("KILL ${KILL_ID_PARAM}"), session, Some(vars)).await?;
	Ok(results)
}

/// Records the live queries a statement registered against the session it ran
/// on.
///
/// A `LIVE SELECT` exists in the datastore the moment its statement succeeds,
/// but its subscriber only arrives on the `subscribe_live` that follows. The
/// session has to know about it from the earlier of the two, or a teardown
/// between them -- an auth change, say -- would leave it running.
fn record_live_queries(state: &SessionState, results: &[QueryResult]) {
	for result in results.iter().filter(|result| result.query_type == QueryType::Live) {
		if let Ok(Value::Uuid(id)) = &result.result {
			// Subscribed already, in principle: keep whatever is there rather
			// than dropping a subscriber that beat this.
			let id = id.into_inner();
			if state.live_queries.get(&id).is_none() {
				state.live_queries.insert(id, None);
			}
		}
	}
}

/// Ends this session's live queries when an auth operation changed the
/// principal.
///
/// SECURITY: a subscription captures the principal that registered it, so one
/// left running across a principal change keeps dispatching under the previous
/// access controls (GHSA-2xrp-m9c6-75rj). See [`AuthPrincipalSnapshot`].
///
/// Deletion goes straight to the datastore rather than through a `KILL`
/// statement, as the WebSocket transport does it: the session now belongs to
/// the *new* principal, which may have no permission to kill what the previous
/// one registered -- and a teardown that can be refused is not a teardown.
async fn cleanup_lqs_on_principal_change(
	kvs: &Datastore,
	state: &SessionState,
	before: &AuthPrincipalSnapshot,
) {
	if !before.differs_from(&*state.session.read().await) {
		return;
	}
	let mut gc = Vec::new();
	state.live_queries.retain(|id, _| {
		gc.push(*id);
		false
	});
	if gc.is_empty() {
		return;
	}
	if let Err(error) = kvs.delete_queries(gc).await {
		warn!("Failed to end live queries after the session's principal changed; {error}");
	}
}

/// Rejects an operation that arrives on a session whose authentication has
/// expired.
///
/// `Datastore::execute` refuses an expired session itself, so the query path is
/// covered wherever it is reached from. This is for the operations that change
/// session state without going through it.
async fn ensure_session_active(state: &SessionState) -> Result<(), Error> {
	if state.session.read().await.expired() {
		return Err(surrealdb_rpc::error::session_expired());
	}
	Ok(())
}

#[cfg(not(target_family = "wasm"))]
async fn export_file(
	kvs: &Datastore,
	sess: &Session,
	chn: Sender<Vec<u8>>,
	config: Option<DbExportConfig>,
) -> Result<(), Error> {
	let res = match config {
		Some(config) => {
			kvs.export_with_config(sess, chn, config).await.map_err(std_error_to_types_error)?.await
		}
		None => kvs.export(sess, chn).await.map_err(std_error_to_types_error)?.await,
	};

	if let Err(error) = res {
		// Check if this is a channel error by examining the error message
		let error_str = error.to_string();
		if error_str.contains("channel") || error_str.contains("Channel") {
			// This is not really an error. Just logging it for improved visibility.
			trace!("{error_str}");
			return Ok(());
		}

		return Err(Error::internal(error.to_string()));
	}
	Ok(())
}

#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
async fn export_ml(
	kvs: &Datastore,
	sess: &Session,
	chn: Sender<Vec<u8>>,
	MlExportConfig {
		name,
		version,
	}: MlExportConfig,
) -> Result<(), Error> {
	let (nsv, dbv) = check_ns_db(sess).map_err(|e| Error::internal(e.to_string()))?;
	// Check the permissions level
	kvs.check(sess, Action::View, ResourceKind::Model.on_db(&nsv, &dbv))
		.map_err(|e| Error::internal(e.to_string()))?;

	// Attempt to get the model definition
	let Some(model) = kvs
		.get_db_model(&nsv, &dbv, &name, &version)
		.await
		.map_err(|e| Error::internal(e.to_string()))?
	else {
		// Attempt to get the model definition
		return Err(Error::not_found("Model not found".to_string(), None));
	};
	// Export the file data in to the store
	let mut data = surrealdb_core::obs::stream(model.hash.to_string())
		.await
		.map_err(|e| Error::internal(e.to_string()))?;
	// Process all stream values
	while let Some(Ok(bytes)) = data.next().await {
		if chn.send(bytes.to_vec()).await.is_err() {
			break;
		}
	}
	Ok(())
}

#[cfg(not(target_family = "wasm"))]
async fn copy<'a, R, W>(path: PathBuf, reader: &'a mut R, writer: &'a mut W) -> Result<(), Error>
where
	R: tokio::io::AsyncRead + Unpin + ?Sized,
	W: tokio::io::AsyncWrite + Unpin + ?Sized,
{
	io::copy(reader, writer)
		.await
		.map(|_| ())
		.map_err(|error| Error::internal(format!("Failed to read `{}`: {}", path.display(), error)))
}

/// Forwards to `bytes` what `export` sends on the channel it is given.
///
/// Started on a task of its own and returns immediately: the caller reads
/// `bytes`, and the export fills a bounded channel, so the two have to make
/// progress against each other.
#[cfg(not(target_family = "wasm"))]
fn export_to_channel<F, Fut>(bytes: Sender<Result<Vec<u8>, Error>>, export: F)
where
	F: FnOnce(Sender<Vec<u8>>) -> Fut + Send + 'static,
	Fut: Future<Output = Result<(), Error>> + Send,
{
	crate::spawn(async move {
		let (tx, rx) = async_channel::bounded(1);
		let produce = async {
			if let Err(error) = export(tx).await {
				bytes.send(Err(error)).await.ok();
			}
		};

		let bridge = async {
			while let Ok(chunk) = rx.recv().await {
				if bytes.send(Ok(chunk)).await.is_err() {
					// The caller stopped reading. Closing the export's channel
					// is what ends it: the channel is bounded and this loop was
					// the only thing draining it, so the next send would never
					// complete and this task would never finish.
					rx.close();
					return;
				}
			}
		};

		tokio::join!(produce, bridge);
	});
}

/// Writes to `path` what `export` sends on the channel it is given.
///
/// The export owns the sender, so the bridge ends when the export does.
#[cfg(not(target_family = "wasm"))]
async fn export_to_file<F, Fut>(path: PathBuf, export: F) -> Result<(), Error>
where
	F: FnOnce(Sender<Vec<u8>>) -> Fut,
	Fut: Future<Output = Result<(), Error>>,
{
	let (tx, rx) = async_channel::bounded(1);
	let (mut writer, mut reader) = io::duplex(10_240);

	// Write to channel.
	let export = export(tx);

	// Read from channel and write to pipe.
	let bridge = async move {
		while let Ok(value) = rx.recv().await {
			if writer.write_all(&value).await.is_err() {
				// Broken pipe. Let either side's error be propagated.
				break;
			}
		}
		Ok(())
	};

	// Output to stdout or file.
	let mut output =
		match OpenOptions::new().write(true).create(true).truncate(true).open(&path).await {
			Ok(file) => file,
			Err(error) => {
				return Err(Error::internal(format!(
					"Failed to open `{}`: {}",
					path.display(),
					error
				)));
			}
		};

	// Copy from pipe to output.
	let copy = copy(path, &mut reader, &mut output);

	tokio::try_join!(export, bridge, copy)?;
	Ok(())
}

/// Abandons the transaction a query is running in if the query is dropped
/// before it finishes.
///
/// The execution runs on the caller's own task, so a caller that stops awaiting
/// -- a `timeout`, a `select!` branch that lost -- aborts it part-way through
/// the batch. What it wrote is still in the transaction and the caller still
/// holds that transaction's id, so without this a later `commit` would commit a
/// batch that only half ran.
///
/// Setting it aside is what makes that impossible: the `commit` that follows
/// reports it instead of persisting it. It is set aside rather than dropped
/// because some backends panic when a live transaction is dropped, so it has to
/// be cancelled by something that can await -- which `Drop` cannot.
struct AbandonOnCancel<'a> {
	state: &'a SessionState,
	txn: Uuid,
	finished: bool,
}

impl<'a> AbandonOnCancel<'a> {
	fn new(state: &'a SessionState, txn: Uuid) -> Self {
		Self {
			state,
			txn,
			finished: false,
		}
	}

	/// The execution ran to its own conclusion, so the transaction is the
	/// caller's again -- to commit or to roll back, whichever its result calls
	/// for.
	fn finished(mut self) {
		self.finished = true;
	}
}

impl Drop for AbandonOnCancel<'_> {
	fn drop(&mut self) {
		if !self.finished
			&& let Some(tx) = self.state.transactions.take(&self.txn)
		{
			self.state.abandoned.insert(self.txn, tx);
		}
	}
}

impl SurrealEngine for LocalEngine {
	fn query(
		&self,
		ctx: EngineContext,
		query: Cow<'static, str>,
		variables: Variables,
	) -> EngineFuture<'_, Vec<QueryResult>> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// Merge session vars with query vars
			let mut vars = state.vars.read().await.clone();
			vars.extend(variables);

			let session = state.session.read().await;
			let results = match ctx.transaction {
				Some(txn) => match state.transactions.get(&txn) {
					Some(tx) => {
						let abandon = AbandonOnCancel::new(&state, txn);
						let results = self
							.kvs
							.run(
								QueryRequest::new(query.as_ref(), &session)
									.with_variables(Some(vars))
									.with_transaction(tx),
							)
							.await;
						abandon.finished();
						results
					}
					// Reported as the statement's own failure rather than the
					// call's, so a caller reading a multi-statement response
					// sees it where the statement was.
					None => Ok(vec![
						QueryResultBuilder::started_now()
							.finish_with_result(Err(transaction_not_found())),
					]),
				},
				None => self.kvs.execute(query.as_ref(), &session, Some(vars)).await,
			};
			if let Ok(results) = &results {
				record_live_queries(&state, results);
			}
			results
		})
	}

	fn query_stream(
		&self,
		ctx: EngineContext,
		query: Cow<'static, str>,
		variables: Variables,
		items: Sender<QueryStreamItem>,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// Merge session vars with query vars
			let mut vars = state.vars.read().await.clone();
			vars.extend(variables);

			// Preparing the job is what can fail synchronously -- a parse error,
			// an expired session -- so it happens while the session guard is
			// held. The job itself owns everything it needs from here.
			let job = {
				let session = state.session.read().await;
				let transaction = match ctx.transaction {
					// The statement never runs, so there is no statement
					// result to report this on; it belongs to the call.
					Some(txn) => {
						Some(state.transactions.get(&txn).ok_or_else(transaction_not_found)?)
					}
					None => None,
				};
				self.kvs.run_streaming(
					QueryRequest::new(query.as_ref(), &session)
						.with_variables(Some(vars))
						.with_optional_transaction(transaction),
					items,
				)?
			};

			// The caller drains the item channel, so driving the execution here
			// is what lets the two make progress against each other. The results
			// come back only so the execution's own failures do; the rows have
			// already gone to the caller.
			job.run.await?;
			Ok(())
		})
	}

	fn run(
		&self,
		ctx: EngineContext,
		name: String,
		version: Option<String>,
		args: Array,
	) -> EngineFuture<'_, Value> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// Format arguments as comma-separated SQL values
			let formatted_args = args.iter().map(|v| v.to_sql()).collect::<Vec<_>>().join(", ");

			// Build SQL query: name<version>(args) or name(args)
			let sql = match version {
				Some(v) => format!("{name}<{v}>({formatted_args})"),
				None => format!("{name}({formatted_args})"),
			};

			let results = self
				.kvs
				.execute(&sql, &*state.session.read().await, Some(state.vars.read().await.clone()))
				.await?;
			single_result(results)
		})
	}

	fn use_ns_db(
		&self,
		ctx: EngineContext,
		namespace: Option<String>,
		database: Option<String>,
	) -> EngineFuture<'_, (Option<String>, Option<String>)> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let mut session = state.session.write().await;
			self.kvs.process_use(None, &mut session, namespace, database).await?;
			Ok((session.ns.clone(), session.db.clone()))
		})
	}

	fn set(&self, ctx: EngineContext, key: String, value: Value) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// SECURITY: an expired session must not be able to change session
			// state, the same as over the RPC transports. `SECURITY_GUIDE.md`
			// section 3 requires expiry to be checked before any statement or
			// method is processed; `Datastore::execute` covers the query path,
			// so without this the session methods are the way past it.
			ensure_session_active(&state).await?;
			surrealdb_rpc::check_protected_param(&key)
				.map_err(|e| Error::internal(e.to_string()))?;
			// Need to compute because certain keys might not be allowed to be set and those
			// should be rejected by an error.
			match value {
				Value::None => state.vars.write().await.remove(&key),
				v => state.vars.write().await.insert(key, v),
			};
			Ok(())
		})
	}

	fn unset(&self, ctx: EngineContext, key: String) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			ensure_session_active(&state).await?;
			state.vars.write().await.remove(&key);
			Ok(())
		})
	}

	fn signup(&self, ctx: EngineContext, credentials: Object) -> EngineFuture<'_, Token> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let before = AuthPrincipalSnapshot::capture(&*state.session.read().await);
			let token = iam::signup::signup(
				&self.kvs,
				&mut *state.session.write().await,
				credentials.into(),
			)
			.await
			.map_err(surrealdb_core::err::anyhow_to_types_error);
			cleanup_lqs_on_principal_change(&self.kvs, &state, &before).await;
			token
		})
	}

	fn signin(&self, ctx: EngineContext, credentials: Object) -> EngineFuture<'_, Token> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let before = AuthPrincipalSnapshot::capture(&*state.session.read().await);
			let token = iam::signin::signin(
				&self.kvs,
				&mut *state.session.write().await,
				credentials.into(),
			)
			.await
			.map_err(surrealdb_core::err::anyhow_to_types_error);
			cleanup_lqs_on_principal_change(&self.kvs, &state, &before).await;
			token
		})
	}

	fn authenticate(&self, ctx: EngineContext, token: Token) -> EngineFuture<'_, Token> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let before = AuthPrincipalSnapshot::capture(&*state.session.read().await);
			// Extract the access token and check if this token supports refresh
			let (access, with_refresh) = match &token {
				Token::Access(access) => (access, false),
				Token::WithRefresh {
					access,
					..
				} => (access, true),
			};
			// Bound the guard to this block rather than letting it live as a
			// temporary in the `match` scrutinee below. A scrutinee's temporaries
			// are dropped only at the end of the whole `match`, and the refresh
			// arm takes the same lock -- which `RwLock` does not grant twice to
			// one task.
			let verified = {
				let mut session = state.session.write().await;
				iam::verify::token(&self.kvs, &mut session, access).await
			};
			let result = match verified {
				// Authentication successful - return the original token
				Ok(_) => Ok(token),
				Err(error) => {
					// Automatic refresh token handling:
					// If the access token is expired and we have a refresh token,
					// automatically attempt to refresh and return new tokens.
					if with_refresh && iam::is_expired_token_error(&error) {
						iam::token::refresh(token, &self.kvs, &mut *state.session.write().await)
							.await
							.map_err(|error| Error::internal(error.to_string()))
					} else {
						// If authentication failed and automatic refresh isn't applicable,
						// return the authentication error
						Err(Error::internal(error.to_string()))
					}
				}
			};
			cleanup_lqs_on_principal_change(&self.kvs, &state, &before).await;
			result
		})
	}

	fn refresh(&self, ctx: EngineContext, token: Token) -> EngineFuture<'_, Token> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let before = AuthPrincipalSnapshot::capture(&*state.session.read().await);
			let result = iam::token::refresh(token, &self.kvs, &mut *state.session.write().await)
				.await
				.map_err(|error| Error::internal(error.to_string()));
			cleanup_lqs_on_principal_change(&self.kvs, &state, &before).await;
			result
		})
	}

	fn revoke(&self, ctx: EngineContext, token: Token) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			// Revoking is about the token, not the session, but a request still
			// belongs to one -- and resolving it is what reports a session the
			// engine does not know.
			self.state(ctx).await?;
			iam::token::revoke_refresh_token(token, &self.kvs)
				.await
				.map_err(|error| Error::internal(error.to_string()))
		})
	}

	fn invalidate(&self, ctx: EngineContext) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let before = AuthPrincipalSnapshot::capture(&*state.session.read().await);
			let result = iam::clear::clear(&mut *state.session.write().await)
				.map_err(|error| Error::internal(error.to_string()));
			cleanup_lqs_on_principal_change(&self.kvs, &state, &before).await;
			result
		})
	}

	fn begin(&self, ctx: EngineContext) -> EngineFuture<'_, Uuid> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let txn = self
				.kvs
				.transaction(TransactionType::Write)
				.await
				.map_err(|error| Error::internal(error.to_string()))?;
			let id = Uuid::now_v7();
			state.transactions.insert(id, Arc::new(txn));
			Ok(id)
		})
	}

	fn commit(&self, ctx: EngineContext, txn: Uuid) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// A transaction whose query was abandoned is rolled back here, where
			// there is something to await on, and the caller is told rather than
			// having half a batch committed under it.
			if let Some(tx) = state.abandoned.take(&txn) {
				tx.cancel().await.map_err(std_error_to_types_error)?;
				return Err(transaction_abandoned());
			}
			// Removing is what claims the transaction, so two concurrent calls
			// naming the same one cannot both go on to finalise it.
			let tx = state.transactions.take(&txn).ok_or_else(transaction_not_found)?;
			tx.commit().await.map_err(std_error_to_types_error)?;
			Ok(())
		})
	}

	fn rollback(&self, ctx: EngineContext, txn: Uuid) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			// Rolling back what was abandoned is exactly what the caller asked
			// for, so this is not an error on that path.
			let tx = state
				.abandoned
				.take(&txn)
				.or_else(|| state.transactions.take(&txn))
				.ok_or_else(transaction_not_found)?;
			tx.cancel().await.map_err(std_error_to_types_error)?;
			Ok(())
		})
	}

	fn health(&self, ctx: EngineContext) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			// The datastore is in this process, so there is no round trip to
			// report on -- but a session the engine cannot resolve is exactly
			// what a caller checking the connection needs to hear about.
			self.state(ctx).await?;
			Ok(())
		})
	}

	fn version(&self, ctx: EngineContext) -> EngineFuture<'_, String> {
		Box::pin(async move {
			self.state(ctx).await?;
			Ok(surrealdb_core::env::VERSION.to_string())
		})
	}

	fn subscribe_live(
		&self,
		ctx: EngineContext,
		uuid: Uuid,
		notifications: Sender<Result<Notification, Error>>,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			state.live_queries.insert(uuid, Some(notifications));
			Ok(())
		})
	}

	fn kill(&self, ctx: EngineContext, uuid: Uuid) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			state.live_queries.remove(&uuid);
			let results = kill_live_query(
				&self.kvs,
				uuid,
				&*state.session.read().await,
				state.vars.read().await.clone(),
			)
			.await?;
			single_result(results)?;
			Ok(())
		})
	}

	#[cfg(not(target_family = "wasm"))]
	fn export_file(
		&self,
		ctx: EngineContext,
		path: PathBuf,
		config: Option<DbExportConfig>,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let session = state.session.read().await.clone();
			export_to_file(path, |chn| export_file(&self.kvs, &session, chn, config)).await
		})
	}

	#[cfg(not(target_family = "wasm"))]
	fn export_bytes(
		&self,
		ctx: EngineContext,
		bytes: Sender<Result<Vec<u8>, Error>>,
		config: Option<DbExportConfig>,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let kvs = Arc::clone(&self.kvs);
			let session = state.session.read().await.clone();
			export_to_channel(bytes, |chn| async move {
				export_file(&kvs, &session, chn, config).await
			});
			Ok(())
		})
	}

	#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
	fn export_ml_file(
		&self,
		ctx: EngineContext,
		path: PathBuf,
		config: MlExportConfig,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let session = state.session.read().await.clone();
			export_to_file(path, |chn| export_ml(&self.kvs, &session, chn, config)).await
		})
	}

	#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
	fn export_ml_bytes(
		&self,
		ctx: EngineContext,
		bytes: Sender<Result<Vec<u8>, Error>>,
		config: MlExportConfig,
	) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let kvs = Arc::clone(&self.kvs);
			let session = state.session.read().await.clone();
			export_to_channel(
				bytes,
				|chn| async move { export_ml(&kvs, &session, chn, config).await },
			);
			Ok(())
		})
	}

	#[cfg(not(target_family = "wasm"))]
	fn import_file(&self, ctx: EngineContext, path: PathBuf) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let file = match OpenOptions::new().read(true).open(&path).await {
				Ok(file) => file,
				Err(error) => {
					return Err(Error::internal(format!(
						"Failed to open `{}`: {}",
						path.display(),
						error
					)));
				}
			};

			let mut file = pin!(file);
			let mut buffer = BytesMut::with_capacity(4096);

			let stream = poll_fn(|ctx| {
				// Doing it this way optimizes allocation.
				// It is highly likely that the buffer we return from this stream will be
				// dropped between calls to this function.
				// If this is the case than instead of allocating new memory the call to reserve
				// will instead reclaim the existing used memory.
				if buffer.capacity() == 0 {
					buffer.reserve(4096);
				}

				let future = pin!(file.read_buf(&mut buffer));
				match ready!(future.poll(ctx)) {
					Ok(0) => Poll::Ready(None),
					Ok(_) => Poll::Ready(Some(Ok(buffer.split().freeze()))),
					Err(e) => Poll::Ready(Some(Err(anyhow::anyhow!("{}", e)))),
				}
			});

			let responses = self
				.kvs
				.execute_import(
					&*state.session.read().await,
					Some(state.vars.read().await.clone()),
					stream,
				)
				.await
				.map_err(std_error_to_types_error)?;

			for response in responses {
				response.result?;
			}

			Ok(())
		})
	}

	#[cfg(all(not(target_family = "wasm"), feature = "ml"))]
	fn import_ml_file(&self, ctx: EngineContext, path: PathBuf) -> EngineFuture<'_, ()> {
		Box::pin(async move {
			let state = self.state(ctx).await?;
			let mut file = match OpenOptions::new().read(true).open(&path).await {
				Ok(file) => file,
				Err(error) => {
					return Err(Error::internal(format!(
						"Failed to open `{}`: {}",
						path.display(),
						error
					)));
				}
			};

			// Ensure a NS and DB are set
			let (nsv, dbv) =
				check_ns_db(&*state.session.read().await).map_err(std_error_to_types_error)?;
			// Check the permissions level
			self.kvs
				.check(
					&*state.session.read().await,
					Action::Edit,
					ResourceKind::Model.on_db(&nsv, &dbv),
				)
				.map_err(std_error_to_types_error)?;
			// Create a new buffer
			let mut buffer = Vec::new();
			// Load all the uploaded file chunks
			if let Err(error) = file.read_to_end(&mut buffer).await {
				return Err(Error::internal(format!(
					"Failed to read `{}`: {}",
					path.display(),
					error
				)));
			}
			// Check that the SurrealML file is valid
			let file = match SurMlFile::from_bytes(buffer) {
				Ok(file) => file,
				Err(error) => {
					return Err(Error::internal(format!(
						"Invalid SurrealML file: {}",
						error.message
					)));
				}
			};
			// Convert the file back in to raw bytes
			let data = file.to_bytes();

			self.kvs
				.put_ml_model(
					&*state.session.read().await,
					&file.header.name.to_string(),
					&file.header.version.to_string(),
					&file.header.description.to_string(),
					data,
				)
				.await
				.map_err(std_error_to_types_error)?;

			Ok(())
		})
	}
}

#[cfg(test)]
mod tests {
	use surrealdb_rpc::QueryResultBuilder;
	use surrealdb_types::Uuid as PublicUuid;

	use super::*;

	/// A live query belongs to the session from the moment its statement
	/// succeeds, not from the `subscribe_live` that follows: an auth change
	/// landing between the two tears down what it can see, and the datastore
	/// already has this one.
	#[test]
	fn a_live_query_is_recorded_before_its_subscriber_arrives() {
		let state = SessionState::new(Uuid::now_v7());
		let live = Uuid::now_v7();

		record_live_queries(
			&state,
			&[
				QueryResultBuilder::started_now()
					.with_query_type(QueryType::Live)
					.finish_with_result(Ok(Value::Uuid(PublicUuid::from(live)))),
				QueryResultBuilder::instant_none(),
			],
		);

		assert!(
			matches!(state.live_queries.get(&live), Some(None)),
			"the live query is the session's, with no subscriber yet"
		);
		assert_eq!(state.live_queries.len(), 1, "only the live statement registers one");
	}

	/// Recording runs on every statement, so it must not displace a subscriber
	/// that has already arrived for the same live query.
	#[test]
	fn recording_does_not_displace_an_existing_subscriber() {
		let state = SessionState::new(Uuid::now_v7());
		let live = Uuid::now_v7();
		let (sender, _receiver) = async_channel::bounded(1);
		state.live_queries.insert(live, Some(sender));

		record_live_queries(
			&state,
			&[QueryResultBuilder::started_now()
				.with_query_type(QueryType::Live)
				.finish_with_result(Ok(Value::Uuid(PublicUuid::from(live))))],
		);

		assert!(
			matches!(state.live_queries.get(&live), Some(Some(_))),
			"the subscriber must survive a later recording pass"
		);
	}
}

/// Live-query teardown, against a datastore that can actually register one.
///
/// These run against the datastore rather than the session map because the two
/// disagree exactly where the interesting failure is: `kill` empties the
/// session map before it reaches the datastore, so a teardown that never
/// removed the subscription looks, from the subscriber's end, the same as one
/// that did.
#[cfg(all(test, feature = "kv-mem"))]
mod live_query_tests {
	use std::time::Duration;

	use async_channel::{Receiver, Sender};
	use surrealdb_engine_api::SessionId;
	use surrealdb_types::Action;
	use tokio::time::timeout;

	use super::*;

	/// An engine over a fresh in-memory datastore, holding the datastore's own
	/// notification channel rather than letting the engine's pump drain it.
	struct Harness {
		engine: Arc<LocalEngine>,
		ctx: EngineContext,
		notifications: Receiver<Notification>,
		/// The engine shuts the datastore down when this closes, so the test
		/// keeps it open for as long as it holds the engine.
		_sessions: Sender<SessionId>,
	}

	impl Harness {
		async fn new() -> Self {
			let (notify_tx, notifications) = async_channel::bounded(100);
			let kvs = Datastore::builder()
				.with_auth(false)
				.with_notify(notify_tx)
				.build_with_path("memory")
				.await
				.expect("an in-memory datastore should open");

			let (sessions_tx, sessions_rx) = async_channel::unbounded();
			// No notification pump: this test reads the datastore's end of the
			// channel itself.
			let engine = crate::from_datastore(kvs, None, sessions_rx);

			let session = Uuid::now_v7();
			sessions_tx.send(SessionId::Initial(session)).await.expect("the engine is running");
			let ctx = EngineContext::new(session);
			engine
				.use_ns_db(ctx, Some("test".to_string()), Some("test".to_string()))
				.await
				.expect("selecting a namespace and database should succeed");

			let harness = Self {
				engine,
				ctx,
				notifications,
				_sessions: sessions_tx,
			};
			harness.query("DEFINE TABLE person").await;
			harness
		}

		/// Runs a statement, failing the test if it does not succeed.
		async fn query(&self, sql: &'static str) -> Value {
			let mut results = self
				.engine
				.query(self.ctx, Cow::Borrowed(sql), Variables::new())
				.await
				.unwrap_or_else(|error| panic!("`{sql}` failed: {error}"));
			results
				.remove(0)
				.result
				.unwrap_or_else(|error| panic!("`{sql}` returned an error: {error}"))
		}

		/// The next notification the datastore emits.
		async fn notification(&self, expected: &str) -> Notification {
			timeout(Duration::from_secs(10), self.notifications.recv())
				.await
				.unwrap_or_else(|_| panic!("timed out waiting for {expected}"))
				.expect("the notification channel is open")
		}

		/// Registers a live query on `person` and returns its id.
		async fn live(&self) -> Uuid {
			match self.query("LIVE SELECT * FROM person").await {
				Value::Uuid(id) => id.into_inner(),
				other => panic!("LIVE SELECT should return a uuid, got {other:?}"),
			}
		}
	}

	/// `kill` has to end the subscription in the datastore, not merely return.
	///
	/// Nothing downstream of the call can tell the two apart: the session's own
	/// entry is gone before the statement runs, and the only caller --
	/// `Stream::drop`, in the SDK -- has nowhere to report a failure to. What a
	/// surviving subscription costs is paid by the datastore on every later
	/// write, so that is where the assertion belongs.
	#[test_log::test(tokio::test)]
	async fn kill_removes_the_subscription_from_the_datastore() {
		let harness = Harness::new().await;
		let live = harness.live().await;

		// PROOF THE SUBSCRIPTION IS LIVE: without this, the absence of a
		// notification after the kill would prove nothing.
		harness.query("CREATE person:one").await;
		let created = harness.notification("the change on the live subscription").await;
		assert_eq!(created.id.into_inner(), live, "the notification belongs to this subscription");
		assert_eq!(created.action, Action::Create);

		harness.engine.kill(harness.ctx, live).await.expect("kill should end the subscription");

		// The datastore queues this as the kill commits, so a `Killed`
		// notification is the datastore's own account of having run it.
		let killed = harness.notification("the killed notification").await;
		assert_eq!(killed.id.into_inner(), live, "the kill names this subscription");
		assert_eq!(killed.action, Action::Killed);
		// Where this notification stops. The engine's pump routes by session,
		// and `KILL` queues its notification without one, so this is as far as
		// the end of a subscription travels: a subscriber is told by the end of
		// its stream, never by a notification.
		assert!(killed.session.is_none(), "the kill notification carries no session");

		// THE POINT: a later change must no longer be captured for it.
		harness.query("CREATE person:two").await;
		let leaked = timeout(Duration::from_millis(500), harness.notifications.recv()).await;
		assert!(
			leaked.is_err(),
			"the subscription outlived the kill and is still capturing changes: {:?}",
			leaked.map(|notification| notification.map(|n| (n.id, n.action))),
		);
	}

	/// The datastore's own record of the subscription has to be gone with it: a
	/// `KILL` of a subscription the node no longer holds fails, so a second one
	/// failing is what says the first one committed.
	#[test_log::test(tokio::test)]
	async fn a_killed_subscription_cannot_be_killed_again() {
		let harness = Harness::new().await;
		let live = harness.live().await;

		harness.engine.kill(harness.ctx, live).await.expect("kill should end the subscription");
		assert!(
			harness.engine.kill(harness.ctx, live).await.is_err(),
			"the datastore still holds a subscription the kill was supposed to remove"
		);
	}
}