leveldb_datastore.h 3.14 KB
Newer Older
1 2 3 4 5 6 7
// Copyright (c) 2017, Los Alamos National Security, LLC.
// All rights reserved.
#ifndef ldb_datastore_h
#define ldb_datastore_h

#include "kv-config.h"
#include <leveldb/db.h>
8
#include <leveldb/comparator.h>
9
#include <leveldb/env.h>
10
#include "sdskv-common.h"
11
#include "datastore/datastore.h"
12

13

14 15
// may want to implement some caching for persistent stores like LevelDB
class LevelDBDataStore : public AbstractDataStore {
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
    private:

        class LevelDBDataStoreComparator : public leveldb::Comparator {
            private:
                LevelDBDataStore* _store;

            public:
                LevelDBDataStoreComparator(LevelDBDataStore* store)
                    : _store(store) {}

                int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const {
                    if(_store->_less) {
                        return _store->_less((const void*)a.data(), a.size(), (const void*)b.data(), b.size());
                    } else {
                        return a.compare(b);
                    }
                }

                // Ignore the following methods for now:
                const char* Name() const { return "LevelDBDataStoreComparator"; }
                void FindShortestSeparator(std::string*, const leveldb::Slice&) const {}
                void FindShortSuccessor(std::string*) const {}
        };

    public:
        LevelDBDataStore();
        LevelDBDataStore(Duplicates duplicates, bool eraseOnGet, bool debug);
        virtual ~LevelDBDataStore();
44
        virtual bool openDatabase(const std::string& db_name, const std::string& path);
45 46 47
        virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data);
        virtual bool get(const ds_bulk_t &key, ds_bulk_t &data);
        virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data);
48
        virtual bool exists(const ds_bulk_t &key);
49 50
        virtual bool erase(const ds_bulk_t &key);
        virtual void set_in_memory(bool enable); // not supported, a no-op
51
        virtual void set_comparison_function(const std::string& name, comparator_fn less);
52 53 54
        virtual void set_no_overwrite() {
            _no_overwrite = true;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
55
        virtual void sync();
56 57 58
#ifdef USE_REMI
        virtual remi_fileset_t create_and_populate_fileset() const;
#endif
59
    protected:
60 61 62 63 64 65 66 67
        virtual std::vector<ds_bulk_t> vlist_keys(
                const ds_bulk_t &start, size_t count, const ds_bulk_t &prefix) const;
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t &prefix) const;
        virtual std::vector<ds_bulk_t> vlist_key_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, size_t max_keys) const;
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, size_t max_keys) const;
68 69 70 71 72 73
        leveldb::DB *_dbm = NULL;
    private:
        std::string toString(const ds_bulk_t &key);
        ds_bulk_t fromString(const std::string &keystr);
        AbstractDataStore::comparator_fn _less;
        LevelDBDataStoreComparator _keycmp;
74 75 76
};

#endif // ldb_datastore_h