Fix some non-64-bit-ness in database.c
Minor API changes for types of record arguments and return values.
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);
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)
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 */
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;
}
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;
}
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