jdb_trait 0.1.5

异步存储引擎数据库抽象层 / Async database abstraction layer for storage engines
use bytes::Bytes;

pub trait JdbEmbed {
  type Error;

  /// 获取键对应的值 / Get the val associated with the key
  fn get(&self, key: impl AsRef<[u8]>) -> Result<Option<Bytes>, Self::Error>;

  /// 设置键值对 / Set the key-val pair
  fn set(&self, key: impl AsRef<[u8]>, val: impl AsRef<[u8]>) -> Result<(), Self::Error>;

  /// 设置键值对,仅在键不存在时设置 / Set the key-val pair only if the key does not exist
  fn setnx(&self, key: impl AsRef<[u8]>, val: impl AsRef<[u8]>) -> Result<bool, Self::Error>;

  /// 设置键值对,仅在键存在时设置 / Set the key-val pair only if the key exists
  fn setxx(&self, key: impl AsRef<[u8]>, val: impl AsRef<[u8]>) -> Result<bool, Self::Error>;

  /// 设置键值对并获取旧值 / Set the key-val pair and return the old val
  fn setget(
    &self,
    key: impl AsRef<[u8]>,
    val: impl AsRef<[u8]>,
  ) -> Result<Option<Bytes>, Self::Error>;

  /// 设置键值对并指定过期时间(秒) / Set the key-val pair with expiration time in sec
  fn setex(
    &self,
    key: impl AsRef<[u8]>,
    val: impl AsRef<[u8]>,
    sec: u64,
  ) -> Result<(), Self::Error>;

  /// 设置键值对并指定过期时间(毫秒) / Set the key-val pair with expiration time in msec
  fn setpx(
    &self,
    key: impl AsRef<[u8]>,
    val: impl AsRef<[u8]>,
    ms: u64,
  ) -> Result<(), Self::Error>;

  /// 在指定键的值后追加内容 / Append a value to the key's value
  fn append(&self, key: impl AsRef<[u8]>, val: impl AsRef<[u8]>) -> Result<usize, Self::Error>;

  /// 将键对应的值加1 / Increment the numeric val of the key by 1
  fn incr(&self, key: impl AsRef<[u8]>) -> Result<i64, Self::Error>;

  /// 将键对应的值减1 / Decrement the numeric val of the key by 1
  fn decr(&self, key: impl AsRef<[u8]>) -> Result<i64, Self::Error>;

  /// 将键对应的值增加指定数量 / Increment the numeric val of the key by the specified amount
  fn incrby(&self, key: impl AsRef<[u8]>, increment: i64) -> Result<i64, Self::Error>;

  /// 将键对应的值减少指定数量 / Decrement the numeric val of the key by the specified amount
  fn decrby(&self, key: impl AsRef<[u8]>, decrement: i64) -> Result<i64, Self::Error>;

  /// 获取字符串值的长度 / Get the length of the string val
  fn strlen(&self, key: impl AsRef<[u8]>) -> Result<usize, Self::Error>;

  /// 从指定偏移量开始覆盖字符串值 / Overwrite the string val starting at the specified offset
  fn setrange(
    &self,
    key: impl AsRef<[u8]>,
    offset: usize,
    val: impl AsRef<[u8]>,
  ) -> Result<usize, Self::Error>;

  /// 获取字符串值的子串 / Get a substring of the string val
  fn getrange(&self, key: impl AsRef<[u8]>, start: isize, end: isize)
  -> Result<Bytes, Self::Error>;

  /// 批量获取多个键的值 / Get vals for multiple keys
  fn mget(&self, keys: Vec<impl AsRef<[u8]>>) -> Result<Vec<Option<Bytes>>, Self::Error>;

  /// 批量设置多个键值对 / Set multiple key-val pairs
  fn mset(&self, keyvalpairs: Vec<(impl AsRef<[u8]>, impl AsRef<[u8]>)>)
  -> Result<(), Self::Error>;

  /// 删除键 / Delete the key(s) and return the number of deleted keys
  fn del(&self, keys: &[impl AsRef<[u8]>]) -> Result<usize, Self::Error>;

  /// 检查键是否存在 / Check if the key exists
  fn exists(&self, key: impl AsRef<[u8]>) -> Result<bool, Self::Error>;

  /// 设置键的过期时间(秒) / Set the expiration time for the key in sec
  fn expire(&self, key: impl AsRef<[u8]>, sec: u64) -> Result<bool, Self::Error>;

  /// 设置键的过期时间(毫秒) / Set the expiration time for the key in msec
  fn pexpire(&self, key: impl AsRef<[u8]>, ms: u64) -> Result<bool, Self::Error>;

  /// 获取键的剩余过期时间(秒) / Get the remaining expiration time in sec
  fn ttl(&self, key: impl AsRef<[u8]>) -> Result<Option<i64>, Self::Error>;

  /// 获取键的剩余过期时间(毫秒) / Get the remaining expiration time in msec
  fn pttl(&self, key: impl AsRef<[u8]>) -> Result<Option<i64>, Self::Error>;

  /// 移除键的过期时间 / Remove the expiration time from the key
  fn persist(&self, key: impl AsRef<[u8]>) -> Result<bool, Self::Error>;

  /// 获取键的值并设置过期时间(秒) / Get the val and set expiration time in sec
  fn getex(&self, key: impl AsRef<[u8]>, sec: u64) -> Result<Option<Bytes>, Self::Error>;

  /// 获取键的值并删除键 / Get the val and delete the key
  fn getdel(&self, key: impl AsRef<[u8]>) -> Result<Option<Bytes>, Self::Error>;
}