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 struct fastbuf *b = obuck_fb;
160 bucket_start = ((sh_off_t) oid) << OBUCK_SHIFT;
162 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
163 obuck_broken("Short header read");
164 b->fdpos = bucket_start + sizeof(obuck_hdr);
165 if (obuck_hdr.magic != OBUCK_MAGIC)
166 obuck_broken("Missing magic number");
167 if (obuck_hdr.oid == OBUCK_OID_DELETED)
168 obuck_broken("Access to deleted bucket");
169 if (obuck_hdr.oid != oid)
170 obuck_broken("Invalid backlink");
174 obuck_find_by_oid(struct obuck_header *hdrp)
176 oid_t oid = hdrp->oid;
181 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
185 obuck_find_first(struct obuck_header *hdrp, int full)
189 return obuck_find_next(hdrp, full);
193 obuck_find_next(struct obuck_header *hdrp, int full)
196 struct fastbuf *b = obuck_fb;
201 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
202 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
205 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
209 if (c != sizeof(obuck_hdr))
210 obuck_broken("Short header read");
211 b->fdpos = bucket_start + sizeof(obuck_hdr);
212 if (obuck_hdr.magic != OBUCK_MAGIC)
213 obuck_broken("Missing magic number");
214 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
216 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
225 obuck_remains = obuck_hdr.length;
226 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
231 obuck_fetch_end(struct fastbuf *b UNUSED)
240 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
241 if (bucket_start & (OBUCK_ALIGN - 1))
242 obuck_broken("Misaligned file");
243 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
244 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
245 obuck_hdr.length = obuck_hdr.orig_length = 0;
246 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
247 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
252 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
255 obuck_hdr.magic = OBUCK_MAGIC;
256 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
257 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
260 bputl(obuck_fb, OBUCK_TRAILER);
262 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
263 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
265 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
269 obuck_delete(oid_t oid)
273 obuck_hdr.oid = OBUCK_OID_DELETED;
274 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
285 #define LEN(i) ((259309*(i))%MAXLEN)
290 unsigned int i, j, cnt;
291 struct obuck_header h;
295 for(j=0; j<COUNT; j++)
298 for(i=0; i<LEN(j); i++)
299 bputc(b, (i+j) % 256);
300 obuck_create_end(b, &h);
301 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
304 for(j=0; j<COUNT; j++)
305 if (j % 100 < KILLPERC)
307 printf("Deleting %08x\n", ids[j]);
308 obuck_delete(ids[j]);
311 for(j=0; j<COUNT; j++)
312 if (j % 100 >= KILLPERC)
316 obuck_find_by_oid(&h);
318 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
319 if (h.orig_length != LEN(j))
320 die("Invalid length");
321 for(i=0; i<h.orig_length; i++)
322 if ((unsigned) bgetc(b) != (i+j) % 256)
323 die("Contents mismatch");
328 if (obuck_find_first(&h, 0))
331 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
334 while (obuck_find_next(&h, 0));
336 die("Walk mismatch");