Add new interface for creating new segments.
[cumulus.git] / store.cc
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2006  Michael Vrable
3  *
4  * Backup data is stored in a collection of objects, which are grouped together
5  * into segments for storage purposes.  This file provides interfaces for
6  * reading and writing objects and segments. */
7
8 #include <assert.h>
9 #include <uuid/uuid.h>
10
11 #include "store.h"
12
13 using std::string;
14
15 OutputStream::OutputStream()
16     : bytes_written(0)
17 {
18 }
19
20 void OutputStream::write(const void *data, size_t len)
21 {
22     write_internal(data, len);
23     bytes_written += len;
24 }
25
26 void OutputStream::write_u8(uint8_t val)
27 {
28     write(&val, 1);
29 }
30
31 void OutputStream::write_u16(uint16_t val)
32 {
33     unsigned char buf[2];
34
35     buf[0] = val & 0xff;
36     buf[1] = (val >> 8) & 0xff;
37     write(buf, 2);
38 }
39
40 void OutputStream::write_u32(uint32_t val)
41 {
42     unsigned char buf[4];
43
44     buf[0] = val & 0xff;
45     buf[1] = (val >> 8) & 0xff;
46     buf[2] = (val >> 16) & 0xff;
47     buf[3] = (val >> 24) & 0xff;
48     write(buf, 4);
49 }
50
51 void OutputStream::write_u64(uint64_t val)
52 {
53     unsigned char buf[8];
54
55     buf[0] = val & 0xff;
56     buf[1] = (val >> 8) & 0xff;
57     buf[2] = (val >> 16) & 0xff;
58     buf[3] = (val >> 24) & 0xff;
59     buf[4] = (val >> 32) & 0xff;
60     buf[5] = (val >> 40) & 0xff;
61     buf[6] = (val >> 48) & 0xff;
62     buf[7] = (val >> 56) & 0xff;
63     write(buf, 8);
64 }
65
66 /* Writes an integer to an output stream using a variable-sized representation:
67  * seven bits are written at a time (little-endian), and the eigth bit of each
68  * byte is set if more data follows. */
69 void OutputStream::write_varint(uint64_t val)
70 {
71     do {
72         uint8_t remainder = (val & 0x7f);
73         val >>= 7;
74         if (val)
75             remainder |= 0x80;
76         write_u8(remainder);
77     } while (val);
78 }
79
80 /* Write an arbitrary string by first writing out the length, followed by the
81  * data itself. */
82 void OutputStream::write_string(const string &s)
83 {
84     size_t len = s.length();
85     write_varint(len);
86     write(s.data(), len);
87 }
88
89 void OutputStream::write_dictionary(const dictionary &d)
90 {
91     size_t size = d.size();
92     size_t written = 0;
93
94     write_varint(size);
95
96     for (dictionary::const_iterator i = d.begin(); i != d.end(); ++i) {
97         write_string(i->first);
98         write_string(i->second);
99         written++;
100     }
101
102     assert(written == size);
103 }
104
105 StringOutputStream::StringOutputStream()
106     : buf(std::ios_base::out)
107 {
108 }
109
110 void StringOutputStream::write_internal(const void *data, size_t len)
111 {
112     buf.write((const char *)data, len);
113     if (!buf.good())
114         throw IOException("error writing to StringOutputStream");
115 }
116
117 FileOutputStream::FileOutputStream(FILE *file)
118 {
119     f = file;
120 }
121
122 FileOutputStream::~FileOutputStream()
123 {
124     fclose(f);
125 }
126
127 void FileOutputStream::write_internal(const void *data, size_t len)
128 {
129     size_t res;
130
131     res = fwrite(data, 1, len, f);
132     if (res != len) {
133         throw IOException("write error");
134     }
135 }
136
137 WrapperOutputStream::WrapperOutputStream(OutputStream &o)
138     : real(o)
139 {
140 }
141
142 void WrapperOutputStream::write_internal(const void *data, size_t len)
143 {
144     real.write(data, len);
145 }
146
147 /* Utility functions, for encoding data types to strings. */
148 string encode_u16(uint16_t val)
149 {
150     StringOutputStream s;
151     s.write_u16(val);
152     return s.contents();
153 }
154
155 string encode_u32(uint32_t val)
156 {
157     StringOutputStream s;
158     s.write_u32(val);
159     return s.contents();
160 }
161
162 string encode_u64(uint64_t val)
163 {
164     StringOutputStream s;
165     s.write_u64(val);
166     return s.contents();
167 }
168
169 SegmentWriter::SegmentWriter(OutputStream *output, struct uuid u)
170     : out(output),
171       id(u),
172       object_stream(NULL)
173 {
174     /* Write out the segment header first. */
175     static const char signature[] = "LBSSEG0\n";
176     out->write(signature, strlen(signature));
177     out->write(id.bytes, sizeof(struct uuid));
178 }
179
180 SegmentWriter::~SegmentWriter()
181 {
182     if (object_stream)
183         finish_object();
184
185     // Write out the object table which gives the sizes and locations of all
186     // objects, and then add the trailing signature, which indicates the end of
187     // the segment and gives the offset of the object table.
188     int64_t index_offset = out->get_pos();
189
190     for (object_table::const_iterator i = objects.begin();
191          i != objects.end(); ++i) {
192         out->write_s64(i->first);
193         out->write_s64(i->second);
194     }
195
196     static const char signature2[] = "LBSEND";
197     out->write(signature2, strlen(signature2));
198     out->write_s64(index_offset);
199     out->write_u32(objects.size());
200
201     /* The SegmentWriter takes ownership of the OutputStream it is writing to,
202      * and destroys it automatically when done with the segment. */
203     delete out;
204 }
205
206 OutputStream *SegmentWriter::new_object()
207 {
208     if (object_stream)
209         finish_object();
210
211     object_start_offset = out->get_pos();
212     object_stream = new WrapperOutputStream(*out);
213
214     return object_stream;
215 }
216
217 void SegmentWriter::finish_object()
218 {
219     assert(object_stream != NULL);
220
221     // store (start, length) information for locating this object
222     objects.push_back(std::make_pair(object_start_offset,
223                                      object_stream->get_pos()));
224
225     delete object_stream;
226     object_stream = NULL;
227 }
228
229 struct uuid SegmentWriter::generate_uuid()
230 {
231     struct uuid u;
232
233     uuid_generate(u.bytes);
234
235     return u;
236 }
237
238 string SegmentWriter::format_uuid(const struct uuid u)
239 {
240     // A UUID only takes 36 bytes, plus the trailing '\0', so this is safe.
241     char buf[40];
242
243     uuid_unparse_lower(u.bytes, buf);
244
245     return string(buf);
246 }
247
248 SegmentStore::SegmentStore(const string &path)
249     : directory(path)
250 {
251 }
252
253 SegmentWriter *SegmentStore::new_segment()
254 {
255     struct uuid id = SegmentWriter::generate_uuid();
256     string filename = directory + "/" + SegmentWriter::format_uuid(id);
257
258     FILE *f = fopen(filename.c_str(), "wb");
259     if (f == NULL)
260         throw IOException("Unable to open new segment");
261
262     return new SegmentWriter(new FileOutputStream(f), id);
263 }