Rename tarstore -> store, since it is the only implementation now.
[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_TARSTORE_H
10 #define _LBS_TARSTORE_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 /* In memory datatype to represent key/value pairs of information, such as file
25  * metadata.  Currently implemented as map<string, string>. */
26 typedef std::map<std::string, std::string> dictionary;
27
28 /* IOException will be thrown if an error occurs while reading or writing in
29  * one of the I/O wrappers.  Depending upon the context; this may be fatal or
30  * not--typically, errors reading/writing the store will be serious, but errors
31  * reading an individual file are less so. */
32 class IOException : public std::exception {
33 private:
34     std::string error;
35 public:
36     explicit IOException(const std::string &err) { error = err; }
37     virtual ~IOException() throw () { }
38     std::string getError() const { return error; }
39 };
40
41 /* A simple wrapper around a single TAR file to represent a segment.  Objects
42  * may only be written out all at once, since the tar header must be written
43  * first; incremental writing is not supported. */
44 class Tarfile {
45 public:
46     Tarfile(const std::string &path, const std::string &segment);
47     ~Tarfile();
48
49     void write_object(int id, const char *data, size_t len);
50
51     // Return an estimate of the size of the file.
52     size_t size_estimate() { return size; }
53
54     void internal_write_object(const std::string &path,
55                                const char *data, size_t len);
56
57 private:
58     size_t size;
59     std::string segment_name;
60     std::ostringstream checksums;
61     TAR *t;
62 };
63
64 class TarSegmentStore {
65 public:
66     // New segments will be stored in the given directory.
67     TarSegmentStore(const std::string &path) { this->path = path; }
68     ~TarSegmentStore() { sync(); }
69
70     // Writes an object to segment in the store, and returns the name
71     // (segment/object) to refer to it.  The optional parameter group can be
72     // used to control object placement; objects with different group
73     // parameters are kept in separate segments.
74     std::string write_object(const char *data, size_t len,
75                              const std::string &group = "",
76                              const std::list<std::string> &refs = norefs);
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     // An empty list which can be used as an argument to write_object to
93     // indicate that this object depends on no others.
94     static std::list<std::string> norefs;
95
96     // Ensure that all segments in the given group have been fully written.
97     void close_segment(const std::string &group);
98
99     // Parse an object reference string and return just the segment name
100     // portion.
101     std::string object_reference_to_segment(const std::string &object);
102 };
103
104 #endif // _LBS_TARSTORE_H