X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database%2Fdatabase.c;h=e6e87075e43cbeb896728ad32f8cfe8fa101bad3;hb=c65ce42fec106b10019bae07456f651bb175bbaa;hp=cb5cf0c58b43a7c5465fc2d2ad20017aef032f94;hpb=54789cc8538714fc3622646721f6f2ecceae94ac;p=blerg.git diff --git a/database/database.c b/database/database.c index cb5cf0c..e6e8707 100644 --- a/database/database.c +++ b/database/database.c @@ -13,6 +13,7 @@ #include #include #include "database.h" +#include "subscription.h" #include "util.h" #include "config.h" @@ -163,10 +164,17 @@ struct blerg *blerg_open(const char *name) { goto open_failed_meta_open; } fstat(blerg->meta_fd, &st); - if (st.st_size == 0) { - char *buf = (char *) malloc(sizeof(struct meta)); - memset(buf, 0, sizeof(struct meta)); - write(blerg->meta_fd, buf, sizeof(struct meta)); + if (st.st_size < sizeof(struct meta)) { + // Fill the difference in size between sizeof(struct meta) and + // the file size with nulls. This allows seamless upgrades as + // long as struct meta only adds members. + int len = sizeof(struct meta) - st.st_size; + char *buf = (char *) malloc(len); + memset(buf, 0, len); + int tmpfd = dup(blerg->meta_fd); + FILE* tmp = fdopen(tmpfd, "a"); + fwrite(buf, len, 1, tmp); + fclose(tmp); free(buf); } blerg->meta = (struct meta *) mmap(NULL, sizeof(struct meta), PROT_READ | PROT_WRITE, MAP_SHARED, blerg->meta_fd, 0); @@ -246,11 +254,12 @@ int blerg_store(struct blerg *blerg, const char *data, int len) { blerg->index[seg_rec].length = len; blerg->index[seg_rec].timestamp = time(NULL); - tag_scan(blerg->name, data, len, record); - flock(blerg->data_fd, LOCK_UN); flock(blerg->index_fd, LOCK_UN); + tag_scan(blerg->name, data, len, record); + subscription_notify(blerg->name, record); + return record; } @@ -322,3 +331,11 @@ time_t blerg_get_timestamp(struct blerg *blerg, int record) { return blerg->index[seg_rec].timestamp; } + +int blerg_set_subscription_mark(struct blerg *blerg) { + blerg->meta->subscription_mark = subscription_count_items(blerg->name); +} + +uint64_t blerg_get_subscription_mark(struct blerg *blerg) { + return blerg->meta->subscription_mark; +}