wedb_embed 0.1.4

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
Documentation
/// Implements MetaOps trait for metadata structures.
/// 为 meta 结构体批量实现 MetaOps trait(仅需指定 tag)
macro_rules! impl_meta_ops {
  ($ty:ty, $tag:expr, $enc_ty:ty) => {
    impl $crate::meta::MetaOps for $ty {
      const TAG: &[u8] = $tag;
      type EncodedBytes = $enc_ty;

      #[inline]
      fn decode(bytes: &[u8]) -> Option<Self> {
        <$ty>::decode(bytes)
      }

      #[inline]
      fn is_expired(&self, now_ms: u64) -> bool {
        self.base.is_expired(now_ms)
      }

      #[inline]
      fn encode_bytes(&self) -> Self::EncodedBytes {
        self.encode()
      }

      #[inline]
      fn base(&self) -> &$crate::meta::KeyMeta {
        &self.base
      }

      #[inline]
      fn base_mut(&mut self) -> &mut $crate::meta::KeyMeta {
        &mut self.base
      }
    }
  };
  ($ty:ty, $tag:expr) => {
    impl_meta_ops!($ty, $tag, [u8; <$ty>::ENCODED_SIZE]);
  };
}

/// Defines single KeyMeta wrapper structure and implements MetaOps.
/// 为复合数据类型批量定义单 KeyMeta 封装结构体并实现对应方法及 MetaOps
macro_rules! define_simple_key_meta {
  ($meta_name:ident, $redis_type:ident, $tag:expr, $doc:expr) => {
    #[doc = $doc]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
    pub struct $meta_name {
      pub base: $crate::meta::KeyMeta,
    }

    impl $meta_name {
      pub const ENCODED_SIZE: usize = $crate::meta::KeyMeta::ENCODED_SIZE;
      pub const KVROCKS_ENCODED_SIZE: usize = $crate::meta::KeyMeta::KVROCKS_COMPLEX_ENCODED_SIZE;

      #[inline]
      pub const fn new(expire_at: u64, version: u64, size: u64) -> Self {
        Self {
          base: $crate::meta::KeyMeta::new(
            $crate::meta::RedisType::$redis_type,
            expire_at,
            version,
            size,
          ),
        }
      }

      #[inline]
      pub fn new_with_version(expire_at: u64, size: u64) -> Self {
        Self {
          base: $crate::meta::KeyMeta::new_with_version(
            $crate::meta::RedisType::$redis_type,
            expire_at,
            size,
          ),
        }
      }

      #[inline]
      pub const fn size(&self) -> u64 {
        self.base.size
      }

      #[inline]
      pub const fn version(&self) -> u64 {
        self.base.version
      }

      #[inline]
      pub const fn expire_at(&self) -> u64 {
        self.base.expire_at
      }

      #[inline]
      pub const fn ttl(&self, now_ms: u64) -> i64 {
        self.base.ttl(now_ms)
      }

      #[inline]
      pub const fn is_empty(&self) -> bool {
        self.base.size == 0
      }

      #[inline]
      pub const fn is_expired(&self, now_ms: u64) -> bool {
        self.base.is_expired(now_ms)
      }

      #[inline]
      pub fn encode(&self) -> [u8; Self::ENCODED_SIZE] {
        self.base.encode()
      }

      #[inline]
      pub fn encode_kvrocks(&self) -> Vec<u8> {
        self.base.encode_kvrocks()
      }

      #[inline]
      pub fn decode(bytes: &[u8]) -> Option<Self> {
        let base = $crate::meta::KeyMeta::decode(bytes)?;
        if base.rtype == $crate::meta::RedisType::$redis_type {
          Some(Self { base })
        } else {
          None
        }
      }
    }

    impl_meta_ops!($meta_name, $tag);
  };
}