44 #define DEBUG DEBUG_NONE
47 static db_result_t create(index_t *);
48 static db_result_t destroy(index_t *);
49 static db_result_t load(index_t *);
50 static db_result_t release(index_t *);
51 static db_result_t insert(index_t *, attribute_value_t *, tuple_id_t);
52 static db_result_t
delete(index_t *, attribute_value_t *);
53 static tuple_id_t get_next(index_iterator_t *);
55 index_api_t index_memhash = {
69 attribute_value_t value;
71 typedef struct hash_item hash_item_t;
73 typedef hash_item_t hash_map_t[DB_MEMHASH_TABLE_SIZE];
75 MEMB(hash_map_memb, hash_map_t, DB_MEMHASH_INDEX_LIMIT);
78 calculate_hash(attribute_value_t *value)
80 unsigned char *cp, *end;
83 cp = (
unsigned char *)value;
84 end = cp +
sizeof(*value);
88 hash_value = hash_value * 33 + *cp++;
91 return hash_value % DB_MEMHASH_TABLE_SIZE;
95 create(index_t *index)
100 PRINTF(
"Creating a memory-resident hash map index\n");
103 if(hash_map ==
NULL) {
104 return DB_ALLOCATION_ERROR;
107 for(i = 0; i < DB_MEMHASH_TABLE_SIZE; i++) {
108 hash_map[i]->tuple_id = INVALID_TUPLE;
111 index->opaque_data = hash_map;
117 destroy(index_t *index)
119 memb_free(&hash_map_memb, index->opaque_data);
127 return create(index);
131 release(index_t *index)
133 return destroy(index);
137 insert(index_t *index, attribute_value_t *value, tuple_id_t tuple_id)
139 hash_map_t *hash_map;
142 hash_map = index->opaque_data;
144 hash_value = calculate_hash(value);
145 hash_map[hash_value]->tuple_id = tuple_id;
146 hash_map[hash_value]->value = *value;
148 PRINTF(
"DB: Inserted value %ld into the hash table\n", VALUE_LONG(value));
154 delete(index_t *index, attribute_value_t *value)
156 hash_map_t *hash_map;
159 hash_map = index->opaque_data;
161 hash_value = calculate_hash(value);
162 if(memcmp(&hash_map[hash_value]->value, value,
sizeof(*value)) != 0) {
163 return DB_INDEX_ERROR;
166 hash_map[hash_value]->tuple_id = INVALID_TUPLE;
171 get_next(index_iterator_t *iterator)
173 hash_map_t *hash_map;
176 if(iterator->next_item_no == 1) {
178 return INVALID_TUPLE;
181 hash_map = iterator->index->opaque_data;
183 hash_value = calculate_hash(&iterator->min_value);
184 if(memcmp(&hash_map[hash_value]->value, &iterator->min_value,
sizeof(iterator->min_value)) != 0) {
185 return INVALID_TUPLE;
188 iterator->next_item_no++;
190 PRINTF(
"DB: Found value %ld in the hash table\n",
191 VALUE_LONG(&iterator->min_value));
193 return hash_map[hash_value]->tuple_id;
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.
Database configuration options.
#define MEMB(name, structure, num)
Declare a memory block.
A set of debugging macros.
Memory block allocation routines.