X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database.c;h=1e33811c1d29f13f638ff603fa2481f32f3c5a29;hb=da5af7e043fdf8992ae30ac19f33446f8133f178;hp=c3ac954f512423b5433e6877ed7a8ad785f5daf3;hpb=650b60caf46762f4a16ed9f3f522e6a2ada7ffaf;p=blerg.git diff --git a/database.c b/database.c index c3ac954..1e33811 100644 --- a/database.c +++ b/database.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -11,8 +12,6 @@ #include "database.h" #include "config.h" -#define RECORDS_PER_SEGMENT 65536 - uint64_t blerg_get_record_count(struct blerg *blerg) { uint64_t count; flock(blerg->meta_fd, LOCK_SH); @@ -21,7 +20,7 @@ uint64_t blerg_get_record_count(struct blerg *blerg) { return count; } -// Returns last usable record +/* Returns last usable record */ uint64_t blerg_increment_record_count(struct blerg *blerg) { uint64_t count; flock(blerg->meta_fd, LOCK_EX); @@ -53,7 +52,7 @@ int blerg_segment_switch(struct blerg *blerg, int new_segment) { blerg_segment_close(blerg); - // Load and map the index + /* Load and map the index */ snprintf(filename, 512, "%s/index%d", blerg->base_path, new_segment); blerg->index_fd = open(filename, O_RDWR | O_CREAT, 0600); if (blerg->index_fd == -1) { @@ -78,7 +77,7 @@ int blerg_segment_switch(struct blerg *blerg, int new_segment) { goto open_failed_index_mmap; } - // Load data file + /* Load data file */ sprintf(filename, "%s/data%d", blerg->base_path, new_segment); blerg->data_fd = open(filename, O_RDWR | O_APPEND | O_CREAT, 0600); fstat(blerg->data_fd, &st); @@ -101,7 +100,7 @@ int blerg_segment_switch(struct blerg *blerg, int new_segment) { open_failed_data_mmap: close(blerg->data_fd); open_failed_data_open: - munmap((void *)blerg->index, sizeof(65536 * sizeof(struct record))); + munmap((void *)blerg->index, sizeof(RECORDS_PER_SEGMENT * sizeof(struct record))); open_failed_index_mmap: close(blerg->index_fd); open_failed_index_open: @@ -146,13 +145,13 @@ struct blerg *blerg_open(const char *name) { blerg->index = NULL; blerg->data = NULL; - // Make the directory if it doesn't exist + /* Make the directory if it doesn't exist */ blerg->base_path = malloc(512); snprintf(blerg->base_path, 512, "%s/%s", DATA_PATH, name); if (access(blerg->base_path, F_OK) == -1) mkdir(blerg->base_path, 0755); - // Open and map metadata + /* Open and map metadata */ snprintf(filename, 512, "%s/meta", blerg->base_path); blerg->meta_fd = open(filename, O_RDWR | O_CREAT, 0600); if (blerg->meta_fd == -1) { @@ -172,7 +171,7 @@ struct blerg *blerg_open(const char *name) { goto open_failed_meta_mmap; } - // Open and map index and data for the current segment + /* Open and map index and data for the current segment */ blerg->current_segment = blerg_get_record_count(blerg) / RECORDS_PER_SEGMENT; if (!blerg_segment_switch(blerg, blerg->current_segment)) { fprintf(stderr, "Could not switch segment\n"); @@ -203,7 +202,7 @@ int blerg_close(struct blerg *blerg) { } int blerg_store(struct blerg *blerg, const char *data, int len) { - if (len > 65536) { + if (len > MAX_RECORD_SIZE) { printf("len > 64K\n"); return -1; } @@ -221,7 +220,7 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { blerg_segment_switch(blerg, segment); int seg_rec = record % RECORDS_PER_SEGMENT; - // Get the position for the new data + /* Get the position for the new data */ FILE *datafile = fdopen(dup(blerg->data_fd), "a"); fseek(datafile, 0, SEEK_END); int curpos = ftell(datafile); @@ -232,7 +231,7 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { int n = write(blerg->data_fd, data + bytes, len); if (n == -1) { perror("Could not write data"); - // Truncate anything we may have written + /* Truncate anything we may have written */ ftruncate(blerg->data_fd, curpos); return -1; } @@ -241,6 +240,7 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { blerg->index[seg_rec].flags = 0x0001; blerg->index[seg_rec].offset = curpos; blerg->index[seg_rec].length = len; + blerg->index[seg_rec].timestamp = time(NULL); tag_scan(blerg->name, data, len, record); @@ -269,8 +269,8 @@ int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) { int rec_offset = blerg->index[seg_rec].offset; int rec_length = blerg->index[seg_rec].length; if (rec_offset >= blerg->data_size) { - // We're accessing an out-of-bounds record in our mmap. - // Recheck size and remap. + /* We're accessing an out-of-bounds record in our mmap. + Recheck size and remap. */ struct stat st; fstat(blerg->data_fd, &st); blerg->data_size = st.st_size; @@ -299,3 +299,22 @@ int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) { return 1; } + +time_t blerg_get_timestamp(struct blerg *blerg, int record) { + if (record < 0) { + printf("Invalid record\n"); + return 0; + } + + int segment = record / RECORDS_PER_SEGMENT; + if (segment != blerg->current_segment) + blerg_segment_switch(blerg, segment); + int seg_rec = record % RECORDS_PER_SEGMENT; + + if ((blerg->index[seg_rec].flags & 0x1) == 0) { + printf("Invalid record\n"); + return 0; + } + + return blerg->index[seg_rec].timestamp; +}