File: comparator_wrapper.hpp

package info (click to toggle)
python-rocksdb 0.8.0~rc3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 480 kB
  • sloc: python: 798; cpp: 408; makefile: 158
file content (63 lines) | stat: -rw-r--r-- 1,791 bytes parent folder | download
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
#include "rocksdb/comparator.h"
#include "rocksdb/env.h"
#include <stdexcept>

using std::string;
using rocksdb::Comparator;
using rocksdb::Slice;
using rocksdb::Logger;

namespace py_rocks {
    class ComparatorWrapper: public Comparator {
        public:
            typedef int (*compare_func)(
                void*,
                Logger*,
                string&,
                const Slice&,
                const Slice&);

            ComparatorWrapper(
                string name,
                void* compare_context,
                compare_func compare_callback):
                    name(name),
                    compare_context(compare_context),
                    compare_callback(compare_callback)
            {}

            virtual int Compare(const Slice& a, const Slice& b) const {
                string error_msg;
                int val;

                val = this->compare_callback(
                    this->compare_context,
                    this->info_log.get(),
                    error_msg,
                    a,
                    b);

                if (error_msg.size()) {
                    throw std::runtime_error(error_msg.c_str());
                }
                return val;
            }

            virtual const char* Name() const {
                return this->name.c_str();
            }

            virtual void FindShortestSeparator(string*, const Slice&) const {}
            virtual void FindShortSuccessor(string*) const {}

            void set_info_log(std::shared_ptr<Logger> info_log) {
                this->info_log = info_log;
            }

        private:
            string name;
            void* compare_context;
            compare_func compare_callback;
            std::shared_ptr<Logger> info_log;
    };
}