-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCollectionPipedInsert.java
More file actions
359 lines (301 loc) · 10.7 KB
/
CollectionPipedInsert.java
File metadata and controls
359 lines (301 loc) · 10.7 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
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
/*
* arcus-java-client : Arcus Java client
* Copyright 2010-2014 NAVER Corp.
* Copyright 2014-2021 JaM2in Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.spy.memcached.collection;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import net.spy.memcached.CachedData;
import net.spy.memcached.KeyUtil;
import net.spy.memcached.transcoders.Transcoder;
public abstract class CollectionPipedInsert<T> extends CollectionPipe {
public static final int MAX_PIPED_ITEM_COUNT = 500;
protected final String key;
protected final CollectionAttributes attribute;
protected final Transcoder<T> tc;
protected CollectionPipedInsert(String key, CollectionAttributes attribute,
Transcoder<T> tc, int itemCount) {
super(itemCount);
this.key = key;
this.attribute = attribute;
this.tc = tc;
}
/**
*
*/
public static class ListPipedInsert<T> extends CollectionPipedInsert<T> {
private static final String COMMAND = "lop insert";
private final Collection<T> list;
private final int index;
public ListPipedInsert(String key, int index, Collection<T> list,
CollectionAttributes attr, Transcoder<T> tc) {
super(key, attr, tc, list.size());
if (attr != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.list, attr.getOverflowAction());
}
this.index = index;
this.list = list;
}
public ByteBuffer getAsciiCommand() {
int capacity = 0;
// encode values
List<byte[]> encodedList = new ArrayList<byte[]>(list.size());
CachedData cd = null;
for (T each : list) {
cd = tc.encode(each);
encodedList.add(cd.getData());
}
// estimate the buffer capacity
for (byte[] each : encodedList) {
capacity += KeyUtil.getKeyBytes(key).length;
capacity += each.length;
capacity += 128;
}
// allocate the buffer
ByteBuffer bb = ByteBuffer.allocate(capacity);
// create ascii operation string
int eSize = encodedList.size();
String createOption = attribute != null ?
CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : "";
for (int i = this.nextOpIndex; i < eSize; i++) {
byte[] each = encodedList.get(i);
int eIndex = index;
if (index >= 0) {
eIndex += i;
}
setArguments(bb, COMMAND, key, eIndex, each.length,
createOption, (i < eSize - 1) ? PIPE : "");
bb.put(each);
bb.put(CRLF);
}
// flip the buffer
((Buffer) bb).flip();
return bb;
}
}
/**
*
*/
public static class SetPipedInsert<T> extends CollectionPipedInsert<T> {
private static final String COMMAND = "sop insert";
private final Collection<T> set;
public SetPipedInsert(String key, Collection<T> set,
CollectionAttributes attr, Transcoder<T> tc) {
super(key, attr, tc, set.size());
if (attr != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.set, attr.getOverflowAction());
}
this.set = set;
}
public ByteBuffer getAsciiCommand() {
int capacity = 0;
// encode values
List<byte[]> encodedList = new ArrayList<byte[]>(set.size());
CachedData cd = null;
for (T each : set) {
cd = tc.encode(each);
encodedList.add(cd.getData());
}
// estimate the buffer capacity
for (byte[] each : encodedList) {
capacity += KeyUtil.getKeyBytes(key).length;
capacity += each.length;
capacity += 128;
}
// allocate the buffer
ByteBuffer bb = ByteBuffer.allocate(capacity);
// create ascii operation string
int eSize = encodedList.size();
String createOption = attribute != null ?
CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : "";
for (int i = this.nextOpIndex; i < eSize; i++) {
byte[] each = encodedList.get(i);
setArguments(bb, COMMAND, key, each.length,
createOption, (i < eSize - 1) ? PIPE : "");
bb.put(each);
bb.put(CRLF);
}
// flip the buffer
((Buffer) bb).flip();
return bb;
}
}
/**
*
*/
public static class BTreePipedInsert<T> extends CollectionPipedInsert<T> {
private static final String COMMAND = "bop insert";
private final Map<Long, T> map;
public BTreePipedInsert(String key, Map<Long, T> map,
CollectionAttributes attr, Transcoder<T> tc) {
super(key, attr, tc, map.size());
if (attr != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.btree, attr.getOverflowAction());
}
this.map = map;
}
public ByteBuffer getAsciiCommand() {
int capacity = 0;
// encode parameters
List<byte[]> encodedList = new ArrayList<byte[]>(map.size());
CachedData cd = null;
for (T each : map.values()) {
cd = tc.encode(each);
encodedList.add(cd.getData());
}
// estimate the buffer capacity
int i = 0;
for (Long eachBkey : map.keySet()) {
capacity += KeyUtil.getKeyBytes(key).length;
capacity += KeyUtil.getKeyBytes(String.valueOf(eachBkey)).length;
capacity += encodedList.get(i++).length;
capacity += 128;
}
// allocate the buffer
ByteBuffer bb = ByteBuffer.allocate(capacity);
// create ascii operation string
int keySize = map.keySet().size();
List<Long> keyList = new ArrayList<Long>(map.keySet());
String createOption = attribute != null ?
CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : "";
for (i = this.nextOpIndex; i < keySize; i++) {
Long bkey = keyList.get(i);
byte[] value = encodedList.get(i);
setArguments(bb, getCommand(), key, bkey, value.length,
createOption, (i < keySize - 1) ? PIPE : "");
bb.put(value);
bb.put(CRLF);
}
// flip the buffer
((Buffer) bb).flip();
return bb;
}
public String getCommand() {
return COMMAND;
}
}
/**
*
*/
public static class ByteArraysBTreePipedInsert<T> extends
CollectionPipedInsert<T> {
private static final String COMMAND = "bop insert";
private final List<Element<T>> elements;
public ByteArraysBTreePipedInsert(String key, List<Element<T>> elements,
CollectionAttributes attr, Transcoder<T> tc) {
super(key, attr, tc, elements.size());
if (attr != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.btree, attr.getOverflowAction());
}
this.elements = elements;
}
public ByteBuffer getAsciiCommand() {
int capacity = 0;
// encode parameters
List<byte[]> encodedList = new ArrayList<byte[]>(elements.size());
CachedData cd = null;
for (Element<T> each : elements) {
cd = tc.encode(each.getValue());
encodedList.add(cd.getData());
}
// estimate the buffer capacity
int i = 0;
for (Element<T> each : elements) {
capacity += KeyUtil.getKeyBytes(key).length;
capacity += KeyUtil.getKeyBytes(each.getStringBkey()).length;
capacity += KeyUtil.getKeyBytes(each.getStringEFlag()).length;
capacity += encodedList.get(i++).length;
capacity += 128;
}
// allocate the buffer
ByteBuffer bb = ByteBuffer.allocate(capacity);
// create ascii operation string
int eSize = elements.size();
String createOption = attribute != null ?
CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : "";
for (i = this.nextOpIndex; i < eSize; i++) {
Element<T> element = elements.get(i);
byte[] value = encodedList.get(i);
setArguments(bb, getCommand(), key,
element.getStringBkey(), element.getStringEFlag(), value.length,
createOption, (i < eSize - 1) ? PIPE : "");
bb.put(value);
bb.put(CRLF);
}
// flip the buffer
((Buffer) bb).flip();
return bb;
}
public String getCommand() {
return COMMAND;
}
}
/**
*
*/
public static class MapPipedInsert<T> extends CollectionPipedInsert<T> {
private static final String COMMAND = "mop insert";
private final Map<String, T> map;
public MapPipedInsert(String key, Map<String, T> map,
CollectionAttributes attr, Transcoder<T> tc) {
super(key, attr, tc, map.size());
if (attr != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.map, attr.getOverflowAction());
}
this.map = map;
}
public ByteBuffer getAsciiCommand() {
int capacity = 0;
// encode values
List<byte[]> encodedList = new ArrayList<byte[]>(map.size());
CachedData cd = null;
for (T each : map.values()) {
cd = tc.encode(each);
encodedList.add(cd.getData());
}
// estimate the buffer capacity
int i = 0;
for (String eachMkey : map.keySet()) {
capacity += KeyUtil.getKeyBytes(key).length;
capacity += KeyUtil.getKeyBytes(eachMkey).length;
capacity += encodedList.get(i++).length;
capacity += 128;
}
// allocate the buffer
ByteBuffer bb = ByteBuffer.allocate(capacity);
// create ascii operation string
int mkeySize = map.keySet().size();
List<String> keyList = new ArrayList<String>(map.keySet());
String createOption = attribute != null ?
CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : "";
for (i = this.nextOpIndex; i < mkeySize; i++) {
String mkey = keyList.get(i);
byte[] value = encodedList.get(i);
setArguments(bb, COMMAND, key, mkey, value.length,
createOption, (i < mkeySize - 1) ? PIPE : "");
bb.put(value);
bb.put(CRLF);
}
// flip the buffer
((Buffer) bb).flip();
return bb;
}
}
}