commit:7a6eb2318ad7e2c245649213780ad14ecc9a64a7
author:Chip Black
committer:Chip Black
date:Fri Nov 15 22:10:24 2013 -0600
parents:9777ec792051ef0e7c3e9eec300a15233077e1a8
Fix some non-64-bit-ness in database.c

Minor API changes for types of record arguments and return values.
diff --git a/database/database.c b/database/database.c
line changes: +15/-15
index 52ebe8c..9079ff4
--- a/database/database.c
+++ b/database/database.c
@@ -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;
 	}

diff --git a/database/database.h b/database/database.h
line changes: +11/-9
index aa2403a..e60d609
--- a/database/database.h
+++ b/database/database.h
@@ -38,16 +38,18 @@ struct blerg {
 	int data_size;
 };
 
-int blerg_exists(const char *);
-struct blerg *blerg_open(const char *);
-int blerg_close(struct blerg *);
-int blerg_store(struct blerg *, const char *, int);
-int blerg_fetch(struct blerg *, int, char **, int *);
+#define BLERG_INVALID_RECORD 0xFFFFFFFFFFFFFFFF
+
+int      blerg_exists(const char *name);
+struct   blerg *blerg_open(const char *name);
+int      blerg_close(struct blerg *blerg);
+uint64_t blerg_store(struct blerg *blerg, const char *data, int length);
+int      blerg_fetch(struct blerg *blerg, uint64_t record, char **data, int *length);
 uint64_t blerg_get_record_count(struct blerg *);
-time_t blerg_get_timestamp(struct blerg *blerg, int record);
-int blerg_set_subscription_mark(struct blerg *blerg);
+time_t   blerg_get_timestamp(struct blerg *blerg, uint64_t record);
+int      blerg_set_subscription_mark(struct blerg *blerg);
 uint64_t blerg_get_subscription_mark(struct blerg *blerg);
-int blerg_set_mute(struct blerg *blerg, int v);
-int blerg_get_mute(struct blerg *blerg);
+int      blerg_set_mute(struct blerg *blerg, int v);
+int      blerg_get_mute(struct blerg *blerg);
 
 #endif //_DATABASE_H