The map::at method does not always exist, so instead use map::find.
[cumulus.git] / localdb.h
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2007-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 /* When creating backup snapshots, maintain a local database of data blocks and
22  * checksums, in addition to the data contents (which may be stored remotely).
23  * This database is consulted when attempting to build incremental snapshots,
24  * as it says which objects can be reused.
25  *
26  * The database is implemented as an SQLite3 database, but this implementation
27  * detail is kept internal to this file, so that the storage format may be
28  * changed later. */
29
30 #ifndef _LBS_LOCALDB_H
31 #define _LBS_LOCALDB_H
32
33 #include <sqlite3.h>
34
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,
45                      const std::string &checksum, int64_t size, double age);
46     ObjectReference FindObject(const std::string &checksum, int64_t size);
47     bool IsOldObject(const std::string &checksum, int64_t size, double *age,
48                      int *group);
49     bool IsAvailable(const ObjectReference &ref);
50     void UseObject(const ObjectReference& ref);
51     void UseSegment(const std::string &segment, double utilization);
52
53     void SetSegmentChecksum(const std::string &segment, const std::string &path,
54                             const std::string &checksum, int 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