leveldb_datastore.h 3.56 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 45 46 47 48
        virtual bool openDatabase(const std::string& db_name, const std::string& path) override;
        /*
        virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data) override;
        virtual bool put(ds_bulk_t &&key, ds_bulk_t &&data) override;
        */
49
        virtual bool put(const void* key, size_t ksize, const void* kdata, size_t dsize) override;
50 51 52 53 54 55 56
        virtual bool get(const ds_bulk_t &key, ds_bulk_t &data) override;
        virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data) override;
        virtual bool exists(const void* key, size_t ksize) const override;
        virtual bool erase(const ds_bulk_t &key) override;
        virtual void set_in_memory(bool enable) override; // not supported, a no-op
        virtual void set_comparison_function(const std::string& name, comparator_fn less) override;
        virtual void set_no_overwrite() override {
57 58
            _no_overwrite = true;
        }
59
        virtual void sync() override;
60
#ifdef USE_REMI
61
        virtual remi_fileset_t create_and_populate_fileset() const override;
62
#endif
63
    protected:
64
        virtual std::vector<ds_bulk_t> vlist_keys(
65
                const ds_bulk_t &start, size_t count, const ds_bulk_t &prefix) const override;
66
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
67
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t &prefix) const override;
68
        virtual std::vector<ds_bulk_t> vlist_key_range(
69
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, size_t max_keys) const override;
70
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
71
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, size_t max_keys) const override;
72 73
        leveldb::DB *_dbm = NULL;
    private:
74 75 76
        static std::string toString(const ds_bulk_t &key);
        static std::string toString(const char* bug, size_t buf_size);
        static ds_bulk_t fromString(const std::string &keystr);
77 78
        AbstractDataStore::comparator_fn _less;
        LevelDBDataStoreComparator _keycmp;
79 80 81
};

#endif // ldb_datastore_h