forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.hpp
2185 lines (2089 loc) · 98.7 KB
/
HashTable.hpp
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
**/
#ifndef QUICKSTEP_STORAGE_HASH_TABLE_HPP_
#define QUICKSTEP_STORAGE_HASH_TABLE_HPP_
#include <atomic>
#include <cstddef>
#include <cstdlib>
#include <type_traits>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#include "storage/HashTableBase.hpp"
#include "storage/StorageBlob.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "storage/StorageConstants.hpp"
#include "storage/StorageManager.hpp"
#include "storage/TupleReference.hpp"
#include "storage/ValueAccessor.hpp"
#include "storage/ValueAccessorUtil.hpp"
#include "threading/SpinSharedMutex.hpp"
#include "types/Type.hpp"
#include "types/TypedValue.hpp"
#include "utility/BloomFilter.hpp"
#include "utility/HashPair.hpp"
#include "utility/Macros.hpp"
namespace quickstep {
/** \addtogroup Storage
* @{
*/
/**
* @brief Base class for hash table.
*
* This class is templated so that the core hash-table logic can be reused in
* different contexts requiring different value types and semantics (e.g.
* hash-joins vs. hash-based grouping for aggregates vs. hash-based indices).
* The base template defines the interface that HashTables provide to clients
* and implements some common functionality for all HashTables. There a few
* different (also templated) implementation classes that inherit from this
* base class and have different physical layouts with different performance
* characteristics. As of this writing, they are:
* 1. LinearOpenAddressingHashTable - All keys/values are stored directly
* in a single array of buckets. Collisions are handled by simply
* advancing to the "next" adjacent bucket until an empty bucket is
* found. This implementation is vulnerable to performance degradation
* due to the formation of bucket chains when there are many duplicate
* and/or consecutive keys.
* 2. SeparateChainingHashTable - Keys/values are stored in a separate
* region of memory from the base hash table slot array. Every bucket
* has a "next" pointer so that entries that collide (i.e. map to the
* same base slot) form chains of pointers with each other. Although
* this implementation has some extra indirection compared to
* LinearOpenAddressingHashTable, it does not have the same
* vulnerabilities to key skew, and it additionally supports a very
* efficient bucket-preallocation mechanism that minimizes cache
* coherency overhead when multiple threads are building a HashTable
* as part of a hash-join.
* 3. SimpleScalarSeparateChainingHashTable - A simplified version of
* SeparateChainingHashTable that is only usable for single, scalar
* keys with a reversible hash function. This implementation exploits
* the reversible hash to avoid storing separate copies of keys at all,
* and to skip an extra key comparison when hash codes collide.
*
* @note If you need to create a HashTable and not just use it as a client, see
* HashTableFactory, which simplifies the process of creating a
* HashTable.
*
* @param ValueT The mapped value in this hash table. Must be
* copy-constructible. For a serializable hash table, ValueT must also
* be trivially copyable and trivially destructible (and beware of
* pointers to external memory).
* @param resizable Whether this hash table is resizable (using memory from a
* StorageManager) or not (using a private, fixed memory allocation).
* @param serializable If true, this hash table can safely be saved to and
* loaded from disk. If false, some out of band memory may be used (e.g.
* to store variable length keys).
* @param force_key_copy If true, inserted keys are always copied into this
* HashTable's memory. If false, pointers to external values may be
* stored instead. force_key_copy should be true if the hash table will
* outlive the external key values which are inserted into it. Note that
* if serializable is true and force_key_copy is false, then relative
* offsets will be used instead of absolute pointers to keys, meaning
* that the pointed-to keys must be serialized and deserialized in
* exactly the same relative byte order (e.g. as part of the same
* StorageBlock), and keys must not change position relative to this
* HashTable (beware TupleStorageSubBlocks that may self-reorganize when
* modified). If serializable and resizable are both true, then
* force_key_copy must also be true.
* @param allow_duplicate_keys If true, multiple values can be mapped to the
* same key. If false, one and only one value may be mapped.
**/
template <typename ValueT,
bool resizable,
bool serializable,
bool force_key_copy,
bool allow_duplicate_keys>
class HashTable : public HashTableBase<resizable,
serializable,
force_key_copy,
allow_duplicate_keys> {
static_assert(std::is_copy_constructible<ValueT>::value,
"A HashTable can not be used with a Value type which is not "
"copy-constructible.");
static_assert((!serializable) || std::is_trivially_destructible<ValueT>::value,
"A serializable HashTable can not be used with a Value type "
"which is not trivially destructible.");
static_assert(!(serializable && resizable && !force_key_copy),
"A HashTable must have force_key_copy=true when serializable "
"and resizable are both true.");
// TODO(chasseur): GCC 4.8.3 doesn't yet implement
// std::is_trivially_copyable. In the future, we should include a
// static_assert that prevents a serializable HashTable from being used with
// a ValueT which is not trivially copyable.
public:
// Shadow template parameters. This is useful for shared test harnesses.
typedef ValueT value_type;
static constexpr bool template_resizable = resizable;
static constexpr bool template_serializable = serializable;
static constexpr bool template_force_key_copy = force_key_copy;
static constexpr bool template_allow_duplicate_keys = allow_duplicate_keys;
// Some HashTable implementations (notably LinearOpenAddressingHashTable)
// use a special hash code to represent an empty bucket, and another special
// code to indicate that a bucket is currently being inserted into. For those
// HashTables, this is a surrogate hash value for empty buckets. Keys which
// actually hash to this value should have their hashes mutated (e.g. by
// adding 1). We use zero, since we will often be using memory which is
// already zeroed-out and this saves us the trouble of a memset. This has
// some downside, as the hash function we use is the identity hash for
// integers, and the integer 0 is common in many data sets and must be
// adjusted (and will then spuriously collide with 1). Nevertheless, this
// expense is outweighed by no longer having to memset large regions of
// memory when initializing a HashTable.
static constexpr unsigned char kEmptyHashByte = 0x0;
static constexpr std::size_t kEmptyHash = 0x0;
// A surrogate hash value for a bucket which is currently being inserted
// into. As with kEmptyHash, keys which actually hash to this value should
// have their hashes adjusted.
static constexpr std::size_t kPendingHash = ~kEmptyHash;
/**
* @brief Virtual destructor.
**/
virtual ~HashTable() {
if (resizable) {
if (blob_.valid()) {
if (serializable) {
DEV_WARNING("Destroying a resizable serializable HashTable's underlying "
"StorageBlob.");
}
const block_id blob_id = blob_->getID();
blob_.release();
storage_manager_->deleteBlockOrBlobFile(blob_id);
}
}
}
/**
* @brief Get the ID of the StorageBlob used to store a resizable HashTable.
*
* @warning This method must not be used for a non-resizable HashTable.
*
* @return The ID of the StorageBlob used to store this HashTable.
**/
inline block_id getBlobId() const {
DEBUG_ASSERT(resizable);
return blob_->getID();
}
/**
* @brief Erase all entries in this hash table.
*
* @warning This method is not guaranteed to be threadsafe.
**/
virtual void clear() = 0;
/**
* @brief Add a new entry into the hash table.
*
* @warning The key must not be null.
* @warning This method is threadsafe with regard to other calls to put(),
* putCompositeKey(), putValueAccessor(), and
* putValueAccessorCompositeKey(), but should not be used
* simultaneously with upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey().
* @note This version is for single scalar keys, see also putCompositeKey().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param key The key.
* @param value The value payload.
* @return HashTablePutResult::kOK if an entry was successfully inserted,
* HashTablePutResult::kDuplicateKey if allow_duplicate_keys is false
* and key was a duplicate, or HashTablePutResult::kOutOfSpace if
* resizable is false and storage space for the hash table has been
* exhausted.
**/
HashTablePutResult put(const TypedValue &key,
const ValueT &value);
/**
* @brief Add a new entry into the hash table (composite key version).
*
* @warning No component of the key may be null.
* @warning This method is threadsafe with regard to other calls to put(),
* putCompositeKey(), putValueAccessor(), and
* putValueAccessorCompositeKey(), but should not be used
* simultaneously with upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey().
* @note This version is for composite keys, see also put().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param key The components of the key.
* @param value The value payload.
* @return HashTablePutResult::kOK if an entry was successfully inserted,
* HashTablePutResult::kDuplicateKey if allow_duplicate_keys is false
* and key was a duplicate, or HashTablePutResult::kOutOfSpace if
* resizable is false and storage space for the hash table has been
* exhausted.
**/
HashTablePutResult putCompositeKey(const std::vector<TypedValue> &key,
const ValueT &value);
/**
* @brief Add (multiple) new entries into the hash table from a
* ValueAccessor.
*
* @warning This method is threadsafe with regard to other calls to put(),
* putCompositeKey(), putValueAccessor(), and
* putValueAccessorCompositeKey(), but should not be used
* simultaneously with upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey().
* @note This version is for scalar keys, see also
* putValueAccessorCompositeKey().
* @note If the hash table fills up while this call is in progress and
* resizable is true, this might result in rebuilding the entire hash
* table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before inserting it (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes const ValueAccessor& as an argument (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as an argument) and returns either
* a ValueT or a reference to a ValueT. The functor should generate
* the appropriate mapped value for the current tuple the accessor is
* iterating on.
* @return HashTablePutResult::kOK if all keys and generated values from
* accessor were successfully inserted.
* HashTablePutResult::kOutOfSpace is returned if this hash-table is
* non-resizable and ran out of space (note that some entries may
* still have been inserted, and accessor's iteration will be left on
* the first tuple which could not be inserted).
* HashTablePutResult::kDuplicateKey is returned if
* allow_duplicate_keys is false and a duplicate key is encountered
* (as with HashTablePutResult::kOutOfSpace, some entries may have
* been inserted, and accessor will be left on the tuple with a
* duplicate key).
**/
template <typename FunctorT>
HashTablePutResult putValueAccessor(ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
FunctorT *functor);
/**
* @brief Add (multiple) new entries into the hash table from a
* ValueAccessor (composite key version).
*
* @warning This method is threadsafe with regard to other calls to put(),
* putCompositeKey(), putValueAccessor(), and
* putValueAccessorCompositeKey(), but should not be used
* simultaneously with upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey().
* @note This version is for composite keys, see also putValueAccessor().
* @note If the hash table fills up while this call is in progress and
* resizable is true, this might result in rebuilding the entire hash
* table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_ids The attribute IDs of each key component to be read
* from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* has a null component before inserting it (null keys are skipped).
* This must be set to true if some of the keys that will be read from
* accessor may be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes const ValueAccessor& as an argument (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as an argument) and returns either
* a ValueT or a reference to a ValueT. The functor should generate
* the appropriate mapped value for the current tuple the accessor is
* iterating on.
* @return HashTablePutResult::kOK if all keys and generated values from
* accessor were successfully inserted.
* HashTablePutResult::kOutOfSpace is returned if this hash-table is
* non-resizable and ran out of space (note that some entries may
* still have been inserted, and accessor's iteration will be left on
* the first tuple which could not be inserted).
* HashTablePutResult::kDuplicateKey is returned if
* allow_duplicate_keys is false and a duplicate key is encountered
* (as with HashTablePutResult::kOutOfSpace, some entries may have
* been inserted, and accessor will be left on the tuple with a
* duplicate key).
**/
template <typename FunctorT>
HashTablePutResult putValueAccessorCompositeKey(
ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
FunctorT *functor);
/**
* @brief Apply a functor to the value mapped to a key, first inserting a new
* value if one is not already present.
*
* @warning The key must not be null.
* @warning This method is only usable if allow_duplicate_keys is false.
* @warning This method is threadsafe with regard to other calls to upsert(),
* upsertCompositeKey(), upsertValueAccessor(), and
* upsertValueAccessorCompositeKey(), but should not be used
* simultaneously with put(), putCompositeKey(), putValueAccessor(),
* or putValueAccessorCompositeKey().
* @warning The ValueT* pointer passed to functor's call operator is only
* guaranteed to be valid for the duration of the call. The functor
* should not store a copy of the pointer and assume that it remains
* valid.
* @warning Although this method itself is threadsafe, the ValueT object
* accessed by functor is not guaranteed to be (although it is
* guaranteed that its initial insertion will be atomic). If it is
* possible for multiple threads to call upsert() with the same key
* at the same time, then their access to ValueT should be made
* threadsafe (e.g. with the use of atomic types, mutexes, or some
* other external synchronization).
* @note This version is for single scalar keys, see also
* upsertCompositeKey().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param key The key.
* @param initial_value If there was not already a preexisting entry in this
* HashTable for the specified key, then the value will be initialized
* with a copy of initial_value. This parameter is ignored if a value
* is already present for key.
* @param functor A pointer to a functor, which should provide a call
* operator which takes ValueT* as an argument. The call operator will
* be invoked once on the value corresponding to key (which may be
* newly inserted and default-constructed).
* @return True on success, false if upsert failed because there was not
* enough space to insert a new entry in this HashTable.
**/
template <typename FunctorT>
bool upsert(const TypedValue &key,
const ValueT &initial_value,
FunctorT *functor);
/**
* @brief Apply a functor to the value mapped to a key, first inserting a new
* value if one is not already present.
*
* @warning The key must not be null.
* @warning This method is only usable if allow_duplicate_keys is false.
* @warning This method is threadsafe with regard to other calls to upsert(),
* upsertCompositeKey(), upsertValueAccessor(), and
* upsertValueAccessorCompositeKey(), but should not be used
* simultaneously with put(), putCompositeKey(), putValueAccessor(),
* or putValueAccessorCompositeKey().
* @warning The ValueT* pointer passed to functor's call operator is only
* guaranteed to be valid for the duration of the call. The functor
* should not store a copy of the pointer and assume that it remains
* valid.
* @warning Although this method itself is threadsafe, the ValueT object
* accessed by functor is not guaranteed to be (although it is
* guaranteed that its initial insertion will be atomic). If it is
* possible for multiple threads to call upsertCompositeKey() with
* the same key at the same time, then their access to ValueT should
* be made threadsafe (e.g. with the use of atomic types, mutexes,
* or some other external synchronization).
* @note This version is for composite keys, see also upsert().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param key The key.
* @param initial_value If there was not already a preexisting entry in this
* HashTable for the specified key, then the value will be initialized
* with a copy of initial_value. This parameter is ignored if a value
* is already present for key.
* @param functor A pointer to a functor, which should provide a call
* operator which takes ValueT* as an argument. The call operator will
* be invoked once on the value corresponding to key (which may be
* newly inserted and default-constructed).
* @return True on success, false if upsert failed because there was not
* enough space to insert a new entry in this HashTable.
**/
template <typename FunctorT>
bool upsertCompositeKey(const std::vector<TypedValue> &key,
const ValueT &initial_value,
FunctorT *functor);
/**
* @brief Apply a functor to (multiple) entries in this hash table, with keys
* drawn from a ValueAccessor. New values are first inserted if not
* already present.
*
* @warning This method is only usable if allow_duplicate_keys is false.
* @warning This method is threadsafe with regard to other calls to upsert(),
* upsertCompositeKey(), upsertValueAccessor(), and
* upsertValueAccessorCompositeKey(), but should not be used
* simultaneously with put(), putCompositeKey(), putValueAccessor(),
* or putValueAccessorCompositeKey().
* @warning The ValueAccessor reference and ValueT* pointer passed to
* functor's call operator are only guaranteed to be valid for the
* duration of the call. The functor should not store a copy of
* these pointers and assume that they remain valid.
* @warning Although this method itself is threadsafe, the ValueT object
* accessed by functor is not guaranteed to be (although it is
* guaranteed that its initial insertion will be atomic). If it is
* possible for multiple threads to call upsertValueAccessor() with
* the same key at the same time, then their access to ValueT should
* be made threadsafe (e.g. with the use of atomic types, mutexes,
* or some other external synchronization).
* @note This version is for single scalar keys, see also
* upsertValueAccessorCompositeKey().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before upserting it (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes two arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and ValueT*.
* The call operator will be invoked once for every tuple with a
* non-null key in accessor.
* @return True on success, false if upsert failed because there was not
* enough space to insert new entries for all the keys in accessor
* (note that some entries may still have been upserted, and
* accessor's iteration will be left on the first tuple which could
* not be inserted).
**/
template <typename FunctorT>
bool upsertValueAccessor(ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
const ValueT &initial_value,
FunctorT *functor);
/**
* @brief Apply a functor to (multiple) entries in this hash table, with keys
* drawn from a ValueAccessor. New values are first inserted if not
* already present. Composite key version.
*
* @warning This method is only usable if allow_duplicate_keys is false.
* @warning This method is threadsafe with regard to other calls to upsert(),
* upsertCompositeKey(), upsertValueAccessor(), and
* upsertValueAccessorCompositeKey(), but should not be used
* simultaneously with put(), putCompositeKey(), putValueAccessor(),
* or putValueAccessorCompositeKey().
* @warning The ValueAccessor reference and ValueT* pointer passed to
* functor's call operator are only guaranteed to be valid for the
* duration of the call. The functor should not store a copy of
* these pointers and assume that they remain valid.
* @warning Although this method itself is threadsafe, the ValueT object
* accessed by functor is not guaranteed to be (although it is
* guaranteed that its initial insertion will be atomic). If it is
* possible for multiple threads to call upsertValueAccessor() with
* the same key at the same time, then their access to ValueT should
* be made threadsafe (e.g. with the use of atomic types, mutexes,
* or some other external synchronization).
* @note This version is for composite keys, see also upsertValueAccessor().
* @note If the hash table is (close to) full and resizable is true, this
* routine might result in rebuilding the entire hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_ids The attribute IDs of each key component to be read
* from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before upserting it (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes two arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and ValueT*.
* The call operator will be invoked once for every tuple with a
* non-null key in accessor.
* @return True on success, false if upsert failed because there was not
* enough space to insert new entries for all the keys in accessor
* (note that some entries may still have been upserted, and
* accessor's iteration will be left on the first tuple which could
* not be inserted).
**/
template <typename FunctorT>
bool upsertValueAccessorCompositeKey(
ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
const ValueT &initial_value,
FunctorT *functor);
/**
* @brief Get the size of the hash table at the time this function is called.
**/
virtual std::size_t getHashTableMemorySizeBytes() const = 0;
/**
* @brief Determine the number of entries (key-value pairs) contained in this
* HashTable.
* @note For some HashTable implementations, this is O(1), but for others it
* may be O(n) where n is the number of buckets.
*
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
*
* @return The number of entries in this HashTable.
**/
virtual std::size_t numEntries() const = 0;
/**
* @brief Lookup a key against this hash table to find a matching entry.
*
* @warning Only usable with the hash table that does not allow duplicate
* keys.
* @warning The key must not be null.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for single scalar keys. See also
* getSingleCompositeKey().
*
* @param key The key to look up.
* @return The value of a matched entry if a matching key is found.
* Otherwise, return NULL.
**/
virtual const ValueT* getSingle(const TypedValue &key) const = 0;
/**
* @brief Lookup a composite key against this hash table to find a matching
* entry.
*
* @warning Only usable with the hash table that does not allow duplicate
* keys.
* @warning The key must not be null.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for composite keys. See also getSingle().
*
* @param key The key to look up.
* @return The value of a matched entry if a matching key is found.
* Otherwise, return NULL.
**/
virtual const ValueT* getSingleCompositeKey(const std::vector<TypedValue> &key) const = 0;
/**
* @brief Lookup a key against this hash table to find matching entries.
*
* @warning The key must not be null.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note It is more efficient to call getSingle() if the hash table does not
* allow duplicate keys.
* @note This version is for single scalar keys. See also
* getAllCompositeKey().
*
* @param key The key to look up.
* @param values A vector to hold values of all matching entries. Matches
* will be appended to the vector.
**/
virtual void getAll(const TypedValue &key, std::vector<const ValueT*> *values) const = 0;
/**
* @brief Lookup a composite key against this hash table to find matching
* entries.
*
* @warning The key must not be null.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note It is more efficient to call getSingleCompositeKey() if the hash
* table does not allow duplicate keys.
* @note This version is for composite keys. See also getAll().
*
* @param key The key to look up.
* @param values A vector to hold values of all matching entries. Matches
* will be appended to the vector.
**/
virtual void getAllCompositeKey(const std::vector<TypedValue> &key,
std::vector<const ValueT*> *values) const = 0;
/**
* @brief Lookup (multiple) keys from a ValueAccessor and apply a functor to
* the matching values.
*
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for single scalar keys. See also
* getAllFromValueAccessorCompositeKey().
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes 2 arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and
* const ValueT&. The functor will be invoked once for each pair of a
* key taken from accessor and matching value.
**/
template <typename FunctorT>
void getAllFromValueAccessor(ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
FunctorT *functor) const;
/**
* @brief Lookup (multiple) keys from a ValueAccessor, apply a functor to the
* matching values and additionally call a recordMatch() function of
* the functor when the first match for a key is found.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for single scalar keys. See also
* getAllFromValueAccessorCompositeKeyWithExtraWorkForFirstMatch().
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide two functions:
* 1) An operator that takes 2 arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and
* const ValueT&. The operator will be invoked once for each pair of a
* key taken from accessor and matching value.
* 2) A function hasMatch that takes 1 argument: const ValueAccessor&.
* The function will be called only once for a key from accessor when
* the first match is found.
*/
template <typename FunctorT>
void getAllFromValueAccessorWithExtraWorkForFirstMatch(
ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
FunctorT *functor) const;
/**
* @brief Lookup (multiple) keys from a ValueAccessor, apply a functor to the
* matching values and additionally call a recordMatch() function of
* the functor when the first match for a key is found. Composite key
* version.
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor, which should provide two functions:
* 1) An operator that takes 2 arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and
* const ValueT&. The operator will be invoked once for each pair of a
* key taken from accessor and matching value.
* 2) A function hasMatch that takes 1 argument: const ValueAccessor&.
* The function will be called only once for a key from accessor when
* the first match is found.
*/
template <typename FunctorT>
void getAllFromValueAccessorCompositeKeyWithExtraWorkForFirstMatch(
ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
FunctorT *functor) const;
/**
* @brief Lookup (multiple) keys from a ValueAccessor and apply a functor to
* the matching values. Composite key version.
*
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for composite keys. See also
* getAllFromValueAccessor().
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_ids The attribute IDs of each key component to be read
* from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* has a null component before inserting it (null keys are skipped).
* This must be set to true if some of the keys that will be read from
* accessor may be null.
* @param functor A pointer to a functor, which should provide a call
* operator that takes 2 arguments: const ValueAccessor& (or better
* yet, a templated call operator which takes a const reference to
* some subclass of ValueAccessor as its first argument) and
* const ValueT&. The functor will be invoked once for each pair of a
* key taken from accessor and matching value.
**/
template <typename FunctorT>
void getAllFromValueAccessorCompositeKey(ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
FunctorT *functor) const;
/**
* @brief Apply the functor to each key with a match in the hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor which should provide an operator that
* takes 1 argument: const ValueAccessor&. The operator will be called
* only once for a key from accessor if there is a match.
*/
template <typename FunctorT>
void runOverKeysFromValueAccessorIfMatchFound(ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
FunctorT *functor) const {
return runOverKeysFromValueAccessor<true>(accessor,
key_attr_id,
check_for_null_keys,
functor);
}
/**
* @brief Apply the functor to each key with a match in the hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor which should provide an operator that
* takes 1 argument: const ValueAccessor&. The operator will be called
* only once for a key from accessor if there is a match.
*/
template <typename FunctorT>
void runOverKeysFromValueAccessorIfMatchFoundCompositeKey(
ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
FunctorT *functor) const {
return runOverKeysFromValueAccessorCompositeKey<true>(accessor,
key_attr_ids,
check_for_null_keys,
functor);
}
/**
* @brief Apply the functor to each key without a match in the hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor which should provide an operator that
* takes 1 argument: const ValueAccessor&. The operator will be called
* only once for a key from accessor if there is no match.
*/
template <typename FunctorT>
void runOverKeysFromValueAccessorIfMatchNotFound(
ValueAccessor *accessor,
const attribute_id key_attr_id,
const bool check_for_null_keys,
FunctorT *functor) const {
return runOverKeysFromValueAccessor<false>(accessor,
key_attr_id,
check_for_null_keys,
functor);
}
/**
* @brief Apply the functor to each key without a match in the hash table.
*
* @param accessor A ValueAccessor which will be used to access keys.
* beginIteration() should be called on accessor before calling this
* method.
* @param key_attr_id The attribute ID of the keys to be read from accessor.
* @param check_for_null_keys If true, each key will be checked to see if it
* is null before looking it up (null keys are skipped). This must be
* set to true if some of the keys that will be read from accessor may
* be null.
* @param functor A pointer to a functor which should provide an operator that
* takes 1 argument: const ValueAccessor&. The operator will be called
* only once for a key from accessor if there is no match.
*/
template <typename FunctorT>
void runOverKeysFromValueAccessorIfMatchNotFoundCompositeKey(
ValueAccessor *accessor,
const std::vector<attribute_id> &key_attr_ids,
const bool check_for_null_keys,
FunctorT *functor) const {
return runOverKeysFromValueAccessorCompositeKey<false>(accessor,
key_attr_ids,
check_for_null_keys,
functor);
}
/**
* @brief Apply a functor to each key, value pair in this hash table.
*
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for single scalar keys. See also
* forEachCompositeKey().
*
* @param functor A pointer to a functor, which should provide a call
* operator which takes 2 arguments: const TypedValue&, const ValueT&.
* The call operator will be invoked once on each key, value pair in
* this hash table (note that if allow_duplicate_keys is true,
* the call may occur multiple times for the same key with different
* values).
* @return The number of key-value pairs visited.
**/
template <typename FunctorT>
std::size_t forEach(FunctorT *functor) const;
/**
* @brief Apply a functor to each key, value pair in this hash table.
*
* @warning This method assumes that no concurrent calls to put(),
* putCompositeKey(), putValueAccessor(),
* putValueAccessorCompositeKey(), upsert(), upsertCompositeKey(),
* upsertValueAccessor(), or upsertValueAccessorCompositeKey() are
* taking place (i.e. that this HashTable is immutable for the
* duration of the call and as long as the returned pointer may be
* dereferenced). Concurrent calls to getSingle(),
* getSingleCompositeKey(), getAll(), getAllCompositeKey(),
* getAllFromValueAccessor(), getAllFromValueAccessorCompositeKey(),
* forEach(), and forEachCompositeKey() are safe.
* @note This version is for composite keys. See also forEach().
*
* @param functor A pointer to a functor, which should provide a call
* operator which takes 2 arguments: const std::vector<TypedValue>&,
* const ValueT&. The call operator will be invoked once on each key,
* value pair in this hash table (note that if allow_duplicate_keys is
* true, the call may occur multiple times for the same key with
* different values).
* @return The number of key-value pairs visited.
**/
template <typename FunctorT>
std::size_t forEachCompositeKey(FunctorT *functor) const;
protected:
/**
* @brief Constructor for new resizable hash table.
*
* @param key_types A vector of one or more types (>1 indicates a composite
* key).
* @param num_entries The estimated number of entries this hash table will
* hold.
* @param storage_manager The StorageManager to use (a StorageBlob will be
* allocated to hold this hash table's contents).
* @param adjust_hashes If true, the hash of a key should be modified by
* applying AdjustHash() so that it does not collide with one of the