The map::at method does not always exist, so instead use map::find.
[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     // Store the signatures for the most recently-analyzed block in the local
55     // database (linked to the specified object), if the block is sufficiently
56     // large.  If signatures already exist, they will be overwritten.
57     void store_analyzed_signatures(ObjectReference ref);
58
59     std::list<ObjectReference> create_incremental(TarSegmentStore *tss,
60                                                   LbsObject *o,
61                                                   double block_age);
62
63     static const int HASH_SIZE = 20;
64
65 private:
66     struct chunk_info {
67         char hash[HASH_SIZE];
68         int offset, len;
69     };
70
71     struct block_summary {
72         ObjectReference ref;
73         int num_chunks;
74         struct chunk_info *chunks;
75     };
76
77     LocalDb *db;
78     bool checksums_loaded;
79     std::set<ObjectReference> old_blocks;
80     std::vector<block_summary> block_list;
81     std::map<std::string, std::pair<int, int> > chunk_index;
82
83     bool new_block_summary_valid;
84     block_summary new_block_summary;
85
86     const char *analyzed_buf;
87     size_t analyzed_len;
88
89     void ensure_signatures_loaded();
90     void index_chunks(ObjectReference ref);
91     void free_analysis();
92     void store_block_signatures(ObjectReference ref, block_summary summary);
93
94     std::string get_algorithm() {
95         return chunk_algorithm_name() + "/sha1";
96     }
97 };
98
99 #endif // _LBS_SUBFILE_H