]> mj.ucw.cz Git - libucw.git/blob - lib/bucket.h
Replaced obuck_fetch_end() by bclose() (which is a nop as obuck_fetch_end was :) ).
[libucw.git] / lib / bucket.h
1 /*
2  *      Sherlock Library -- Object Buckets
3  *
4  *      (c) 2001--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_BUCKET_H
11 #define _SHERLOCK_BUCKET_H
12
13 /*
14  * Format: The object pool is merely a sequence of object buckets.
15  * Each bucket starts with struct obuck_header and it's padded
16  * by zeros to a multiple of OBUCK_ALIGN bytes.
17  *
18  * Locking: Each operation on the pool is protected by a flock.
19  *
20  * The buckets emulate non-seekable fastbuf streams.
21  *
22  * fork()'ing if you don't have any bucket open is safe.
23  */
24
25 extern byte *obuck_name;        /* Internal, for use by buckettool only! */
26
27 #define OBUCK_SHIFT 7
28 #define OBUCK_ALIGN (1<<OBUCK_SHIFT)
29 #define OBUCK_MAGIC 0xdeadf00d
30 #define OBUCK_INCOMPLETE_MAGIC 0xdeadfeel
31 #define OBUCK_TRAILER 0xfeedcafe
32 #define OBUCK_OID_DELETED (~(oid_t)0)
33 #define OBUCK_OID_FIRST_SPECIAL (~(oid_t)0xffff)
34
35 struct obuck_header {
36   u32 magic;                    /* OBUCK_MAGIC should dwell here */
37   oid_t oid;                    /* ID of this object or OBUCK_OID_DELETED */
38   u32 length;                   /* Length of data in the bucket */
39   u32 type;                     /* Data type */
40   /* Bucket data continue here */
41 };
42
43 enum bucket_type {
44   BUCKET_TYPE_COMPAT = 0x7fffffff,      /* and less -- buckets created by older versions of Sherlock */
45   BUCKET_TYPE_PLAIN = 0x80000000,       /* plain textual buckets */
46   BUCKET_TYPE_V30 = 0x80000001,         /* v3.0 uncompressed buckets */
47   BUCKET_TYPE_V30C = 0x80000002         /* v3.0 compressed buckets */
48 };
49
50 struct fastbuf;
51
52 void obuck_init(int writeable); /* Initialize the bucket module */
53 void obuck_cleanup(void);       /* Clean up the bucket module */
54 void obuck_sync(void);          /* Flush all buffers to disk */
55 void obuck_lock_read(void);     /* Explicit locking to make sure other threads don't touch buckets now */
56 void obuck_lock_write(void);
57 void obuck_unlock(void);
58 oid_t obuck_predict_last_oid(void); /* Get OID corresponding to the next to be created bucket (i.e., bucket file size estimate) */
59
60 /* Searching for buckets */
61 void obuck_find_by_oid(struct obuck_header *hdrp);
62 int obuck_find_first(struct obuck_header *hdrp, int full);
63 int obuck_find_next(struct obuck_header *hdrp, int full);
64
65 /* Reading current bucket */
66 struct fastbuf *obuck_fetch(void);
67
68 /* Creating buckets */
69 struct fastbuf *obuck_create(u32 type);
70 void obuck_create_end(struct fastbuf *b, struct obuck_header *hdrp);
71
72 /* Deleting buckets */
73 void obuck_delete(oid_t oid);
74
75 /* Fast reading of the whole pool */
76 struct fastbuf *obuck_slurp_pool(struct obuck_header *hdrp);
77
78 /* Convert bucket ID to file position (for size limitations etc.) */
79
80 static inline sh_off_t obuck_get_pos(oid_t oid)
81 {
82   return ((sh_off_t) oid) << OBUCK_SHIFT;
83 }
84
85 /* Shaking down bucket file */
86 void obuck_shakedown(int (*kibitz)(struct obuck_header *old, oid_t new, byte *buck));
87
88 #endif