|
@@ -1,45 +1,25 @@
|
|
|
|
+pub mod checksum;
|
|
|
|
+pub mod locked;
|
|
|
|
+
|
|
use crate::{error::Error, value_try_from, value_vec_try_from};
|
|
use crate::{error::Error, value_try_from, value_vec_try_from};
|
|
use bytes::{Bytes, BytesMut};
|
|
use bytes::{Bytes, BytesMut};
|
|
use redis_zero_protocol_parser::Value as ParsedValue;
|
|
use redis_zero_protocol_parser::Value as ParsedValue;
|
|
use std::{
|
|
use std::{
|
|
- collections::HashMap,
|
|
|
|
|
|
+ collections::{HashMap, LinkedList},
|
|
convert::{TryFrom, TryInto},
|
|
convert::{TryFrom, TryInto},
|
|
str::FromStr,
|
|
str::FromStr,
|
|
- sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
|
|
|
|
};
|
|
};
|
|
|
|
|
|
-#[derive(Debug)]
|
|
|
|
-pub struct LockedValue<T: Clone + PartialEq>(pub RwLock<T>);
|
|
|
|
-
|
|
|
|
-impl<T: Clone + PartialEq> Clone for LockedValue<T> {
|
|
|
|
- fn clone(&self) -> Self {
|
|
|
|
- Self(RwLock::new(self.0.read().unwrap().clone()))
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: PartialEq + Clone> PartialEq for LockedValue<T> {
|
|
|
|
- fn eq(&self, other: &LockedValue<T>) -> bool {
|
|
|
|
- self.0.read().unwrap().eq(&other.0.read().unwrap())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: PartialEq + Clone> LockedValue<T> {
|
|
|
|
- pub fn new(obj: T) -> Self {
|
|
|
|
- Self(RwLock::new(obj))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- pub fn write(&self) -> RwLockWriteGuard<'_, T> {
|
|
|
|
- self.0.write().unwrap()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- pub fn read(&self) -> RwLockReadGuard<'_, T> {
|
|
|
|
- self.0.read().unwrap()
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+#[derive(Debug, PartialEq, Clone)]
|
|
|
|
+/// Stores binary data as Bytes but has built-in verification to make
|
|
|
|
+/// comparisions faster. The verification (CRC32) is a quicker process to
|
|
|
|
+/// compare two objects, which will return MAYBE or NOT.
|
|
|
|
+pub struct BytesWithChecksum((Bytes, u32));
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
pub enum Value {
|
|
pub enum Value {
|
|
- Hash(LockedValue<HashMap<Bytes, Bytes>>),
|
|
|
|
|
|
+ Hash(locked::Value<HashMap<Bytes, Bytes>>),
|
|
|
|
+ List(locked::Value<LinkedList<checksum::Value>>),
|
|
Array(Vec<Value>),
|
|
Array(Vec<Value>),
|
|
Blob(Bytes),
|
|
Blob(Bytes),
|
|
String(String),
|
|
String(String),
|
|
@@ -138,6 +118,7 @@ impl<'a> From<&ParsedValue<'a>> for Value {
|
|
}
|
|
}
|
|
|
|
|
|
value_try_from!(f64, Value::Float);
|
|
value_try_from!(f64, Value::Float);
|
|
|
|
+value_try_from!(i32, Value::Integer);
|
|
value_try_from!(i64, Value::Integer);
|
|
value_try_from!(i64, Value::Integer);
|
|
value_try_from!(i128, Value::BigInteger);
|
|
value_try_from!(i128, Value::BigInteger);
|
|
|
|
|
|
@@ -149,7 +130,13 @@ impl From<&str> for Value {
|
|
|
|
|
|
impl From<HashMap<Bytes, Bytes>> for Value {
|
|
impl From<HashMap<Bytes, Bytes>> for Value {
|
|
fn from(value: HashMap<Bytes, Bytes>) -> Value {
|
|
fn from(value: HashMap<Bytes, Bytes>) -> Value {
|
|
- Value::Hash(LockedValue::new(value))
|
|
|
|
|
|
+ Value::Hash(locked::Value::new(value))
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<LinkedList<checksum::Value>> for Value {
|
|
|
|
+ fn from(value: LinkedList<checksum::Value>) -> Value {
|
|
|
|
+ Value::List(locked::Value::new(value))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|