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(sizeof(struct fastbuf) + buflen + OBUCK_ALIGN + 4);
119 bzero(b, sizeof(struct fastbuf));
121 b->buffer = (char *)(b+1);
122 b->bptr = b->bstop = b->buffer;
123 b->bufend = b->buffer + buflen;
126 b->refill = obuck_fb_refill;
127 b->spout = obuck_fb_spout;
128 b->close = obuck_fb_close;
130 size = sh_seek(obuck_fd, 0, SEEK_END);
133 /* If the bucket pool is not empty, check consistency of its end */
135 bucket_start = size - 4; /* for error reporting */
136 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
137 check != OBUCK_TRAILER)
138 obuck_broken("Missing trailer of last object");
149 void /* FIXME: Call somewhere :) */
159 struct fastbuf *b = obuck_fb;
161 bucket_start = ((sh_off_t) oid) << OBUCK_SHIFT;
163 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
164 obuck_broken("Short header read");
165 b->fdpos = bucket_start + sizeof(obuck_hdr);
166 if (obuck_hdr.magic != OBUCK_MAGIC)
167 obuck_broken("Missing magic number");
168 if (obuck_hdr.oid == OBUCK_OID_DELETED)
169 obuck_broken("Access to deleted bucket");
170 if (obuck_hdr.oid != oid)
171 obuck_broken("Invalid backlink");
175 obuck_find_by_oid(struct obuck_header *hdrp)
177 oid_t oid = hdrp->oid;
182 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
186 obuck_find_first(struct obuck_header *hdrp, int full)
190 return obuck_find_next(hdrp, full);
194 obuck_find_next(struct obuck_header *hdrp, int full)
197 struct fastbuf *b = obuck_fb;
202 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
203 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
206 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
210 if (c != sizeof(obuck_hdr))
211 obuck_broken("Short header read");
212 b->fdpos = bucket_start + sizeof(obuck_hdr);
213 if (obuck_hdr.magic != OBUCK_MAGIC)
214 obuck_broken("Missing magic number");
215 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
217 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
226 obuck_remains = obuck_hdr.length;
227 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
232 obuck_fetch_end(struct fastbuf *b UNUSED)
241 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
242 if (bucket_start & (OBUCK_ALIGN - 1))
243 obuck_broken("Misaligned file");
244 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
245 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
246 obuck_hdr.length = obuck_hdr.orig_length = 0;
247 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
248 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
253 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
256 obuck_hdr.magic = OBUCK_MAGIC;
257 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
258 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
261 bputl(obuck_fb, OBUCK_TRAILER);
263 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
264 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
266 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
270 obuck_delete(oid_t oid)
274 obuck_hdr.oid = OBUCK_OID_DELETED;
275 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
286 #define LEN(i) ((259309*(i))%MAXLEN)
291 unsigned int i, j, cnt;
292 struct obuck_header h;
296 for(j=0; j<COUNT; j++)
299 for(i=0; i<LEN(j); i++)
300 bputc(b, (i+j) % 256);
301 obuck_create_end(b, &h);
302 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
305 for(j=0; j<COUNT; j++)
306 if (j % 100 < KILLPERC)
308 printf("Deleting %08x\n", ids[j]);
309 obuck_delete(ids[j]);
312 for(j=0; j<COUNT; j++)
313 if (j % 100 >= KILLPERC)
317 obuck_find_by_oid(&h);
319 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
320 if (h.orig_length != LEN(j))
321 die("Invalid length");
322 for(i=0; i<h.orig_length; i++)
323 if ((unsigned) bgetc(b) != (i+j) % 256)
324 die("Contents mismatch");
329 if (obuck_find_first(&h, 0))
332 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
335 while (obuck_find_next(&h, 0));
337 die("Walk mismatch");