X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database%2Fdatabase.c;h=91e6aa22f9b0e9e4c04c0e4b5968f5deb9b71f45;hb=0647b76522be38edfe4b1fbae32addbbf8bb4128;hp=52ebe8c7eeabca1c29be40d8b84b55bb29d2dace;hpb=1b5012f54151382cddfe5ead3475f993ea947273;p=blerg.git diff --git a/database/database.c b/database/database.c index 52ebe8c..91e6aa2 100644 --- a/database/database.c +++ b/database/database.c @@ -13,6 +13,7 @@ #include #include #include "database.h" +#include "configuration.h" #include "subscription.h" #include "util.h" #include "config.h" @@ -23,6 +24,13 @@ return r; \ } +int blerg_init() { + if (!blerg_configuration_init()) { + return 0; + } + return 1; +} + uint64_t blerg_get_record_count(struct blerg *blerg) { uint64_t count; flock(blerg->meta_fd, LOCK_SH); @@ -234,15 +242,15 @@ int blerg_close(struct blerg *blerg) { return 1; } -int blerg_store(struct blerg *blerg, const char *data, int len) { +uint64_t blerg_store(struct blerg *blerg, const char *data, int length) { struct stat st; int n; - CHECK_VALID_BLERG(-1) + CHECK_VALID_BLERG(BLERG_INVALID_RECORD) - if (len > MAX_RECORD_SIZE || len <= 0) { - fprintf(stderr, "len out of bounds\n"); - return -1; + if (length > MAX_RECORD_SIZE || length <= 0) { + fprintf(stderr, "length out of bounds\n"); + return BLERG_INVALID_RECORD; } flock(blerg->index_fd, LOCK_EX); @@ -251,7 +259,7 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { uint64_t record = blerg_get_record_count(blerg); if (record == -1) { /* Intentional signed-unsigned coercion */ fprintf(stderr, "Could not find free record\n"); - return -1; + return BLERG_INVALID_RECORD; } int segment = record / RECORDS_PER_SEGMENT; if (segment != blerg->current_segment) @@ -263,18 +271,18 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { int curpos = st.st_size; /* Write data to the data log */ - n = write(blerg->data_fd, data, len); - if (n < len) { + n = write(blerg->data_fd, data, length); + if (n < length) { perror("Could not write data"); /* Truncate anything we may have written */ ftruncate(blerg->data_fd, curpos); - return -1; + return BLERG_INVALID_RECORD; } /* Update the index */ blerg->index[seg_rec].flags = 0x0001; blerg->index[seg_rec].offset = curpos; - blerg->index[seg_rec].length = len; + blerg->index[seg_rec].length = length; blerg->index[seg_rec].timestamp = time(NULL); /* And finally increment the record count */ @@ -285,16 +293,16 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { if (!blerg_get_mute(blerg)) { /* Now do those dirty microblogging deeds */ - tag_scan(blerg->name, data, len, record); + tag_scan(blerg->name, data, length, record); subscription_notify(blerg->name, record); } return record; } -int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) { +int blerg_fetch(struct blerg *blerg, uint64_t record, char **data, int *length) { CHECK_VALID_BLERG(0) - if (record < 0 || record >= blerg_get_record_count(blerg)) { + if (record == BLERG_INVALID_RECORD || record >= blerg_get_record_count(blerg)) { fprintf(stderr, "Invalid record\n"); return 0; } @@ -340,9 +348,9 @@ int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) { return 1; } -time_t blerg_get_timestamp(struct blerg *blerg, int record) { +time_t blerg_get_timestamp(struct blerg *blerg, uint64_t record) { CHECK_VALID_BLERG(0) - if (record < 0 || record >= blerg_get_record_count(blerg)) { + if (record == BLERG_INVALID_RECORD || record >= blerg_get_record_count(blerg)) { fprintf(stderr, "Invalid record\n"); return 0; }