forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClientList.cs
More file actions
236 lines (199 loc) · 4.92 KB
/
RedisClientList.cs
File metadata and controls
236 lines (199 loc) · 4.92 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
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
//
// 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;
using System.Collections.Generic;
namespace ServiceStack.Redis
{
/// <summary>
/// Wrap the common redis list operations under a IList[string] interface.
/// </summary>
internal class RedisClientList
: IRedisList
{
private readonly RedisClient client;
private readonly string listId;
private const int PageLimit = 1000;
public RedisClientList(RedisClient client, string listId)
{
this.listId = listId;
this.client = client;
}
public string Id
{
get { return listId; }
}
public IEnumerator<string> GetEnumerator()
{
return this.Count <= PageLimit
? client.GetAllItemsFromList(listId).GetEnumerator()
: GetPagingEnumerator();
}
public IEnumerator<string> GetPagingEnumerator()
{
var skip = 0;
List<string> pageResults;
do
{
pageResults = client.GetRangeFromList(listId, skip, skip + PageLimit - 1);
foreach (var result in pageResults)
{
yield return result;
}
skip += PageLimit;
} while (pageResults.Count == PageLimit);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(string item)
{
client.AddItemToList(listId, item);
}
public void Clear()
{
client.RemoveAllFromList(listId);
}
public bool Contains(string item)
{
//TODO: replace with native implementation when exists
foreach (var existingItem in this)
{
if (existingItem == item) return true;
}
return false;
}
public void CopyTo(string[] array, int arrayIndex)
{
var allItemsInList = client.GetAllItemsFromList(listId);
allItemsInList.CopyTo(array, arrayIndex);
}
public bool Remove(string item)
{
return client.RemoveItemFromList(listId, item) > 0;
}
public int Count
{
get
{
return (int)client.GetListCount(listId);
}
}
public bool IsReadOnly { get { return false; } }
public int IndexOf(string item)
{
//TODO: replace with native implementation when exists
var i = 0;
foreach (var existingItem in this)
{
if (existingItem == item) return i;
i++;
}
return -1;
}
public void Insert(int index, string item)
{
//TODO: replace with implementation involving creating on new temp list then replacing
//otherwise wait for native implementation
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
//TODO: replace with native implementation when one exists
var markForDelete = Guid.NewGuid().ToString();
client.SetItemInList(listId, index, markForDelete);
client.RemoveItemFromList(listId, markForDelete);
}
public string this[int index]
{
get { return client.GetItemFromList(listId, index); }
set { client.SetItemInList(listId, index, value); }
}
public List<string> GetAll()
{
return client.GetAllItemsFromList(listId);
}
public List<string> GetRange(int startingFrom, int endingAt)
{
return client.GetRangeFromList(listId, startingFrom, endingAt);
}
public List<string> GetRangeFromSortedList(int startingFrom, int endingAt)
{
return client.GetRangeFromSortedList(listId, startingFrom, endingAt);
}
public void RemoveAll()
{
client.RemoveAllFromList(listId);
}
public void Trim(int keepStartingFrom, int keepEndingAt)
{
client.TrimList(listId, keepStartingFrom, keepEndingAt);
}
public long RemoveValue(string value)
{
return client.RemoveItemFromList(listId, value);
}
public long RemoveValue(string value, int noOfMatches)
{
return client.RemoveItemFromList(listId, value, noOfMatches);
}
public void Append(string value)
{
Add(value);
}
public string RemoveStart()
{
return client.RemoveStartFromList(listId);
}
public string BlockingRemoveStart(TimeSpan? timeOut)
{
return client.BlockingRemoveStartFromList(listId, timeOut);
}
public string RemoveEnd()
{
return client.RemoveEndFromList(listId);
}
public void Enqueue(string value)
{
client.EnqueueItemOnList(listId, value);
}
public void Prepend(string value)
{
client.PrependItemToList(listId, value);
}
public void Push(string value)
{
client.PushItemToList(listId, value);
}
public string Pop()
{
return client.PopItemFromList(listId);
}
public string BlockingPop(TimeSpan? timeOut)
{
return client.BlockingPopItemFromList(listId, timeOut);
}
public string Dequeue()
{
return client.DequeueItemFromList(listId);
}
public string BlockingDequeue(TimeSpan? timeOut)
{
return client.BlockingDequeueItemFromList(listId, timeOut);
}
public string PopAndPush(IRedisList toList)
{
return client.PopAndPushItemBetweenLists(listId, toList.Id);
}
}
}