2 * Sherlock Library -- Object Buckets
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
18 static unsigned int obuck_remains, obuck_check_pad;
19 static struct fastbuf *obuck_fb;
20 static struct obuck_header obuck_hdr;
21 static sh_off_t bucket_start;
22 byte *obuck_name = "db/objects"; /* FIXME */
24 /*** Internal operations ***/
27 obuck_broken(char *msg)
29 die("Object pool corrupted: %s (pos=%Lx)", msg, (long long) bucket_start); /* FIXME */
35 flock(obuck_fd, LOCK_SH);
39 obuck_lock_write(void)
41 flock(obuck_fd, LOCK_EX);
47 flock(obuck_fd, LOCK_UN);
50 /*** FastIO emulation ***/
52 /* We need to use pread/pwrite since we work on fd's shared between processes */
55 obuck_fb_refill(struct fastbuf *f)
57 unsigned limit = (f->buflen < obuck_remains) ? f->buflen : obuck_remains;
58 unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
63 l = sh_pread(f->fd, f->buffer, size, f->fdpos);
65 die("Error reading bucket: %m");
66 if ((unsigned) l != size)
67 obuck_broken("Short read");
69 f->bstop = f->buffer + limit;
72 obuck_remains -= limit;
73 if (!obuck_remains) /* Should check the trailer */
76 memcpy(&check, f->buffer + size - 4, 4);
77 if (check != OBUCK_TRAILER)
78 obuck_broken("Missing trailer");
84 obuck_fb_spout(struct fastbuf *f)
86 int l = f->bptr - f->buffer;
91 int z = sh_pwrite(f->fd, c, l, f->fdpos);
93 die("Error writing bucket: %m");
103 obuck_fb_close(struct fastbuf *f)
108 /*** Exported functions ***/
111 obuck_init(int writeable)
117 obuck_fd = open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
118 obuck_fb = b = xmalloc_zero(sizeof(struct fastbuf) + buflen + OBUCK_ALIGN + 4);
120 b->buffer = (char *)(b+1);
121 b->bptr = b->bstop = b->buffer;
122 b->bufend = b->buffer + buflen;
125 b->refill = obuck_fb_refill;
126 b->spout = obuck_fb_spout;
127 b->close = obuck_fb_close;
129 size = sh_seek(obuck_fd, 0, SEEK_END);
132 /* If the bucket pool is not empty, check consistency of its end */
134 bucket_start = size - 4; /* for error reporting */
135 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
136 check != OBUCK_TRAILER)
137 obuck_broken("Missing trailer of last object");
148 void /* FIXME: Call somewhere :) */
158 return sh_seek(obuck_fd, 0, SEEK_END);
164 struct fastbuf *b = obuck_fb;
166 bucket_start = ((sh_off_t) oid) << OBUCK_SHIFT;
168 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
169 obuck_broken("Short header read");
170 b->fdpos = bucket_start + sizeof(obuck_hdr);
171 if (obuck_hdr.magic != OBUCK_MAGIC)
172 obuck_broken("Missing magic number");
173 if (obuck_hdr.oid == OBUCK_OID_DELETED)
174 obuck_broken("Access to deleted bucket");
175 if (obuck_hdr.oid != oid)
176 obuck_broken("Invalid backlink");
180 obuck_find_by_oid(struct obuck_header *hdrp)
182 oid_t oid = hdrp->oid;
184 ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
188 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
192 obuck_find_first(struct obuck_header *hdrp, int full)
196 return obuck_find_next(hdrp, full);
200 obuck_find_next(struct obuck_header *hdrp, int full)
203 struct fastbuf *b = obuck_fb;
208 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
209 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
212 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
216 if (c != sizeof(obuck_hdr))
217 obuck_broken("Short header read");
218 b->fdpos = bucket_start + sizeof(obuck_hdr);
219 if (obuck_hdr.magic != OBUCK_MAGIC)
220 obuck_broken("Missing magic number");
221 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
223 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
232 obuck_remains = obuck_hdr.length;
233 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
238 obuck_fetch_end(struct fastbuf *b UNUSED)
247 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
248 if (bucket_start & (OBUCK_ALIGN - 1))
249 obuck_broken("Misaligned file");
250 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
251 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
252 obuck_hdr.length = obuck_hdr.orig_length = 0;
253 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
254 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
259 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
262 obuck_hdr.magic = OBUCK_MAGIC;
263 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
264 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
267 bputl(obuck_fb, OBUCK_TRAILER);
269 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
270 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
272 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
276 obuck_delete(oid_t oid)
280 obuck_hdr.oid = OBUCK_OID_DELETED;
281 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
292 #define LEN(i) ((259309*(i))%MAXLEN)
297 unsigned int i, j, cnt;
298 struct obuck_header h;
302 for(j=0; j<COUNT; j++)
305 for(i=0; i<LEN(j); i++)
306 bputc(b, (i+j) % 256);
307 obuck_create_end(b, &h);
308 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
311 for(j=0; j<COUNT; j++)
312 if (j % 100 < KILLPERC)
314 printf("Deleting %08x\n", ids[j]);
315 obuck_delete(ids[j]);
318 for(j=0; j<COUNT; j++)
319 if (j % 100 >= KILLPERC)
323 obuck_find_by_oid(&h);
325 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
326 if (h.orig_length != LEN(j))
327 die("Invalid length");
328 for(i=0; i<h.orig_length; i++)
329 if ((unsigned) bgetc(b) != (i+j) % 256)
330 die("Contents mismatch");
335 if (obuck_find_first(&h, 0))
338 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
341 while (obuck_find_next(&h, 0));
343 die("Walk mismatch");