1 /*
2    Copyright (c) 2012,2015 Monty Program Ab
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
16 #pragma once
17 
18 /* C++ system header files */
19 #include <string>
20 
21 /* MySQL includes */
22 #include "./m_ctype.h"
23 
24 /* RocksDB header files */
25 #include "rocksdb/comparator.h"
26 
27 /* MyRocks header files */
28 #include "./rdb_utils.h"
29 
30 namespace myrocks {
31 
32 /*
33   The keys are in form: {index_number} {mem-comparable-key}
34 
35   (todo: knowledge about this format is shared between this class and
36    Rdb_key_def)
37 */
38 class Rdb_pk_comparator : public rocksdb::Comparator {
39 public:
40   Rdb_pk_comparator(const Rdb_pk_comparator &) = delete;
41   Rdb_pk_comparator &operator=(const Rdb_pk_comparator &) = delete;
42   Rdb_pk_comparator() = default;
43 
bytewise_compare(const rocksdb::Slice & a,const rocksdb::Slice & b)44   static int bytewise_compare(const rocksdb::Slice &a,
45                               const rocksdb::Slice &b) {
46     const size_t a_size = a.size();
47     const size_t b_size = b.size();
48     const size_t len = (a_size < b_size) ? a_size : b_size;
49     int res;
50 
51     if ((res = memcmp(a.data(), b.data(), len)))
52       return res;
53 
54     /* Ok, res== 0 */
55     if (a_size != b_size) {
56       return a_size < b_size ? -1 : 1;
57     }
58     return HA_EXIT_SUCCESS;
59   }
60 
61   /* Override virtual methods of interest */
62 
Compare(const rocksdb::Slice & a,const rocksdb::Slice & b)63   int Compare(const rocksdb::Slice &a, const rocksdb::Slice &b) const override {
64     return bytewise_compare(a, b);
65   }
66 
Name()67   const char *Name() const override { return "RocksDB_SE_v3.10"; }
68 
69   // TODO: advanced funcs:
70   // - FindShortestSeparator
71   // - FindShortSuccessor
72 
73   // for now, do-nothing implementations:
FindShortestSeparator(std::string * start,const rocksdb::Slice & limit)74   void FindShortestSeparator(std::string *start,
75                              const rocksdb::Slice &limit) const override {}
FindShortSuccessor(std::string * key)76   void FindShortSuccessor(std::string *key) const override {}
77 };
78 
79 class Rdb_rev_comparator : public rocksdb::Comparator {
80 public:
81   Rdb_rev_comparator(const Rdb_rev_comparator &) = delete;
82   Rdb_rev_comparator &operator=(const Rdb_rev_comparator &) = delete;
83   Rdb_rev_comparator() = default;
84 
bytewise_compare(const rocksdb::Slice & a,const rocksdb::Slice & b)85   static int bytewise_compare(const rocksdb::Slice &a,
86                               const rocksdb::Slice &b) {
87     return -Rdb_pk_comparator::bytewise_compare(a, b);
88   }
89 
Compare(const rocksdb::Slice & a,const rocksdb::Slice & b)90   int Compare(const rocksdb::Slice &a, const rocksdb::Slice &b) const override {
91     return -Rdb_pk_comparator::bytewise_compare(a, b);
92   }
Name()93   const char *Name() const override { return "rev:RocksDB_SE_v3.10"; }
FindShortestSeparator(std::string * start,const rocksdb::Slice & limit)94   void FindShortestSeparator(std::string *start,
95                              const rocksdb::Slice &limit) const override {}
FindShortSuccessor(std::string * key)96   void FindShortSuccessor(std::string *key) const override {}
97 };
98 
99 } // namespace myrocks
100