1239.3.111
by Toru Maesaka
Separated BlitzCursor code out to it's own file. |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2009 - 2010 Toru Maesaka
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
8 |
* the Free Software Foundation; version 2 of the License.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 |
*/
|
|
19 |
||
20 |
#include <config.h> |
|
21 |
#include "ha_blitz.h" |
|
22 |
||
23 |
char *BlitzCursor::first_key(int *key_len) { |
|
24 |
if (!tcbdbcurfirst(cursor)) |
|
25 |
return NULL; |
|
26 |
||
27 |
return (char *)tcbdbcurkey(cursor, key_len); |
|
28 |
}
|
|
29 |
||
30 |
char *BlitzCursor::final_key(int *key_len) { |
|
31 |
if (!tcbdbcurlast(cursor)) |
|
32 |
return NULL; |
|
33 |
||
34 |
return (char *)tcbdbcurkey(cursor, key_len); |
|
35 |
}
|
|
36 |
||
37 |
/* It's possible that the cursor had been implicitly moved
|
|
38 |
forward. If so, we obtain the key at the current position
|
|
39 |
and return that. This can happen in delete_key(). */
|
|
40 |
char *BlitzCursor::next_key(int *key_len) { |
|
41 |
if (!moved) { |
|
42 |
if (!tcbdbcurnext(cursor)) |
|
43 |
return NULL; |
|
44 |
} else |
|
45 |
moved = false; |
|
46 |
||
47 |
return (char *)tcbdbcurkey(cursor, key_len); |
|
48 |
}
|
|
49 |
||
50 |
char *BlitzCursor::prev_key(int *key_len) { |
|
51 |
if (!tcbdbcurprev(cursor)) |
|
52 |
return NULL; |
|
53 |
||
54 |
return (char *)tcbdbcurkey(cursor, key_len); |
|
55 |
}
|
|
56 |
||
57 |
char *BlitzCursor::next_logical_key(int *key_len) { |
|
58 |
int origin_len, a_cmp_len, b_cmp_len; |
|
59 |
char *rv, *origin; |
|
60 |
||
61 |
/* Get the key to scan away from. */
|
|
62 |
if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL) |
|
63 |
return NULL; |
|
64 |
||
65 |
rv = NULL; |
|
66 |
||
67 |
/* Traverse the tree from the cursor position until we hit
|
|
68 |
a key greater than the origin or EOF. */
|
|
69 |
while (1) { |
|
70 |
/* Move the cursor and get the next key. */
|
|
71 |
if (!tcbdbcurnext(cursor)) |
|
72 |
break; |
|
73 |
||
74 |
rv = (char *)tcbdbcurkey(cursor, key_len); |
|
75 |
||
76 |
/* Compare the fetched key with origin. */
|
|
77 |
if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) > 0) |
|
78 |
break; |
|
79 |
||
80 |
free(rv); |
|
81 |
}
|
|
82 |
||
83 |
free(origin); |
|
84 |
return rv; |
|
85 |
}
|
|
86 |
||
87 |
char *BlitzCursor::prev_logical_key(int *key_len) { |
|
88 |
int origin_len, a_cmp_len, b_cmp_len; |
|
89 |
char *rv, *origin; |
|
90 |
||
91 |
/* Get the key to scan away from. */
|
|
92 |
if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL) |
|
93 |
return NULL; |
|
94 |
||
95 |
rv = NULL; |
|
96 |
||
97 |
while (1) { |
|
98 |
if (!tcbdbcurprev(cursor)) |
|
99 |
break; |
|
100 |
||
101 |
rv = (char *)tcbdbcurkey(cursor, key_len); |
|
102 |
||
103 |
/* Compare the fetched key with origin. */
|
|
104 |
if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) < 0) |
|
105 |
break; |
|
106 |
||
107 |
free(rv); |
|
108 |
}
|
|
109 |
||
110 |
free(origin); |
|
111 |
return rv; |
|
112 |
}
|
|
113 |
||
114 |
/* A cursor based lookup on a B+Tree doesn't guarantee that the
|
|
115 |
returned key is identical. This is because it would return the
|
|
116 |
next logical key if one exists. */
|
|
117 |
char *BlitzCursor::find_key(const int search_mode, const char *key, |
|
118 |
const int klen, int *rv_len) { |
|
119 |
||
120 |
if (!tcbdbcurjump(cursor, (void *)key, klen)) |
|
121 |
return NULL; |
|
122 |
||
123 |
char *rv = (char *)tcbdbcurkey(cursor, rv_len); |
|
124 |
||
125 |
if (rv == NULL) |
|
126 |
return NULL; |
|
127 |
||
128 |
int cmp, a_cmp_len, b_cmp_len; |
|
129 |
cmp = packed_key_cmp(this->tree, rv, key, &a_cmp_len, &b_cmp_len); |
|
130 |
||
131 |
switch (search_mode) { |
|
132 |
case drizzled::HA_READ_KEY_EXACT: |
|
133 |
if (cmp != 0) { |
|
134 |
free(rv); |
|
135 |
rv = NULL; |
|
136 |
}
|
|
137 |
break; |
|
138 |
case drizzled::HA_READ_AFTER_KEY: |
|
139 |
if (cmp > 0) { |
|
140 |
break; |
|
141 |
} else if (cmp == 0) { |
|
142 |
free(rv); |
|
143 |
rv = this->next_logical_key(rv_len); |
|
144 |
} else { |
|
145 |
free(rv); |
|
146 |
rv = NULL; |
|
147 |
}
|
|
148 |
break; |
|
149 |
case drizzled::HA_READ_BEFORE_KEY: |
|
150 |
free(rv); |
|
151 |
rv = this->prev_logical_key(rv_len); |
|
152 |
break; |
|
153 |
//case drizzled::HA_READ_KEY_OR_NEXT:
|
|
154 |
//case drizzled::HA_READ_KEY_OR_PREV:
|
|
155 |
//case drizzled::HA_READ_PREFIX:
|
|
156 |
//case drizzled::HA_READ_PREFIX_LAST:
|
|
157 |
//case drizzled::HA_READ_PREFIX_LAST_OR_PREV:
|
|
158 |
default: |
|
159 |
break; |
|
160 |
}
|
|
161 |
||
162 |
return rv; |
|
163 |
}
|
|
164 |
||
165 |
int BlitzCursor::delete_position(void) { |
|
166 |
if (!tcbdbcurout(cursor)) |
|
167 |
return -1; |
|
168 |
||
169 |
moved = true; |
|
170 |
return 0; |
|
171 |
}
|