+/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
+ * BSD-style license. Please see the COPYING file for details.
+ */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <fcntl.h>
#include "database.h"
+#include "subscription.h"
+#include "util.h"
#include "config.h"
uint64_t blerg_get_record_count(struct blerg *blerg) {
int namelen = strlen(name);
char filename[512];
- if (namelen > 32) {
- perror("Name too long");
+ if (!valid_name(name)) {
+ fprintf(stderr, "Invalid name\n");
return 0;
}
struct stat st;
uint64_t sequence;
- if (namelen > 32) {
- perror("Name too long");
+ if (!valid_name(name)) {
+ fprintf(stderr, "Invalid name\n");
return NULL;
}
struct blerg *blerg = malloc(sizeof(struct blerg));
int blerg_store(struct blerg *blerg, const char *data, int len) {
if (len > MAX_RECORD_SIZE) {
- printf("len > 64K\n");
+ fprintf(stderr, "len > 64K\n");
return -1;
}
uint64_t record = blerg_increment_record_count(blerg);
if (record == -1) {
- printf("Could not find free record\n");
+ fprintf(stderr, "Could not find free record\n");
return -1;
}
int segment = record / RECORDS_PER_SEGMENT;
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;
}
int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) {
if (record < 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
int seg_rec = record % RECORDS_PER_SEGMENT;
if ((blerg->index[seg_rec].flags & 0x1) == 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
fstat(blerg->data_fd, &st);
blerg->data_size = st.st_size;
if (rec_offset > blerg->data_size) {
- printf("Record offset outside of data!?");
+ fprintf(stderr, "Record offset outside of data!?");
return 0;
}
time_t blerg_get_timestamp(struct blerg *blerg, int record) {
if (record < 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
int seg_rec = record % RECORDS_PER_SEGMENT;
if ((blerg->index[seg_rec].flags & 0x1) == 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}