|
| 1 | +use std::os::raw::{c_long, c_longlong}; |
| 2 | +use std::ffi::CString; |
| 3 | + |
| 4 | +use crate::raw; |
| 5 | +use crate::error::Error; |
| 6 | +use crate::{RedisString, LogLevel, Reply}; |
| 7 | +use crate::key::{RedisKey,RedisKeyWritable}; |
| 8 | + |
| 9 | +/// Redis is a structure that's designed to give us a high-level interface to |
| 10 | +/// the Redis module API by abstracting away the raw C FFI calls. |
| 11 | +pub struct Context { |
| 12 | + ctx: *mut raw::RedisModuleCtx, |
| 13 | +} |
| 14 | + |
| 15 | +impl Context { |
| 16 | + pub fn new(ctx: *mut raw::RedisModuleCtx) -> Self { |
| 17 | + Self {ctx} |
| 18 | + } |
| 19 | + |
| 20 | + /// Coerces a Redis string as an integer. |
| 21 | + /// |
| 22 | + /// Redis is pretty dumb about data types. It nominally supports strings |
| 23 | + /// versus integers, but an integer set in the store will continue to look |
| 24 | + /// like a string (i.e. "1234") until some other operation like INCR forces |
| 25 | + /// its coercion. |
| 26 | + /// |
| 27 | + /// This method coerces a Redis string that looks like an integer into an |
| 28 | + /// integer response. All other types of replies are passed through |
| 29 | + /// unmodified. |
| 30 | + pub fn coerce_integer( |
| 31 | + &self, |
| 32 | + reply_res: Result<Reply, Error>, |
| 33 | + ) -> Result<Reply, Error> { |
| 34 | + match reply_res { |
| 35 | + Ok(Reply::String(s)) => match s.parse::<i64>() { |
| 36 | + Ok(n) => Ok(Reply::Integer(n)), |
| 37 | + _ => Ok(Reply::String(s)), |
| 38 | + }, |
| 39 | + _ => reply_res, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn create_string(&self, s: &str) -> RedisString { |
| 44 | + RedisString::create(self.ctx, s) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn log(&self, level: LogLevel, message: &str) { |
| 48 | + let level = CString::new(format!("{:?}", level).to_lowercase()).unwrap(); |
| 49 | + let fmt = CString::new(message).unwrap(); |
| 50 | + raw::log( |
| 51 | + self.ctx, |
| 52 | + level.as_ptr(), |
| 53 | + fmt.as_ptr(), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + pub fn log_debug(&self, message: &str) { |
| 58 | + // Note that we log our debug messages as notice level in Redis. This |
| 59 | + // is so that they'll show up with default configuration. Our debug |
| 60 | + // logging will get compiled out in a release build so this won't |
| 61 | + // result in undue noise in production. |
| 62 | + self.log(LogLevel::Notice, message); |
| 63 | + } |
| 64 | + |
| 65 | + /// Opens a Redis key for read access. |
| 66 | + pub fn open_key(&self, key: &str) -> RedisKey { |
| 67 | + RedisKey::open(self.ctx, key) |
| 68 | + } |
| 69 | + |
| 70 | + /// Opens a Redis key for read and write access. |
| 71 | + pub fn open_key_writable(&self, key: &str) -> RedisKeyWritable { |
| 72 | + RedisKeyWritable::open(self.ctx, key) |
| 73 | + } |
| 74 | + |
| 75 | + /// Tells Redis that we're about to reply with an (Redis) array. |
| 76 | + /// |
| 77 | + /// Used by invoking once with the expected length and then calling any |
| 78 | + /// combination of the other reply_* methods exactly that number of times. |
| 79 | + pub fn reply_array(&self, len: i64) -> Result<(), Error> { |
| 80 | + handle_status( |
| 81 | + raw::reply_with_array(self.ctx, len as c_long), |
| 82 | + "Could not reply with long", |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn reply_integer(&self, integer: i64) -> Result<(), Error> { |
| 87 | + handle_status( |
| 88 | + raw::reply_with_long_long(self.ctx, integer as c_longlong), |
| 89 | + "Could not reply with longlong", |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn reply_string(&self, message: &str) -> Result<(), Error> { |
| 94 | + let redis_str = self.create_string(message); |
| 95 | + handle_status( |
| 96 | + raw::reply_with_string(self.ctx, redis_str.str_inner), |
| 97 | + "Could not reply with string", |
| 98 | + ) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn handle_status(status: raw::Status, message: &str) -> Result<(), Error> { |
| 103 | + match status { |
| 104 | + raw::Status::Ok => Ok(()), |
| 105 | + raw::Status::Err => Err(error!(message)), |
| 106 | + } |
| 107 | +} |
0 commit comments