Skip to content

Commit 6969a28

Browse files
committed
new version. it's a class now
1 parent cd3e793 commit 6969a28

22 files changed

+530
-0
lines changed

LICENCE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <[email protected]>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.
14+

README.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# go-redis
2+
3+
go-redis - The GNU Octave redis client
4+
5+
6+
A [Redis](http://redis.io) client for [GNU Octave](http://www.gnu.org/software/octave/), written in pure Octave, using
7+
[instrumen-control](http://octave.sourceforge.net/instrument-control/index.html) package.
8+
9+
This client works by establishing a TCP connection to the specified Redis server and using the [Redis protocol](http://redis.io/topics/protocol).
10+
It's fast. It writes 1*10^6 Values in ~10 seconds (tested on AMD E-450) on localhost.
11+
12+
13+
# Versions
14+
15+
### go-redis [developer Version]
16+
17+
git clone https://github.com/markuman/go-redis.git
18+
19+
* (will be go-redis-2.0 one day)
20+
* some improvements + matlab compatibility (maybe, maybe not...not finished yet)
21+
22+
23+
### go-redis 1.0 [stable]
24+
25+
git clone https://github.com/markuman/go-redis.git && cd go-redis
26+
git checkout b8b6b1d
27+
28+
* for Octave >= 3.6, instrument-control >= 0.2, redis >= 2.6
29+
30+
31+
# Documentation
32+
33+
## set and get in go-redis
34+
35+
set can save single values (num or str) or numeric n-dimension Matrix _(and structs of depth one)_.
36+
Furthermore, it is important to know, how go-redis is saving n-dimension Matrix. It use RPUSH (a list of values) in redis and reshape
37+
in octave. But the first(!) value in the RPUSH list is reservated for the dimension of your Matrix. This is important, if you want to use the
38+
values with other applications or programming languages too! E.g. for 4x7 Matrix, the first Value is "4 7 ".
39+
40+
## usage
41+
42+
Make a redis connection:
43+
44+
r = redis() % connect to localhost on port 6379
45+
r = redis('192.168.1.1') % connect to 192.168.1.1 on port 6379
46+
r = redis('foo.com', 4242) % connect to foo.com on port 4242
47+
48+
Authenticate if needed:
49+
50+
status = auth(r,'password');
51+
52+
For set are no options. It knows if you want to store a string, a matrice or a single value.
53+
54+
status = set(r,'keyName',variablename);
55+
% if you don't name a keyname, the name of the variable will be taken as keyname
56+
status = set(r,variablename);
57+
58+
For get are no options too
59+
60+
matrix = get(r,'keyName');
61+
62+
To test the connection or keep your session alive, you can use redisPing
63+
64+
pong = redisPing(r)
65+
66+
To change the database on the connected redis server, use redisSelect. By default, redisConnection connects to database 0, whitch is the first
67+
database
68+
69+
feedback = select(r,2); % Connects to the 3rd database
70+
71+
Increase or Decrease Integer Values
72+
73+
incr(r,'keyname'); % just increase a value without feedback
74+
tmp = decr(r,'keyname'); % decrease a value and asign the new value to 'tmp' variable in octave
75+
76+
Rename or moving keys
77+
78+
rename(r,'oldkeyname','newkeyname');
79+
80+
To get the size of the database
81+
82+
size_of_db = dbsize(r);
83+
84+
Synchronously save the dataset to disk
85+
86+
reply = save(r);
87+
88+
With command you can use any command with redis. But the output is raw! So you have to parse the output by yourself (redis protocol)! You
89+
just want to use this for debugging. At least, you need two or three arguments (atm very limited)!
90+
91+
redis 127.0.0.1:6379[1]> keys *
92+
1) "test"
93+
2) "wurst"
94+
----
95+
octave:7> command(R,'keys','*')
96+
ans = *2
97+
$4
98+
test
99+
$5
100+
wurst
101+
102+
redis 127.0.0.1:6379> LLEN SportB
103+
(integer) 3
104+
----
105+
octave:9> command(R,'LLEN', 'SportB')
106+
ans = :3
107+
108+
redis 127.0.0.1:6379> ping
109+
PONG
110+
----
111+
octave:10> command(R,'PING')
112+
ans = +PONG
113+
114+
115+
# Thanks
116+
* https://github.com/dac922/
117+

bsp.m

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pkg load instrument-control
2+
addpath inst/
3+
4+
r=redis() % default connection settings
5+
6+
meinematrix=rand(2,2,2)
7+
8+
set(r, meinematrix);
9+
10+
hu=get(r, "meinematrix");
11+
12+
str="gegeben sei dass das hier serialisierter outout ist";
13+
14+
save(r, str);
15+
16+
ja=load(r, "str");

inst/@redis/auth.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function value = auth(obj, passphrase)
2+
3+
R=obj.redis;
4+
5+
__redisWrite(R, 'AUTH', passphrase);
6+
value = __redisRead(R, 5000);
7+
8+
if !strcmp(value,"+OK\r\n")
9+
warning("redis: auth not successful");
10+
end

inst/@redis/command.m

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function value = command(obj, Command, opt)
2+
3+
R=obj.redis;
4+
5+
if nargin < 3
6+
__redisWrite(R, Command);
7+
value = __redisRead(R, 5000);
8+
else
9+
__redisWrite(R, Command, opt);
10+
value = __redisRead(R, 5000);
11+
end

inst/@redis/dbsize.m

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function dbs = dbsize(obj)
2+
3+
R=obj.redis;
4+
5+
__redisWrite(R, 'DBSIZE');
6+
dbsize = __redisRead(R, 5000);
7+

inst/@redis/decr.m

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function value = decr(obj, key)
2+
3+
R=obj.redis;
4+
5+
__redisWrite (R, 'DECR', key);
6+
value=__redisRead (R, 5000);
7+

inst/@redis/get.m

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function value = get (obj, key)
2+
3+
R=obj.redis;
4+
5+
if nargin < 2,
6+
disp('ERROR: redisGet needs 2 arguments! redisGet(R, key)');
7+
else
8+
9+
__redisWrite (R, 'TYPE', key);
10+
t=__redisRead (R, 5000);
11+
12+
if strfind (t,'+list')
13+
__redisWrite (R, 'LRANGE', key, 0, 7e6);
14+
reply = __redisRead(R, 5000);
15+
16+
reply(reply==13)=[];
17+
tmp = strsplit (reply, char(10));
18+
eval (sprintf ("dim = [%s];",tmp{3}));
19+
tmp = str2double ({tmp{5:2:end}});
20+
value = reshape (tmp,dim);
21+
22+
else
23+
__redisWrite (R, 'GET', key);
24+
reply = __redisRead (R, 5000);
25+
26+
reply(reply==13)=[];
27+
value = strsplit(reply,char(10)){2};
28+
if 1 == __isnum(value)
29+
value = str2num (value);
30+
end
31+
32+
end
33+
end

inst/@redis/incr.m

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function value = incr(obj, key)
2+
3+
R=obj.redis;
4+
5+
__redisWrite (R, 'INCR', key);
6+
value=__redisRead (R, 5000);
7+

inst/@redis/load.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function value = load (obj, key)
2+
3+
R=obj.redis;
4+
5+
if nargin < 2,
6+
disp('ERROR: redisGet needs 2 arguments! redisGet(R, key)');
7+
else
8+
9+
__redisWrite (R, 'TYPE', key);
10+
t=__redisRead (R, 5000);
11+
12+
if strfind (t,'+list')
13+
__redisWrite (R, 'LRANGE', key, 0, 7e6);
14+
reply = __redisRead(R, 5000);
15+
16+
reply(reply==13)=[];
17+
tmp = strsplit (reply, char(10));
18+
eval (sprintf ('dim = [%s];',tmp{3}));
19+
tmp = str2double ({tmp{5:2:end}});
20+
value = reshape (tmp,dim);
21+
22+
else
23+
__redisWrite (R, 'GET', key);
24+
reply = __redisRead (R, 5000);
25+
reply(reply==13)=[];
26+
27+
value = strsplit(reply,char(10));
28+
value = value{2};
29+
30+
%% FIXME
31+
%% Optional deserialize here!!
32+
if 1 == __isnum(value)
33+
value = str2num (value);
34+
end
35+
36+
end
37+
end

inst/@redis/move.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function value = move(obj, key, db)
2+
3+
R=obj.redis;
4+
5+
__redisWrite(R, 'MOVE', key, db);
6+
value = __redisRead(R, 5000);
7+
8+
if !strcmp(value,":1\r\n")
9+
sprintf('WARNING: Failed to move key %s to database %d!', key, db)
10+
end

inst/@redis/ping.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function pong = ping(obj)
2+
3+
R=obj.redis;
4+
5+
__redisWrite(R, 'PING');
6+
pong = __redisRead(R, 5000);
7+
8+
if !strcmp(pong,"+PONG\r\n")
9+
warning('redis do not respond!');
10+
end

inst/@redis/private/__isnum.m

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
% http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Octave
2+
function r = __isnum(a)
3+
if ( isnumeric(a) )
4+
r = 1;
5+
else
6+
o = str2double(a);
7+
r = !isnan(o);
8+
endif
9+
endfunction

inst/@redis/private/__redisRead.m

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function reply = __redisRead(R, timeout, looptime=100)
2+
3+
start=tic;
4+
5+
% get reply type
6+
rtype = char (tcp_read (R,1,timeout)); % minimum read count = 3 (status byte, \r\n)
7+
reply = rtype;
8+
9+
if isempty (rtype)
10+
error ("unexpected reply");
11+
end
12+
13+
% check for valid replies
14+
if ~(rtype == '+' || rtype == '-' || rtype == ':' || rtype == '$' || rtype == '*')
15+
error ("unexpected reply");
16+
end
17+
18+
19+
lastread = 0;
20+
fprintf(stdout,"\r");fflush(stdout);
21+
22+
% read complete response
23+
while (tic-start < timeout*1000)
24+
fprintf(stdout,"%d", lastread); fflush(stdout);
25+
26+
reply = [reply char (tcp_read (R,1000000,looptime))];
27+
% if read at least one byte, increase timeout
28+
if lastread < length(reply)
29+
timeout = timeout + looptime*1.5;
30+
end
31+
lastread = length(reply);
32+
33+
fprintf(stdout," \r");
34+
35+
lines = length (strfind (reply, "\r\n"));
36+
% break after first line for error, status and integer replies
37+
if (rtype == '+' || rtype == '-' || rtype == ':') && lines > 0
38+
break;
39+
end
40+
41+
% bulk reply is always two lines
42+
if rtype == '$' && lines > 1
43+
break;
44+
end
45+
46+
% multi bulk reply, not binary, not integer safe !
47+
if rtype == '*' && lines > 0
48+
bulkreplies = str2num (strsplit (reply, "\n"){1}(2:end)); % interprete first line
49+
if lines > 2 * bulkreplies
50+
break;
51+
end
52+
end
53+
54+
end
55+
56+
end

0 commit comments

Comments
 (0)