~ubuntu-branches/ubuntu/vivid/mariadb-5.5/vivid-proposed

« back to all changes in this revision

Viewing changes to storage/tokudb/ft-index/ft/comparator.h

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2014-11-14 21:04:24 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20141114210424-xlyna0ozl11647o5
Tags: 5.5.40-0ubuntu0.14.10.1
* SECURITY UPDATE: Update to 5.5.40 to fix security issues (LP: #1391676)
  - CVE-2014-6507
  - CVE-2014-6491
  - CVE-2014-6500
  - CVE-2014-6469
  - CVE-2014-6555
  - CVE-2014-6559
  - CVE-2014-6494
  - CVE-2014-6496
  - CVE-2014-6464
* Add bsdutils as mariadb-server dependency like upstream does in 5.5.40.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
COPYRIGHT NOTICE:
30
30
 
31
 
  TokuDB, Tokutek Fractal Tree Indexing Library.
 
31
  TokuFT, Tokutek Fractal Tree Indexing Library.
32
32
  Copyright (C) 2007-2013 Tokutek, Inc.
33
33
 
34
34
DISCLAIMER:
85
85
  under this License.
86
86
*/
87
87
 
 
88
#pragma once
 
89
 
88
90
#ident "Copyright (c) 2007-2013 Tokutek Inc.  All rights reserved."
89
91
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
90
92
 
91
 
 
92
 
#pragma once
93
 
 
94
93
#include <db.h>
95
94
#include <string.h>
96
95
 
97
 
#include <ft/ybt.h>
98
 
#include <ft/fttypes.h>
 
96
#include "portability/memory.h"
 
97
 
 
98
#include "util/dbt.h"
 
99
 
 
100
typedef int (*ft_compare_func)(DB *db, const DBT *a, const DBT *b);
 
101
 
 
102
int toku_keycompare(const void *key1, uint32_t key1len, const void *key2, uint32_t key2len);
 
103
 
 
104
int toku_builtin_compare_fun (DB *, const DBT *, const DBT*) __attribute__((__visibility__("default")));
99
105
 
100
106
namespace toku {
101
107
 
102
 
// a comparator object encapsulates the data necessary for 
103
 
// comparing two keys in a fractal tree. it further understands
104
 
// that points may be positive or negative infinity.
105
 
 
106
 
class comparator {
107
 
public:
108
 
    void set_descriptor(DESCRIPTOR desc) {
109
 
        m_fake_db.cmp_descriptor = desc;
110
 
    }
111
 
 
112
 
    void create(ft_compare_func cmp, DESCRIPTOR desc) {
113
 
        m_cmp = cmp;
114
 
        memset(&m_fake_db, 0, sizeof(m_fake_db));
115
 
        m_fake_db.cmp_descriptor = desc;
116
 
    }
117
 
 
118
 
    int compare(const DBT *a, const DBT *b) {
119
 
        if (toku_dbt_is_infinite(a) || toku_dbt_is_infinite(b)) {
120
 
            return toku_dbt_infinite_compare(a, b);
121
 
        } else {
122
 
            return m_cmp(&m_fake_db, a, b);
123
 
        }
124
 
    }
125
 
 
126
 
private:
127
 
    struct __toku_db m_fake_db;
128
 
    ft_compare_func m_cmp;
129
 
};
 
108
    // a comparator object encapsulates the data necessary for 
 
109
    // comparing two keys in a fractal tree. it further understands
 
110
    // that points may be positive or negative infinity.
 
111
 
 
112
    class comparator {
 
113
        void init(ft_compare_func cmp, DESCRIPTOR desc, uint8_t memcmp_magic) {
 
114
            _cmp = cmp;
 
115
            _fake_db->cmp_descriptor = desc;
 
116
            _memcmp_magic = memcmp_magic;
 
117
        }
 
118
 
 
119
    public:
 
120
        // This magic value is reserved to mean that the magic has not been set.
 
121
        static const uint8_t MEMCMP_MAGIC_NONE = 0;
 
122
 
 
123
        void create(ft_compare_func cmp, DESCRIPTOR desc, uint8_t memcmp_magic = MEMCMP_MAGIC_NONE) {
 
124
            XCALLOC(_fake_db);
 
125
            init(cmp, desc, memcmp_magic);
 
126
        }
 
127
 
 
128
        // inherit the attributes of another comparator, but keep our own
 
129
        // copy of fake_db that is owned separately from the one given.
 
130
        void inherit(const comparator &cmp) {
 
131
            invariant_notnull(_fake_db);
 
132
            invariant_notnull(cmp._cmp);
 
133
            invariant_notnull(cmp._fake_db);
 
134
            init(cmp._cmp, cmp._fake_db->cmp_descriptor, cmp._memcmp_magic);
 
135
        }
 
136
 
 
137
        // like inherit, but doesn't require that the this comparator
 
138
        // was already created
 
139
        void create_from(const comparator &cmp) {
 
140
            XCALLOC(_fake_db);
 
141
            inherit(cmp);
 
142
        }
 
143
 
 
144
        void destroy() {
 
145
            toku_free(_fake_db);
 
146
        }
 
147
 
 
148
        const DESCRIPTOR_S *get_descriptor() const {
 
149
            return _fake_db->cmp_descriptor;
 
150
        }
 
151
 
 
152
        ft_compare_func get_compare_func() const {
 
153
            return _cmp;
 
154
        }
 
155
 
 
156
        uint8_t get_memcmp_magic() const {
 
157
            return _memcmp_magic;
 
158
        }
 
159
 
 
160
        bool valid() const {
 
161
            return _cmp != nullptr;
 
162
        }
 
163
 
 
164
        inline bool dbt_has_memcmp_magic(const DBT *dbt) const {
 
165
            return *reinterpret_cast<const char *>(dbt->data) == _memcmp_magic;
 
166
        }
 
167
 
 
168
        int operator()(const DBT *a, const DBT *b) const {
 
169
            if (__builtin_expect(toku_dbt_is_infinite(a) || toku_dbt_is_infinite(b), 0)) {
 
170
                return toku_dbt_infinite_compare(a, b);
 
171
            } else if (_memcmp_magic != MEMCMP_MAGIC_NONE
 
172
                       // If `a' has the memcmp magic..
 
173
                       && dbt_has_memcmp_magic(a)
 
174
                       // ..then we expect `b' to also have the memcmp magic
 
175
                       && __builtin_expect(dbt_has_memcmp_magic(b), 1)) {
 
176
                return toku_builtin_compare_fun(nullptr, a, b);
 
177
            } else {
 
178
                // yikes, const sadness here
 
179
                return _cmp(const_cast<DB *>(_fake_db), a, b);
 
180
            }
 
181
        }
 
182
 
 
183
    private:
 
184
        DB *_fake_db;
 
185
        ft_compare_func _cmp;
 
186
        uint8_t _memcmp_magic;
 
187
    };
130
188
 
131
189
} /* namespace toku */