Initial support for efficient sub-file incrementals.
[cumulus.git] / subfile.h
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2008  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /* Allow for sub-file incremental backups: if only a portion of a file changes,
22  * allow the new data to be written out, and the old data to simply be
23  * referenced from the new metadata log. */
24
25 #ifndef _LBS_SUBFILE_H
26 #define _LBS_SUBFILE_H
27
28 #include <list>
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <vector>
33
34 #include "chunk.h"
35 #include "localdb.h"
36 #include "ref.h"
37 #include "store.h"
38
39 class Subfile {
40 public:
41     Subfile(LocalDb *localdb);
42     ~Subfile();
43
44     // Prepare to compute a subfile incremental by loading signatures for data
45     // in the old file.
46     void load_old_blocks(const std::list<ObjectReference> &blocks);
47
48     // Break a new block of data into small chunks, and compute checksums of
49     // the chunks.  After doing so, a delta can be computed, or the signatures
50     // can be written out to the database.  The caller must not modify the
51     // buffer until all operations referring to it are finished.
52     void analyze_new_block(const char *buf, size_t len);
53
54     std::list<ObjectReference> create_incremental(TarSegmentStore *tss,
55                                                   LbsObject *o,
56                                                   double block_age);
57
58     static const int HASH_SIZE = 20;
59
60 private:
61     struct chunk_info {
62         char hash[HASH_SIZE];
63         int offset, len;
64     };
65
66     struct block_summary {
67         ObjectReference ref;
68         int num_chunks;
69         struct chunk_info *chunks;
70     };
71
72     LocalDb *db;
73     bool checksums_loaded;
74     std::set<ObjectReference> old_blocks;
75     std::vector<block_summary> block_list;
76     std::map<std::string, std::pair<int, int> > chunk_index;
77
78     bool new_block_summary_valid;
79     block_summary new_block_summary;
80
81     const char *analyzed_buf;
82     size_t analyzed_len;
83
84     void ensure_signatures_loaded();
85     void index_chunks(ObjectReference ref);
86     void free_analysis();
87     void store_block_signatures(ObjectReference ref, block_summary summary);
88
89     std::string get_algorithm() {
90         return chunk_algorithm_name() + "/sha1";
91     }
92 };
93
94 #endif // _LBS_SUBFILE_H