@@ -58,6 +58,11 @@ fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
5858const MODULE_NAME : & str = "alloc" ;
5959const MODULE_VERSION : c_int = 1 ;
6060
61+ const COMMANDS : & [ Command ] = & [
62+ Command { name : "alloc.set" , handler : alloc_set, flags : "write" } ,
63+ Command { name : "alloc.get" , handler : alloc_get, flags : "write" } ,
64+ ] ;
65+
6166/*
6267redis_module!(
6368 MODULE_NAME,
@@ -71,6 +76,48 @@ redis_module!(
7176);
7277*/
7378
79+ macro_rules! redis_command {
80+ ( $ctx: expr, $command_name: expr, $command_handler: expr, $command_flags: expr) => {
81+ {
82+ ///////////////////////////
83+ //let command_name = "alloc.set";
84+ //let command_flags = "write";
85+ ///////////////////////////
86+
87+ let name = CString :: new( $command_name) . unwrap( ) ;
88+ let flags = CString :: new( $command_flags) . unwrap( ) ;
89+ let ( firstkey, lastkey, keystep) = ( 1 , 1 , 1 ) ;
90+
91+ /////////////////////
92+ extern fn do_command(
93+ ctx: * mut raw:: RedisModuleCtx ,
94+ argv: * mut * mut raw:: RedisModuleString ,
95+ argc: c_int,
96+ ) -> c_int {
97+ let context = Context :: new( ctx) ;
98+
99+ let args: Vec <String > = unsafe { slice:: from_raw_parts( argv, argc as usize ) }
100+ . into_iter( )
101+ . map( |a| RedisString :: from_ptr( * a) . expect( "UTF8 encoding error in handler args" ) . to_string( ) )
102+ . collect( ) ;
103+
104+ let response = $command_handler( & context, args) ;
105+ context. reply( response) as c_int
106+ }
107+ /////////////////////
108+
109+ if raw:: RedisModule_CreateCommand . unwrap( ) (
110+ $ctx,
111+ name. as_ptr( ) ,
112+ Some ( do_command) ,
113+ flags. as_ptr( ) ,
114+ firstkey, lastkey, keystep,
115+ ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _; }
116+ }
117+ }
118+ }
119+
120+
74121use std:: os:: raw:: c_int;
75122use std:: ffi:: CString ;
76123use std:: slice;
@@ -115,85 +162,11 @@ pub extern "C" fn RedisModule_OnLoad(
115162 eprintln ! ( "*** NOT USING Redis allocator ***" ) ;
116163 }
117164
118- //////////////////////////////
119- // Command 1
120- {
121- ///////////////////////////
122- let command_name = "alloc.set" ;
123- let command_flags = "write" ;
124- ///////////////////////////
125-
126- let name = CString :: new ( command_name) . unwrap ( ) ;
127- let flags = CString :: new ( command_flags) . unwrap ( ) ;
128- let ( firstkey, lastkey, keystep) = ( 1 , 1 , 1 ) ;
129-
130- /////////////////////
131- extern fn do_command (
132- ctx : * mut raw:: RedisModuleCtx ,
133- argv : * mut * mut raw:: RedisModuleString ,
134- argc : c_int ,
135- ) -> c_int {
136- let context = Context :: new ( ctx) ;
137-
138- let args: Vec < String > = unsafe { slice:: from_raw_parts ( argv, argc as usize ) }
139- . into_iter ( )
140- . map ( |a| RedisString :: from_ptr ( * a) . expect ( "UTF8 encoding error in handler args" ) . to_string ( ) )
141- . collect ( ) ;
142-
143- let response = alloc_set ( & context, args) ;
144- context. reply ( response) as c_int
145- }
146- /////////////////////
147-
148- if raw:: RedisModule_CreateCommand . unwrap ( ) (
149- ctx,
150- name. as_ptr ( ) ,
151- Some ( do_command) ,
152- flags. as_ptr ( ) ,
153- firstkey, lastkey, keystep,
154- ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _ ; }
155- }
156- //////////////////////////////
157-
158- //////////////////////////////
159- // Command 2
160- {
161- ///////////////////////////
162- let command_name = "alloc.get" ;
163- let command_flags = "write" ;
164- ///////////////////////////
165-
166- let name = CString :: new ( command_name) . unwrap ( ) ;
167- let flags = CString :: new ( command_flags) . unwrap ( ) ;
168- let ( firstkey, lastkey, keystep) = ( 1 , 1 , 1 ) ;
169-
170- /////////////////////
171- extern fn do_command (
172- ctx : * mut raw:: RedisModuleCtx ,
173- argv : * mut * mut raw:: RedisModuleString ,
174- argc : c_int ,
175- ) -> c_int {
176- let context = Context :: new ( ctx) ;
177-
178- let args: Vec < String > = unsafe { slice:: from_raw_parts ( argv, argc as usize ) }
179- . into_iter ( )
180- . map ( |a| RedisString :: from_ptr ( * a) . expect ( "UTF8 encoding error in handler args" ) . to_string ( ) )
181- . collect ( ) ;
182-
183- let response = alloc_get ( & context, args) ;
184- context. reply ( response) as c_int
185- }
186- /////////////////////
187-
188- if raw:: RedisModule_CreateCommand . unwrap ( ) (
189- ctx,
190- name. as_ptr ( ) ,
191- Some ( do_command) ,
192- flags. as_ptr ( ) ,
193- firstkey, lastkey, keystep,
194- ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _ ; }
195- }
196- //////////////////////////////
165+ // for command in COMMANDS {
166+ // redis_command!(ctx, command.name, command.handler, command.flags);
167+ // }
168+ redis_command ! ( ctx, "alloc.set" , alloc_set, "write" ) ;
169+ redis_command ! ( ctx, "alloc.get" , alloc_get, "write" ) ;
197170
198171 raw:: Status :: Ok as _
199172 }
0 commit comments