42 #define DEBUG DEBUG_NONE
51 static index_api_t *index_components[] = {&index_inline,
55 MEMB(index_memb, index_t, DB_INDEX_POOL_SIZE);
57 static process_event_t load_request_event;
58 PROCESS(db_indexer,
"DB Indexer");
61 find_index_api(index_type_t index_type)
65 for(i = 0; i <
sizeof(index_components) /
sizeof(index_components[0]); i++) {
66 if(index_components[i]->type == index_type) {
67 return index_components[i];
83 index_create(index_type_t index_type, relation_t *rel, attribute_t *attr)
85 tuple_id_t cardinality;
89 cardinality = relation_cardinality(rel);
90 if(cardinality == INVALID_TUPLE) {
91 return DB_STORAGE_ERROR;
94 if(attr->domain != DOMAIN_INT && attr->domain != DOMAIN_LONG) {
95 PRINTF(
"DB: Cannot create an index for a non-number attribute!\n");
96 return DB_INDEX_ERROR;
99 api = find_index_api(index_type);
101 PRINTF(
"DB: No API for index type %d\n", (
int)index_type);
102 return DB_INDEX_ERROR;
105 if(attr->index !=
NULL) {
107 PRINTF(
"DB: The attribute %s is already indexed\n", attr->name);
108 return DB_INDEX_ERROR;
113 PRINTF(
"DB: Failed to allocate an index\n");
114 return DB_ALLOCATION_ERROR;
121 index->opaque_data =
NULL;
122 index->descriptor_file[0] =
'\0';
123 index->type = index_type;
125 if(DB_ERROR(api->create(index))) {
127 PRINTF(
"DB: Index-specific creation failed for attribute %s\n", attr->name);
128 return DB_INDEX_ERROR;
134 if(index->descriptor_file[0] !=
'\0' &&
135 DB_ERROR(storage_put_index(index))) {
138 PRINTF(
"DB: Failed to store index data in file \"%s\"\n",
139 index->descriptor_file);
140 return DB_INDEX_ERROR;
143 if(!(api->flags & INDEX_API_INLINE) && cardinality > 0) {
144 PRINTF(
"DB: Created an index for an old relation; issuing a load request\n");
145 index->flags = INDEX_LOAD_NEEDED;
150 PRINTF(
"DB: Index created for attribute %s\n", attr->name);
151 index->flags |= INDEX_READY;
158 index_destroy(index_t *index)
160 if(DB_ERROR(index_release(index)) ||
161 DB_ERROR(index->api->destroy(index))) {
162 return DB_INDEX_ERROR;
169 index_load(relation_t *rel, attribute_t *attr)
174 PRINTF(
"DB: Attempting to load an index over %s.%s\n", rel->name, attr->name);
178 PRINTF(
"DB: No more index objects available\n");
179 return DB_ALLOCATION_ERROR;
182 if(DB_ERROR(storage_get_index(index, rel, attr))) {
183 PRINTF(
"DB: Failed load an index descriptor from storage\n");
185 return DB_INDEX_ERROR;
190 index->opaque_data =
NULL;
192 api = find_index_api(index->type);
194 PRINTF(
"DB: No API for index type %d\n", index->type);
195 return DB_INDEX_ERROR;
200 if(DB_ERROR(api->load(index))) {
201 PRINTF(
"DB: Index-specific load failed\n");
202 return DB_INDEX_ERROR;
207 index->flags = INDEX_READY;
213 index_release(index_t *index)
215 if(DB_ERROR(index->api->release(index))) {
216 return DB_INDEX_ERROR;
219 index->attr->index =
NULL;
227 index_insert(index_t *index, attribute_value_t *value,
230 return index->api->insert(index, value, tuple_id);
234 index_delete(index_t *index, attribute_value_t *value)
236 if(index->flags != INDEX_READY) {
237 return DB_INDEX_ERROR;
240 return index->api->delete(index, value);
244 index_get_iterator(index_iterator_t *iterator, index_t *index,
245 attribute_value_t *min_value,
246 attribute_value_t *max_value)
248 tuple_id_t cardinality;
250 unsigned long max_range;
254 cardinality = relation_cardinality(index->rel);
255 if(cardinality == INVALID_TUPLE) {
256 return DB_STORAGE_ERROR;
259 if(index->flags != INDEX_READY) {
260 return DB_INDEX_ERROR;
263 min = db_value_to_long(min_value);
264 max = db_value_to_long(max_value);
266 range = (
unsigned long)max - min;
281 if(!(index->api->flags & INDEX_API_RANGE_QUERIES)) {
282 PRINTF(
"DB: Range query requested for an index that does not support it\n");
283 max_range = cardinality / DB_INDEX_COST;
284 if(range > max_range) {
285 return DB_INDEX_ERROR;
287 PRINTF(
"DB: Using the index anyway because the range is small enough (%lu <= %lu)\n",
292 iterator->index = index;
293 iterator->min_value = *min_value;
294 iterator->max_value = *max_value;
295 iterator->next_item_no = 0;
297 PRINTF(
"DB: Acquired an index iterator for %s.%s over the range (%ld,%ld)\n",
298 index->rel->name, index->attr->name,
299 min_value->u.long_value, max_value->u.long_value);
305 index_get_next(index_iterator_t *iterator)
310 if(iterator->index ==
NULL) {
312 return INVALID_TUPLE;
315 if((iterator->index->attr->flags & ATTRIBUTE_FLAG_UNIQUE) &&
316 iterator->next_item_no == 1) {
317 min = db_value_to_long(&iterator->min_value);
318 max = db_value_to_long(&iterator->max_value);
324 PRINTF(
"DB: Equivalence search finished\n");
325 return INVALID_TUPLE;
329 return iterator->index->api->get_next(iterator);
333 index_exists(attribute_t *attr)
337 index = (index_t *)attr->index;
338 if(index ==
NULL || index->flags != INDEX_READY) {
346 get_next_index_to_load(
void)
350 for(index =
list_head(indices); index !=
NULL; index = index->next) {
351 if(index->flags & INDEX_LOAD_NEEDED) {
361 static index_t *index;
362 static db_handle_t handle;
363 static tuple_id_t row;
365 attribute_value_t value;
374 index = get_next_index_to_load();
376 PRINTF(
"DB: Request to load an index, but no index is set to be loaded\n");
380 PRINTF(
"DB: Loading the index for %s.%s...\n",
381 index->rel->name, index->attr->name);
385 if(DB_ERROR(db_query(&handle,
"SELECT %s FROM %s;", index->attr->name, index->rel->name))) {
386 index->flags |= INDEX_LOAD_ERROR;
387 index->flags &= ~INDEX_LOAD_NEEDED;
394 result = db_process(&handle);
395 if(DB_ERROR(result)) {
396 PRINTF(
"DB: Index loading failed while processing: %s\n",
397 db_get_result_message(result));
398 index->flags |= INDEX_LOAD_ERROR;
401 if(result == DB_FINISHED) {
405 for(column = 0; column < handle.ncolumns; column++) {
406 if(DB_ERROR(db_get_value(&value, &handle, column))) {
407 index->flags |= INDEX_LOAD_ERROR;
411 if(DB_ERROR(index_insert(index, &value, row))) {
412 index->flags |= INDEX_LOAD_ERROR;
418 PRINTF(
"DB: Loaded %lu rows into the index\n",
419 (
unsigned long)handle.current_row);
422 if(index->flags & INDEX_LOAD_ERROR) {
423 PRINTF(
"DB: Failed to load the index for %s.%s\n",
424 index->rel->name, index->attr->name);
426 index->flags &= ~INDEX_LOAD_NEEDED;
427 index->flags |= INDEX_READY;
Linked list manipulation routines.
Declarations of the main Antelope functions.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
void list_push(list_t list, void *item)
Add an item to the start of the list.
#define PROCESS_BEGIN()
Define the beginning of a process.
The storage interface used by the database.
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
#define NULL
The null pointer.
process_event_t process_alloc_event(void)
Allocate a global event number.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
Database configuration options.
void list_init(list_t list)
Initialize a list.
void * list_head(list_t list)
Get a pointer to the first element of a list.
#define MEMB(name, structure, num)
Declare a memory block.
#define PROCESS_PAUSE()
Yield the process for a short while.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
#define PROCESS(name, strname)
Declare a process.
#define LIST(name)
Declare a linked list.
Definitions for attributes.
void process_start(struct process *p, process_data_t data)
Start a process.
A set of debugging macros.
Memory block allocation routines.