Cleaned up table engine to make sure index gets updated correctly

This commit is contained in:
Mike Dvorkin
2010-08-14 20:38:40 -07:00
parent 7ecace633f
commit f8ba6a911b
2 changed files with 128 additions and 81 deletions

View File

@@ -7,7 +7,8 @@
/* /*
** Initialize the table by alloocating necessary memory chunks. ** Initialize the table by alloocating necessary memory chunks.
*/ */
PTable pit_table_initialize(int record_size, int flags) { PTable pit_table_initialize(int record_size, int flags)
{
PTable pt = calloc(1, sizeof(Table)); PTable pt = calloc(1, sizeof(Table));
pt->flags = flags; pt->flags = flags;
@@ -16,6 +17,7 @@ PTable pit_table_initialize(int record_size, int flags) {
pt->number_of_records = 0; pt->number_of_records = 0;
pt->auto_increment = 0; pt->auto_increment = 0;
pt->current = 0; pt->current = 0;
pt->index_size = TABLE_INCREMENT;
pt->slots = calloc(TABLE_INCREMENT, pt->record_size); pt->slots = calloc(TABLE_INCREMENT, pt->record_size);
pt->index = calloc(TABLE_INCREMENT, sizeof(char *)); pt->index = calloc(TABLE_INCREMENT, sizeof(char *));
@@ -25,14 +27,16 @@ PTable pit_table_initialize(int record_size, int flags) {
/* /*
** Return the address of next avaiable slot within pt->slots chunk. ** Return the address of next avaiable slot within pt->slots chunk.
*/ */
static char *table_available_slot(PTable pt) { static char *table_available_slot(PTable pt)
{
return pt->slots + pt->number_of_records * pt->record_size; return pt->slots + pt->number_of_records * pt->record_size;
} }
/* /*
** Return the address of last stored record. ** Return the address of last stored record.
*/ */
static char *table_last_record(PTable pt) { static char *table_last_record(PTable pt)
{
if (pt->number_of_records == 0) { if (pt->number_of_records == 0) {
return pt->slots; return pt->slots;
} else { } else {
@@ -43,28 +47,48 @@ static char *table_last_record(PTable pt) {
/* /*
** Return the address of next available pointer within pt->index chunk. ** Return the address of next available pointer within pt->index chunk.
*/ */
static char **table_available_index(PTable pt) { static char **table_available_index(PTable pt)
return pt->index + pt->auto_increment; {
return HAS_ID(pt) ? (pt->index + pt->auto_increment) : NULL;
} }
/* /*
** Return the address of last pointer within pt->index chunk. ** Return the address of last pointer within pt->index chunk.
*/ */
static char **table_last_index(PTable pt) { static char **table_last_index(PTable pt)
if (pt->auto_increment == 0) { {
return pt->index; if (HAS_ID(pt)) {
} else { if (pt->auto_increment == 0) {
return table_last_index(pt) - sizeof(char *); return pt->index;
} else {
return table_last_index(pt) - sizeof(char *);
}
} }
return NULL;
} }
/* /*
** Extend the table involves three steps. First, we reallocate pt->slots ** Re-allocate the index and set newly added memory chunk to 0.
** chunk adding TABLE_INCREMENT empty slots. Then we add the same number
** of empty indices to the the pt->index chunk. Finally, we adjust
** existing indices to make them point to reallocated record slots.
*/ */
static PTable table_extend(PTable pt) { static char **table_extend_index(PTable pt)
{
if (HAS_ID(pt)) {
pt->index_size += TABLE_INCREMENT;
pt->index = realloc(pt->index, pt->index_size * sizeof(char *));
memset(table_available_index(pt), 0, TABLE_INCREMENT * sizeof(char *));
return pt->index;
}
return NULL;
}
/*
** Extend the table involves three steps. First, reallocate pt->slots
** chunk adding TABLE_INCREMENT empty slots. Then extend the index.
** Finally, adjust existing indices to point to reallocated record slots.
*/
static PTable table_extend(PTable pt)
{
register int i; register int i;
register char **pi; register char **pi;
@@ -74,17 +98,16 @@ static PTable table_extend(PTable pt) {
*/ */
pt->slots = realloc(pt->slots, pt->number_of_slots * pt->record_size); pt->slots = realloc(pt->slots, pt->number_of_slots * pt->record_size);
memset(table_available_slot(pt), 0, TABLE_INCREMENT * pt->record_size); memset(table_available_slot(pt), 0, TABLE_INCREMENT * pt->record_size);
/*
** Re-allocate the index and set newly added memory chunk to 0. if (HAS_ID(pt)) {
*/ /*
pt->index = realloc(pt->index, pt->number_of_slots * sizeof(char *)); ** Extend the index and make its entries point to reallocatted records.
memset(table_available_index(pt), 0, TABLE_INCREMENT * sizeof(char *)); */
/* table_extend_index(pt);
** Reassign index entries to point to the re-allocatted records. for (i = 0, pi = pt->index; i < pt->auto_increment; i++, pi++) {
*/ if (*pi != NULL) {
for (i = 0, pi = pt->index; i < pt->auto_increment; i++, pi++) { *pi = pt->slots + i * pt->record_size;
if (*pi != NULL) { }
*pi = pt->slots + i * pt->record_size;
} }
} }
return pt; return pt;
@@ -93,29 +116,30 @@ static PTable table_extend(PTable pt) {
/* /*
** Find a record by id and return its address. ** Find a record by id and return its address.
*/ */
char *pit_table_find(PTable pt, int id) { char *pit_table_find(PTable pt, int id)
// TODO: retrn NULL (or raise?) if table doesn't have id. {
if (pt->number_of_records == 0 || id <= 0 || id > pt->auto_increment) { if (HAS_ID(pt)) {
return NULL; if (pt->number_of_records == 0 || id <= 0 || id > pt->auto_increment) {
} else { return NULL;
return *(pt->index + id - 1); } else {
return *(pt->index + id - 1);
}
} }
return NULL;
} }
/* /*
** Delete a record by its ID. Return the address of deleted record or NULL ** Delete a record by its id. Return the address of deleted record or NULL
** if the record was not found. ** if the record was not found.
*/ */
char *pit_table_delete(PTable pt, int id) { char *pit_table_delete(PTable pt, int id)
// TODO: return NULL (or die?) if table doesn't have id. {
register char *pr = (char *)pit_table_find(pt, id); register char *pr = (char *)pit_table_find(pt, id);
if (pr) { if (pr) {
register int i;
register char **pi;
register char *last = table_last_record(pt); register char *last = table_last_record(pt);
/* /*
** Overwrite current record by shifting over remaining records ** Overwrite current record by shifting over remaining slots.
*/ */
if (pr != last) { if (pr != last) {
memmove(pr, pr + pt->record_size, last - pr); memmove(pr, pr + pt->record_size, last - pr);
@@ -124,15 +148,20 @@ char *pit_table_delete(PTable pt, int id) {
** Set the slot occupied by the last record to zero. ** Set the slot occupied by the last record to zero.
*/ */
memset(last, 0, pt->record_size); memset(last, 0, pt->record_size);
/*
** Set current record pointer to NULL, then update the rest of
** the index to point to the shifted records.
*/
pi = pt->index + id - 1;
*pi++ = NULL;
for (i = id; i < pt->auto_increment; i++) { if (HAS_ID(pt)) {
*pi++ -= pt->record_size; register int i;
register char **pi;
/*
** Set current record pointer to NULL, then update the rest of
** the index to point to the shifted records.
*/
pi = pt->index + id - 1;
*pi++ = NULL;
for (i = id; i < pt->auto_increment; i++) {
*pi++ -= pt->record_size;
}
} }
pt->number_of_records--; pt->number_of_records--;
} }
@@ -144,58 +173,70 @@ char *pit_table_delete(PTable pt, int id) {
** Insert a record and return its address. The table gets extended ** Insert a record and return its address. The table gets extended
** as necessary. ** as necessary.
*/ */
char *pit_table_insert(PTable pt, char *record) { char *pit_table_insert(PTable pt, char *record)
register char **pi; {
register char *pr;
register time_t now = time(NULL); register time_t now = time(NULL);
if (pt->number_of_records >= pt->number_of_slots) { if (pt->number_of_records >= pt->number_of_slots) {
pt = table_extend(pt); pt = table_extend(pt);
} else {
if (HAS_ID(pt) && pt->auto_increment >= pt->index_size) {
table_extend_index(pt);
}
} }
pi = table_available_index(pt); pr = table_available_slot(pt);
*pi = table_available_slot(pt); memmove(pr, record, pt->record_size);
memmove(table_available_slot(pt), record, pt->record_size);
pt->number_of_records++; pt->number_of_records++;
pt->auto_increment++;
/* if (HAS_ID(pt)) {
** Update record id if the table has primary key. The id must be the first /*
** record field of type "unsigned long". ** Save current slot address in the index.
*/ */
if (pt->flags & TABLE_HAS_ID) { register char **pi = table_available_index(pt);
*pi = pr;
/*
** Update record id if the table has primary key. The id must be the first
** record field of type "unsigned long".
*/
pt->auto_increment++;
*(int *)*pi = pt->auto_increment; *(int *)*pi = pt->auto_increment;
} }
/* /*
** Update created_at and/or updated_at which must be last one or two record ** Update created_at and/or updated_at which must be last one or two record
** fields of type "time_t". ** fields of type "time_t".
*/ */
if (pt->flags & TABLE_HAS_CREATED_AT || pt->flags & TABLE_HAS_UPDATED_AT) { if (HAS_CREATED_AT(pt) || HAS_UPDATED_AT(pt)) {
*(time_t *)(*pi + pt->record_size - sizeof(time_t)) = now; *(time_t *)(pr + pt->record_size - sizeof(time_t)) = now;
} }
if (pt->flags & TABLE_HAS_CREATED_AT && pt->flags & TABLE_HAS_UPDATED_AT) { if (HAS_CREATED_AT(pt) && HAS_UPDATED_AT(pt)) {
*(time_t *)(*pi + pt->record_size - sizeof(time_t) * 2) = now; *(time_t *)(pr + pt->record_size - sizeof(time_t) * 2) = now;
} }
return *pi; return pr;
} }
/* /*
** Find current record. ** Find current record.
*/ */
char *pit_table_current(PTable pt) { char *pit_table_current(PTable pt)
{
return pit_table_find(pt, pt->current); return pit_table_find(pt, pt->current);
} }
/* /*
** Set current record as indicated by the id, then find and return it. ** Set current record as indicated by the id, then find and return it.
*/ */
char *pit_table_mark(PTable pt, int id) { char *pit_table_mark(PTable pt, int id)
{
return pit_table_find(pt, pt->current = id); return pit_table_find(pt, pt->current = id);
} }
/* /*
** Release pt->slots and pt->index memory chunks, then free the table itself. ** Release pt->slots and pt->index memory chunks, then free the table itself.
*/ */
void pit_table_free(PTable pt) { void pit_table_free(PTable pt)
{
if (pt) { if (pt) {
if (pt->index) { if (pt->index) {
free(pt->index); free(pt->index);
@@ -211,13 +252,14 @@ void pit_table_free(PTable pt) {
** Save the contents of the table to file. The file handle should be ** Save the contents of the table to file. The file handle should be
** open with for writing. ** open with for writing.
*/ */
int pit_table_save(FILE *file, PTable pt) { int pit_table_save(FILE *file, PTable pt)
{
register int written = 0; register int written = 0;
/* /*
** Save table header data: flags, record_size, number_of_slots, ** Save table header data: flags, record_size, number_of_slots,
** number_of_records, and auto_increment, current. ** number_of_records, auto_increment, current, and index_size.
*/ */
written += fwrite(pt, sizeof(int), 6, file); written += fwrite(pt, sizeof(int), 7, file);
/* /*
** Save the records. Note that we save the actual (not allocated) data. ** Save the records. Note that we save the actual (not allocated) data.
*/ */
@@ -229,29 +271,32 @@ int pit_table_save(FILE *file, PTable pt) {
** Load the contents of the table from file. The file handle should be ** Load the contents of the table from file. The file handle should be
** open with for reading. ** open with for reading.
*/ */
PTable pit_table_load(FILE *file) { PTable pit_table_load(FILE *file)
{
PTable pt; PTable pt;
register int read = 0; register int read = 0;
register int i; register int i;
char *pr, **pi; char *pr, **pi = NULL;
pt = calloc(1, sizeof(Table)); pt = calloc(1, sizeof(Table));
/* /*
** First read the header. ** First read the header.
*/ */
read += fread(pt, sizeof(int), 6, file); read += fread(pt, sizeof(int), 7, file);
/* /*
** Now allocate slots and index based on the original number of slots. ** Now allocate slots and index based on the original number of slots.
*/ */
/*** printf("Allocating %d slots\n", pt->number_of_slots); ***/ /*** printf("Allocating %d slots\n", pt->number_of_slots); ***/
pt->slots = pr = calloc(pt->number_of_slots, pt->record_size); pt->slots = pr = calloc(pt->number_of_slots, pt->record_size);
pt->index = pi = calloc(pt->number_of_slots, sizeof(char *)); if (HAS_ID(pt)) {
pt->index = pi = calloc(pt->index_size, sizeof(char *));
}
/* /*
** Now read the records into the slots and rebuild the index if the ** Now read the records into the slots and rebuild the index if the
** table has primary key. ** table has primary key.
*/ */
read += fread(pt->slots, pt->record_size, pt->number_of_records, file); read += fread(pt->slots, pt->record_size, pt->number_of_records, file);
if (pt->flags & TABLE_HAS_ID) { if (HAS_ID(pt)) {
for(i = 1; i <= pt->number_of_slots; i++, pi++) { for(i = 1; i <= pt->number_of_slots; i++, pi++) {
if ((int)*pr == i) { if ((int)*pr == i) {
*pi = pr; *pi = pr;
@@ -275,8 +320,8 @@ typedef struct {
void dump(Record *prec) { void dump(Record *prec) {
if (prec) { if (prec) {
printf("(%08lX) id: %08d, value: %d, created_at: %d, updated_at: %d\n", printf("(%08lX) id: %08d, value: %d, created_at: %ld, updated_at: %ld\n",
(int)prec, prec->id, prec->value, prec->created_at, prec->updated_at); (unsigned long)prec, prec->id, prec->value, (time_t)prec->created_at, (time_t)prec->updated_at);
} else { } else {
printf("(NULL)\n"); printf("(NULL)\n");
} }
@@ -316,11 +361,7 @@ int main() {
pit_table_mark(pt, prec->id); pit_table_mark(pt, prec->id);
printf("current: %d\n", pt->current); printf("current: %d\n", pt->current);
// for (i = 20; i < total; i++) { printf("Deleting %d\n", 1);
// printf("Deleting %d\n", i + 1);
// prec = (Record *)pit_table_delete(pt, i + 1);
// }
printf("Deleting %d\n", 1L);
prec = (Record *)pit_table_delete(pt, 1); prec = (Record *)pit_table_delete(pt, 1);
printf("current: %d\n", pt->current); printf("current: %d\n", pt->current);
dump_all(pt); dump_all(pt);

View File

@@ -6,12 +6,18 @@
#define TABLE_HAS_UPDATED_AT 4 #define TABLE_HAS_UPDATED_AT 4
#define TABLE_HAS_TIMESTAMPS (TABLE_HAS_CREATED_AT | TABLE_HAS_UPDATED_AT) #define TABLE_HAS_TIMESTAMPS (TABLE_HAS_CREATED_AT | TABLE_HAS_UPDATED_AT)
#define HAS_ID(pt) (pt && (pt->flags & TABLE_HAS_ID))
#define HAS_CREATED_AT(pt) (pt && (pt->flags & TABLE_HAS_CREATED_AT))
#define HAS_UPDATED_AT(pt) (pt && (pt->flags & TABLE_HAS_UPDATED_AT))
#define HAS_TIMESTAMPS(pt) (HAS_CREATED_AT(pt) && HAS_UPDATED_AT(pt))
typedef struct _Table { typedef struct _Table {
int flags; /* Bit mask with table flags. */ int flags; /* Bit mask with table flags. */
int record_size; /* Record size in bytes; all records are of fixed size. */ int record_size; /* Record size in bytes; all records are of fixed size. */
int number_of_slots; /* Number of slots allocated, each slot is 'record_size' long. */ int number_of_slots; /* Number of slots allocated, each slot is 'record_size' long. */
int number_of_records; /* Number of records currently stored in slots. */ int number_of_records; /* Number of records currently stored in slots. */
int auto_increment; /* Current value of record id. */ int auto_increment; /* Current value of record id. */
int index_size; /* The size of the index. */
int current; /* The id of currently selected record, one per table. */ int current; /* The id of currently selected record, one per table. */
char *slots; /* Memory chunk to store records; compacted when a record gets deleted (no holes). */ char *slots; /* Memory chunk to store records; compacted when a record gets deleted (no holes). */
char **index; /* Memory chunk to store pointers to individual records, holes for deleted record IDs. */ char **index; /* Memory chunk to store pointers to individual records, holes for deleted record IDs. */