blob: a0391b51f99c483e9e3401e0c657fdee1aaa7266 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wang9f77a8c2016-12-25 14:44:04 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
Lijing Wang9f77a8c2016-12-25 14:44:04 -080021#ifndef CHRONOSHARE_SRC_OBJECT_DB_HPP
22#define CHRONOSHARE_SRC_OBJECT_DB_HPP
23
24#include "db-helper.hpp"
25#include "core/chronoshare-common.hpp"
26
27#include <sqlite3.h>
28
29#include <ctime>
30#include <vector>
31
32#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/name.hpp>
Zhenkai Zhu7717d202012-12-29 18:01:36 -080034
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080035#include <boost/filesystem.hpp>
Lijing Wang9f77a8c2016-12-25 14:44:04 -080036
37namespace ndn {
38namespace chronoshare {
Zhenkai Zhu7717d202012-12-29 18:01:36 -080039
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080040class ObjectDb
Zhenkai Zhu7717d202012-12-29 18:01:36 -080041{
42public:
Lijing Wang9f77a8c2016-12-25 14:44:04 -080043 class Error : public DbHelper::Error
44 {
45 public:
46 explicit Error(const std::string& what)
47 : DbHelper::Error(what)
48 {
49 }
50 };
51
52public:
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080053 // database will be create in <folder>/<first-pair-of-hash-bytes>/<rest-of-hash>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 ObjectDb(const boost::filesystem::path& folder, const std::string& hash);
Lijing Wang9f77a8c2016-12-25 14:44:04 -080055
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 ~ObjectDb();
Zhenkai Zhu7717d202012-12-29 18:01:36 -080057
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080058 void
Lijing Wang9f77a8c2016-12-25 14:44:04 -080059 saveContentObject(const Name& deviceName, sqlite3_int64 segment, const Data& data);
Zhenkai Zhu7717d202012-12-29 18:01:36 -080060
Lijing Wang9f77a8c2016-12-25 14:44:04 -080061 shared_ptr<Data>
62 fetchSegment(const Name& deviceName, sqlite3_int64 segment);
Zhenkai Zhu7717d202012-12-29 18:01:36 -080063
Lijing Wang9f77a8c2016-12-25 14:44:04 -080064 const time::steady_clock::TimePoint&
65 getLastUsed() const;
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080066
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080067 static bool
Lijing Wang9f77a8c2016-12-25 14:44:04 -080068 doesExist(const boost::filesystem::path& folder, const Name& deviceName, const std::string& hash);
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080069
70private:
71 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 willStartSave();
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080073
74 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080075 didStopSave();
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080076
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080077private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 sqlite3* m_db;
Lijing Wang9f77a8c2016-12-25 14:44:04 -080079 time::steady_clock::TimePoint m_lastUsed;
Zhenkai Zhu7717d202012-12-29 18:01:36 -080080};
81
Lijing Wang9f77a8c2016-12-25 14:44:04 -080082} // namespace chronoshare
83} // namespace ndn
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080084
Lijing Wang9f77a8c2016-12-25 14:44:04 -080085#endif // CHRONOSHARE_SRC_OBJECT_DB_HPP