2022-01-13 17:40:20 +00:00
|
|
|
use crate::err::Error;
|
2022-04-09 09:09:01 +00:00
|
|
|
use crate::sql::array::Array;
|
2022-01-13 17:40:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-03-15 12:36:41 +00:00
|
|
|
use storekey::{deserialize, serialize};
|
2022-01-13 17:40:20 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Index {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: u8,
|
|
|
|
_a: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub ns: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub db: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_c: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub tb: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_d: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub ix: String,
|
2022-04-09 09:09:01 +00:00
|
|
|
pub fd: Array,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:01:32 +00:00
|
|
|
impl From<Index> for Vec<u8> {
|
|
|
|
fn from(val: Index) -> Vec<u8> {
|
|
|
|
val.encode().unwrap()
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for Index {
|
|
|
|
fn from(val: Vec<u8>) -> Self {
|
|
|
|
Index::decode(&val).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:31:45 +00:00
|
|
|
impl From<&Vec<u8>> for Index {
|
|
|
|
fn from(val: &Vec<u8>) -> Self {
|
|
|
|
Index::decode(val).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Array) -> Index {
|
2022-01-13 17:40:20 +00:00
|
|
|
Index::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd)
|
|
|
|
}
|
|
|
|
|
2022-03-18 07:21:22 +00:00
|
|
|
pub fn prefix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec<u8> {
|
|
|
|
let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap();
|
|
|
|
k.extend_from_slice(&[0x00]);
|
|
|
|
k
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn suffix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec<u8> {
|
|
|
|
let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap();
|
|
|
|
k.extend_from_slice(&[0xff]);
|
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:40:20 +00:00
|
|
|
impl Index {
|
2022-04-09 09:09:01 +00:00
|
|
|
pub fn new(ns: String, db: String, tb: String, ix: String, fd: Array) -> Index {
|
2022-01-13 17:40:20 +00:00
|
|
|
Index {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: 0x2f, // /
|
|
|
|
_a: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
ns,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
db,
|
2022-03-16 15:33:43 +00:00
|
|
|
_c: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
tb,
|
2022-03-16 15:33:43 +00:00
|
|
|
_d: 0xa4, // ¤
|
2022-01-13 17:40:20 +00:00
|
|
|
ix,
|
|
|
|
fd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn encode(&self) -> Result<Vec<u8>, Error> {
|
2022-05-21 00:35:59 +00:00
|
|
|
crate::sql::serde::beg_internal_serialization();
|
|
|
|
let v = serialize(self);
|
|
|
|
crate::sql::serde::end_internal_serialization();
|
|
|
|
Ok(v?)
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
pub fn decode(v: &[u8]) -> Result<Index, Error> {
|
|
|
|
Ok(deserialize(v)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let val = Index::new(
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
2022-04-09 09:09:01 +00:00
|
|
|
vec!["test"].into(),
|
2022-01-13 17:40:20 +00:00
|
|
|
);
|
|
|
|
let enc = Index::encode(&val).unwrap();
|
|
|
|
let dec = Index::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
|
|
|
}
|