1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #pragma once
40 
41 #include <limits.h>
42 
43 #include "ft/comparator.h"
44 #include "util/dbt.h"
45 
46 namespace toku {
47 
48     __attribute__((__unused__))
min_dbt(void)49     static DBT min_dbt(void) {
50         static int64_t min = INT_MIN;
51         DBT dbt;
52         toku_fill_dbt(&dbt, &min, sizeof(int64_t));
53         dbt.flags = DB_DBT_USERMEM;
54         return dbt;
55     }
56 
57     __attribute__((__unused__))
max_dbt(void)58     static DBT max_dbt(void) {
59         static int64_t max = INT_MAX;
60         DBT dbt;
61         toku_fill_dbt(&dbt, &max, sizeof(int64_t));
62         dbt.flags = DB_DBT_USERMEM;
63         return dbt;
64     }
65 
66     __attribute__((__unused__))
get_dbt(int64_t key)67     static const DBT *get_dbt(int64_t key) {
68         static const int NUM_DBTS = 1000;
69         static bool initialized;
70         static int64_t static_ints[NUM_DBTS];
71         static DBT static_dbts[NUM_DBTS];
72         invariant(key < NUM_DBTS);
73         if (!initialized) {
74             for (int i = 0; i < NUM_DBTS; i++) {
75                 static_ints[i] = i;
76                 toku_fill_dbt(&static_dbts[i],
77                         &static_ints[i],
78                         sizeof(int64_t));
79                 static_dbts[i].flags = DB_DBT_USERMEM;
80             }
81             initialized = true;
82         }
83 
84         invariant(key < NUM_DBTS);
85         return &static_dbts[key];
86     }
87 
88     __attribute__((__unused__))
compare_dbts(DB * db,const DBT * key1,const DBT * key2)89     static int compare_dbts(DB *db, const DBT *key1, const DBT *key2) {
90         (void) db;
91 
92         // this emulates what a "infinity-aware" comparator object does
93         if (toku_dbt_is_infinite(key1) || toku_dbt_is_infinite(key2)) {
94             return toku_dbt_infinite_compare(key1, key2);
95         } else {
96             invariant(key1->size == sizeof(int64_t));
97             invariant(key2->size == sizeof(int64_t));
98             int64_t a = *(int64_t*) key1->data;
99             int64_t b = *(int64_t*) key2->data;
100             if (a < b) {
101                 return -1;
102             } else if (a == b) {
103                 return 0;
104             } else {
105                 return 1;
106             }
107         }
108     }
109 
110     __attribute__((__unused__)) comparator dbt_comparator;
111 
112     __attribute__((__constructor__))
construct_dbt_comparator(void)113     static void construct_dbt_comparator(void) {
114         dbt_comparator.create(compare_dbts, nullptr);
115     }
116 
117     __attribute__((__destructor__))
destruct_dbt_comparator(void)118     static void destruct_dbt_comparator(void) {
119         dbt_comparator.destroy();
120     }
121 
122 } /* namespace toku */
123