Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * 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 Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * 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 Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * 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 Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 21 | #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 Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 35 | #include <boost/filesystem.hpp> |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace chronoshare { |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 40 | class ObjectDb |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 41 | { |
| 42 | public: |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 43 | class Error : public DbHelper::Error |
| 44 | { |
| 45 | public: |
| 46 | explicit Error(const std::string& what) |
| 47 | : DbHelper::Error(what) |
| 48 | { |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | public: |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 53 | // database will be create in <folder>/<first-pair-of-hash-bytes>/<rest-of-hash> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | ObjectDb(const boost::filesystem::path& folder, const std::string& hash); |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | ~ObjectDb(); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 58 | void |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 59 | saveContentObject(const Name& deviceName, sqlite3_int64 segment, const Data& data); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 60 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 61 | shared_ptr<Data> |
| 62 | fetchSegment(const Name& deviceName, sqlite3_int64 segment); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 63 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 64 | const time::steady_clock::TimePoint& |
| 65 | getLastUsed() const; |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 67 | static bool |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 68 | doesExist(const boost::filesystem::path& folder, const Name& deviceName, const std::string& hash); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | willStartSave(); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 73 | |
| 74 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 75 | didStopSave(); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 77 | private: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | sqlite3* m_db; |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 79 | time::steady_clock::TimePoint m_lastUsed; |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 82 | } // namespace chronoshare |
| 83 | } // namespace ndn |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 84 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 85 | #endif // CHRONOSHARE_SRC_OBJECT_DB_HPP |