Skip to main content

kevy_client_async/
cmd_hash.rs

1//! Async mirror of hash commands on `kevy_client::Connection`.
2
3use std::io;
4
5use kevy_resp::Reply;
6
7use crate::conn::AsyncConnection;
8use crate::reply::{array_to_bulks, string, unexpected, vec2, vec3};
9
10impl AsyncConnection {
11    /// `HSET key field value [field value ...]`. Returns count of
12    /// fields newly created (overwrites don't count).
13    pub async fn hset(
14        &mut self,
15        key: &[u8],
16        pairs: &[(&[u8], &[u8])],
17    ) -> io::Result<usize> {
18        let mut args = Vec::with_capacity(2 + pairs.len() * 2);
19        args.push(b"HSET".to_vec());
20        args.push(key.to_vec());
21        for (f, v) in pairs {
22            args.push(f.to_vec());
23            args.push(v.to_vec());
24        }
25        match self.codec_mut().request(&args).await? {
26            Reply::Int(n) if n >= 0 => Ok(n as usize),
27            Reply::Error(e) => Err(io::Error::other(string(e))),
28            other => Err(unexpected(other)),
29        }
30    }
31
32    /// `HGET key field`. `None` if key or field absent.
33    pub async fn hget(&mut self, key: &[u8], field: &[u8]) -> io::Result<Option<Vec<u8>>> {
34        match self.codec_mut().request(&vec3(b"HGET", key, field)).await? {
35            Reply::Bulk(v) => Ok(Some(v)),
36            Reply::Nil => Ok(None),
37            Reply::Error(e) => Err(io::Error::other(string(e))),
38            other => Err(unexpected(other)),
39        }
40    }
41
42    /// `HDEL key field [field ...]`. Returns count actually removed.
43    pub async fn hdel(&mut self, key: &[u8], fields: &[&[u8]]) -> io::Result<usize> {
44        let mut args = Vec::with_capacity(fields.len() + 2);
45        args.push(b"HDEL".to_vec());
46        args.push(key.to_vec());
47        args.extend(fields.iter().map(|f| f.to_vec()));
48        match self.codec_mut().request(&args).await? {
49            Reply::Int(n) if n >= 0 => Ok(n as usize),
50            Reply::Error(e) => Err(io::Error::other(string(e))),
51            other => Err(unexpected(other)),
52        }
53    }
54
55    /// `HLEN key`. 0 if absent.
56    pub async fn hlen(&mut self, key: &[u8]) -> io::Result<usize> {
57        match self.codec_mut().request(&vec2(b"HLEN", key)).await? {
58            Reply::Int(n) if n >= 0 => Ok(n as usize),
59            Reply::Error(e) => Err(io::Error::other(string(e))),
60            other => Err(unexpected(other)),
61        }
62    }
63
64    /// `HGETALL key`. Flat `[f0, v0, f1, v1, ...]`. Empty if absent.
65    pub async fn hgetall(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
66        match self.codec_mut().request(&vec2(b"HGETALL", key)).await? {
67            Reply::Array(items) => array_to_bulks(items),
68            Reply::Error(e) => Err(io::Error::other(string(e))),
69            other => Err(unexpected(other)),
70        }
71    }
72
73    /// `HKEYS key`. Hash's field names.
74    pub async fn hkeys(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
75        match self.codec_mut().request(&vec2(b"HKEYS", key)).await? {
76            Reply::Array(items) => array_to_bulks(items),
77            Reply::Error(e) => Err(io::Error::other(string(e))),
78            other => Err(unexpected(other)),
79        }
80    }
81
82    /// `HVALS key`. Hash's values.
83    pub async fn hvals(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
84        match self.codec_mut().request(&vec2(b"HVALS", key)).await? {
85            Reply::Array(items) => array_to_bulks(items),
86            Reply::Error(e) => Err(io::Error::other(string(e))),
87            other => Err(unexpected(other)),
88        }
89    }
90}