Remove checksums and reference tracking; I think they are not needed.
[cumulus.git] / store.h
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 implementation of the object store
6  * is built on top of libtar, and represents segments as TAR files and objects
7  * as files within them. */
8
9 #ifndef _LBS_STORE_H
10 #define _LBS_STORE_H
11
12 #include <stdint.h>
13 #include <libtar.h>
14
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <iostream>
20 #include <sstream>
21
22 #include "sha1.h"
23
24 class LbsObject;
25
26 /* In memory datatype to represent key/value pairs of information, such as file
27  * metadata.  Currently implemented as map<string, string>. */
28 typedef std::map<std::string, std::string> dictionary;
29
30 /* IOException will be thrown if an error occurs while reading or writing in
31  * one of the I/O wrappers.  Depending upon the context; this may be fatal or
32  * not--typically, errors reading/writing the store will be serious, but errors
33  * reading an individual file are less so. */
34 class IOException : public std::exception {
35 private:
36     std::string error;
37 public:
38     explicit IOException(const std::string &err) { error = err; }
39     virtual ~IOException() throw () { }
40     std::string getError() const { return error; }
41 };
42
43 /* A simple wrapper around a single TAR file to represent a segment.  Objects
44  * may only be written out all at once, since the tar header must be written
45  * first; incremental writing is not supported. */
46 class Tarfile {
47 public:
48     Tarfile(const std::string &path, const std::string &segment);
49     ~Tarfile();
50
51     void write_object(int id, const char *data, size_t len);
52
53     // Return an estimate of the size of the file.
54     size_t size_estimate() { return size; }
55
56     void internal_write_object(const std::string &path,
57                                const char *data, size_t len);
58
59 private:
60     size_t size;
61     std::string segment_name;
62     TAR *t;
63 };
64
65 class TarSegmentStore {
66 public:
67     // New segments will be stored in the given directory.
68     TarSegmentStore(const std::string &path) { this->path = path; }
69     ~TarSegmentStore() { sync(); }
70
71     // Writes an object to segment in the store, and returns the name
72     // (segment/object) to refer to it.  The optional parameter group can be
73     // used to control object placement; objects with different group
74     // parameters are kept in separate segments.
75     std::string write_object(const char *data, size_t len,
76                              const std::string &group = "");
77
78     // Ensure all segments have been fully written.
79     void sync();
80
81 private:
82     struct segment_info {
83         Tarfile *file;
84         std::string name;           // UUID
85         std::set<std::string> refs; // Other segments this one refers to
86         int count;                  // Objects written to this segment
87     };
88
89     std::string path;
90     std::map<std::string, struct segment_info *> segments;
91
92     // Ensure that all segments in the given group have been fully written.
93     void close_segment(const std::string &group);
94
95     // Parse an object reference string and return just the segment name
96     // portion.
97     std::string object_reference_to_segment(const std::string &object);
98 };
99
100 /* An in-memory representation of an object, which can be incrementally built
101  * before it is written out to a segment. */
102 class LbsObject {
103 public:
104     LbsObject();
105     ~LbsObject();
106
107     // If an object is placed in a group, it will be written out to segments
108     // only containing other objects in the same group.  A group name is simply
109     // a string.
110     //std::string get_group() const { return group; }
111     void set_group(const std::string &g) { group = g; }
112
113     // Data in an object must be written all at once, and cannot be generated
114     // incrementally.  Data can be an arbitrary block of binary data of any
115     // size.  The pointer to the data need only remain valid until write() is
116     // called.
117     //const char *get_data() const { return data; }
118     //size_t get_data_len() const { return data_len; }
119     void set_data(const char *d, size_t len) { data = d; data_len = len; }
120
121     // Write an object to a segment, thus making it permanent.  This function
122     // can be called at most once.
123     void write(TarSegmentStore *store);
124
125     // An object is assigned a permanent name once it has been written to a
126     // segment.  Until that time, its name cannot be determined.
127     std::string get_name() const { return name; }
128
129 private:
130     std::string group;
131     const char *data;
132     size_t data_len;
133
134     bool written;
135     std::string name;
136
137     std::set<std::string> refs;
138 };
139
140 #endif // _LBS_STORE_H