forked from migueldeicaza/redis-sharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
os-redis-sharp-debug.cs
952 lines (788 loc) · 26.5 KB
/
os-redis-sharp-debug.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//c#
//
// os-redis-sharp.cs: ECMA CLI Binding to the Redis key-value storage system
// runnable in a OpenSim script
//
// Authors:
// Miguel de Icaza ([email protected])
// Giovanni De Gasperis ([email protected])
//
// Licensed under the same terms of reddis: new BSD license.
//
// using <Library> cannot be used in a OpenSim script
public class Redis : System.IDisposable {
System.Net.Sockets.Socket socket;
System.IO.BufferedStream bstream;
public enum KeyType {
None, String, List, Set
}
public class ResponseException : System.Exception {
public ResponseException (string code) : base ("Response error")
{
Code = code;
}
public string Code { get; private set; }
}
public Redis (string host, int port)
{
if (host == null)
throw new System.ArgumentNullException ("host");
Host = host;
Port = port;
SendTimeout = -1;
}
public Redis (string host) : this (host, 6379)
{
}
public Redis () : this ("localhost", 6379)
{
}
public string Host { get; private set; }
public int Port { get; private set; }
public int RetryTimeout { get; set; }
public int RetryCount { get; set; }
public int SendTimeout { get; set; }
public string Password { get; set; }
int db;
public int Db {
get {
return db;
}
set {
db = value;
SendExpectSuccess ("SELECT", db);
}
}
public string this [string key] {
get { return GetString (key); }
set { Set (key, value); }
}
public void Set (string key, string value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
Set (key, System.Text.Encoding.UTF8.GetBytes (value));
}
public void Set (string key, byte [] value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
if (value.Length > 1073741824)
throw new System.ArgumentException ("value exceeds 1G", "value");
if (!SendDataCommand (value, "SET", key))
throw new System.Exception ("Unable to connect");
ExpectSuccess ();
}
public bool SetNX (string key, string value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
return SetNX (key, System.Text.Encoding.UTF8.GetBytes (value));
}
public bool SetNX (string key, byte [] value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
if (value.Length > 1073741824)
throw new System.ArgumentException ("value exceeds 1G", "value");
return SendDataExpectInt (value, "SETNX", key) > 0 ? true : false;
}
// problems findind IDictionary that works here... GDG
/*public void Set (System.Linq.IDictionary<string,string> dict)
{
if (dict == null)
throw new System.ArgumentNullException ("dict");
Set (dict.ToDictionary(k => k.Key, v => System.Text.Encoding.UTF8.GetBytes(v.Value)));
}
public void Set (System.Linq.IDictionary<string,byte []> dict)
{
if (dict == null)
throw new System.ArgumentNullException ("dict");
MSet (dict.Keys.ToArray (), dict.Values.ToArray ());
}*/
public void MSet (string [] keys, byte [][] values)
{
if (keys.Length != values.Length)
throw new System.ArgumentException ("keys and values must have the same size");
byte [] nl = System.Text.Encoding.UTF8.GetBytes ("\r\n");
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
for (int i = 0; i < keys.Length; i++) {
byte [] key = System.Text.Encoding.UTF8.GetBytes(keys[i]);
byte [] val = values[i];
byte [] kLength = System.Text.Encoding.UTF8.GetBytes ("$" + key.Length + "\r\n");
byte [] k = System.Text.Encoding.UTF8.GetBytes (keys[i] + "\r\n");
byte [] vLength = System.Text.Encoding.UTF8.GetBytes ("$" + val.Length + "\r\n");
ms.Write (kLength, 0, kLength.Length);
ms.Write (k, 0, k.Length);
ms.Write (vLength, 0, vLength.Length);
ms.Write (val, 0, val.Length);
ms.Write (nl, 0, nl.Length);
}
SendDataRESP (ms.ToArray (), "*" + (keys.Length * 2 + 1) + "\r\n$4\r\nMSET\r\n");
ExpectSuccess ();
}
public byte [] Get (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectData ("GET", key);
}
public string GetString (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return System.Text.Encoding.UTF8.GetString (Get (key));
}
public byte [][] Sort (SortOptions options)
{
return Sort (options.Key, options.StoreInKey, options.ToArgs());
}
public byte [][] Sort (string key, string destination, params object [] options)
{
if (key == null)
throw new System.ArgumentNullException ("key");
int offset = string.IsNullOrEmpty (destination) ? 1 : 3;
object [] args = new object [offset + options.Length];
args [0] = key;
System.Array.Copy (options, 0, args, offset, options.Length);
if (offset == 1) {
return SendExpectDataArray ("SORT", args);
}
else {
args [1] = "STORE";
args [2] = destination;
int n = SendExpectInt ("SORT", args);
return new byte [n][];
}
}
public byte [] GetSet (string key, byte [] value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
if (value.Length > 1073741824)
throw new System.ArgumentException ("value exceeds 1G", "value");
if (!SendDataCommand (value, "GETSET", key))
throw new System.Exception ("Unable to connect");
return ReadData ();
}
public string GetSet (string key, string value)
{
if (key == null)
throw new System.ArgumentNullException ("key");
if (value == null)
throw new System.ArgumentNullException ("value");
return System.Text.Encoding.UTF8.GetString (GetSet (key, System.Text.Encoding.UTF8.GetBytes (value)));
}
string ReadLine ()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
int c;
while ((c = bstream.ReadByte ()) != -1){
if (c == '\r')
continue;
if (c == '\n')
break;
sb.Append ((char) c);
}
return sb.ToString ();
}
void Connect ()
{
socket = new System.Net.Sockets.Socket (System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
socket.NoDelay = true;
socket.SendTimeout = SendTimeout;
socket.Connect (Host, Port);
if (!socket.Connected){
socket.Close ();
socket = null;
return;
}
bstream = new System.IO.BufferedStream (new System.Net.Sockets.NetworkStream (socket), 16*1024);
if (Password != null)
SendExpectSuccess ("AUTH", Password);
}
byte [] end_data = new byte [] { (byte) '\r', (byte) '\n' };
bool SendDataCommand (byte [] data, string cmd, params object [] args)
{
string resp = "*" + (1 + args.Length + 1).ToString () + "\r\n";
resp += "$" + cmd.Length + "\r\n" + cmd + "\r\n";
foreach (object arg in args) {
string argStr = arg.ToString ();
int argStrLength = System.Text.Encoding.UTF8.GetByteCount(argStr);
resp += "$" + argStrLength + "\r\n" + argStr + "\r\n";
}
resp += "$" + data.Length + "\r\n";
return SendDataRESP (data, resp);
}
bool SendDataRESP (byte [] data, string resp)
{
if (socket == null)
Connect ();
if (socket == null)
return false;
byte [] r = System.Text.Encoding.UTF8.GetBytes (resp);
try {
Log ("C", resp);
socket.Send (r);
if (data != null){
socket.Send (data);
socket.Send (end_data);
}
} catch (System.Net.Sockets.SocketException){
// timeout;
socket.Close ();
socket = null;
return false;
}
return true;
}
bool SendCommand (string cmd, params object [] args)
{
if (socket == null)
Connect ();
if (socket == null)
return false;
string resp = "*" + (1 + args.Length).ToString () + "\r\n";
resp += "$" + cmd.Length + "\r\n" + cmd + "\r\n";
foreach (object arg in args) {
string argStr = arg.ToString ();
int argStrLength = System.Text.Encoding.UTF8.GetByteCount(argStr);
resp += "$" + argStrLength + "\r\n" + argStr + "\r\n";
}
byte [] r = System.Text.Encoding.UTF8.GetBytes (resp);
try {
Log ("C", resp);
socket.Send (r);
} catch (System.Net.Sockets.SocketException){
// timeout;
socket.Close ();
socket = null;
return false;
}
return true;
}
void Log (string id, string message)
{
System.Console.WriteLine(id + ": " + message.Trim().Replace("\r\n", " "));
}
void ExpectSuccess ()
{
int c = bstream.ReadByte ();
if (c == -1)
throw new ResponseException ("No more data");
string s = ReadLine ();
Log ("S", (char)c + s);
if (c == '-')
throw new ResponseException (s.StartsWith ("ERR ") ? s.Substring (4) : s);
}
void SendExpectSuccess (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception ("Unable to connect");
ExpectSuccess ();
}
int SendDataExpectInt (byte[] data, string cmd, params object [] args)
{
if (!SendDataCommand (data, cmd, args))
throw new System.Exception ("Unable to connect");
int c = bstream.ReadByte ();
if (c == -1)
throw new ResponseException ("No more data");
string s = ReadLine ();
Log ("S", (char)c + s);
if (c == '-')
throw new ResponseException (s.StartsWith ("ERR ") ? s.Substring (4) : s);
if (c == ':'){
int i;
if (int.TryParse (s, out i))
return i;
}
throw new ResponseException ("Unknown reply on integer request: " + c + s);
}
int SendExpectInt (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception ("Unable to connect");
int c = bstream.ReadByte ();
if (c == -1)
throw new ResponseException ("No more data");
string s = ReadLine ();
Log ("S", (char)c + s);
if (c == '-')
throw new ResponseException (s.StartsWith ("ERR ") ? s.Substring (4) : s);
if (c == ':'){
int i;
if (int.TryParse (s, out i))
return i;
}
throw new ResponseException ("Unknown reply on integer request: " + c + s);
}
string SendExpectString (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception ("Unable to connect");
int c = bstream.ReadByte ();
if (c == -1)
throw new ResponseException ("No more data");
string s = ReadLine ();
Log ("S", (char)c + s);
if (c == '-')
throw new ResponseException (s.StartsWith ("ERR ") ? s.Substring (4) : s);
if (c == '+')
return s;
throw new ResponseException ("Unknown reply on integer request: " + c + s);
}
//
// This one does not throw errors
//
string SendGetString (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception ("Unable to connect");
return ReadLine ();
}
byte [] SendExpectData (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception ("Unable to connect");
return ReadData ();
}
byte [] ReadData ()
{
string s = ReadLine ();
Log ("S", s);
if (s.Length == 0)
throw new ResponseException ("Zero length respose");
char c = s [0];
if (c == '-')
throw new ResponseException (s.StartsWith ("-ERR ") ? s.Substring (5) : s.Substring (1));
if (c == '$'){
if (s == "$-1")
return null;
int n;
if (System.Int32.TryParse (s.Substring (1), out n)){
byte [] retbuf = new byte [n];
int bytesRead = 0;
do {
int read = bstream.Read (retbuf, bytesRead, n - bytesRead);
if (read < 1)
throw new ResponseException("Invalid termination mid stream");
bytesRead += read;
}
while (bytesRead < n);
if (bstream.ReadByte () != '\r' || bstream.ReadByte () != '\n')
throw new ResponseException ("Invalid termination");
return retbuf;
}
throw new ResponseException ("Invalid length");
}
/* don't treat arrays here because only one element works -- use DataArray!
//returns the number of matches
if (c == '*') {
int n;
if (Int32.TryParse(s.Substring(1), out n))
return n <= 0 ? new byte [0] : ReadData();
throw new ResponseException ("Unexpected length parameter" + r);
}
*/
throw new ResponseException ("Unexpected reply: " + s);
}
public bool ContainsKey (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("EXISTS", key) == 1;
}
public bool Remove (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("DEL", key) == 1;
}
public int Remove (params string [] args)
{
if (args == null)
throw new System.ArgumentNullException ("args");
return SendExpectInt ("DEL", args);
}
public int Increment (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("INCR", key);
}
public int Increment (string key, int count)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("INCRBY", key, count);
}
public int Decrement (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("DECR", key);
}
public int Decrement (string key, int count)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("DECRBY", key, count);
}
public KeyType TypeOf (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
switch (SendExpectString ("TYPE", key)) {
case "none":
return KeyType.None;
case "string":
return KeyType.String;
case "set":
return KeyType.Set;
case "list":
return KeyType.List;
}
throw new ResponseException ("Invalid value");
}
public string RandomKey ()
{
return SendExpectString ("RANDOMKEY");
}
public bool Rename (string oldKeyname, string newKeyname)
{
if (oldKeyname == null)
throw new System.ArgumentNullException ("oldKeyname");
if (newKeyname == null)
throw new System.ArgumentNullException ("newKeyname");
return SendGetString ("RENAME", oldKeyname, newKeyname) [0] == '+';
}
public bool Expire (string key, int seconds)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("EXPIRE", key, seconds) == 1;
}
public bool ExpireAt (string key, int time)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("EXPIREAT", key, time) == 1;
}
public int TimeToLive (string key)
{
if (key == null)
throw new System.ArgumentNullException ("key");
return SendExpectInt ("TTL", key);
}
public int DbSize {
get {
return SendExpectInt ("DBSIZE");
}
}
public void Save ()
{
SendExpectSuccess ("SAVE");
}
public void BackgroundSave ()
{
SendExpectSuccess ("BGSAVE");
}
public void Shutdown ()
{
SendCommand ("SHUTDOWN");
try {
// the server may return an error
string s = ReadLine ();
Log ("S", s);
if (s.Length == 0)
throw new ResponseException ("Zero length respose");
throw new ResponseException (s.StartsWith ("-ERR ") ? s.Substring (5) : s.Substring (1));
} catch (System.IO.IOException) {
// this is the expected good result
socket.Close ();
socket = null;
}
}
public void FlushAll ()
{
SendExpectSuccess ("FLUSHALL");
}
public void FlushDb ()
{
SendExpectSuccess ("FLUSHDB");
}
const long UnixEpoch = 621355968000000000L;
public System.DateTime LastSave {
get {
int t = SendExpectInt ("LASTSAVE");
return new System.DateTime (UnixEpoch) + System.TimeSpan.FromSeconds (t);
}
}
public Dictionary<string,string> GetInfo ()
{
byte [] r = SendExpectData ("INFO");
var dict = new Dictionary<string,string>();
foreach (var line in System.Text.Encoding.UTF8.GetString (r).Split ('\n')){
int p = line.IndexOf (':');
if (p == -1)
continue;
dict.Add (line.Substring (0, p), line.Substring (p+1));
}
return dict;
}
public string [] Keys {
get {
return GetKeys("*");
}
}
public string [] GetKeys (string pattern)
{
if (pattern == null)
throw new System.ArgumentNullException ("pattern");
return SendExpectStringArray ("KEYS", pattern);
}
public byte [][] MGet (params string [] keys)
{
if (keys == null)
throw new System.ArgumentNullException ("keys");
if (keys.Length == 0)
throw new System.ArgumentException ("keys");
return SendExpectDataArray ("MGET", keys);
}
public string [] SendExpectStringArray (string cmd, params object [] args)
{
byte [][] reply = SendExpectDataArray (cmd, args);
string [] keys = new string [reply.Length];
for (int i = 0; i < reply.Length; i++)
keys[i] = System.Text.Encoding.UTF8.GetString (reply[i]);
return keys;
}
public byte[][] SendExpectDataArray (string cmd, params object [] args)
{
if (!SendCommand (cmd, args))
throw new System.Exception("Unable to connect");
int c = bstream.ReadByte();
if (c == -1)
throw new ResponseException("No more data");
string s = ReadLine();
Log("S", (char)c + s);
if (c == '-')
throw new ResponseException(s.StartsWith("ERR ") ? s.Substring(4) : s);
if (c == '*') {
int count;
if (int.TryParse (s, out count)) {
byte [][] result = new byte [count][];
for (int i = 0; i < count; i++)
result[i] = ReadData();
return result;
}
}
throw new ResponseException("Unknown reply on multi-request: " + c + s);
}
#region List commands
public byte[][] ListRange(string key, int start, int end)
{
return SendExpectDataArray ("LRANGE", key, start, end);
}
public void LeftPush(string key, string value)
{
LeftPush(key, System.Text.Encoding.UTF8.GetBytes (value));
}
public void LeftPush(string key, byte [] value)
{
SendDataCommand (value, "LPUSH", key);
ExpectSuccess();
}
public void RightPush(string key, string value)
{
RightPush(key, System.Text.Encoding.UTF8.GetBytes (value));
}
public void RightPush(string key, byte [] value)
{
SendDataCommand (value, "RPUSH", key);
ExpectSuccess();
}
public int ListLength (string key)
{
return SendExpectInt ("LLEN", key);
}
public byte[] ListIndex (string key, int index)
{
SendCommand ("LINDEX", key, index);
return ReadData ();
}
public byte[] LeftPop(string key)
{
SendCommand ("LPOP", key);
return ReadData ();
}
public byte[] RightPop(string key)
{
SendCommand ("RPOP", key);
return ReadData ();
}
#endregion
#region Set commands
public bool AddToSet (string key, byte[] member)
{
return SendDataExpectInt(member, "SADD", key) > 0;
}
public bool AddToSet (string key, string member)
{
return AddToSet (key, System.Text.Encoding.UTF8.GetBytes(member));
}
public int CardinalityOfSet (string key)
{
return SendExpectInt ("SCARD", key);
}
public bool IsMemberOfSet (string key, byte[] member)
{
return SendDataExpectInt (member, "SISMEMBER", key) > 0;
}
public bool IsMemberOfSet(string key, string member)
{
return IsMemberOfSet(key, System.Text.Encoding.UTF8.GetBytes(member));
}
public byte[][] GetMembersOfSet (string key)
{
return SendExpectDataArray ("SMEMBERS", key);
}
public byte[] GetRandomMemberOfSet (string key)
{
return SendExpectData ("SRANDMEMBER", key);
}
public byte[] PopRandomMemberOfSet (string key)
{
return SendExpectData ("SPOP", key);
}
public bool RemoveFromSet (string key, byte[] member)
{
return SendDataExpectInt (member, "SREM", key) > 0;
}
public bool RemoveFromSet (string key, string member)
{
return RemoveFromSet (key, System.Text.Encoding.UTF8.GetBytes(member));
}
public byte[][] GetUnionOfSets (params string[] keys)
{
if (keys == null)
throw new System.ArgumentNullException();
return SendExpectDataArray ("SUNION", keys);
}
void StoreSetCommands (string cmd, params string[] keys)
{
if (string.IsNullOrEmpty(cmd))
throw new System.ArgumentNullException ("cmd");
if (keys == null)
throw new System.ArgumentNullException ("keys");
SendExpectSuccess (cmd, keys);
}
public void StoreUnionOfSets (params string[] keys)
{
StoreSetCommands ("SUNIONSTORE", keys);
}
public byte[][] GetIntersectionOfSets (params string[] keys)
{
if (keys == null)
throw new System.ArgumentNullException();
return SendExpectDataArray ("SINTER", keys);
}
public void StoreIntersectionOfSets (params string[] keys)
{
StoreSetCommands ("SINTERSTORE", keys);
}
public byte[][] GetDifferenceOfSets (params string[] keys)
{
if (keys == null)
throw new System.ArgumentNullException();
return SendExpectDataArray ("SDIFF", keys);
}
public void StoreDifferenceOfSets (params string[] keys)
{
StoreSetCommands ("SDIFFSTORE", keys);
}
public bool MoveMemberToSet (string srcKey, string destKey, byte[] member)
{
return SendDataExpectInt (member, "SMOVE", srcKey, destKey) > 0;
}
#endregion
public void Dispose ()
{
Dispose (true);
System.GC.SuppressFinalize (this);
}
~Redis ()
{
Dispose (false);
}
protected virtual void Dispose (bool disposing)
{
if (disposing){
SendCommand ("QUIT");
ExpectSuccess ();
socket.Close ();
socket = null;
}
}
}
public class SortOptions {
public string Key { get; set; }
public bool Descending { get; set; }
public bool Lexographically { get; set; }
public System.Int32 LowerLimit { get; set; }
public System.Int32 UpperLimit { get; set; }
public string By { get; set; }
public string StoreInKey { get; set; }
public string Get { get; set; }
public object [] ToArgs ()
{
System.Collections.ArrayList args = new System.Collections.ArrayList();
if (LowerLimit != 0 || UpperLimit != 0) {
args.Add ("LIMIT");
args.Add (LowerLimit);
args.Add (UpperLimit);
}
if (Lexographically)
args.Add("ALPHA");
if (!string.IsNullOrEmpty (By)) {
args.Add("BY");
args.Add(By);
}
if (!string.IsNullOrEmpty (Get)) {
args.Add("GET");
args.Add(Get);
}
return args.ToArray ();
}
}
// just to output a string to the viewer console
void debug(string msg){
llOwnerSay("debug: "+msg);
}
// MAIN Script that uses the Redis server
string message = "Hello avatar!";
string REDIS_IP = "192.168.1.111";
public void default_event_state_entry()
{
debug(message);
}
public void default_event_touch_start(
LSL_Types.LSLInteger total_number)
{
Redis R = new Redis(REDIS_IP);
var info = R.GetInfo ();
foreach (var k in info.Keys) {
llSay(0, (string)k+" "+ info[k]);
}
}