50 #define DEBUG DEBUG_NONE
53 struct search_handle {
59 struct search_handle handle;
61 static db_result_t null_op(index_t *);
62 static db_result_t insert(index_t *, attribute_value_t *, tuple_id_t);
63 static db_result_t
delete(index_t *, attribute_value_t *);
64 static tuple_id_t get_next(index_iterator_t *);
73 index_api_t index_inline = {
75 INDEX_API_EXTERNAL | INDEX_API_COMPLETE | INDEX_API_RANGE_QUERIES,
85 static attribute_value_t *
86 get_value(tuple_id_t *index, relation_t *rel, attribute_t *attr)
88 unsigned char row[rel->row_length];
89 static attribute_value_t value;
91 if(DB_ERROR(storage_get_row(rel, index, row))) {
95 if(DB_ERROR(relation_get_value(rel, attr, row, &value))) {
96 PRINTF(
"DB: Unable to retrieve a value from tuple %ld\n", (
long)(*index));
104 binary_search(index_iterator_t *index_iterator,
105 attribute_value_t *target_value,
110 attribute_value_t *cmp_value;
115 rel = index_iterator->index->rel;
116 attr = index_iterator->index->attr;
118 max = relation_cardinality(rel);
119 if(max == INVALID_TUPLE) {
120 return INVALID_TUPLE;
126 center = min + ((max - min) / 2);
128 cmp_value = get_value(¢er, rel, attr);
129 if(cmp_value ==
NULL) {
130 PRINTF(
"DB: Failed to get the center value, index = %ld\n",
132 return INVALID_TUPLE;
135 if(db_value_to_long(target_value) > db_value_to_long(cmp_value)) {
140 }
while(min <= max &&
141 db_value_to_long(target_value) != db_value_to_long(cmp_value));
144 db_value_to_long(target_value) != db_value_to_long(cmp_value)) {
145 PRINTF(
"DB: Could not find value %ld in the inline index\n",
146 db_value_to_long(target_value));
147 return INVALID_TUPLE;
154 range_search(index_iterator_t *index_iterator,
155 tuple_id_t *start, tuple_id_t *end)
157 attribute_value_t *low_target;
158 attribute_value_t *high_target;
161 low_target = &index_iterator->min_value;
162 high_target = &index_iterator->max_value;
164 PRINTF(
"DB: Search index for value range (%ld, %ld)\n",
165 db_value_to_long(low_target), db_value_to_long(high_target));
167 exact_match = db_value_to_long(low_target) == db_value_to_long(high_target);
171 *start = binary_search(index_iterator, low_target, exact_match);
172 if(*start == INVALID_TUPLE) {
173 return DB_INDEX_ERROR;
176 *end = binary_search(index_iterator, high_target, exact_match);
177 if(*end == INVALID_TUPLE) {
178 return DB_INDEX_ERROR;
184 null_op(index_t *index)
190 insert(index_t *index, attribute_value_t *value, tuple_id_t tuple_id)
196 delete(index_t *index, attribute_value_t *value)
202 get_next(index_iterator_t *iterator)
204 static tuple_id_t cached_start;
205 static tuple_id_t cached_end;
207 if(iterator->next_item_no == 0) {
213 if(DB_ERROR(range_search(iterator, &cached_start, &cached_end))) {
216 return INVALID_TUPLE;
218 PRINTF(
"DB: Cached the tuple range (%ld,%ld)\n",
219 (
long)cached_start, (
long)cached_end);
220 ++iterator->next_item_no;
222 }
else if(cached_start + iterator->next_item_no <= cached_end) {
223 return cached_start + iterator->next_item_no++;
226 return INVALID_TUPLE;
The storage interface used by the database.
Declarations for the result acquisition API.
#define NULL
The null pointer.
A set of debugging macros.