1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
11 #include <sys/types.h>
16 #include "subscription.h"
20 uint64_t blerg_get_record_count(struct blerg *blerg) {
22 flock(blerg->meta_fd, LOCK_SH);
23 count = blerg->meta->sequence;
24 flock(blerg->meta_fd, LOCK_UN);
28 /* Returns last usable record */
29 uint64_t blerg_increment_record_count(struct blerg *blerg) {
31 flock(blerg->meta_fd, LOCK_EX);
32 count = blerg->meta->sequence++;
33 flock(blerg->meta_fd, LOCK_UN);
37 void blerg_segment_close(struct blerg *blerg) {
38 if (blerg->data != NULL)
39 munmap((void *)blerg->data, blerg->data_size);
40 if (blerg->data_fd != -1)
41 close(blerg->data_fd);
42 if (blerg->index != NULL)
43 munmap((void *)blerg->index, RECORDS_PER_SEGMENT * sizeof(struct record));
44 if (blerg->index_fd != -1)
45 close(blerg->index_fd);
48 int blerg_segment_switch(struct blerg *blerg, int new_segment) {
50 uint64_t max_sequence = blerg_get_record_count(blerg);
53 if (new_segment > max_sequence / RECORDS_PER_SEGMENT) {
54 fprintf(stderr, "Cannot switch to sequence beyond last record\n");
58 blerg_segment_close(blerg);
60 /* Load and map the index */
61 snprintf(filename, 512, "%s/index%d", blerg->base_path, new_segment);
62 blerg->index_fd = open(filename, O_RDWR | O_CREAT, 0600);
63 if (blerg->index_fd == -1) {
64 perror("Could not open index");
65 goto open_failed_index_open;
67 flock(blerg->index_fd, LOCK_EX);
68 fstat(blerg->index_fd, &st);
69 if (st.st_size == 0) {
72 memset((void *)&r, 0, sizeof(struct record));
73 for (i = 0; i < RECORDS_PER_SEGMENT; i++) {
74 write(blerg->index_fd, &r, sizeof(struct record));
77 flock(blerg->index_fd, LOCK_UN);
79 blerg->index = (struct record *) mmap(NULL, RECORDS_PER_SEGMENT * sizeof(struct record), PROT_READ | PROT_WRITE, MAP_SHARED, blerg->index_fd, 0);
80 if (blerg->index == MAP_FAILED) {
81 perror("Could not mmap index");
82 goto open_failed_index_mmap;
86 sprintf(filename, "%s/data%d", blerg->base_path, new_segment);
87 blerg->data_fd = open(filename, O_RDWR | O_APPEND | O_CREAT, 0600);
88 fstat(blerg->data_fd, &st);
89 blerg->data_size = st.st_size;
90 if (blerg->data_fd == -1) {
91 perror("Could not open data");
92 goto open_failed_data_open;
95 if (blerg->data_size > 0) {
96 blerg->data = (char *) mmap(NULL, blerg->data_size, PROT_READ, MAP_SHARED, blerg->data_fd, 0);
97 if (blerg->data == MAP_FAILED) {
98 perror("Could not mmap data");
99 goto open_failed_data_mmap;
105 open_failed_data_mmap:
106 close(blerg->data_fd);
107 open_failed_data_open:
108 munmap((void *)blerg->index, sizeof(RECORDS_PER_SEGMENT * sizeof(struct record)));
109 open_failed_index_mmap:
110 close(blerg->index_fd);
111 open_failed_index_open:
115 int blerg_exists(const char *name) {
116 int namelen = strlen(name);
119 if (!valid_name(name)) {
120 fprintf(stderr, "Invalid name\n");
124 snprintf(filename, 512, "%s/%s", DATA_PATH, name);
125 if (access(filename, F_OK) == -1)
131 struct blerg *blerg_open(const char *name) {
132 int namelen = strlen(name);
137 if (!valid_name(name)) {
138 fprintf(stderr, "Invalid name\n");
141 struct blerg *blerg = malloc(sizeof(struct blerg));
143 perror("Cannot allocate memory for blerg");
144 goto open_failed_blerg_malloc;
146 blerg->name = malloc(namelen + 1);
147 memcpy(blerg->name, name, namelen + 1);
148 blerg->meta_fd = blerg->index_fd = blerg->data_fd = -1;
153 /* Make the directory if it doesn't exist */
154 blerg->base_path = malloc(512);
155 snprintf(blerg->base_path, 512, "%s/%s", DATA_PATH, name);
156 if (access(blerg->base_path, F_OK) == -1)
157 mkdir(blerg->base_path, 0755);
159 /* Open and map metadata */
160 snprintf(filename, 512, "%s/meta", blerg->base_path);
161 blerg->meta_fd = open(filename, O_RDWR | O_CREAT, 0600);
162 if (blerg->meta_fd == -1) {
163 perror("Could not open metadata");
164 goto open_failed_meta_open;
166 fstat(blerg->meta_fd, &st);
167 if (st.st_size == 0) {
168 char *buf = (char *) malloc(sizeof(struct meta));
169 memset(buf, 0, sizeof(struct meta));
170 write(blerg->meta_fd, buf, sizeof(struct meta));
173 blerg->meta = (struct meta *) mmap(NULL, sizeof(struct meta), PROT_READ | PROT_WRITE, MAP_SHARED, blerg->meta_fd, 0);
174 if (blerg->meta == MAP_FAILED) {
175 perror("Could not map metadata");
176 goto open_failed_meta_mmap;
179 /* Open and map index and data for the current segment */
180 blerg->current_segment = blerg_get_record_count(blerg) / RECORDS_PER_SEGMENT;
181 if (!blerg_segment_switch(blerg, blerg->current_segment)) {
182 fprintf(stderr, "Could not switch segment\n");
183 goto open_failed_segment_switch;
188 open_failed_segment_switch:
189 munmap((void *)blerg->meta, sizeof(struct meta));
190 open_failed_meta_mmap:
191 close(blerg->meta_fd);
192 open_failed_meta_open:
195 open_failed_blerg_malloc:
199 int blerg_close(struct blerg *blerg) {
200 blerg_segment_close(blerg);
201 munmap((void *)blerg->meta, sizeof(struct meta));
202 close(blerg->meta_fd);
203 free(blerg->base_path);
209 int blerg_store(struct blerg *blerg, const char *data, int len) {
210 if (len > MAX_RECORD_SIZE) {
211 fprintf(stderr, "len > 64K\n");
215 flock(blerg->index_fd, LOCK_EX);
216 flock(blerg->data_fd, LOCK_EX);
218 uint64_t record = blerg_increment_record_count(blerg);
220 fprintf(stderr, "Could not find free record\n");
223 int segment = record / RECORDS_PER_SEGMENT;
224 if (segment != blerg->current_segment)
225 blerg_segment_switch(blerg, segment);
226 int seg_rec = record % RECORDS_PER_SEGMENT;
228 /* Get the position for the new data */
229 FILE *datafile = fdopen(dup(blerg->data_fd), "a");
230 fseek(datafile, 0, SEEK_END);
231 int curpos = ftell(datafile);
236 int n = write(blerg->data_fd, data + bytes, len);
238 perror("Could not write data");
239 /* Truncate anything we may have written */
240 ftruncate(blerg->data_fd, curpos);
244 } while (bytes < len);
245 blerg->index[seg_rec].flags = 0x0001;
246 blerg->index[seg_rec].offset = curpos;
247 blerg->index[seg_rec].length = len;
248 blerg->index[seg_rec].timestamp = time(NULL);
250 flock(blerg->data_fd, LOCK_UN);
251 flock(blerg->index_fd, LOCK_UN);
253 tag_scan(blerg->name, data, len, record);
254 subscription_notify(blerg->name, record);
259 int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) {
261 fprintf(stderr, "Invalid record\n");
265 int segment = record / RECORDS_PER_SEGMENT;
266 if (segment != blerg->current_segment)
267 blerg_segment_switch(blerg, segment);
268 int seg_rec = record % RECORDS_PER_SEGMENT;
270 if ((blerg->index[seg_rec].flags & 0x1) == 0) {
271 fprintf(stderr, "Invalid record\n");
275 int rec_offset = blerg->index[seg_rec].offset;
276 int rec_length = blerg->index[seg_rec].length;
277 if (rec_offset >= blerg->data_size) {
278 /* We're accessing an out-of-bounds record in our mmap.
279 Recheck size and remap. */
281 fstat(blerg->data_fd, &st);
282 blerg->data_size = st.st_size;
283 if (rec_offset > blerg->data_size) {
284 fprintf(stderr, "Record offset outside of data!?");
288 munmap(blerg->data, blerg->data_size);
289 blerg->data = (char *) mmap(NULL, blerg->data_size, PROT_READ, MAP_SHARED, blerg->data_fd, 0);
290 if (blerg->data == MAP_FAILED) {
291 perror("Could not remap data");
296 *data = malloc(rec_length);
298 perror("Could not allocate string in fetch");
302 memcpy(*data, blerg->data + rec_offset, rec_length);
304 *length = rec_length;
309 time_t blerg_get_timestamp(struct blerg *blerg, int record) {
311 fprintf(stderr, "Invalid record\n");
315 int segment = record / RECORDS_PER_SEGMENT;
316 if (segment != blerg->current_segment)
317 blerg_segment_switch(blerg, segment);
318 int seg_rec = record % RECORDS_PER_SEGMENT;
320 if ((blerg->index[seg_rec].flags & 0x1) == 0) {
321 fprintf(stderr, "Invalid record\n");
325 return blerg->index[seg_rec].timestamp;