Add timestamp field to blerg record
authorChip Black <bytex64@bytex64.net>
Fri, 24 Dec 2010 08:18:29 +0000 (02:18 -0600)
committerChip Black <bytex64@bytex64.net>
Fri, 24 Dec 2010 08:18:29 +0000 (02:18 -0600)
database.c
database.h
http_blerg.c

index 4b16d32..1e33811 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -19,7 +20,7 @@ uint64_t blerg_get_record_count(struct blerg *blerg) {
        return count;
 }
 
        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);
 uint64_t blerg_increment_record_count(struct blerg *blerg) {
        uint64_t count;
        flock(blerg->meta_fd, LOCK_EX);
@@ -51,7 +52,7 @@ int blerg_segment_switch(struct blerg *blerg, int new_segment) {
 
        blerg_segment_close(blerg);
 
 
        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) {
        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) {
@@ -76,7 +77,7 @@ int blerg_segment_switch(struct blerg *blerg, int new_segment) {
                goto open_failed_index_mmap;
        }
 
                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);
        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);
@@ -144,13 +145,13 @@ struct blerg *blerg_open(const char *name) {
        blerg->index = NULL;
        blerg->data = NULL;
 
        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);
 
        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) {
        snprintf(filename, 512, "%s/meta", blerg->base_path);
        blerg->meta_fd = open(filename, O_RDWR | O_CREAT, 0600);
        if (blerg->meta_fd == -1) {
@@ -170,7 +171,7 @@ struct blerg *blerg_open(const char *name) {
                goto open_failed_meta_mmap;
        }
 
                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");
        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");
@@ -219,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;
 
                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);
        FILE *datafile = fdopen(dup(blerg->data_fd), "a");
        fseek(datafile, 0, SEEK_END);
        int curpos = ftell(datafile);
@@ -230,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");
                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;
                }
                        ftruncate(blerg->data_fd, curpos);
                        return -1;
                }
@@ -239,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].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);
 
 
        tag_scan(blerg->name, data, len, record);
 
@@ -267,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) {
        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;
                struct stat st;
                fstat(blerg->data_fd, &st);
                blerg->data_size = st.st_size;
@@ -297,3 +299,22 @@ int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) {
 
        return 1;
 }
 
        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;
+}
index ed9707d..0ef1c7d 100644 (file)
@@ -2,11 +2,13 @@
 #define _DATABASE_H
 
 #include <stdint.h>
 #define _DATABASE_H
 
 #include <stdint.h>
+#include <time.h>
 
 struct record {
        uint32_t offset;
        uint16_t length;
        uint16_t flags;
 
 struct record {
        uint32_t offset;
        uint16_t length;
        uint16_t flags;
+       time_t timestamp;
 };
 
 struct meta {
 };
 
 struct meta {
index 9078f53..845f312 100644 (file)
@@ -119,6 +119,8 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
        yajl_gen_string(gs->g, "record", 6);
        snprintf(number, 21, "%llu", gs->entries[gs->i]);
        yajl_gen_string(gs->g, number, strlen(number));
        yajl_gen_string(gs->g, "record", 6);
        snprintf(number, 21, "%llu", gs->entries[gs->i]);
        yajl_gen_string(gs->g, number, strlen(number));
+       yajl_gen_string(gs->g, "timestamp", 9);
+       yajl_gen_integer(gs->g, blerg_get_timestamp(gs->b, gs->entries[gs->i]));
        yajl_gen_string(gs->g, "data", 4);
        yajl_gen_string(gs->g, data, len);
        yajl_gen_map_close(gs->g);
        yajl_gen_string(gs->g, "data", 4);
        yajl_gen_string(gs->g, data, len);
        yajl_gen_map_close(gs->g);