Skip to content

Commit

Permalink
Add: wrap function for setting keys with generic values.
Browse files Browse the repository at this point in the history
This is done for making the RedisCtx::kb member private. so is not accesible from outside the module.
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 9d33e6a commit 921124e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
7 changes: 5 additions & 2 deletions rust/nvtcache/src/nvtcache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const NVTCACHE: &str = "nvticache";

pub mod nvtcache {

// use super::*;
use super::*;
use crate::dberror::dberror::Result;
use crate::redisconnector::redisconnector::*;

Expand All @@ -11,7 +13,7 @@ pub mod nvtcache {

/// NvtCache implementation.
impl NvtCache {
/// initialize the NVT Cache.
/// Initialize and return an NVT Cache Object
pub fn init() -> Result<NvtCache> {
let rctx = RedisCtx::new()?;
Ok(NvtCache {
Expand All @@ -20,6 +22,7 @@ pub mod nvtcache {
})
}

/// Return a bool telling if the NVT Cache is initialized
pub fn is_init(&mut self) -> bool {
self.init == true
}
Expand Down
27 changes: 16 additions & 11 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use redis::*;

const GLOBAL_DBINDEX_NAME: &str = "GVM.__GlobalDBIndex";
const REDIS_DEFAULT_PATH: &str = "unix:///run/redis/redis-server.sock";
const NVTCACHE: &str = "nvticache";

pub mod redisconnector {
use std::result;
Expand All @@ -12,12 +11,13 @@ pub mod redisconnector {
use crate::dberror::dberror::Result;

pub struct RedisCtx {
pub kb: Connection, //a redis connection
db: u32, // the name space
maxdb: u32, // max db index
kb: Connection, //a redis connection
db: u32, // the name space
maxdb: u32, // max db index
}

impl RedisCtx {
/// Connect to the redis server and return a redis context object
pub fn new() -> Result<RedisCtx> {
let client = redis::Client::open(REDIS_DEFAULT_PATH)?;
let kb = client.get_connection()?;
Expand All @@ -30,7 +30,8 @@ pub mod redisconnector {
Ok(redisctx)
}

pub fn max_db_index(&mut self) -> Result<u32> {
/// Get the max db index configured for the redis server instance
fn max_db_index(&mut self) -> Result<u32> {
if self.maxdb > 0 {
return Ok(self.maxdb);
}
Expand All @@ -53,7 +54,9 @@ pub mod redisconnector {
)))
}
}

/// Redis always replies about config with a vector
/// of 2 string ["databases", "Number"]
/// Therefore we convert the "Number" to uint32
fn max_db_index_to_uint(res: Vec<String>) -> u32 {
if res.len() == 2 {
match res[1].to_string().parse::<u32>() {
Expand All @@ -72,7 +75,8 @@ pub mod redisconnector {
let db: u32 = self.db;
Ok(db)
}
pub fn set_namespace(&mut self, db_index: u32) -> Result<String> {

fn set_namespace(&mut self, db_index: u32) -> Result<String> {
if db_index <= 0 {
return Err(DbError::CustomErr(String::from(
"Invalid selected db index {db_index}. It must be greater than ",
Expand All @@ -87,12 +91,12 @@ pub mod redisconnector {
return Ok(String::from("ok"));
}

pub fn try_database(&mut self, dbi: u32) -> Result<u32> {
fn try_database(&mut self, dbi: u32) -> Result<u32> {
let ret = self.kb.hset_nx(GLOBAL_DBINDEX_NAME, dbi, 1)?;
Ok(ret)
}

pub fn select_database(&mut self) -> Result<u32> {
fn select_database(&mut self) -> Result<u32> {
let maxdb: u32 = self.max_db_index()?;
let mut selected_db: u32 = 0;
for i in 1..maxdb {
Expand All @@ -111,12 +115,13 @@ pub mod redisconnector {
)));
}

pub fn redis_set_key_int(&mut self, key: &str, val: i32) -> Result<()> {
//Wrapper function to avoid accessing kb member directly.
pub fn redis_set_key<T: ToRedisArgs>(&mut self, key: &str, val: T) -> Result<()> {
let _: () = self.kb.set(key, val)?;
Ok(())
}

pub fn redis_get_int(&mut self, key: &str) -> String {
pub fn redis_get_key(&mut self, key: &str) -> String {
match self.kb.get(key) {
Ok(x) => return x,
Err(e) => e.to_string(),
Expand Down

0 comments on commit 921124e

Please sign in to comment.