42#define DEBUG DEBUG_NONE
52static index_api_t *index_components[] = {&index_inline,
56MEMB(index_memb, index_t, DB_INDEX_POOL_SIZE);
58static process_event_t load_request_event;
59PROCESS(db_indexer,
"DB Indexer");
62find_index_api(index_type_t index_type)
67 if(index_components[i]->type == index_type) {
68 return index_components[i];
84index_create(index_type_t index_type, relation_t *rel, attribute_t *attr)
86 tuple_id_t cardinality;
90 cardinality = relation_cardinality(rel);
91 if(cardinality == INVALID_TUPLE) {
92 return DB_STORAGE_ERROR;
95 if(attr->domain != DOMAIN_INT && attr->domain != DOMAIN_LONG) {
96 PRINTF(
"DB: Cannot create an index for a non-number attribute!\n");
97 return DB_INDEX_ERROR;
100 api = find_index_api(index_type);
102 PRINTF(
"DB: No API for index type %d\n", (
int)index_type);
103 return DB_INDEX_ERROR;
106 if(attr->index != NULL) {
108 PRINTF(
"DB: The attribute %s is already indexed\n", attr->name);
109 return DB_INDEX_ERROR;
114 PRINTF(
"DB: Failed to allocate an index\n");
115 return DB_ALLOCATION_ERROR;
122 index->opaque_data = NULL;
123 index->descriptor_file[0] =
'\0';
124 index->type = index_type;
126 if(DB_ERROR(api->create(index))) {
128 PRINTF(
"DB: Index-specific creation failed for attribute %s\n", attr->name);
129 return DB_INDEX_ERROR;
135 if(index->descriptor_file[0] !=
'\0' &&
136 DB_ERROR(storage_put_index(index))) {
137 PRINTF(
"DB: Failed to store index data in file \"%s\"\n",
138 index->descriptor_file);
143 return DB_INDEX_ERROR;
146 if(!(api->flags & INDEX_API_INLINE) && cardinality > 0) {
147 PRINTF(
"DB: Created an index for an old relation; issuing a load request\n");
148 index->flags = INDEX_LOAD_NEEDED;
153 PRINTF(
"DB: Index created for attribute %s\n", attr->name);
154 index->flags |= INDEX_READY;
161index_destroy(index_t *index)
163 if(DB_ERROR(index_release(index)) ||
164 DB_ERROR(index->api->destroy(index))) {
165 return DB_INDEX_ERROR;
172index_load(relation_t *rel, attribute_t *attr)
177 PRINTF(
"DB: Attempting to load an index over %s.%s\n", rel->name, attr->name);
181 PRINTF(
"DB: No more index objects available\n");
182 return DB_ALLOCATION_ERROR;
185 if(DB_ERROR(storage_get_index(index, rel, attr))) {
186 PRINTF(
"DB: Failed load an index descriptor from storage\n");
188 return DB_INDEX_ERROR;
193 index->opaque_data = NULL;
195 api = find_index_api(index->type);
197 PRINTF(
"DB: No API for index type %d\n", index->type);
198 return DB_INDEX_ERROR;
203 if(DB_ERROR(api->load(index))) {
204 PRINTF(
"DB: Index-specific load failed\n");
205 return DB_INDEX_ERROR;
210 index->flags = INDEX_READY;
216index_release(index_t *index)
218 if(DB_ERROR(index->api->release(index))) {
219 return DB_INDEX_ERROR;
222 index->attr->index = NULL;
230index_insert(index_t *index, attribute_value_t *
value,
233 return index->api->insert(index,
value, tuple_id);
237index_delete(index_t *index, attribute_value_t *
value)
239 if(index->flags != INDEX_READY) {
240 return DB_INDEX_ERROR;
243 return index->api->delete(index,
value);
247index_get_iterator(index_iterator_t *iterator, index_t *index,
248 attribute_value_t *min_value,
249 attribute_value_t *max_value)
251 tuple_id_t cardinality;
253 unsigned long max_range;
257 cardinality = relation_cardinality(index->rel);
258 if(cardinality == INVALID_TUPLE) {
259 return DB_STORAGE_ERROR;
262 if(index->flags != INDEX_READY) {
263 return DB_INDEX_ERROR;
266 min = db_value_to_long(min_value);
267 max = db_value_to_long(max_value);
269 range = (
unsigned long)max - min;
284 if(!(index->api->flags & INDEX_API_RANGE_QUERIES)) {
285 PRINTF(
"DB: Range query requested for an index that does not support it\n");
286 max_range = cardinality / DB_INDEX_COST;
287 if(range > max_range) {
288 return DB_INDEX_ERROR;
290 PRINTF(
"DB: Using the index anyway because the range is small enough (%lu <= %lu)\n",
295 iterator->index = index;
296 iterator->min_value = *min_value;
297 iterator->max_value = *max_value;
298 iterator->next_item_no = 0;
300 PRINTF(
"DB: Acquired an index iterator for %s.%s over the range (%ld,%ld)\n",
301 index->rel->name, index->attr->name,
302 min_value->u.long_value, max_value->u.long_value);
308index_get_next(index_iterator_t *iterator)
313 if(iterator->index == NULL) {
315 return INVALID_TUPLE;
318 if((iterator->index->attr->flags & ATTRIBUTE_FLAG_UNIQUE) &&
319 iterator->next_item_no == 1) {
320 min = db_value_to_long(&iterator->min_value);
321 max = db_value_to_long(&iterator->max_value);
327 PRINTF(
"DB: Equivalence search finished\n");
328 return INVALID_TUPLE;
332 return iterator->index->api->get_next(iterator);
336index_exists(attribute_t *attr)
340 index = (index_t *)attr->index;
341 if(index == NULL || index->flags != INDEX_READY) {
349get_next_index_to_load(
void)
353 for(index =
list_head(indices); index != NULL; index = index->next) {
354 if(index->flags & INDEX_LOAD_NEEDED) {
364 static index_t *index;
365 static db_handle_t handle;
366 static tuple_id_t row;
368 attribute_value_t
value;
377 index = get_next_index_to_load();
379 PRINTF(
"DB: Request to load an index, but no index is set to be loaded\n");
383 PRINTF(
"DB: Loading the index for %s.%s...\n",
384 index->rel->name, index->attr->name);
388 if(DB_ERROR(db_query(&handle,
"SELECT %s FROM %s;", index->attr->name, index->rel->name))) {
389 index->flags |= INDEX_LOAD_ERROR;
390 index->flags &= ~INDEX_LOAD_NEEDED;
397 result = db_process(&handle);
398 if(DB_ERROR(result)) {
399 PRINTF(
"DB: Index loading failed while processing: %s\n",
400 db_get_result_message(result));
401 index->flags |= INDEX_LOAD_ERROR;
404 if(result == DB_FINISHED) {
408 for(column = 0; column < handle.ncolumns; column++) {
409 if(DB_ERROR(db_get_value(&
value, &handle, column))) {
410 index->flags |= INDEX_LOAD_ERROR;
414 if(DB_ERROR(index_insert(index, &
value, row))) {
415 index->flags |= INDEX_LOAD_ERROR;
421 PRINTF(
"DB: Loaded %lu rows into the index\n",
422 (
unsigned long)handle.current_row);
425 if(index->flags & INDEX_LOAD_ERROR) {
426 PRINTF(
"DB: Failed to load the index for %s.%s\n",
427 index->rel->name, index->attr->name);
429 index->flags &= ~INDEX_LOAD_NEEDED;
430 index->flags |= INDEX_READY;
Declarations of the main Antelope functions.
Definitions for attributes.
Default definitions of C compiler quirk work-arounds.
#define CC_ARRAY_LENGTH(array)
Counts the number of elements of an array.
Database configuration options.
static int value(int type)
static void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
void list_push(list_t list, void *item)
Add an item to the start of the list.
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
int 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().
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
#define MEMB(name, structure, num)
Declare a memory block.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_PAUSE()
Yield the process for a short while.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
process_event_t process_alloc_event(void)
Allocate a global event number.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
#define PROCESS_END()
Define the end of a process.
void process_start(struct process *p, process_data_t data)
Start a process.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Linked list manipulation routines.
Memory block allocation routines.
The storage interface used by the database.
A set of debugging macros for the IP stack.