Start work on tests for Cumulus.
[cumulus.git] / localdb.h
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2007-2008 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /* When creating backup snapshots, maintain a local database of data blocks and
21  * checksums, in addition to the data contents (which may be stored remotely).
22  * This database is consulted when attempting to build incremental snapshots,
23  * as it says which objects can be reused.
24  *
25  * The database is implemented as an SQLite3 database, but this implementation
26  * detail is kept internal to this file, so that the storage format may be
27  * changed later. */
28
29 #ifndef _LBS_LOCALDB_H
30 #define _LBS_LOCALDB_H
31
32 #include <sqlite3.h>
33
34 #include <set>
35 #include <string>
36
37 #include "ref.h"
38
39 class LocalDb {
40 public:
41     void Open(const char *path, const char *snapshot_name,
42               const char *snapshot_scheme, double intent);
43     void Close();
44     void StoreObject(const ObjectReference& ref, double age);
45     ObjectReference FindObject(const std::string &checksum, int64_t size);
46     bool IsOldObject(const std::string &checksum, int64_t size, double *age,
47                      int *group);
48     bool IsAvailable(const ObjectReference &ref);
49     void UseObject(const ObjectReference& ref);
50
51     std::set<std::string> GetUsedSegments();
52     void SetSegmentChecksum(const std::string &segment, const std::string &path,
53                             const std::string &checksum,
54                             int data_size, int disk_size);
55     bool GetSegmentChecksum(const std::string &segment,
56                             std::string *seg_path, std::string *seg_checksum);
57
58     bool LoadChunkSignatures(ObjectReference ref,
59                              void **buf, size_t *len,
60                              std::string *algorithm);
61     void StoreChunkSignatures(ObjectReference ref,
62                               const void *buf, size_t len,
63                               const std::string &algorithm);
64 private:
65     sqlite3 *db;
66     int64_t snapshotid;
67
68     sqlite3_stmt *Prepare(const char *sql);
69     void ReportError(int rc);
70     int64_t SegmentToId(const std::string &segment);
71     std::string IdToSegment(int64_t segmentid);
72 };
73
74 #endif // _LBS_LOCALDB_H