X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database%2Fdatabase.c;h=033875b55eb60aa45648f6fcfe86639ada4061ad;hb=c38b2e192d8de8ef38389cc8071881464fff99bb;hp=c0ec591553b8ff0170ce0e6d4d71c59d82da345a;hpb=5b9a1c597b87485253a11339067b60d08534f8b0;p=blerg.git diff --git a/database/database.c b/database/database.c index c0ec591..033875b 100644 --- a/database/database.c +++ b/database/database.c @@ -13,10 +13,25 @@ #include #include #include "database.h" +#include "configuration.h" #include "subscription.h" +#include "tags.h" #include "util.h" #include "config.h" +#define CHECK_VALID_BLERG(r) \ + if (blerg == NULL) { \ + fprintf(stderr, "Invalid struct blerg\n"); \ + 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); @@ -51,16 +66,16 @@ int blerg_remap_data(struct blerg *blerg) { if (blerg->data != NULL) munmap(blerg->data, blerg->data_size); fstat(blerg->data_fd, &st); - if (st.st_size == 0) { + blerg->data_size = st.st_size; + if (blerg->data_size == 0) { /* Can't map an empty data file. */ return 1; } - blerg->data = (char *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, blerg->data_fd, 0); + blerg->data = (char *) mmap(NULL, blerg->data_size, PROT_READ, MAP_SHARED, blerg->data_fd, 0); if (blerg->data == MAP_FAILED) { perror("Could not remap data"); return 0; } - blerg->data_size = st.st_size; return 1; } @@ -142,7 +157,7 @@ int blerg_exists(const char *name) { return 0; } - snprintf(filename, 512, "%s/%s", DATA_PATH, name); + snprintf(filename, 512, "%s/%s", blergconf.data_path, name); if (access(filename, F_OK) == -1) return 0; else @@ -173,7 +188,7 @@ struct blerg *blerg_open(const char *name) { /* Make the directory if it doesn't exist */ blerg->base_path = malloc(512); - snprintf(blerg->base_path, 512, "%s/%s", DATA_PATH, name); + snprintf(blerg->base_path, 512, "%s/%s", blergconf.data_path, name); if (access(blerg->base_path, F_OK) == -1) mkdir(blerg->base_path, 0755); @@ -218,6 +233,7 @@ open_failed_blerg_malloc: } int blerg_close(struct blerg *blerg) { + CHECK_VALID_BLERG(0) blerg_segment_close(blerg); munmap((void *)blerg->meta, sizeof(struct meta)); close(blerg->meta_fd); @@ -227,13 +243,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; - if (len > MAX_RECORD_SIZE) { - fprintf(stderr, "len > 64K\n"); - return -1; + CHECK_VALID_BLERG(BLERG_INVALID_RECORD) + + if (length > MAX_RECORD_SIZE || length <= 0) { + fprintf(stderr, "length out of bounds\n"); + return BLERG_INVALID_RECORD; } flock(blerg->index_fd, LOCK_EX); @@ -242,7 +260,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) @@ -254,18 +272,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 */ @@ -274,20 +292,23 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { flock(blerg->data_fd, LOCK_UN); flock(blerg->index_fd, LOCK_UN); - /* Now do those dirty microblogging deeds */ - tag_scan(blerg->name, data, len, record); - subscription_notify(blerg->name, record); + if (!blerg_get_mute(blerg)) { + /* Now do those dirty microblogging deeds */ + 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) { - if (record < 0) { +int blerg_fetch(struct blerg *blerg, uint64_t record, char **data, int *length) { + CHECK_VALID_BLERG(0) + if (record == BLERG_INVALID_RECORD || record >= blerg_get_record_count(blerg)) { fprintf(stderr, "Invalid record\n"); return 0; } - if (record >= blerg_get_record_count(blerg)) { - fprintf(stderr, "Invalid record\n"); + if (data == NULL || length == NULL) { + fprintf(stderr, "data or length is null\n"); return 0; } @@ -328,12 +349,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) { - if (record < 0) { - fprintf(stderr, "Invalid record\n"); - return 0; - } - if (record >= blerg_get_record_count(blerg)) { +time_t blerg_get_timestamp(struct blerg *blerg, uint64_t record) { + CHECK_VALID_BLERG(0) + if (record == BLERG_INVALID_RECORD || record >= blerg_get_record_count(blerg)) { fprintf(stderr, "Invalid record\n"); return 0; } @@ -352,9 +370,27 @@ time_t blerg_get_timestamp(struct blerg *blerg, int record) { } int blerg_set_subscription_mark(struct blerg *blerg) { + CHECK_VALID_BLERG(0) blerg->meta->subscription_mark = subscription_count_items(blerg->name); + return 1; } uint64_t blerg_get_subscription_mark(struct blerg *blerg) { + CHECK_VALID_BLERG(0) return blerg->meta->subscription_mark; } + +int blerg_set_mute(struct blerg *blerg, int v) { + CHECK_VALID_BLERG(0) + if (v) { + blerg->meta->status |= BLERGMETA_MUTED; + } else { + blerg->meta->status &= ~BLERGMETA_MUTED; + } + return 1; +} + +int blerg_get_mute(struct blerg *blerg) { + CHECK_VALID_BLERG(0) + return (blerg->meta->status & BLERGMETA_MUTED) > 0; +}