aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorGeir Storli <geirst@verizonmedia.com>2019-09-09 12:22:58 +0000
committerGeir Storli <geirst@verizonmedia.com>2019-09-09 13:02:17 +0000
commitf6af721145f1609bca2274970acbf314983fdc10 (patch)
tree9c5fa37608abc0c0f57293c753c0dc85ba320921 /vespalib
parentdd25c2a3c4c16ba96eda61e996f5347f1f8eaa9c (diff)
Reduce code duplication by letting enum store comparators inherit unique store comparators.
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/vespa/vespalib/datastore/entry_comparator.h14
-rw-r--r--vespalib/src/vespa/vespalib/datastore/unique_store.h1
-rw-r--r--vespalib/src/vespa/vespalib/datastore/unique_store_comparator.h44
-rw-r--r--vespalib/src/vespa/vespalib/datastore/unique_store_string_comparator.h29
4 files changed, 52 insertions, 36 deletions
diff --git a/vespalib/src/vespa/vespalib/datastore/entry_comparator.h b/vespalib/src/vespa/vespalib/datastore/entry_comparator.h
index a598ca195ae..09b850a4746 100644
--- a/vespalib/src/vespa/vespalib/datastore/entry_comparator.h
+++ b/vespalib/src/vespa/vespalib/datastore/entry_comparator.h
@@ -6,17 +6,19 @@
namespace search::datastore {
-/*
- * Compare two entries based on entry refs. Valid entry ref is mapped
- * to an entry in a data store. Invalid entry ref is mapped to a
- * temporary entry owned or referenced by comparator instance.
+/**
+ * Less-than comparator for two entries based on entry refs.
+ *
+ * Valid entry ref is mapped to an entry in a data store.
+ * Invalid entry ref is mapped to a temporary entry owned or referenced by comparator instance.
*/
class EntryComparator {
public:
virtual ~EntryComparator() {}
+
/**
- * Compare the values represented by the given unique store entry refs.
- **/
+ * Returns true if the value represented by lhs ref is less than the value represented by rhs ref.
+ */
virtual bool operator()(const EntryRef lhs, const EntryRef rhs) const = 0;
};
diff --git a/vespalib/src/vespa/vespalib/datastore/unique_store.h b/vespalib/src/vespa/vespalib/datastore/unique_store.h
index b4ed3f6d86b..cbaa6aa7db0 100644
--- a/vespalib/src/vespa/vespalib/datastore/unique_store.h
+++ b/vespalib/src/vespa/vespalib/datastore/unique_store.h
@@ -61,6 +61,7 @@ public:
Allocator& get_allocator() { return _allocator; }
const Allocator& get_allocator() const { return _allocator; }
IUniqueStoreDictionary& get_dictionary() { return *_dict; }
+ inline const DataStoreType& get_data_store() const { return _allocator.get_data_store(); }
// Pass on hold list management to underlying store
void transferHoldLists(generation_t generation);
diff --git a/vespalib/src/vespa/vespalib/datastore/unique_store_comparator.h b/vespalib/src/vespa/vespalib/datastore/unique_store_comparator.h
index acd4f531e2c..ae52791ae0f 100644
--- a/vespalib/src/vespa/vespalib/datastore/unique_store_comparator.h
+++ b/vespalib/src/vespa/vespalib/datastore/unique_store_comparator.h
@@ -9,7 +9,7 @@
namespace search::datastore {
-/*
+/**
* Helper class for comparing elements in unique store.
*/
template <typename EntryT>
@@ -20,7 +20,7 @@ public:
}
};
-/*
+/**
* Helper class for comparing floating point elements in unique store with
* special handling of NAN.
*/
@@ -39,7 +39,7 @@ public:
}
};
-/*
+/**
* Specialized helper class for comparing float elements in unique store with
* special handling of NAN.
*/
@@ -47,7 +47,7 @@ template <>
class UniqueStoreComparatorHelper<float> : public UniqueStoreFloatingPointComparatorHelper<float> {
};
-/*
+/**
* Specialized helper class for comparing double elements in unique store with
* special handling of NAN.
*/
@@ -55,35 +55,45 @@ template <>
class UniqueStoreComparatorHelper<double> : public UniqueStoreFloatingPointComparatorHelper<double> {
};
-/*
- * Compare two entries based on entry refs. Valid entry ref is mapped
- * to an entry in a data store. Invalid entry ref is mapped to a
- * temporary entry referenced by comparator instance.
+/**
+ * Compare two entries based on entry refs.
+ *
+ * Valid entry ref is mapped to an entry in a data store.
+ * Invalid entry ref is mapped to a temporary entry referenced by comparator instance.
*/
template <typename EntryT, typename RefT>
class UniqueStoreComparator : public EntryComparator {
+protected:
using EntryType = EntryT;
using WrappedEntryType = UniqueStoreEntry<EntryType>;
using RefType = RefT;
using DataStoreType = DataStoreT<RefT>;
const DataStoreType &_store;
- const EntryType &_value;
-public:
- UniqueStoreComparator(const DataStoreType &store, const EntryType &value)
- : _store(store),
- _value(value)
- {
- }
+ const EntryType _fallback_value;
+
inline const EntryType &get(EntryRef ref) const {
if (ref.valid()) {
RefType iRef(ref);
return _store.template getEntry<WrappedEntryType>(iRef)->value();
} else {
- return _value;
+ return _fallback_value;
}
}
- bool operator()(const EntryRef lhs, const EntryRef rhs) const override
+
+public:
+ UniqueStoreComparator(const DataStoreType &store, const EntryType &fallback_value)
+ : _store(store),
+ _fallback_value(fallback_value)
+ {
+ }
+
+ UniqueStoreComparator(const DataStoreType &store)
+ : _store(store),
+ _fallback_value()
{
+ }
+
+ bool operator()(const EntryRef lhs, const EntryRef rhs) const override {
const EntryType &lhsValue = get(lhs);
const EntryType &rhsValue = get(rhs);
return UniqueStoreComparatorHelper<EntryT>::less(lhsValue, rhsValue);
diff --git a/vespalib/src/vespa/vespalib/datastore/unique_store_string_comparator.h b/vespalib/src/vespa/vespalib/datastore/unique_store_string_comparator.h
index e5d3888a5e2..80e7a501cfa 100644
--- a/vespalib/src/vespa/vespalib/datastore/unique_store_string_comparator.h
+++ b/vespalib/src/vespa/vespalib/datastore/unique_store_string_comparator.h
@@ -7,24 +7,21 @@
namespace search::datastore {
-/*
- * Compare two strings based on entry refs. Valid entry ref is mapped
- * to a string in a data store. Invalid entry ref is mapped to a
- * temporary string pointed to by comparator instance.
+/**
+ * Compare two strings based on entry refs.
+ *
+ * Valid entry ref is mapped to a string in a data store.
+ * Invalid entry ref is mapped to a temporary string pointed to by comparator instance.
*/
template <typename RefT>
class UniqueStoreStringComparator : public EntryComparator {
+protected:
using RefType = RefT;
using WrappedExternalEntryType = UniqueStoreEntry<std::string>;
using DataStoreType = DataStoreT<RefT>;
const DataStoreType &_store;
- const char *_value;
-public:
- UniqueStoreStringComparator(const DataStoreType &store, const char *value)
- : _store(store),
- _value(value)
- {
- }
+ const char *_fallback_value;
+
const char *get(EntryRef ref) const {
if (ref.valid()) {
RefType iRef(ref);
@@ -36,12 +33,18 @@ public:
return _store.template getEntry<WrappedExternalEntryType>(iRef)->value().c_str();
}
} else {
- return _value;
+ return _fallback_value;
}
}
- bool operator()(const EntryRef lhs, const EntryRef rhs) const override
+public:
+ UniqueStoreStringComparator(const DataStoreType &store, const char *fallback_value)
+ : _store(store),
+ _fallback_value(fallback_value)
{
+ }
+
+ bool operator()(const EntryRef lhs, const EntryRef rhs) const override {
const char *lhs_value = get(lhs);
const char *rhs_value = get(rhs);
return (strcmp(lhs_value, rhs_value) < 0);