Commit 32321fb2 authored by Lubos Slovak's avatar Lubos Slovak

Code refactoring.

Function main() moved to new file main.c.
Bitset test moved to new files test.[ch].
Tests related to cuckoo table remain in cuckoo-test.[ch].
Removed rwlocks from ck_hash_table (will be replaced by standalone
  item lock implementation).
parent a448fb8e
...@@ -96,18 +96,6 @@ static inline uint8_t NEXT_GENERATION( uint8_t flags ) { ...@@ -96,18 +96,6 @@ static inline uint8_t NEXT_GENERATION( uint8_t flags ) {
return (flags ^ FLAG_GENERATION_BOTH); return (flags ^ FLAG_GENERATION_BOTH);
} }
//static int REHASH_IN_PROGRESS( uint8_t flags ) {
// return ((flags & FLAG_REHASH) != 0);
//}
//
//static void SET_REHASH( uint8_t *flags ) {
// (*flags) |= FLAG_REHASH;
//}
//
//static void UNSET_REHASH( uint8_t *flags ) {
// (*flags) &= ~FLAG_REHASH;
//}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
#define CK_SIZE_NEAREST 1 #define CK_SIZE_NEAREST 1
...@@ -259,6 +247,17 @@ int ck_insert_to_buffer( ck_hash_table *table, ck_hash_table_item *item ) ...@@ -259,6 +247,17 @@ int ck_insert_to_buffer( ck_hash_table *table, ck_hash_table_item *item )
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
static inline uint ck_items_match( const ck_hash_table_item* item,
const char *key, size_t length,
uint generation )
{
return (length == item->key_length
&& (strncmp(item->key, key, length) == 0)
/*&& (GET_GENERATION(item->timestamp) == generation)*/) ? 0 : -1;
}
/*----------------------------------------------------------------------------*/
ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key, ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key,
uint length, uint generation ) uint length, uint generation )
{ {
...@@ -267,8 +266,7 @@ ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key, ...@@ -267,8 +266,7 @@ ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key,
#endif #endif
uint i = 0; uint i = 0;
while (i < table->buf_i while (i < table->buf_i
&& ((strncmp(table->buffer[i].key, key, length) != 0) && ck_items_match(&table->buffer[i], key, length, generation))
|| GET_GENERATION(table->buffer[i].timestamp) != generation))
{ {
++i; ++i;
} }
...@@ -278,7 +276,7 @@ ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key, ...@@ -278,7 +276,7 @@ ck_hash_table_item *ck_find_in_buffer( ck_hash_table *table, const char *key,
} }
assert(strncmp(table->buffer[i].key, key, length) == 0); assert(strncmp(table->buffer[i].key, key, length) == 0);
assert(GET_GENERATION(table->buffer[i].timestamp) == generation); //assert(GET_GENERATION(table->buffer[i].timestamp) == generation);
return &table->buffer[i]; return &table->buffer[i];
} }
...@@ -353,10 +351,6 @@ ck_hash_table *ck_create_table( uint items, void (*dtor_item)( void *value ) ) ...@@ -353,10 +351,6 @@ ck_hash_table *ck_create_table( uint items, void (*dtor_item)( void *value ) )
// initialize rehash mutex // initialize rehash mutex
pthread_mutex_init(&table->mtx_table, NULL); pthread_mutex_init(&table->mtx_table, NULL);
// initialize rwlocks for items
pthread_rwlock_init(&table->rwlock_item1, NULL);
pthread_rwlock_init(&table->rwlock_item2, NULL);
// set the generation to 1 and initialize the universal system // set the generation to 1 and initialize the universal system
CLEAR_FLAGS(&table->generation); CLEAR_FLAGS(&table->generation);
SET_GENERATION1(&table->generation); SET_GENERATION1(&table->generation);
...@@ -434,13 +428,6 @@ void ck_destroy_table( ck_hash_table **table ) ...@@ -434,13 +428,6 @@ void ck_destroy_table( ck_hash_table **table )
// destroy mutex, assuming that here noone will lock the mutex again // destroy mutex, assuming that here noone will lock the mutex again
pthread_mutex_destroy(&(*table)->mtx_table); pthread_mutex_destroy(&(*table)->mtx_table);
// wait for other threads to unlock the rwlocks
while (pthread_rwlock_trywrlock(&(*table)->rwlock_item1) != 0
|| pthread_rwlock_trywrlock(&(*table)->rwlock_item2) != 0 ) {}
// destroy rwlocks, assuming that here noone will lock them again
pthread_rwlock_destroy(&(*table)->rwlock_item1);
pthread_rwlock_destroy(&(*table)->rwlock_item2);
free((*table)->table1); free((*table)->table1);
(*table)->table1 = NULL; (*table)->table1 = NULL;
free((*table)->table2); free((*table)->table2);
...@@ -624,30 +611,28 @@ int ck_hash_item( ck_hash_table *table, ck_hash_table_item *old, ...@@ -624,30 +611,28 @@ int ck_hash_item( ck_hash_table *table, ck_hash_table_item *old,
next_table = TABLE_1; next_table = TABLE_1;
} }
switch (next_table) { if (next_table == TABLE_1) {
case TABLE_1: hash = HASH1(next->key, next->key_length, table->table_size_exp,
hash = HASH1(next->key, next->key_length, table->table_size_exp, NEXT_GENERATION(table->generation));
NEXT_GENERATION(table->generation)); next = &table->table1[hash];
next = &table->table1[hash];
// check if this cell wasn't already used in this item's hashing
// check if this cell wasn't already used in this item's hashing if (ck_check_used2(used1, used_i1, hash) != 0) {
if (ck_check_used2(used1, used_i1, hash) != 0) { next = free;
next = free;
goto moving;
}
break; break;
case TABLE_2: }
hash = HASH2(next->key, next->key_length, table->table_size_exp, } else if (next_table == TABLE_2) {
NEXT_GENERATION(table->generation)); hash = HASH2(next->key, next->key_length, table->table_size_exp,
next = &table->table2[hash]; NEXT_GENERATION(table->generation));
// check if this cell wasn't already used in this item's hashing next = &table->table2[hash];
if (ck_check_used2(used2, used_i2, hash) != 0) { // check if this cell wasn't already used in this item's hashing
next = free; if (ck_check_used2(used2, used_i2, hash) != 0) {
goto moving; next = free;
}
break; break;
default: }
assert(0);
} else {
assert(0);
} }
NEXT_TABLE(next_table); NEXT_TABLE(next_table);
...@@ -655,8 +640,6 @@ int ck_hash_item( ck_hash_table *table, ck_hash_table_item *old, ...@@ -655,8 +640,6 @@ int ck_hash_item( ck_hash_table *table, ck_hash_table_item *old,
assert(next->value == 0); assert(next->value == 0);
moving:
ck_copy_item_contents(moving, next); ck_copy_item_contents(moving, next);
// set the new generation for the inserted item // set the new generation for the inserted item
SET_NEXT_GENERATION(&next->timestamp); SET_NEXT_GENERATION(&next->timestamp);
...@@ -676,14 +659,14 @@ static inline void ck_set_generation_to_items( ck_hash_table_item *items, ...@@ -676,14 +659,14 @@ static inline void ck_set_generation_to_items( ck_hash_table_item *items,
SET_GENERATION(&items[indexes[i]].timestamp, generation); SET_GENERATION(&items[indexes[i]].timestamp, generation);
} }
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
int ck_rehash( ck_hash_table *table ) int ck_rehash( ck_hash_table *table )
{ {
pthread_mutex_lock(&table->mtx_table); fprintf(stderr, "Rehashing not implemented yet!");
return -1;
//fprintf(stderr, "Rehashing not implemented yet!"); pthread_mutex_lock(&table->mtx_table);
// we already have functions for the next generation, begin rehashing // we already have functions for the next generation, begin rehashing
// we wil use the last item in the buffer as the old cell // we wil use the last item in the buffer as the old cell
...@@ -700,6 +683,7 @@ int ck_rehash( ck_hash_table *table ) ...@@ -700,6 +683,7 @@ int ck_rehash( ck_hash_table *table )
continue; continue;
} }
// otherwise copy the item for rehashing // otherwise copy the item for rehashing
ck_copy_item_contents(&table->table1[rehashed], old); ck_copy_item_contents(&table->table1[rehashed], old);
// clear the place so that this item will not get rehashed again // clear the place so that this item will not get rehashed again
ck_clear_item(&table->table1[rehashed]); ck_clear_item(&table->table1[rehashed]);
...@@ -723,26 +707,6 @@ int ck_rehash( ck_hash_table *table ) ...@@ -723,26 +707,6 @@ int ck_rehash( ck_hash_table *table )
pthread_mutex_unlock(&table->mtx_table); pthread_mutex_unlock(&table->mtx_table);
return 0; return 0;
// // TODO: synchronization!
// // get new function for the next generation
// if (us_next(SWAP_GENERATIONS(&table->generation)) != 0) {
// return -2; // rehashed, but no new functions
// }
//
// return 0;
}
/*----------------------------------------------------------------------------*/
static inline uint ck_items_match( const ck_hash_table_item* item,
const char *key, size_t length,
uint generation )
{
return (length == item->key_length
&& (strncmp(item->key, key, length) == 0)
/*&& (GET_GENERATION(item->timestamp) == generation)*/) ? 0 : -1;
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
......
...@@ -32,11 +32,6 @@ typedef struct { ...@@ -32,11 +32,6 @@ typedef struct {
void (*dtor_item)( void *value ); void (*dtor_item)( void *value );
pthread_mutex_t mtx_table; pthread_mutex_t mtx_table;
unsigned long item1;
unsigned long item2;
pthread_rwlock_t rwlock_item1;
pthread_rwlock_t rwlock_item2;
} ck_hash_table; } ck_hash_table;
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
......
This diff is collapsed.
#ifndef CUCKOO_TEST
#define CUCKOO_TEST
/*----------------------------------------------------------------------------*/
int test_hash_table( char *filename );
int start_server( char *filename );
/*----------------------------------------------------------------------------*/
#endif
#include <stdio.h>
#include "common.h"
#include "cuckoo-test.h"
/*----------------------------------------------------------------------------*/
int main( int argc, char **argv )
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>.\n", argv[0]);
return -1;
}
test_hash_table(argv[1]);
start_server(argv[1]);
return 0;
}
#include "tests.h"
#include "bitset.h"
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
/*----------------------------------------------------------------------------*/
int test_bitset()
{
bitset_t bitset;
uint n = 1048576, i, c, err = 0;
uint *numbers = malloc(n/2 * sizeof(uint));
BITSET_CREATE(&bitset, n);
BITSET_CLEAR(bitset, n);
printf("New bitset created.\n");
// check if empty
for (i = 0; i < n; i++) {
if (BITSET_GET(bitset, i) != 0) {
printf("Bit %u not clear!\n", i);
err++;
}
}
srand(1);
printf("Setting random bits...\n");
// set random bits, but keep track of them
for (i = 0; i < n/2; i++) {
c = rand() % n;
//printf("Setting bit on position %u..\n", c);
numbers[i] = c;
BITSET_SET(bitset, c);
if (!BITSET_ISSET(bitset, c)) {
printf("Bit %u not set successfully!\n", c);
err++;
}
BITSET_UNSET(bitset, c);
}
printf("Testing borders...\n");
// setting bits on the borders
BITSET_SET(bitset, 0);
if (!BITSET_ISSET(bitset, 0)) {
printf("Error setting bit on position 0.\n");
err++;
}
BITSET_UNSET(bitset, 0);
BITSET_SET(bitset, 31);
if (!BITSET_ISSET(bitset, 31)) {
printf("Error setting bit on position 31.\n");
err++;
}
BITSET_UNSET(bitset, 31);
BITSET_SET(bitset, 32);
if (!BITSET_ISSET(bitset, 32)) {
printf("Error setting bit on position 32.\n");
err++;
}
BITSET_UNSET(bitset, 32);
BITSET_SET(bitset, 33);
if (!BITSET_ISSET(bitset, 33)) {
printf("Error setting bit on position 33.\n");
err++;
}
BITSET_UNSET(bitset, 33);
BITSET_SET(bitset, 1048575);
if (!BITSET_ISSET(bitset, 1048575)) {
printf("Error setting bit on position 1048575.\n");
err++;
}
BITSET_UNSET(bitset, 1048575);
// check if empty
for (i = 0; i < n; i++) {
if (BITSET_GET(bitset, i) != 0) {
printf("Bit %u not clear!\n", i);
err++;
}
}
free(numbers);
BITSET_DESTROY(&bitset);
printf("There were %u errors.\n", err);
return 0;
}
#ifndef TESTS
#define TESTS
/*----------------------------------------------------------------------------*/
int test_bitset();
/*----------------------------------------------------------------------------*/
#endif /* TESTS */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment