forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClient_Set.cs
More file actions
207 lines (173 loc) · 6.12 KB
/
RedisClient_Set.cs
File metadata and controls
207 lines (173 loc) · 6.12 KB
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
//
// https://github.com/ServiceStack/ServiceStack.Redis
// ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2013 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of ServiceStack.
//
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Common;
using ServiceStack.Model;
using ServiceStack.Redis.Generic;
using ServiceStack.Redis.Pipeline;
using ServiceStack.Text;
namespace ServiceStack.Redis
{
public partial class RedisClient
: IRedisClient
{
public IHasNamed<IRedisSet> Sets { get; set; }
internal class RedisClientSets
: IHasNamed<IRedisSet>
{
private readonly RedisClient client;
public RedisClientSets(RedisClient client)
{
this.client = client;
}
public IRedisSet this[string setId]
{
get
{
return new RedisClientSet(client, setId);
}
set
{
var col = this[setId];
col.Clear();
col.CopyTo(value.ToArray(), 0);
}
}
}
private static HashSet<string> CreateHashSet(byte[][] multiDataList)
{
var results = new HashSet<string>();
foreach (var multiData in multiDataList)
{
results.Add(multiData.FromUtf8Bytes());
}
return results;
}
public List<string> GetSortedEntryValues(string setId, int startingFrom, int endingAt)
{
var sortOptions = new SortOptions { Skip = startingFrom, Take = endingAt, };
var multiDataList = Sort(setId, sortOptions);
return multiDataList.ToStringList();
}
public HashSet<string> GetAllItemsFromSet(string setId)
{
var multiDataList = SMembers(setId);
return CreateHashSet(multiDataList);
}
public void AddItemToSet(string setId, string item)
{
SAdd(setId, item.ToUtf8Bytes());
}
public void AddRangeToSet(string setId, List<string> items)
{
if (setId.IsNullOrEmpty())
throw new ArgumentNullException("setId");
if (items == null)
throw new ArgumentNullException("items");
if (items.Count == 0)
return;
if (this.Transaction != null || this.Pipeline != null)
{
var queueable = this.Transaction as IRedisQueueableOperation
?? this.Pipeline as IRedisQueueableOperation;
if (queueable == null)
throw new NotSupportedException("Cannot AddRangeToSet() when Transaction is: " + this.Transaction.GetType().Name);
//Complete the first QueuedCommand()
AddItemToSet(setId, items[0]);
//Add subsequent queued commands
for (var i = 1; i < items.Count; i++)
{
var item = items[i];
queueable.QueueCommand(c => c.AddItemToSet(setId, item));
}
}
else
{
var uSetId = setId.ToUtf8Bytes();
var pipeline = CreatePipelineCommand();
foreach (var item in items)
{
pipeline.WriteCommand(Commands.SAdd, uSetId, item.ToUtf8Bytes());
}
pipeline.Flush();
//the number of items after
var intResults = pipeline.ReadAllAsInts();
}
}
public void RemoveItemFromSet(string setId, string item)
{
SRem(setId, item.ToUtf8Bytes());
}
public string PopItemFromSet(string setId)
{
return SPop(setId).FromUtf8Bytes();
}
public List<string> PopItemsFromSet(string setId, int count)
{
return SPop(setId, count).ToStringList();
}
public void MoveBetweenSets(string fromSetId, string toSetId, string item)
{
SMove(fromSetId, toSetId, item.ToUtf8Bytes());
}
public long GetSetCount(string setId)
{
return SCard(setId);
}
public bool SetContainsItem(string setId, string item)
{
return SIsMember(setId, item.ToUtf8Bytes()) == 1;
}
public HashSet<string> GetIntersectFromSets(params string[] setIds)
{
if (setIds.Length == 0)
return new HashSet<string>();
var multiDataList = SInter(setIds);
return CreateHashSet(multiDataList);
}
public void StoreIntersectFromSets(string intoSetId, params string[] setIds)
{
if (setIds.Length == 0) return;
SInterStore(intoSetId, setIds);
}
public HashSet<string> GetUnionFromSets(params string[] setIds)
{
if (setIds.Length == 0)
return new HashSet<string>();
var multiDataList = SUnion(setIds);
return CreateHashSet(multiDataList);
}
public void StoreUnionFromSets(string intoSetId, params string[] setIds)
{
if (setIds.Length == 0) return;
SUnionStore(intoSetId, setIds);
}
public HashSet<string> GetDifferencesFromSet(string fromSetId, params string[] withSetIds)
{
if (withSetIds.Length == 0)
return new HashSet<string>();
var multiDataList = SDiff(fromSetId, withSetIds);
return CreateHashSet(multiDataList);
}
public void StoreDifferencesFromSet(string intoSetId, string fromSetId, params string[] withSetIds)
{
if (withSetIds.Length == 0) return;
SDiffStore(intoSetId, fromSetId, withSetIds);
}
public string GetRandomItemFromSet(string setId)
{
return SRandMember(setId).FromUtf8Bytes();
}
}
}