2 * Sherlock Library -- Object Buckets
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
19 static unsigned int obuck_remains, obuck_check_pad;
20 static struct fastbuf *obuck_fb;
21 static struct obuck_header obuck_hdr;
22 static sh_off_t bucket_start;
24 /*** Configuration ***/
26 byte *obuck_name = "not/configured";
27 static int obuck_io_buflen = 65536;
29 static struct cfitem obuck_config[] = {
30 { "Buckets", CT_SECTION, NULL },
31 { "BucketFile", CT_STRING, &obuck_name },
32 { "BufSize", CT_INT, &obuck_io_buflen },
33 { NULL, CT_STOP, NULL }
36 static void CONSTRUCTOR obuck_init_config(void)
38 cf_register(obuck_config);
41 /*** Internal operations ***/
44 obuck_broken(char *msg)
46 die("Object pool corrupted: %s (pos=%Lx)", msg, (long long) bucket_start); /* FIXME */
52 flock(obuck_fd, LOCK_SH);
56 obuck_lock_write(void)
58 flock(obuck_fd, LOCK_EX);
64 flock(obuck_fd, LOCK_UN);
67 /*** FastIO emulation ***/
69 /* We need to use pread/pwrite since we work on fd's shared between processes */
72 obuck_fb_refill(struct fastbuf *f)
74 unsigned limit = (f->buflen < obuck_remains) ? f->buflen : obuck_remains;
75 unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
80 l = sh_pread(f->fd, f->buffer, size, f->fdpos);
82 die("Error reading bucket: %m");
83 if ((unsigned) l != size)
84 obuck_broken("Short read");
86 f->bstop = f->buffer + limit;
89 obuck_remains -= limit;
90 if (!obuck_remains) /* Should check the trailer */
93 memcpy(&check, f->buffer + size - 4, 4);
94 if (check != OBUCK_TRAILER)
95 obuck_broken("Missing trailer");
101 obuck_fb_spout(struct fastbuf *f)
103 int l = f->bptr - f->buffer;
108 int z = sh_pwrite(f->fd, c, l, f->fdpos);
110 die("Error writing bucket: %m");
120 obuck_fb_close(struct fastbuf *f)
125 /*** Exported functions ***/
128 obuck_init(int writeable)
133 obuck_fd = sh_open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
135 die("Unable to open bucket file %s: %m", obuck_name);
136 obuck_fb = b = xmalloc_zero(sizeof(struct fastbuf) + obuck_io_buflen + OBUCK_ALIGN + 4);
137 b->buflen = obuck_io_buflen;
138 b->buffer = (char *)(b+1);
139 b->bptr = b->bstop = b->buffer;
140 b->bufend = b->buffer + obuck_io_buflen;
143 b->refill = obuck_fb_refill;
144 b->spout = obuck_fb_spout;
145 b->close = obuck_fb_close;
147 size = sh_seek(obuck_fd, 0, SEEK_END);
150 /* If the bucket pool is not empty, check consistency of its end */
152 bucket_start = size - 4; /* for error reporting */
153 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
154 check != OBUCK_TRAILER)
155 obuck_broken("Missing trailer of last object");
166 void /* FIXME: Call somewhere :) */
176 struct fastbuf *b = obuck_fb;
178 bucket_start = obuck_get_pos(oid);
180 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
181 obuck_broken("Short header read");
182 b->fdpos = bucket_start + sizeof(obuck_hdr);
183 if (obuck_hdr.magic != OBUCK_MAGIC)
184 obuck_broken("Missing magic number");
185 if (obuck_hdr.oid == OBUCK_OID_DELETED)
186 obuck_broken("Access to deleted bucket");
187 if (obuck_hdr.oid != oid)
188 obuck_broken("Invalid backlink");
192 obuck_find_by_oid(struct obuck_header *hdrp)
194 oid_t oid = hdrp->oid;
196 ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
200 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
204 obuck_find_first(struct obuck_header *hdrp, int full)
208 return obuck_find_next(hdrp, full);
212 obuck_find_next(struct obuck_header *hdrp, int full)
215 struct fastbuf *b = obuck_fb;
220 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
221 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
224 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
228 if (c != sizeof(obuck_hdr))
229 obuck_broken("Short header read");
230 b->fdpos = bucket_start + sizeof(obuck_hdr);
231 if (obuck_hdr.magic != OBUCK_MAGIC)
232 obuck_broken("Missing magic number");
233 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
235 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
244 obuck_remains = obuck_hdr.length;
245 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
250 obuck_fetch_end(struct fastbuf *b UNUSED)
259 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
260 if (bucket_start & (OBUCK_ALIGN - 1))
261 obuck_broken("Misaligned file");
262 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
263 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
264 obuck_hdr.length = obuck_hdr.orig_length = 0;
265 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
266 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
271 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
274 obuck_hdr.magic = OBUCK_MAGIC;
275 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
276 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
279 bputl(obuck_fb, OBUCK_TRAILER);
281 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
282 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
284 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
288 obuck_delete(oid_t oid)
292 obuck_hdr.oid = OBUCK_OID_DELETED;
293 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
304 #define LEN(i) ((259309*(i))%MAXLEN)
306 int main(int argc, char **argv)
309 unsigned int i, j, cnt;
310 struct obuck_header h;
314 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
316 die("This program has no command-line arguments.");
320 for(j=0; j<COUNT; j++)
323 for(i=0; i<LEN(j); i++)
324 bputc(b, (i+j) % 256);
325 obuck_create_end(b, &h);
326 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
329 for(j=0; j<COUNT; j++)
330 if (j % 100 < KILLPERC)
332 printf("Deleting %08x\n", ids[j]);
333 obuck_delete(ids[j]);
336 for(j=0; j<COUNT; j++)
337 if (j % 100 >= KILLPERC)
341 obuck_find_by_oid(&h);
343 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
344 if (h.orig_length != LEN(j))
345 die("Invalid length");
346 for(i=0; i<h.orig_length; i++)
347 if ((unsigned) bgetc(b) != (i+j) % 256)
348 die("Contents mismatch");
353 if (obuck_find_first(&h, 0))
356 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
359 while (obuck_find_next(&h, 0));
361 die("Walk mismatch");