]> mj.ucw.cz Git - libucw.git/blob - lib/bucket.h
Added tests for the hash table module.
[libucw.git] / lib / bucket.h
1 /*
2  *      Sherlock Library -- Object Buckets
3  *
4  *      (c) 2001--2004 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 fastbuf streams. Read streams act as normal files,
21  * but there can be only one write stream which is non-seekable and you
22  * also shouldn't open new read streams when writing.
23  *
24  * fork()'ing if you don't have any bucket open is safe.
25  */
26
27 extern byte *obuck_name;        /* Internal, for use by buckettool only! */
28
29 #define OBUCK_SHIFT 7
30 #define OBUCK_ALIGN (1<<OBUCK_SHIFT)
31 #define OBUCK_MAGIC 0xdeadf00d
32 #define OBUCK_INCOMPLETE_MAGIC 0xdeadfeel
33 #define OBUCK_TRAILER 0xfeedcafe
34 #define OBUCK_OID_DELETED (~(oid_t)0)
35 #define OBUCK_OID_ANY (~(oid_t)0)
36 #define OBUCK_OID_FIRST_SPECIAL (~(oid_t)0xffff)
37
38 struct obuck_header {
39   u32 magic;                    /* OBUCK_MAGIC should dwell here */
40   oid_t oid;                    /* ID of this object or OBUCK_OID_DELETED */
41   u32 length;                   /* Length of data in the bucket */
42   u32 type;                     /* Data type */
43   /* Bucket data continue here */
44 };
45
46 enum bucket_type {
47   BUCKET_TYPE_COMPAT = 0x7fffffff,      /* and less -- buckets created by older versions of Sherlock */
48   BUCKET_TYPE_PLAIN = 0x80000000,       /* plain textual buckets */
49   BUCKET_TYPE_V30 = 0x80000001,         /* v3.0 uncompressed buckets */
50   BUCKET_TYPE_V33 = 0x80000002,         /* v3.3 uncompressed buckets */
51   BUCKET_TYPE_V33_LIZARD = 0x80000003   /* v3.3 buckets compressed by lizard */
52 };
53
54 struct fastbuf;
55
56 void obuck_init(int writeable); /* Initialize the bucket module */
57 void obuck_cleanup(void);       /* Clean up the bucket module */
58 void obuck_sync(void);          /* Flush all buffers to disk */
59 void obuck_lock_read(void);     /* Explicit locking to make sure other threads don't touch buckets now */
60 void obuck_lock_write(void);
61 void obuck_unlock(void);
62 oid_t obuck_predict_last_oid(void); /* Get OID corresponding to the next to be created bucket (i.e., bucket file size estimate) */
63
64 /* Searching for buckets */
65 void obuck_find_by_oid(struct obuck_header *hdrp);
66 int obuck_find_first(struct obuck_header *hdrp, int full);
67 int obuck_find_next(struct obuck_header *hdrp, int full);
68
69 /* Reading current bucket */
70 struct fastbuf *obuck_fetch(void);
71
72 /* Creating buckets */
73 struct fastbuf *obuck_create(u32 type);
74 void obuck_create_end(struct fastbuf *b, struct obuck_header *hdrp);
75
76 /* Deleting buckets */
77 void obuck_delete(oid_t oid);
78
79 /* Fast reading of the whole pool */
80 struct fastbuf *obuck_slurp_pool(struct obuck_header *hdrp, oid_t next_oid);
81 void obuck_slurp_end(void);
82
83 /* Convert bucket ID to file position (for size limitations etc.) */
84
85 static inline sh_off_t obuck_get_pos(oid_t oid)
86 {
87   return ((sh_off_t) oid) << OBUCK_SHIFT;
88 }
89
90 /* Calculate size of bucket which contains given amount of data */
91
92 static inline uns obuck_bucket_size(uns len)
93 {
94   return ALIGN(sizeof(struct obuck_header) + len + 4, OBUCK_ALIGN);
95 }
96
97 /* Shaking down bucket file */
98 void obuck_shakedown(int (*kibitz)(struct obuck_header *old, oid_t new, byte *buck));
99
100 #endif