surrealdb/key/index/
bk.rs1use crate::idx::docids::DocId;
3use crate::key::error::KeyCategory;
4use crate::key::key_req::KeyRequirements;
5use derive::Key;
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
9pub struct Bk<'a> {
10 __: u8,
11 _a: u8,
12 pub ns: &'a str,
13 _b: u8,
14 pub db: &'a str,
15 _c: u8,
16 pub tb: &'a str,
17 _d: u8,
18 pub ix: &'a str,
19 _e: u8,
20 _f: u8,
21 _g: u8,
22 pub doc_id: DocId,
23}
24
25impl KeyRequirements for Bk<'_> {
26 fn key_category(&self) -> KeyCategory {
27 KeyCategory::IndexTermList
28 }
29}
30
31impl<'a> Bk<'a> {
32 pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, doc_id: DocId) -> Self {
33 Self {
34 __: b'/',
35 _a: b'*',
36 ns,
37 _b: b'*',
38 db,
39 _c: b'*',
40 tb,
41 _d: b'+',
42 ix,
43 _e: b'!',
44 _f: b'b',
45 _g: b'k',
46 doc_id,
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 #[test]
54 fn key() {
55 use super::*;
56 #[rustfmt::skip]
57 let val = Bk::new(
58 "testns",
59 "testdb",
60 "testtb",
61 "testix",
62 7
63 );
64 let enc = Bk::encode(&val).unwrap();
65 assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bk\0\0\0\0\0\0\0\x07");
66
67 let dec = Bk::decode(&enc).unwrap();
68 assert_eq!(val, dec);
69 }
70}