2 * Bucket -> Object converter
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
8 #include "lib/unaligned.h"
10 #include "lib/fastbuf.h"
11 #include "charset/unicode.h"
12 #include "lib/object.h"
13 #include "lib/bucket.h"
14 #include "lib/lizard.h"
15 #include "lib/buck2obj.h"
21 #define MAX_HEADER_SIZE 1024 // extra space for the header not counted in MaxObjSize
22 #define RET_ERR(num) ({ errno = num; return NULL; })
28 struct lizard_buffer *lizard;
33 buck2obj_alloc_internal(struct buck2obj_buf *buf, uns max_len)
35 buf->max_len = max_len;
36 buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
37 buf->raw = xmalloc(buf->raw_len);
41 buck2obj_alloc(uns max_len, struct mempool *mp)
43 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
44 buck2obj_alloc_internal(buf, max_len);
45 buf->lizard = lizard_alloc(max_len);
51 buck2obj_free(struct buck2obj_buf *buf)
53 lizard_free(buf->lizard);
59 buck2obj_realloc(struct buck2obj_buf *buf, uns max_len)
61 if (max_len <= buf->max_len)
63 if (max_len < 2*buf->max_len + 1) // to ensure amortized logarithmic complexity
64 max_len = 2*buf->max_len + 1;
66 buck2obj_alloc_internal(buf, max_len);
67 lizard_realloc(buf->lizard, max_len);
71 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
73 if (can_overwrite >= 2)
83 obj_add_attr_ref(o, type, ptr);
87 else if (can_overwrite == 1)
97 obj_add_attr(o, type, ptr);
109 byte type = ptr[len];
111 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
112 memcpy(dup, ptr, len);
114 obj_add_attr_ref(o, type, dup);
122 buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body, uns want_body)
125 struct odes *o = obj_new(buf->mp);
127 if (buck_type < BUCKET_TYPE_V33)
129 if (want_body) // ignore empty lines, read until EOF
130 obj_read_multi(body, o);
131 else // end on EOF or the first empty line
136 /* Compute the length of the bucket. We cannot fetch this attribute
137 * directly due to remote indexing. */
138 bseek(body, 0, SEEK_END);
139 sh_off_t buck_len = btell(body);
142 /* Read all the bucket into 1 buffer, 0-copy if possible. */
143 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
144 if (can_overwrite < 0)
148 uns len = bdirect_read_prepare(body, &ptr);
150 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
152 /* Copy if the original buffer is too small.
153 * If it is write-protected, copy it also if it is uncompressed. */
154 if (buck_len > buf->raw_len)
155 buck2obj_realloc(buf, buck_len);
156 len = bread(body, buf->raw, buck_len);
162 overwritten = can_overwrite > 1;
165 ptr = decode_attributes(ptr, end, o, can_overwrite);// header
168 if (buck_type == BUCKET_TYPE_V33)
170 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
174 int res = lizard_decompress_safe(ptr, buf->lizard, len);
175 if (res != (int) len)
181 ptr = buf->lizard->ptr;
185 else // unknown bucket type
187 ASSERT(can_overwrite == 2); // because of the policy and decompression
188 ptr = decode_attributes(ptr, end, o, 2); // body
192 /* If (overwritten), bflush(body) might be needed. */