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
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#ifndef FREEZE_MAP_H
#define FREEZE_MAP_H
#include <Ice/Ice.h>
#include <Freeze/DB.h>
#include <iterator>
namespace Freeze
{
//
// Forward declaration
//
template <typename key_type, typename mapped_type, typename KeyCodec, typename ValueCodec>
class DBMap;
template <typename key_type, typename mapped_type, typename KeyCodec, typename ValueCodec>
class ConstDBIterator;
//
// This is necessary for MSVC support.
//
struct DBIteratorBase
{
typedef std::forward_iterator_tag iterator_category;
};
//
// Database iterator. This implements a forward iterator with the
// restriction that it's only possible to explicitely write back into
// the database.
//
// Equality and inequality are based on whether the iterator is
// "valid". An valid iterator contains a valid database and cursor
// pointer, otherwise the iterator is invalid.
//
// TODO: It's possible to implement bidirectional iterators, if
// necessary.
//
template <typename key_type, typename mapped_type, typename KeyCodec, typename ValueCodec>
class DBIterator : public DBIteratorBase
{
public:
typedef ptrdiff_t difference_type;
typedef std::pair<const key_type, const mapped_type> value_type;
typedef value_type* pointer;
typedef value_type& reference;
DBIterator(const DBPtr& db, const DBCursorPtr& cursor)
: _db(db), _cursor(cursor)
{
}
DBIterator()
{
}
DBIterator(const DBIterator& rhs)
{
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
}
DBIterator& operator=(const DBIterator& rhs)
{
if(_cursor)
{
_cursor->close();
}
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
return *this;
}
~DBIterator()
{
if(_cursor)
{
_cursor->close();
}
}
bool operator==(const DBIterator& rhs) const
{
if(!_db && !rhs._db)
{
return true;
}
return false;
}
bool operator!=(const DBIterator& rhs) const
{
return !(*this == rhs);
}
DBIterator& operator++()
{
incr();
return *this;
}
DBIterator operator++(int)
{
DBIterator tmp = *this;
tmp.incr();
return tmp;
}
//
// Note that this doesn't follow the regular iterator mapping:
//
// value_type& operator*(), value_type operator*() const
//
value_type& operator*() const
{
key_type key;
mapped_type value;
getCurrentValue(key, value);
//
// !IMPORTANT!
//
// This method has to cache the returned value to implement
// operator->().
//
const_cast<key_type&>(_ref.first) = key;
const_cast<mapped_type&>(_ref.second) = value;
return _ref;
}
value_type* operator->() { return &(operator*()); }
//
// This special method allows writing back into the database.
//
void set(const mapped_type& value)
{
Freeze::Value v;
ValueCodec::write(value, v, _db->getCommunicator());
_cursor->set(v);
}
private:
void incr()
{
assert(_cursor && _db);
if(!_cursor->next())
{
//
// The iterator has been moved past the end, and is now
// invalid.
//
_cursor->close();
_cursor = 0;
_db = 0;
}
}
void getCurrentValue(key_type& key, mapped_type& value) const
{
Freeze::Key k;
Freeze::Value v;
_cursor->curr(k, v);
::Ice::CommunicatorPtr communicator = _db->getCommunicator();
KeyCodec::read(key, k, communicator);
ValueCodec::read(value, v, communicator);
}
friend class ConstDBIterator<key_type, mapped_type, KeyCodec, ValueCodec>;
friend class DBMap<key_type, mapped_type, KeyCodec, ValueCodec>;
DBPtr _db;
DBCursorPtr _cursor;
//
// Cached last return value. This is so that operator->() can
// actually return a pointer.
//
mutable value_type _ref;
};
//
// See DBIterator comments for design notes
//
template <typename key_type, typename mapped_type, typename KeyCodec, typename ValueCodec>
class ConstDBIterator : public DBIteratorBase
{
public:
typedef ptrdiff_t difference_type;
typedef std::pair<const key_type, const mapped_type> value_type;
typedef value_type* pointer;
typedef value_type& reference;
ConstDBIterator(const DBPtr& db, const DBCursorPtr& cursor)
: _db(db), _cursor(cursor)
{
}
ConstDBIterator() { }
ConstDBIterator(const ConstDBIterator& rhs)
{
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
}
//
// A DBIterator can be converted to a ConstDBIterator (but not
// vice versa) - same for operator=.
//
ConstDBIterator(const DBIterator<key_type, mapped_type, KeyCodec, ValueCodec>& rhs)
{
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
}
ConstDBIterator& operator=(const ConstDBIterator& rhs)
{
if(_cursor)
{
_cursor->close();
}
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
return *this;
}
//
// Create const_iterator from iterator.
//
ConstDBIterator& operator=(const DBIterator<key_type, mapped_type, KeyCodec, ValueCodec>& rhs)
{
if(_cursor)
{
_cursor->close();
}
if(rhs._cursor)
{
_cursor = rhs._cursor->clone();
}
_db = rhs._db;
return *this;
}
~ConstDBIterator()
{
if(_cursor)
{
_cursor->close();
}
}
bool operator==(const ConstDBIterator& rhs)
{
if(!_db && !rhs._db)
{
return true;
}
return false;
}
bool operator!=(const ConstDBIterator& rhs)
{
return !(*this == rhs);
}
ConstDBIterator& operator++()
{
incr();
return *this;
}
ConstDBIterator operator++(int)
{
ConstDBIterator tmp = *this;
tmp.incr();
return tmp;
}
//
// Note that this doesn't follow the regular iterator mapping:
//
// value_type operator*() const
//
value_type& operator*() const
{
key_type key;
mapped_type value;
getCurrentValue(key, value);
//
// !IMPORTANT!
//
// This method has to cache the returned value to implement
// operator->().
//
const_cast<key_type&>(_ref.first) = key;
const_cast<mapped_type&>(_ref.second) = value;
return _ref;
}
pointer operator->() const { return &(operator*()); }
private:
void incr()
{
assert(_cursor);
if(!_cursor->next())
{
//
// The iterator has been moved past the end, and is now
// invalid.
//
_cursor->close();
_cursor = 0;
_db = 0;
}
}
void getCurrentValue(key_type& key, mapped_type& value) const
{
Freeze::Key k;
Freeze::Value v;
_cursor->curr(k, v);
::Ice::CommunicatorPtr communicator = _db->getCommunicator();
KeyCodec::read(key, k, communicator);
ValueCodec::read(value, v, communicator);
}
friend class DBMap<key_type, mapped_type, KeyCodec, ValueCodec>;
DBPtr _db;
DBCursorPtr _cursor;
//
// Cached last return value. This is so that operator->() can
// actually return a pointer.
//
mutable value_type _ref;
};
//
// This is an STL container that matches the requirements of a
// Associated Container - with the restriction that operator[] isn't
// implemented. It also supports the same interface as a Hashed
// Associative Container (with the above restrictions), except the
// hasher & key_equal methods.
//
// TODO: If necessary it would be possible to implement reverse and
// bidirectional iterators.
//
template <typename key_type, typename mapped_type, typename KeyCodec, typename ValueCodec>
class DBMap
{
public:
typedef std::pair<const key_type, const mapped_type> value_type;
//
// These are not supported:
//
// hasher, key_equal, key_compare, value_compare
//
typedef DBIterator<key_type, mapped_type, KeyCodec, ValueCodec > iterator;
typedef ConstDBIterator<key_type, mapped_type, KeyCodec, ValueCodec > const_iterator;
//
// No definition for reference, const_reference, pointer or
// const_pointer.
//
typedef size_t size_type;
typedef ptrdiff_t difference_type;
//
// Allocators are not supported.
//
// allocator_type
//
//
// Constructors
//
DBMap(const DBPtr& db) :
_db(db)
{
}
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIterator>
DBMap(const DBPtr& db, _InputIterator first, _InputIterator last) :
_db(db)
{
while(first != last)
{
insert(*first);
++first;
}
}
#else
DBMap(const DBPtr& db, const value_type* first, const value_type* last) :
_db(db)
{
while(first != last)
{
insert(*first);
++first;
}
}
DBMap(const DBPtr& db, const_iterator first, const_iterator last) :
_db(db)
{
while(first != last)
{
insert(*first);
++first;
}
}
#endif /*__STL_MEMBER_TEMPLATES */
~DBMap()
{
}
//
// Neither of these operations are supported.
//
// key_compare key_comp() const, value_compare value_comp() const,
// hasher hash_funct() const, key_equal key_eq() const
//
bool operator==(const DBMap& rhs) const
{
//
// This does a memberwise equality for the entire contents of
// the database. While slow this is always correct. Database
// equality is not necessarily correct in the context of a
// transaction.
//
if(count() != rhs.count())
return false;
for(const_iterator p = rhs.begin() ; p != rhs.end() ; ++p)
{
const_iterator q = rhs.find(p->first);
if(q == rhs.end())
{
return false;
}
if(p->second != q->second)
{
return false;
}
}
return true;
}
bool operator!=(const DBMap& rhs) const
{
return !(*this == rhs);
}
void swap(DBMap& rhs)
{
DBPtr tmp = _db;
_db = rhs._db;
rhs._db = tmp;
}
iterator begin()
{
try
{
return iterator(_db, _db->getCursor());
}
catch(const DBNotFoundException&)
{
return iterator();
}
}
const_iterator begin() const
{
try
{
return const_iterator(_db, _db->getCursor());
}
catch(const DBNotFoundException&)
{
return const_iterator();
}
}
iterator end()
{
return iterator();
}
const_iterator end() const
{
return const_iterator();
}
bool empty() const
{
return size() == 0;
}
size_type size() const
{
return (size_type)_db->getNumberOfRecords();
}
size_type max_size() const
{
return 0xffffffff; // TODO: is this the max?
}
//
// This method isn't implemented.
//
// mapped_type& operator[](const key_type& key)
//
//
// This method isn't in the STLport library - but it's referenced
// in "STL Tutorial and Refrence Guide, Second Edition". It's not
// currently implemented.
//
// const mapped_type& operator[](const key_type& key) const;
//
//
// No allocators.
//
//allocator_type get_allocator() const;
//
iterator insert(iterator /*position*/, const value_type& key)
{
//
// position is ignored.
//
::Ice::CommunicatorPtr communicator = _db->getCommunicator();
Freeze::Key k;
Freeze::Value v;
KeyCodec::write(key.first, k, communicator);
ValueCodec::write(key.second, v, communicator);
_db->put(k, v);
DBCursorPtr cursor = _db->getCursorAtKey(k);
return iterator(_db, cursor);
}
std::pair<iterator, bool> insert(const value_type& key)
{
::Ice::CommunicatorPtr communicator = _db->getCommunicator();
Freeze::Key k;
Freeze::Value v;
KeyCodec::write(key.first, k, communicator);
ValueCodec::write(key.second, v, communicator);
DBCursorPtr cursor;
bool inserted;
try
{
//
// Does the value exist already?
//
cursor = _db->getCursorAtKey(k);
inserted = false;
}
catch(const DBNotFoundException&)
{
inserted = true;
}
_db->put(k, v);
if(inserted)
{
cursor = _db->getCursorAtKey(k);
}
return std::pair<iterator, bool>(iterator(_db, cursor), inserted);
}
#ifdef __STL_MEMBER_TEMPLATES
template <typename InputIterator>
void insert(InputIterator first, InputIterator last)
{
while(first != last)
{
insert(*first);
++first;
}
}
#else
void insert(const value_type* first, const value_type* last)
{
while(first != last)
{
insert(*first);
++first;
}
}
void insert(const_iterator first, const_iterator last)
{
while(first != last)
{
insert(*first);
++first;
}
}
#endif
void erase(iterator position)
{
position._cursor->del();
}
size_type erase(const key_type& key)
{
Freeze::Key k;
KeyCodec::write(key, k, _db->getCommunicator());
try
{
_db->del(k);
}
catch(const DBNotFoundException&)
{
return 0;
}
return 1;
}
void erase(iterator first, iterator last)
{
while(first != last)
{
first._cursor->del();
++first;
}
}
void clear()
{
_db->clear();
}
iterator find(const key_type& key)
{
Freeze::Key k;
KeyCodec::write(key, k, _db->getCommunicator());
try
{
DBCursorPtr cursor = _db->getCursorAtKey(k);
return iterator(_db, cursor);
}
catch(const DBNotFoundException&)
{
//
// The record doesn't exist, return the end() iterator.
//
}
return end();
}
const_iterator find(const key_type& key) const
{
Freeze::Key k;
KeyCodec::write(key, k, _db->getCommunicator());
try
{
DBCursorPtr cursor = _db->getCursorAtKey(k);
return const_iterator(_db, cursor);
}
catch(const DBNotFoundException&)
{
//
// The record doesn't exist, return the end() iterator.
//
}
return end();
}
size_type count(const key_type& key) const
{
if(find(key) != end())
return 1;
return 0;
}
std::pair<iterator, iterator> equal_range(const key_type& key)
{
iterator p = find(key);
return std::pair<iterator,iterator>(p,p);
}
std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
{
const_iterator p = find(key);
return std::pair<const_iterator,const_iterator>(p,p);
}
private:
DBPtr _db;
};
} // End namespace Freeze
//
// This is for MSVC.
//
# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
namespace std
{
//XXX update
template <class key_type, class mapped_type, class KeyCodec, class ValueCodec>
inline pair<const key_type, const mapped_type>*
value_type(const Freeze::DBIterator<key_type, mapped_type, KeyCodec, ValueCodec>&)
{
return (pair<const key_type, const mapped_type>*)0;
}
template <class key_type, class mapped_type, class KeyCodec, class ValueCodec>
inline pair<const key_type, const mapped_type>*
value_type(const Freeze::ConstDBIterator<key_type, mapped_type, KeyCodec, ValueCodec>&)
{
return (pair<const key_type, const mapped_type>*)0;
}
inline forward_iterator_tag iterator_category(const Freeze::DBIteratorBase&)
{
return forward_iterator_tag();
}
inline ptrdiff_t* distance_type(const Freeze::DBIteratorBase&) { return (ptrdiff_t*) 0; }
} // End namespace std
#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
#endif
|