-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathArcusClient.java
More file actions
4367 lines (3864 loc) · 177 KB
/
ArcusClient.java
File metadata and controls
4367 lines (3864 loc) · 177 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
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* arcus-java-client : Arcus Java client
* Copyright 2010-2014 NAVER Corp.
* Copyright 2014-2022 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;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import net.spy.memcached.collection.Attributes;
import net.spy.memcached.collection.BKeyObject;
import net.spy.memcached.collection.BTreeCount;
import net.spy.memcached.collection.BTreeCreate;
import net.spy.memcached.collection.BTreeDelete;
import net.spy.memcached.collection.BTreeElement;
import net.spy.memcached.collection.BTreeFindPosition;
import net.spy.memcached.collection.BTreeFindPositionWithGet;
import net.spy.memcached.collection.BTreeGet;
import net.spy.memcached.collection.BTreeGetBulk;
import net.spy.memcached.collection.BTreeGetBulkWithByteTypeBkey;
import net.spy.memcached.collection.BTreeGetBulkWithLongTypeBkey;
import net.spy.memcached.collection.BTreeGetByPosition;
import net.spy.memcached.collection.BTreeGetResult;
import net.spy.memcached.collection.BTreeInsert;
import net.spy.memcached.collection.BTreeInsertAndGet;
import net.spy.memcached.collection.BTreeMutate;
import net.spy.memcached.collection.BTreeOrder;
import net.spy.memcached.collection.BTreeSMGet;
import net.spy.memcached.collection.BTreeSMGetWithByteTypeBkey;
import net.spy.memcached.collection.BTreeSMGetWithByteTypeBkeyOld;
import net.spy.memcached.collection.BTreeSMGetWithLongTypeBkey;
import net.spy.memcached.collection.BTreeSMGetWithLongTypeBkeyOld;
import net.spy.memcached.collection.BTreeUpdate;
import net.spy.memcached.collection.BTreeUpsert;
import net.spy.memcached.collection.ByteArrayBKey;
import net.spy.memcached.collection.ByteArrayTreeMap;
import net.spy.memcached.collection.CollectionAttributes;
import net.spy.memcached.collection.CollectionBulkInsert;
import net.spy.memcached.collection.CollectionCount;
import net.spy.memcached.collection.CollectionCreate;
import net.spy.memcached.collection.CollectionDelete;
import net.spy.memcached.collection.CollectionExist;
import net.spy.memcached.collection.CollectionGet;
import net.spy.memcached.collection.CollectionInsert;
import net.spy.memcached.collection.CollectionMutate;
import net.spy.memcached.collection.CollectionPipedInsert;
import net.spy.memcached.collection.CollectionPipedInsert.BTreePipedInsert;
import net.spy.memcached.collection.CollectionPipedInsert.ByteArraysBTreePipedInsert;
import net.spy.memcached.collection.CollectionPipedInsert.ListPipedInsert;
import net.spy.memcached.collection.CollectionPipedInsert.MapPipedInsert;
import net.spy.memcached.collection.CollectionPipedInsert.SetPipedInsert;
import net.spy.memcached.collection.CollectionPipedUpdate;
import net.spy.memcached.collection.CollectionPipedUpdate.BTreePipedUpdate;
import net.spy.memcached.collection.CollectionPipedUpdate.MapPipedUpdate;
import net.spy.memcached.collection.CollectionPipedUpsert;
import net.spy.memcached.collection.CollectionPipedUpsert.BTreePipedUpsert;
import net.spy.memcached.collection.CollectionPipedUpsert.ByteArrayBTreePipedUpsert;
import net.spy.memcached.collection.CollectionResponse;
import net.spy.memcached.collection.CollectionUpdate;
import net.spy.memcached.collection.Element;
import net.spy.memcached.collection.ElementFlagFilter;
import net.spy.memcached.collection.ElementFlagUpdate;
import net.spy.memcached.collection.ElementValueType;
import net.spy.memcached.collection.ListCreate;
import net.spy.memcached.collection.ListDelete;
import net.spy.memcached.collection.ListGet;
import net.spy.memcached.collection.ListInsert;
import net.spy.memcached.collection.MapCreate;
import net.spy.memcached.collection.MapDelete;
import net.spy.memcached.collection.MapGet;
import net.spy.memcached.collection.MapInsert;
import net.spy.memcached.collection.MapUpdate;
import net.spy.memcached.collection.SMGetElement;
import net.spy.memcached.collection.SMGetMode;
import net.spy.memcached.collection.SMGetTrimKey;
import net.spy.memcached.collection.SetCreate;
import net.spy.memcached.collection.SetDelete;
import net.spy.memcached.collection.SetExist;
import net.spy.memcached.collection.SetGet;
import net.spy.memcached.collection.SetInsert;
import net.spy.memcached.collection.SetPipedExist;
import net.spy.memcached.compat.log.Logger;
import net.spy.memcached.compat.log.LoggerFactory;
import net.spy.memcached.internal.BTreeStoreAndGetFuture;
import net.spy.memcached.internal.BroadcastFuture;
import net.spy.memcached.internal.BulkOperationFuture;
import net.spy.memcached.internal.CheckedOperationTimeoutException;
import net.spy.memcached.internal.CollectionFuture;
import net.spy.memcached.internal.CollectionGetBulkFuture;
import net.spy.memcached.internal.CollectionGetFuture;
import net.spy.memcached.internal.OperationFuture;
import net.spy.memcached.internal.PipedCollectionFuture;
import net.spy.memcached.internal.SMGetFuture;
import net.spy.memcached.ops.BTreeFindPositionOperation;
import net.spy.memcached.ops.BTreeFindPositionWithGetOperation;
import net.spy.memcached.ops.BTreeGetBulkOperation;
import net.spy.memcached.ops.BTreeGetByPositionOperation;
import net.spy.memcached.ops.BTreeInsertAndGetOperation;
import net.spy.memcached.ops.BTreeSortMergeGetOperation;
import net.spy.memcached.ops.BTreeSortMergeGetOperationOld;
import net.spy.memcached.ops.CollectionBulkInsertOperation;
import net.spy.memcached.ops.CollectionGetOperation;
import net.spy.memcached.ops.CollectionOperationStatus;
import net.spy.memcached.ops.CollectionPipedExistOperation;
import net.spy.memcached.ops.CollectionPipedInsertOperation;
import net.spy.memcached.ops.CollectionPipedUpdateOperation;
import net.spy.memcached.ops.GetAttrOperation;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.StoreType;
import net.spy.memcached.plugin.FrontCacheMemcachedClient;
import net.spy.memcached.transcoders.CollectionTranscoder;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.BTreeUtil;
/**
* Client to a Arcus.
*
* <h2>Basic usage</h2>
*
* <pre>{@code
* final static String arcusAdminAddrs = "127.0.0.1:2181";
* final static String serviceCode = "cafe";
*
* ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder();
*
* ArcusClient c = ArcusClient.createArcusClient(arcusAdminAddrs, serviceCode, cfb);
*
* // Store a value (async) for one hour
* c.set("someKey", 3600, someObject);
* // Retrieve a value.
* Future<Object> myFuture = c.asyncGet("someKey");
*
* If pool style is needed, it will be used as follows
*
* int poolSize = 4;
* ArcusClientPool pool =
* ArcusClient.createArcusClientPool(arcusAdminAddrs, serviceCode, cfb, poolSize);
*
* // Store a value
* pool.set("someKey", 3600, someObject);
* // Retrieve a value
* Future<Object> myFuture = pool.asyncGet("someKey");
*
* }</pre>
*/
public class ArcusClient extends FrontCacheMemcachedClient implements ArcusClientIF {
private static String VERSION = null;
private static final Object VERSION_LOCK = new Object();
private static final Logger arcusLogger = LoggerFactory.getLogger(ArcusClient.class);
private static final String ARCUS_CLOUD_ADDR = "127.0.0.1:2181";
private static final String DEFAULT_ARCUS_CLIENT_NAME = "ArcusClient";
private boolean dead;
// final BulkService bulkService;
private final Transcoder<Object> collectionTranscoder;
private final int smgetKeyChunkSize;
private static final int BOPGET_BULK_CHUNK_SIZE = 200;
private static final int NON_PIPED_BULK_INSERT_CHUNK_SIZE = 500;
private static final int MAX_GETBULK_ELEMENT_COUNT = 50;
private static final int MAX_SMGET_COUNT = 1000; // server configuration is 2000.
private static final int MAX_MKEY_LENGTH = 250;
private static final int SHUTDOWN_TIMEOUT_MILLISECONDS = 2000;
private CacheManager cacheManager;
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
/**
* @param hostPorts arcus admin addresses
* @param serviceCode service code
* @param cfb ConnectionFactoryBuilder
* @return a single ArcusClient
*/
public static ArcusClient createArcusClient(String hostPorts, String serviceCode,
ConnectionFactoryBuilder cfb) {
return ArcusClient.createArcusClient(hostPorts, serviceCode, cfb, 1, 10000).getClient();
}
/**
* @param serviceCode service code
* @param cfb ConnectionFactoryBuilder
* @return a single ArcusClient
*/
public static ArcusClient createArcusClient(String serviceCode,
ConnectionFactoryBuilder cfb) {
return ArcusClient.createArcusClient(ARCUS_CLOUD_ADDR, serviceCode, cfb, 1, 10000).getClient();
}
/**
* @param hostPorts arcus admin addresses
* @param serviceCode service code
* @param poolSize Arcus client pool size
* @param cfb ConnectionFactoryBuilder
* @return multiple ArcusClient
*/
public static ArcusClientPool createArcusClientPool(String hostPorts, String serviceCode,
ConnectionFactoryBuilder cfb, int poolSize) {
return ArcusClient.createArcusClient(hostPorts, serviceCode, cfb, poolSize, 0);
}
/**
* @param serviceCode service code
* @param poolSize Arcus client pool size
* @param cfb ConnectionFactoryBuilder
* @return multiple ArcusClient
*/
public static ArcusClientPool createArcusClientPool(String serviceCode,
ConnectionFactoryBuilder cfb, int poolSize) {
return ArcusClient.createArcusClient(ARCUS_CLOUD_ADDR, serviceCode, cfb, poolSize, 0);
}
/**
* @param hostPorts arcus admin addresses
* @param serviceCode service code
* @param cfb ConnectionFactoryBuilder
* @param poolSize Arcus client pool size
* @param waitTimeForConnect Connect waiting time for connection establishment(milliseconds)
* @return multiple ArcusClient
*/
private static ArcusClientPool createArcusClient(String hostPorts, String serviceCode,
ConnectionFactoryBuilder cfb, int poolSize,
int waitTimeForConnect) {
if (hostPorts == null) {
throw new NullPointerException("Arcus admin address required.");
}
if (serviceCode == null) {
throw new NullPointerException("Service code required.");
}
if (hostPorts.isEmpty()) {
throw new IllegalArgumentException("Arcus admin address is empty.");
}
if (serviceCode.isEmpty()) {
throw new IllegalArgumentException("Service code is empty.");
}
CacheManager exe = new CacheManager(hostPorts, serviceCode, cfb, poolSize, waitTimeForConnect);
return new ArcusClientPool(poolSize, exe.getAC());
}
/**
* Create an Arcus client for the given memcached server addresses.
*
* @param cf connection factory to configure connections for this client
* @param name client name
* @param addrs socket addresses for the memcached servers
* @return Arcus client
*/
protected static ArcusClient getInstance(ConnectionFactory cf,
String name,
List<InetSocketAddress> addrs) throws IOException {
return new ArcusClient(cf, name, addrs);
}
/**
* Create an Arcus client for the given memcached server addresses.
*
* @param cf connection factory to configure connections for this client
* @param name client name
* @param addrs socket addresses for the memcached servers
* @throws IOException if connections cannot be established
*/
public ArcusClient(ConnectionFactory cf, String name, List<InetSocketAddress> addrs)
throws IOException {
super(cf, name, addrs);
// bulkService = new BulkService(cf.getBulkServiceLoopLimit(),
// cf.getBulkServiceThreadCount(), cf.getBulkServiceSingleOpTimeout());
collectionTranscoder = new CollectionTranscoder();
smgetKeyChunkSize = cf.getDefaultMaxSMGetKeyChunkSize();
registerMbean();
}
/**
* Create an Arcus client for the given memcached server addresses.
*
* @param cf connection factory to configure connections for this client
* @param addrs socket addresses for the memcached servers
* @throws IOException if connections cannot be established
*/
public ArcusClient(ConnectionFactory cf, List<InetSocketAddress> addrs)
throws IOException {
super(cf, DEFAULT_ARCUS_CLIENT_NAME, addrs);
collectionTranscoder = new CollectionTranscoder();
smgetKeyChunkSize = cf.getDefaultMaxSMGetKeyChunkSize();
registerMbean();
}
/**
* Register mbean for Arcus client statistics.
*/
private void registerMbean() {
if ("false".equals(System.getProperty("arcus.mbean", "false").toLowerCase())) {
getLogger().info("Arcus client statistics MBean is NOT registered.");
return;
}
try {
StatisticsHandler mbean = new StatisticsHandler(this);
ArcusMBeanServer.getInstance().registMBean(
mbean,
mbean.getClass().getPackage().getName() + ":type="
+ mbean.getClass().getSimpleName() + "-"
+ mbean.hashCode());
getLogger().info("Arcus client statistics MBean is registered.");
} catch (Exception e) {
getLogger().warn("Failed to initialize statistics mbean.", e);
}
}
@Override
public void shutdown() {
super.shutdown(SHUTDOWN_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
// Connect to Arcus server directly, cache manager may be null.
if (cacheManager != null) {
cacheManager.shutdown();
}
dead = true;
// if (bulkService != null) {
// bulkService.shutdown();
// }
}
private void validateMKey(String mkey) {
byte[] keyBytes = KeyUtil.getKeyBytes(mkey);
if (keyBytes.length > MAX_MKEY_LENGTH) {
throw new IllegalArgumentException("MKey is too long (maxlen = "
+ MAX_MKEY_LENGTH + ")");
}
if (keyBytes.length == 0) {
throw new IllegalArgumentException("MKey must contain at least one character.");
}
// Validate the mkey
for (byte b : keyBytes) {
if (b == ' ' || b == '\n' || b == '\r' || b == 0) {
throw new IllegalArgumentException("MKey contains invalid characters: ``"
+ mkey + "''");
}
}
}
OperationFuture<Boolean> asyncStore(StoreType storeType, String key, int exp, CachedData co) {
final CountDownLatch latch = new CountDownLatch(1);
final OperationFuture<Boolean> rv = new OperationFuture<Boolean>(latch,
operationTimeout);
Operation op = opFact.store(storeType, key, co.getFlags(),
exp, co.getData(), new OperationCallback() {
public void receivedStatus(OperationStatus val) {
rv.set(val.isSuccess(), val);
}
public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
addOp(key, op);
return rv;
}
@Override
public CollectionFuture<Boolean> asyncSetAttr(String key, Attributes attrs) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<Boolean> rv = new CollectionFuture<Boolean>(
latch, operationTimeout);
Operation op = opFact.setAttr(key, attrs, new OperationCallback() {
public void receivedStatus(OperationStatus status) {
if (status instanceof CollectionOperationStatus) {
rv.set(status.isSuccess(), (CollectionOperationStatus) status);
} else {
getLogger().warn("Unhandled state: " + status);
rv.set(status.isSuccess(), new CollectionOperationStatus(status));
}
}
public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
addOp(key, op);
return rv;
}
@Override
public CollectionFuture<CollectionAttributes> asyncGetAttr(final String key) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<CollectionAttributes> rv = new CollectionFuture<CollectionAttributes>(
latch, operationTimeout);
Operation op = opFact.getAttr(key, new GetAttrOperation.Callback() {
private final CollectionAttributes attrs = new CollectionAttributes();
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus stat;
if (status instanceof CollectionOperationStatus) {
stat = (CollectionOperationStatus) status;
} else {
stat = new CollectionOperationStatus(status);
}
rv.set(stat.isSuccess() ? attrs : null, stat);
}
public void complete() {
latch.countDown();
}
public void gotAttribute(String k, String attr) {
assert key.equals(k) : "Wrong key returned";
attrs.setAttribute(attr);
}
});
rv.setOperation(op);
addOp(key, op);
return rv;
}
/**
* Generic get operation for list items. Public methods for list items call this method.
*
* @param k list item's key
* @param collectionGet operation parameters (element key and so on)
* @param tc transcoder to serialize and unserialize value
* @return future holding the fetched value
*/
private <T> CollectionFuture<List<T>> asyncLopGet(final String k,
final CollectionGet collectionGet,
final Transcoder<T> tc) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionGetFuture<List<T>> rv = new CollectionGetFuture<List<T>>(latch, operationTimeout);
Operation op = opFact.collectionGet(k, collectionGet,
new CollectionGetOperation.Callback() {
private final List<T> result = new ArrayList<T>();
private final List<CachedData> cachedDataList = new ArrayList<CachedData>();
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
if (cstatus.isSuccess()) {
rv.set(result, cstatus);
return;
}
switch (cstatus.getResponse()) {
case NOT_FOUND:
rv.set(null, cstatus);
getLogger().debug("Key(%s) not found : %s", k, cstatus);
break;
case NOT_FOUND_ELEMENT:
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found : %s", k, cstatus);
break;
case OUT_OF_RANGE:
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found in condition : %s", k, cstatus);
break;
case UNREADABLE:
rv.set(null, cstatus);
getLogger().debug("Element(%s) is not readable : %s", k, cstatus);
break;
default:
rv.set(null, cstatus);
getLogger().debug("Key(%s) unknown status : %s", k, cstatus);
break;
}
}
public void complete() {
latch.countDown();
}
public void gotData(String subkey, int flags, byte[] data, byte[] eflag) {
cachedDataList.add(new CachedData(flags, data, tc.getMaxSize()));
}
@Override
public void addResult() {
if (result.isEmpty() && !cachedDataList.isEmpty()) {
for (CachedData cachedData : cachedDataList) {
result.add(tc.decode(cachedData));
}
}
}
});
rv.setOperation(op);
addOp(k, op);
return rv;
}
@Override
public <T> CollectionFuture<Boolean> asyncSopExist(String key, T value,
Transcoder<T> tc) {
SetExist<T> exist = new SetExist<T>(value, tc);
return asyncCollectionExist(key, "", exist);
}
@Override
public CollectionFuture<Boolean> asyncSopExist(String key, Object value) {
SetExist<Object> exist = new SetExist<Object>(value, collectionTranscoder);
return asyncCollectionExist(key, "", exist);
}
/**
* Generic get operation for set items. Public methods for set items call this method.
*
* @param k set item's key
* @param collectionGet operation parameters (element key and so on)
* @param tc transcoder to serialize and unserialize value
* @return future holding the fetched value
*/
private <T> CollectionFuture<Set<T>> asyncSopGet(final String k,
final CollectionGet collectionGet,
final Transcoder<T> tc) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionGetFuture<Set<T>> rv =
new CollectionGetFuture<Set<T>>(latch, operationTimeout);
Operation op = opFact.collectionGet(k, collectionGet,
new CollectionGetOperation.Callback() {
private final HashSet<T> result = new HashSet<T>();
private final HashSet<CachedData> cachedDataSet = new HashSet<CachedData>();
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
if (cstatus.isSuccess()) {
rv.set(result, cstatus);
return;
}
switch (cstatus.getResponse()) {
case NOT_FOUND:
rv.set(null, cstatus);
getLogger().debug("Key(%s) not found : %s", k, cstatus);
break;
case NOT_FOUND_ELEMENT:
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found : %s", k, cstatus);
break;
case UNREADABLE:
rv.set(null, cstatus);
getLogger().debug("Collection(%s) is not readable : %s", k, cstatus);
break;
default:
rv.set(null, cstatus);
getLogger().debug("Key(%s) unknown status : %s", k, cstatus);
break;
}
}
public void complete() {
latch.countDown();
}
public void gotData(String subkey, int flags, byte[] data, byte[] eflag) {
cachedDataSet.add(new CachedData(flags, data, tc.getMaxSize()));
}
@Override
public void addResult() {
if (result.isEmpty() && !cachedDataSet.isEmpty()) {
for (CachedData cachedData : cachedDataSet) {
result.add(tc.decode(cachedData));
}
}
}
});
rv.setOperation(op);
addOp(k, op);
return rv;
}
/**
* Generic get operation for b+tree items. Public methods for b+tree items call this method.
*
* @param k b+tree item's key
* @param collectionGet operation parameters (element keys and so on)
* @param reverse false=forward or true=backward
* @param tc transcoder to serialize and unserialize value
* @return future holding the map of fetched elements and their keys
*/
private <T> CollectionFuture<Map<Long, Element<T>>> asyncBopGet(
final String k, final CollectionGet collectionGet,
final boolean reverse, final Transcoder<T> tc) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionGetFuture<Map<Long, Element<T>>> rv =
new CollectionGetFuture<Map<Long, Element<T>>>(latch, operationTimeout);
Operation op = opFact.collectionGet(k, collectionGet,
new CollectionGetOperation.Callback() {
private final TreeMap<Long, Element<T>> result =
new TreeMap<Long, Element<T>>((reverse) ? Collections.reverseOrder() : null);
private final HashMap<Long, CachedData> cachedDataMap = new HashMap<Long, CachedData>();
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
if (cstatus.isSuccess()) {
rv.set(result, cstatus);
return;
}
switch (cstatus.getResponse()) {
case NOT_FOUND:
rv.set(null, cstatus);
getLogger().debug("Key(%s) not found : %s", k, cstatus);
break;
case NOT_FOUND_ELEMENT:
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found : %s", k, cstatus);
break;
case UNREADABLE:
rv.set(null, cstatus);
getLogger().debug("Element(%s) is not readable : %s", k, cstatus);
break;
default:
rv.set(null, cstatus);
getLogger().debug("Key(%s) Unknown response : %s", k, cstatus);
break;
}
}
public void complete() {
latch.countDown();
}
public void gotData(String bKey, int flags, byte[] data, byte[] eflag) {
cachedDataMap.put(Long.parseLong(bKey), new CachedData(flags, data, eflag, tc.getMaxSize()));
}
@Override
public void addResult() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
for (Entry<Long, CachedData> cachedDataEntry : this.cachedDataMap.entrySet()) {
Long bKey = cachedDataEntry.getKey();
CachedData cachedData = cachedDataEntry.getValue();
result.put(bKey, new Element<T>(bKey, tc.decode(cachedData), cachedData.getEFlag()));
}
}
}
});
rv.setOperation(op);
addOp(k, op);
return rv;
}
/**
* Generic get operation for map items. Public methods for b+tree items call this method.
*
* @param k map item's key
* @param collectionGet operation parameters (element keys and so on)
* @param tc transcoder to serialize and unserialize value
* @return future holding the map of fetched elements and their keys
*/
private <T> CollectionFuture<Map<String, T>> asyncMopGet(
final String k, final CollectionGet collectionGet, final Transcoder<T> tc) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionGetFuture<Map<String, T>> rv =
new CollectionGetFuture<Map<String, T>>(latch, operationTimeout);
Operation op = opFact.collectionGet(k, collectionGet,
new CollectionGetOperation.Callback() {
private final HashMap<String, T> result = new HashMap<String, T>();
private final HashMap<String, CachedData> cachedDataMap = new HashMap<String, CachedData>();
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
if (cstatus.isSuccess()) {
rv.set(result, cstatus);
return;
}
switch (cstatus.getResponse()) {
case NOT_FOUND:
rv.set(null, cstatus);
getLogger().debug("Key(%s) not found : %s", k, cstatus);
break;
case NOT_FOUND_ELEMENT:
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found : %s", k, cstatus);
break;
case UNREADABLE:
rv.set(null, cstatus);
getLogger().debug("Element(%s) is not readable : %s", k, cstatus);
break;
default:
rv.set(null, cstatus);
getLogger().debug("Key(%s) Unknown response : %s", k, cstatus);
break;
}
}
public void complete() {
latch.countDown();
}
public void gotData(String mkey, int flags, byte[] data, byte[] eflag) {
cachedDataMap.put(mkey, new CachedData(flags, data, eflag, tc.getMaxSize()));
}
@Override
public void addResult() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
for (Entry<String, CachedData> cachedDataEntry : this.cachedDataMap.entrySet()) {
String mKey = cachedDataEntry.getKey();
CachedData cachedData = cachedDataEntry.getValue();
result.put(mKey, tc.decode(cachedData));
}
}
}
});
rv.setOperation(op);
addOp(k, op);
return rv;
}
/**
* Generic insert operation for collection items.
* Public methods for collection items call this method.
*
* @param key collection item's key
* @param subkey element key (list index, b+tree bkey)
* @param collectionInsert operation parameters (value, eflags, attributes, and so on)
* @param tc transcoder to serialize and unserialize value
* @return future holding the success/failure of the operation
*/
private <T> CollectionFuture<Boolean> asyncCollectionInsert(String key,
String subkey,
CollectionInsert<T> collectionInsert,
Transcoder<T> tc) {
CachedData co = tc.encode(collectionInsert.getValue());
collectionInsert.setFlags(co.getFlags());
return asyncCollectionInsert(key, subkey, collectionInsert, co);
}
/**
* Generic insert operation for collection items.
* Public methods for collection items call this method.
*
* @param key collection item's key
* @param subkey element key (list index, b+tree bkey)
* @param collectionInsert operation parameters (value, eflags, attributes, and so on)
* @param co transcoded value
* @return future holding the success/failure of the operation
*/
<T> CollectionFuture<Boolean> asyncCollectionInsert(final String key,
final String subkey,
final CollectionInsert<T> collectionInsert,
final CachedData co) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<Boolean> rv = new CollectionFuture<Boolean>(
latch, operationTimeout);
Operation op = opFact.collectionInsert(key, subkey, collectionInsert,
co.getData(), new OperationCallback() {
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
rv.set(cstatus.isSuccess(), cstatus);
if (!cstatus.isSuccess()) {
getLogger().debug(
"Insertion to the collection failed : %s (type=%s, key=%s, subkey=%s, value=%s)",
cstatus.getMessage(),
collectionInsert.getClass().getName(),
key,
subkey,
collectionInsert.getValue());
}
}
public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
addOp(key, op);
return rv;
}
/**
* Generic pipelined update operation for collection items.
* Public methods for collection items call this method.
*
* @param key collection item's key
* @param updateList list of operation parameters (values and so on)
* @return future holding the success/failure codes of individual operations and their index
*/
<T> CollectionFuture<Map<Integer, CollectionOperationStatus>> asyncCollectionPipedUpdate(
final String key, final List<CollectionPipedUpdate<T>> updateList) {
final CountDownLatch latch = new CountDownLatch(updateList.size());
final PipedCollectionFuture<Integer, CollectionOperationStatus> rv =
new PipedCollectionFuture<Integer, CollectionOperationStatus>(latch, operationTimeout);
for (int i = 0; i < updateList.size(); i++) {
final CollectionPipedUpdate<T> update = updateList.get(i);
final int idx = i;
Operation op = opFact.collectionPipedUpdate(key, update,
new CollectionPipedUpdateOperation.Callback() {
// each result status
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
rv.addOperationStatus(cstatus);
}
// complete
public void complete() {
latch.countDown();
}
// got status
public void gotStatus(Integer index, OperationStatus status) {
if (status instanceof CollectionOperationStatus) {
rv.addEachResult(index + (idx * CollectionPipedUpdate.MAX_PIPED_ITEM_COUNT),
(CollectionOperationStatus) status);
} else {
rv.addEachResult(index + (idx * CollectionPipedUpdate.MAX_PIPED_ITEM_COUNT),
new CollectionOperationStatus(status));
}
}
});
rv.addOperation(op);
addOp(key, op);
}
return rv;
}
/**
* Generic delete operation for collection items.
* Public methods for collection items call this method.
*
* @param key collection item's key
* @param collectionDelete operation parameters (element index/key, value, and so on)
* @return future holding the success/failure of the operation
*/
private CollectionFuture<Boolean> asyncCollectionDelete(
final String key, final CollectionDelete collectionDelete) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<Boolean> rv = new CollectionFuture<Boolean>(
latch, operationTimeout);
Operation op = opFact.collectionDelete(key, collectionDelete,
new OperationCallback() {
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
rv.set(cstatus.isSuccess(), cstatus);
if (!cstatus.isSuccess()) {
getLogger().debug("Deletion to the collection failed : %s (type=%s, key=%s)",
cstatus.getMessage(),
collectionDelete.getClass().getName(),
key);
}
}
public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
addOp(key, op);
return rv;
}
/**
* Generic existence operation for collection items.
* Public methods for collection items call this method.
*
* @param key collection item's key
* @param subkey element key (list index, b+tree bkey)
* @param collectionExist operation parameters (element value and so on)
* @return future holding the success/failure of the operation
*/
private <T> CollectionFuture<Boolean> asyncCollectionExist(final String key, final String subkey,
final CollectionExist collectionExist) {
final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<Boolean> rv = new CollectionFuture<Boolean>(
latch, operationTimeout);
Operation op = opFact.collectionExist(key, subkey, collectionExist,
new OperationCallback() {
public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
if (status instanceof CollectionOperationStatus) {
cstatus = (CollectionOperationStatus) status;
} else {
getLogger().warn("Unhandled state: " + status);
cstatus = new CollectionOperationStatus(status);
}
boolean isExist = CollectionResponse.EXIST == cstatus.getResponse();
rv.set(isExist, cstatus);
if (!cstatus.isSuccess()) {
getLogger().debug("Exist command to the collection failed : %s (type=%s, key=%s, subkey=%s)",
cstatus.getMessage(),
collectionExist.getClass().getName(),
key,
subkey);
}
}
public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
addOp(key, op);