Fix some non-64-bit-ness in database.c
[blerg.git] / database / database.c
index 52ebe8c..9079ff4 100644 (file)
@@ -234,15 +234,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 +251,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 +263,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 +285,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 +340,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;
        }