surrealdb/key/table/
ix.rs

1use crate::key::error::KeyCategory;
2use crate::key::key_req::KeyRequirements;
3/// Stores a DEFINE INDEX config definition
4use derive::Key;
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
8pub struct Ix<'a> {
9	__: u8,
10	_a: u8,
11	pub ns: &'a str,
12	_b: u8,
13	pub db: &'a str,
14	_c: u8,
15	pub tb: &'a str,
16	_d: u8,
17	_e: u8,
18	_f: u8,
19	pub ix: &'a str,
20}
21
22pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Ix<'a> {
23	Ix::new(ns, db, tb, ix)
24}
25
26pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
27	let mut k = super::all::new(ns, db, tb).encode().unwrap();
28	k.extend_from_slice(&[b'!', b'i', b'x', 0x00]);
29	k
30}
31
32pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
33	let mut k = super::all::new(ns, db, tb).encode().unwrap();
34	k.extend_from_slice(&[b'!', b'i', b'x', 0xff]);
35	k
36}
37
38impl KeyRequirements for Ix<'_> {
39	fn key_category(&self) -> KeyCategory {
40		KeyCategory::IndexDefinition
41	}
42}
43
44impl<'a> Ix<'a> {
45	pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self {
46		Self {
47			__: b'/',
48			_a: b'*',
49			ns,
50			_b: b'*',
51			db,
52			_c: b'*',
53			tb,
54			_d: b'!',
55			_e: b'i',
56			_f: b'x',
57			ix,
58		}
59	}
60}
61
62#[cfg(test)]
63mod tests {
64	#[test]
65	fn key() {
66		use super::*;
67		#[rustfmt::skip]
68		let val = Ix::new(
69			"testns",
70			"testdb",
71			"testtb",
72			"testix",
73		);
74		let enc = Ix::encode(&val).unwrap();
75		assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0!ixtestix\0");
76
77		let dec = Ix::decode(&enc).unwrap();
78		assert_eq!(val, dec);
79	}
80}