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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*-
* Public Domain 2014-2019 MongoDB, Inc.
* Public Domain 2008-2014 WiredTiger, Inc.
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <wiredtiger_ext.h>
/*
* A simple WiredTiger collator for indices having a single integer key,
* where the ordering is descending (reversed). This collator also
* requires that primary key be an integer.
*/
/* Local collator structure. */
typedef struct {
WT_COLLATOR collator; /* Must come first */
WT_EXTENSION_API *wt_api; /* Extension API */
} REVINT_COLLATOR;
/*
* revint_compare --
* WiredTiger reverse integer collation, used for tests.
*/
static int
revint_compare(WT_COLLATOR *collator,
WT_SESSION *session, const WT_ITEM *k1, const WT_ITEM *k2, int *cmp)
{
const REVINT_COLLATOR *revint_collator;
WT_EXTENSION_API *wt_api;
WT_PACK_STREAM *pstream;
int64_t i1, i2, p1, p2;
int ret;
i1 = i2 = p1 = p2 = 0;
revint_collator = (const REVINT_COLLATOR *)collator;
wt_api = revint_collator->wt_api;
/*
* All indices using this collator have an integer key, and the
* primary key is also an integer. A collator is usually passed the
* concatenation of index key and primary key (when available),
* hence we initially unpack using "ii".
*
* A collator may also be called with an item that includes a index
* key and no primary key. Among items having the same index key,
* an item with no primary key should sort before an item with a
* primary key. The reason is that if the application calls
* WT_CURSOR::search on a index key for which there are more than
* one value, the search key will not yet have a primary key. We
* want to position the cursor at the 'first' matching index key so
* that repeated calls to WT_CURSOR::next will see them all.
*
* To keep this code simple, we do not reverse the ordering
* when comparing primary keys.
*/
if ((ret = wt_api->unpack_start(
wt_api, session, "ii", k1->data, k1->size, &pstream)) != 0)
return (ret);
if ((ret = wt_api->unpack_int(wt_api, pstream, &i1)) != 0)
goto err;
if ((ret = wt_api->unpack_int(wt_api, pstream, &p1)) != 0)
/* A missing primary key is OK and sorts first. */
p1 = INT64_MIN;
if ((ret = wt_api->pack_close(wt_api, pstream, NULL)) != 0)
return (ret);
/* Unpack the second pair of numbers. */
if ((ret = wt_api->unpack_start(
wt_api, session, "ii", k2->data, k2->size, &pstream)) != 0)
return (ret);
if ((ret = wt_api->unpack_int(wt_api, pstream, &i2)) != 0) {
err: (void)wt_api->pack_close(wt_api, pstream, NULL);
return (ret);
}
if ((ret = wt_api->unpack_int(wt_api, pstream, &p2)) != 0)
/* A missing primary key is OK and sorts first. */
p2 = INT64_MIN;
if ((ret = wt_api->pack_close(wt_api, pstream, NULL)) != 0)
return (ret);
/* sorting is reversed */
if (i1 < i2)
*cmp = 1;
else if (i1 > i2)
*cmp = -1;
/* compare primary keys next, not reversed */
else if (p1 < p2)
*cmp = -1;
else if (p1 > p2)
*cmp = 1;
else
*cmp = 0; /* index key and primary key are same */
return (0);
}
/*
* revint_terminate --
* Terminate is called to free the collator and any associated memory.
*/
static int
revint_terminate(WT_COLLATOR *collator, WT_SESSION *session)
{
(void)session; /* Unused parameters */
/* Free the allocated memory. */
free(collator);
return (0);
}
/*
* wiredtiger_extension_init --
* WiredTiger revint collation extension.
*/
int
wiredtiger_extension_init(WT_CONNECTION *connection, WT_CONFIG_ARG *config)
{
REVINT_COLLATOR *revint_collator;
int ret;
(void)config; /* Unused parameters */
if ((revint_collator = calloc(1, sizeof(REVINT_COLLATOR))) == NULL)
return (errno);
revint_collator->collator.compare = revint_compare;
revint_collator->collator.terminate = revint_terminate;
revint_collator->wt_api = connection->get_extension_api(connection);
if ((ret = connection->add_collator(
connection, "revint", (WT_COLLATOR *)revint_collator, NULL)) == 0)
return (0);
free(revint_collator);
return (ret);
}
|