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
|
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
package Freeze;
public abstract class Map extends java.util.AbstractMap
{
public
Map(DB db)
{
_db = db;
}
//
// A concrete implementation of a Freeze.Map must provide
// implementations of the following to encode & decode the
// key/value pairs.
//
public abstract byte[] encodeKey(Object o, Ice.Communicator communicator);
public abstract Object decodeKey(byte[] b, Ice.Communicator communicator);
public abstract byte[] encodeValue(Object o, Ice.Communicator communicator);
public abstract Object decodeValue(byte[] b, Ice.Communicator communicator);
public int
size()
{
//
// It's not necessary to close outstanding iterators.
//
// If it was it would be a problem - it doesn't change the Map
// - therefore open iterators should not be invalidated
// (according the Java spec).
//
// closeIterators();
//
// The number of records cannot be cached and then adjusted by
// the member functions since the map would no longer work in
// the presence of transactions - if a record is added (and
// the size adjusted) and the transaction aborted then the
// cached map size() would be incorrect.
//
return (int)_db.getNumberOfRecords();
}
public boolean
containsValue(Object value)
{
//
// It's not necessary to close outstanding iterators.
//
// If it was it would be a problem - it doesn't change the Map
// - therefore open iterators should not be invalidated
// (according the Java spec).
//
// closeIterators();
EntryIterator p = (EntryIterator)entrySet().iterator();
if(value == null)
{
while(p.hasNext())
{
Entry e = (Entry)p.next();
if(e.getValue() == null)
{
return true;
}
}
}
else
{
while(p.hasNext())
{
Entry e = (Entry)p.next();
if(value.equals(e.getValue()))
{
return true;
}
}
}
p.close();
return false;
}
public boolean
containsKey(Object key)
{
//
// It's not necessary to close outstanding iterators.
//
// If it was it would be a problem - it doesn't change the Map
// - therefore open iterators should not be invalidated
// (according the Java spec).
//
//closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
return _db.contains(k);
}
public Object
get(Object key)
{
//
// It's not necessary to close outstanding iterators.
//
// If it was it would be a problem - it doesn't change the Map
// - therefore open iterators should not be invalidated
// (according the Java spec).
//
//closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
try
{
byte[] v = _db.get(k);
return decodeValue(v, _db.getCommunicator());
}
catch(DBNotFoundException e)
{
return null;
}
}
public Object
put(Object key, Object value)
{
closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
Object o;
try
{
byte[] v = _db.get(k);
o = decodeValue(v, _db.getCommunicator());
}
catch(DBNotFoundException e)
{
o = null;
}
byte[] v = encodeValue(value, _db.getCommunicator());
_db.put(k, v);
return o;
}
public Object
remove(Object key)
{
closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
Object o;
try
{
byte[] v = _db.get(k);
o = decodeValue(v, _db.getCommunicator());
}
catch(DBNotFoundException e)
{
o = null;
}
_db.del(k);
return o;
}
//
// Proprietary API calls. These are much faster than the
// corresponding Java collections API methods since the unwanted
// reads are avoided.
//
public void
fastPut(Object key, Object value)
{
closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
byte[] v = encodeValue(value, _db.getCommunicator());
_db.put(k, v);
}
//
// Returns true if the record was removed, false otherwise.
//
public boolean
fastRemove(Object key)
{
closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
try
{
_db.del(k);
}
catch(Freeze.DBNotFoundException e)
{
return false;
}
return true;
}
public void
clear()
{
closeIterators();
_db.clear();
}
public java.util.Set
entrySet()
{
if(_entrySet == null)
{
_entrySet = new java.util.AbstractSet()
{
public java.util.Iterator
iterator()
{
EntryIterator p = new EntryIterator();
_iterators.add(new java.lang.ref.WeakReference(p));
return p;
}
public boolean
contains(Object o)
{
if(!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = (Map.Entry)o;
Object value = entry.getValue();
Entry p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}
public boolean
remove(Object o)
{
if(!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = (Map.Entry)o;
Object value = entry.getValue();
Entry p = getEntry(entry.getKey());
if(p != null && valEquals(p.getValue(), value))
{
closeIterators();
byte[] k = encodeKey(p.getKey(), _db.getCommunicator());
_db.del(k);
return true;
}
return false;
}
public int
size()
{
return Map.this.size();
}
public void
clear()
{
Map.this.clear();
}
};
}
return _entrySet;
}
//
// Because of the way that Berkeley DB cursors implement their
// locking it's necessary to ensure that all cursors are closed
// prior to doing a database operation otherwise self-deadlocks
// will occur. See "Berkeley DB Transactional Data Store locking
// conventions" section in the Berkeley DB reference guide for
// details.
//
private void
closeIterators()
{
closeIteratorsExcept(null);
}
private void
closeIteratorsExcept(java.util.Iterator i)
{
java.util.Iterator p = _iterators.iterator();
while(p.hasNext())
{
java.lang.ref.WeakReference ref = (java.lang.ref.WeakReference)p.next();
EntryIterator q = (EntryIterator)ref.get();
if(q != null && q != i)
{
q.close();
}
}
//
// This is more efficient than removing the list items element
// by element in the iteration loop.
//
_iterators.clear();
if(i != null)
{
_iterators.add(new java.lang.ref.WeakReference(i));
}
}
private Entry
getEntry(Object key)
{
//
// It's not necessary to close outstanding iterators.
//
// If it was it would be a problem - it doesn't change the Map
// - therefore open iterators should not be invalidated
// (according the Java spec).
//
// closeIterators();
byte[] k = encodeKey(key, _db.getCommunicator());
byte[] v = _db.get(k);
Object o = decodeValue(v, _db.getCommunicator());
return new Entry(key, o);
}
private static boolean
valEquals(Object o1, Object o2)
{
return (o1 == null ? o2 == null : o1.equals(o2));
}
/**
*
* The entry iterator class needs to be public to allow clients to
* close explicitly the iterator and free resources allocated for
* the iterator as soon as possible.
*
**/
public class EntryIterator implements java.util.Iterator
{
EntryIterator()
{
try
{
_cursor = _db.getCursor();
_next = getCurr();
}
catch(DBNotFoundException e)
{
_next = null;
}
catch(DBException e)
{
if(_cursor != null)
{
try
{
_cursor.close();
}
catch(DBException ignore)
{
// Ignore
}
}
throw e;
}
}
public boolean
hasNext()
{
return _next != null;
}
public Object
next()
{
return nextEntry();
}
public void
remove()
{
if(_current == null)
{
throw new IllegalStateException();
}
closeIteratorsExcept(this);
//
// Clone the cursor so that error handling is simpler.
//
DBCursor clone = _cursor._clone();
try
{
//
// If _next is null then the iterator is currently at
// the end.
//
if(_next != null)
{
clone.prev();
}
clone.del();
}
finally
{
if(clone != null)
{
try
{
clone.close();
}
catch(DBException ignore)
{
// Ignore
}
}
}
_current = null;
}
//
// Extra operation.
//
public void
close()
{
DBCursor copy = _cursor;
//
// Clear the internal iterator state.
//
_cursor = null;
_next = null;
_current = null;
if(copy != null)
{
copy.close();
}
}
protected void
finalize()
{
close();
}
private Entry
nextEntry()
{
if(_next == null)
{
throw new java.util.NoSuchElementException();
}
_current = _next;
if(_cursor.next())
{
_next = getCurr();
}
else
{
_next = null;
}
return _current;
}
private Entry
getCurr()
{
Freeze.KeyHolder k = new Freeze.KeyHolder();
Freeze.ValueHolder v = new Freeze.ValueHolder();
_cursor.curr(k, v);
Object key = decodeKey(k.value, _db.getCommunicator());
Object value = decodeValue(v.value, _db.getCommunicator());
return new Entry(key, value);
}
private DBCursor _cursor = null;
private Entry _next;
private Entry _current;
}
static class Entry implements java.util.Map.Entry
{
public
Entry(Object key, Object value)
{
_key = key;
_value = value;
}
public
Entry(Map.Entry e)
{
_key = e.getKey();
_value = e.getValue();
}
public Object
getKey()
{
return _key;
}
public Object
getValue()
{
return _value;
}
public Object
setValue(Object value)
{
throw new UnsupportedOperationException();
}
public boolean
equals(Object o)
{
if(!(o instanceof Map.Entry))
{
return false;
}
Map.Entry e = (Map.Entry)o;
return eq(_key, e.getKey()) && eq(_value, e.getValue());
}
public int
hashCode()
{
Object v;
return ((_key == null) ? 0 : _key.hashCode()) ^
((_value == null) ? 0 : _value.hashCode());
}
public String
toString()
{
return _key + "=" + _value;
}
private /*static*/ boolean
eq(Object o1, Object o2)
{
return (o1 == null ? o2 == null : o1.equals(o2));
}
private Object _key;
private Object _value;
}
private java.util.Set _entrySet;
private DB _db;
private java.util.List _iterators = new java.util.LinkedList();
}
|