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
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package Freeze.MapInternal;
import Freeze.ConnectionI;
import Freeze.DatabaseException;
import Freeze.Map;
import Freeze.NavigableMap;
//
// Indexed submap of a Freeze Map or of another submap
//
class IndexedSubMap<K, V, I>
extends java.util.AbstractMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
implements NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
{
private class Value extends java.util.AbstractSet<java.util.Map.Entry<K, V>>
{
public java.util.Iterator<java.util.Map.Entry<K, V>>
iterator()
{
return _index.find(_myKey, true);
}
public int
size()
{
return _index.count(_myKey);
}
public boolean
equals(Object o)
{
if(o instanceof IndexedSubMap.Value)
{
IndexedSubMap.Value v = (IndexedSubMap.Value)o;
return v._myKey.equals(_myKey);
}
else
{
return false;
}
}
public int
hashCode()
{
return _myKey.hashCode();
}
private
Value(I key)
{
_myKey = key;
}
private I
getKey()
{
return _myKey;
}
private I _myKey;
}
private class Entry implements java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
{
public I
getKey()
{
return _value.getKey();
}
public java.util.Set<java.util.Map.Entry<K, V>>
getValue()
{
return _value;
}
public java.util.Set<java.util.Map.Entry<K, V>>
setValue(java.util.Set<java.util.Map.Entry<K, V>> value)
{
throw new UnsupportedOperationException();
}
public boolean
equals(Object o)
{
if(o instanceof IndexedSubMap.Entry)
{
IndexedSubMap.Entry e = (IndexedSubMap.Entry)o;
return e._value.equals(_value);
}
else
{
return false;
}
}
public int
hashCode()
{
return _value.hashCode();
}
IndexedSubMap<K, V, I>
parent()
{
return IndexedSubMap.this;
}
private
Entry(I key)
{
_value = new Value(key);
}
private Value _value;
}
private class Iterator
implements Map.EntryIterator<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>>
{
public boolean
hasNext()
{
return _iterator.hasNext();
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
next()
{
EntryI<K, V> entry = (EntryI<K, V>)_iterator.next();
return new Entry(_index.decodeKey(entry.getIndexBytes(), _map.connection().getCommunicator()));
}
public void
remove()
{
_iterator.remove();
}
public void
close()
{
_iterator.close();
}
public void
destroy()
{
close();
}
private
Iterator()
{
assert(_index != null);
_iterator = new IteratorI<K, V>(_map, _view);
}
Map.EntryIterator<java.util.Map.Entry<K, V>> _iterator;
}
IndexedSubMap(Index<K, V, I> index, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
_map = index.parent();
_index = index;
_view = new AscendingView(fromKey, fromInclusive, toKey, toInclusive);
}
private
IndexedSubMap(Index<K, V, I> index, View v)
{
_map = index.parent();
_index = index;
_view = v;
}
//
// NavigableMap methods
//
public boolean
fastRemove(I key)
{
if(!_view.inRange(key, true))
{
return false;
}
//
// Not yet implemented
//
throw new UnsupportedOperationException();
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
firstEntry()
{
return _view.first();
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
lastEntry()
{
return _view.last();
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
ceilingEntry(I key)
{
return _view.ceiling(key);
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
floorEntry(I key)
{
return _view.floor(key);
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
higherEntry(I key)
{
return _view.higher(key);
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
lowerEntry(I key)
{
return _view.lower(key);
}
public I
ceilingKey(I key)
{
Entry e = _view.ceiling(key);
return e != null ? e.getKey() : null;
}
public I
floorKey(I key)
{
Entry e = _view.floor(key);
return e != null ? e.getKey() : null;
}
public I
higherKey(I key)
{
Entry e = _view.higher(key);
return e != null ? e.getKey() : null;
}
public I
lowerKey(I key)
{
Entry e = _view.lower(key);
return e != null ? e.getKey() : null;
}
public java.util.Set<I>
descendingKeySet()
{
return descendingMap().keySet();
}
public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
descendingMap()
{
if(_descendingMap == null)
{
View v = _view.descendingView();
_descendingMap = new IndexedSubMap<K, V, I>(_index, v);
}
return _descendingMap;
}
public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
headMap(I toKey, boolean inclusive)
{
if(toKey == null)
{
throw new NullPointerException();
}
View v = _view.subView(null, false, toKey, inclusive);
return new IndexedSubMap<K, V, I>(_index, v);
}
public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
subMap(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
if(fromKey == null || toKey == null)
{
throw new NullPointerException();
}
View v = _view.subView(fromKey, fromInclusive, toKey, toInclusive);
return new IndexedSubMap<K, V, I>(_index, v);
}
public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
tailMap(I fromKey, boolean inclusive)
{
if(fromKey == null)
{
throw new NullPointerException();
}
View v = _view.subView(fromKey, inclusive, null, false);
return new IndexedSubMap<K, V, I>(_index, v);
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
pollFirstEntry()
{
//
// Not yet implemented
//
throw new UnsupportedOperationException();
}
public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>
pollLastEntry()
{
//
// Not yet implemented
//
throw new UnsupportedOperationException();
}
//
// SortedMap methods
//
public java.util.Comparator<? super I>
comparator()
{
return _view.comparator();
}
public I
firstKey()
{
Entry e = _view.first();
if(e == null)
{
throw new java.util.NoSuchElementException();
}
return e.getKey();
}
public I
lastKey()
{
Entry e = _view.last();
if(e == null)
{
throw new java.util.NoSuchElementException();
}
return e.getKey();
}
public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
headMap(I toKey)
{
return headMap(toKey, false);
}
public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
tailMap(I fromKey)
{
return tailMap(fromKey, true);
}
public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>>
subMap(I fromKey, I toKey)
{
return subMap(fromKey, true, toKey, false);
}
//
// Map methods
//
public java.util.Set<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>>
entrySet()
{
if(_entrySet == null)
{
_entrySet = new java.util.AbstractSet<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>>()
{
public java.util.Iterator<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>>
iterator()
{
return new Iterator();
}
public boolean
contains(Object o)
{
if(o instanceof IndexedSubMap.Entry)
{
IndexedSubMap.Entry e = (IndexedSubMap.Entry)o;
return e.parent() == IndexedSubMap.this && _index.containsKey(e.getKey());
}
else
{
return false;
}
}
public boolean
remove(Object o)
{
//
// Not yet implemented, should remove all objects that
// match this index-key
//
throw new UnsupportedOperationException();
}
public int
size()
{
throw new UnsupportedOperationException();
}
public boolean
isEmpty()
{
try
{
firstKey();
return false;
}
catch(java.util.NoSuchElementException e)
{
return true;
}
}
};
}
return _entrySet;
}
//
// Put is not implemented (you have to put in the main map view)
//
public boolean
constainsKey(Object key)
{
@SuppressWarnings("unchecked")
I k = (I)key;
if(!_view.inRange(k, true))
{
return false;
}
return _index.containsKey(key);
}
public java.util.Set<java.util.Map.Entry<K, V>>
get(Object key)
{
@SuppressWarnings("unchecked")
I k = (I)key;
if(!_view.inRange(k, true))
{
return null;
}
if(_index.containsKey(k))
{
return new Value(k);
}
else
{
return null;
}
}
public java.util.Set<java.util.Map.Entry<K, V>>
remove(Object key)
{
@SuppressWarnings("unchecked")
I k = (I)key;
if(!_view.inRange(k, true))
{
return null;
}
//
// Not yet implemented
//
throw new UnsupportedOperationException();
}
private Entry
entrySearch(Search.Type type, byte[] key)
{
if(type != Search.Type.FIRST && type != Search.Type.LAST && key == null)
{
throw new NullPointerException();
}
if(_index.db() == null)
{
DatabaseException ex = new DatabaseException();
ex.message = _index.traceLevels().errorPrefix + "\"" + _index.dbName() + "\" has been closed";
throw ex;
}
com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(key);
if(Search.search(type, _map.connection(), _index.dbName(), _index.db(), dbKey, null, _index, _view,
_index.traceLevels()))
{
I k = _index.decodeKey(dbKey.getData(), _map.connection().getCommunicator());
return new Entry(k);
}
return null;
}
private abstract class View implements IteratorModel<K, V>, Search.KeyValidator
{
protected
View(java.util.Comparator<? super I> comparator, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
_comparator = comparator;
_fromKey = fromKey;
_fromInclusive = fromInclusive;
_toKey = toKey;
_toInclusive = toInclusive;
//
// Validate the key range.
//
if(_fromKey != null && _toKey != null)
{
int cmp = comparator.compare(_fromKey, _toKey);
if(cmp > 0 || (cmp == 0 && !(_fromInclusive && _toInclusive)))
{
throw new IllegalArgumentException();
}
}
}
protected
View(View v, java.util.Comparator<? super I> comparator, I fromKey, boolean fromInclusive, I toKey,
boolean toInclusive)
{
this(comparator, fromKey, fromInclusive, toKey, toInclusive);
//
// Verify that the key range is correct with respect to the original view.
//
if(!v.inRange(_fromKey, _fromInclusive) || !v.inRange(_toKey, _toInclusive))
{
throw new IllegalArgumentException();
}
}
abstract Search.Type mapSearchType(Search.Type type);
abstract View copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive);
abstract View descendingView();
final Entry
first()
{
Search.Type type;
byte[] key = null;
if(_fromKey != null)
{
type = _fromInclusive ? mapSearchType(Search.Type.CEILING) : mapSearchType(Search.Type.HIGHER);
key = fromKeyBytes();
}
else
{
type = mapSearchType(Search.Type.FIRST);
}
return entrySearch(type, key);
}
final Entry
last()
{
Search.Type type;
byte[] key = null;
if(_toKey != null)
{
type = _toInclusive ? mapSearchType(Search.Type.FLOOR) : mapSearchType(Search.Type.LOWER);
key = toKeyBytes();
}
else
{
type = mapSearchType(Search.Type.LAST);
}
return entrySearch(type, key);
}
final Entry
ceiling(I key)
{
byte[] k = _index.encodeKey(key, _map.connection().getCommunicator());
return entrySearch(mapSearchType(Search.Type.CEILING), k);
}
final Entry
floor(I key)
{
byte[] k = _index.encodeKey(key, _map.connection().getCommunicator());
return entrySearch(mapSearchType(Search.Type.FLOOR), k);
}
final Entry
higher(I key)
{
byte[] k = _index.encodeKey(key, _map.connection().getCommunicator());
return entrySearch(mapSearchType(Search.Type.HIGHER), k);
}
final Entry
lower(I key)
{
byte[] k = _index.encodeKey(key, _map.connection().getCommunicator());
return entrySearch(mapSearchType(Search.Type.LOWER), k);
}
final View
subView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
if(fromKey == null)
{
fromKey = _fromKey;
fromInclusive = _fromInclusive;
}
if(toKey == null)
{
toKey = _toKey;
toInclusive = _toInclusive;
}
return copy(fromKey, fromInclusive, toKey, toInclusive);
}
//
// IteratorModel methods (partial)
//
final public String
dbName()
{
return _index.dbName();
}
final public TraceLevels
traceLevels()
{
return _index.traceLevels();
}
final public com.sleepycat.db.Cursor
openCursor()
throws com.sleepycat.db.DatabaseException
{
return _index.db().openSecondaryCursor(_map.connection().dbTxn(), null);
}
//
// Search.KeyValidator methods
//
final public boolean
keyInRange(byte[] key)
{
I k = _index.decodeKey(key, _map.connection().getCommunicator());
return inRange(k, true);
}
final boolean
inRange(I key, boolean inclusive)
{
return !tooLow(key, inclusive, _fromKey, _fromInclusive) &&
!tooHigh(key, inclusive, _toKey, _toInclusive);
}
final java.util.Comparator<? super I>
comparator()
{
return _comparator;
}
final protected byte[]
fromKeyBytes()
{
if(_fromKey != null && _fromKeyBytes == null)
{
_fromKeyBytes = _index.encodeKey(_fromKey, _map.connection().getCommunicator());
}
return _fromKeyBytes;
}
final protected byte[]
toKeyBytes()
{
if(_toKey != null && _toKeyBytes == null)
{
_toKeyBytes = _index.encodeKey(_toKey, _map.connection().getCommunicator());
}
return _toKeyBytes;
}
final protected boolean
tooLow(I key, boolean inclusive, I targetKey, boolean targetInclusive)
{
if(key != null && targetKey != null)
{
int cmp = comparator().compare(key, targetKey);
if(cmp < 0 || (cmp == 0 && inclusive && !targetInclusive))
{
return true;
}
}
return false;
}
final protected boolean
tooHigh(I key, boolean inclusive, I targetKey, boolean targetInclusive)
{
if(key != null && targetKey != null)
{
int cmp = comparator().compare(key, targetKey);
if(cmp > 0 || (cmp == 0 && inclusive && !targetInclusive))
{
return true;
}
}
return false;
}
final java.util.Comparator<? super I> _comparator;
final I _fromKey;
final boolean _fromInclusive;
final I _toKey;
final boolean _toInclusive;
private byte[] _fromKeyBytes;
private byte[] _toKeyBytes;
}
private class AscendingView extends View
{
AscendingView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
super(_index.comparator(), fromKey, fromInclusive, toKey, toInclusive);
}
AscendingView(View v, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
super(v, _index.comparator(), fromKey, fromInclusive, toKey, toInclusive);
}
//
// View methods
//
Search.Type
mapSearchType(Search.Type type)
{
return type;
}
View
copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
return new AscendingView(this, fromKey, fromInclusive, toKey, toInclusive);
}
View
descendingView()
{
return new DescendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive);
}
//
// IteratorModel methods
//
public EntryI<K, V>
firstEntry(com.sleepycat.db.Cursor cursor)
throws com.sleepycat.db.DatabaseException
{
return _index.firstEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive);
}
public EntryI<K, V>
nextEntry(com.sleepycat.db.Cursor cursor)
throws com.sleepycat.db.DatabaseException
{
return _index.nextEntry(cursor, _toKey, _toInclusive);
}
}
private class DescendingView extends View
{
DescendingView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
super(java.util.Collections.reverseOrder(_index.comparator()), fromKey, fromInclusive, toKey, toInclusive);
}
DescendingView(View v, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
super(v, java.util.Collections.reverseOrder(_index.comparator()), fromKey, fromInclusive, toKey,
toInclusive);
}
//
// View methods
//
Search.Type
mapSearchType(Search.Type type)
{
return type.descending();
}
View
copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive)
{
return new DescendingView(this, fromKey, fromInclusive, toKey, toInclusive);
}
View
descendingView()
{
return new AscendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive);
}
//
// IteratorModel methods
//
public EntryI<K, V>
firstEntry(com.sleepycat.db.Cursor cursor)
throws com.sleepycat.db.DatabaseException
{
return _index.lastEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive);
}
public EntryI<K, V>
nextEntry(com.sleepycat.db.Cursor cursor)
throws com.sleepycat.db.DatabaseException
{
return _index.previousEntry(cursor, _toKey, _toInclusive);
}
}
private final MapI<K, V> _map;
private final Index<K, V, I> _index;
private final View _view;
private java.util.Set<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>> _entrySet;
private NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> _descendingMap;
}
|