Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -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 | #include "object-db.hpp" |
| 22 | #include "db-helper.hpp" |
| 23 | #include "core/logging.hpp" |
| 24 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include <iostream> |
| 26 | #include <sys/stat.h> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 27 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 29 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace chronoshare { |
| 32 | |
| 33 | _LOG_INIT(ObjectDb); |
| 34 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 36 | using ndn::util::Sqlite3Statement; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 37 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 38 | const std::string INIT_DATABASE = R"SQL( |
| 39 | CREATE TABLE |
| 40 | File( |
| 41 | device_name BLOB NOT NULL, |
| 42 | segment INTEGER, |
| 43 | content_object BLOB, |
| 44 | |
| 45 | PRIMARY KEY (device_name, segment) |
| 46 | ); |
| 47 | CREATE INDEX device ON File(device_name); |
| 48 | )SQL"; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 50 | ObjectDb::ObjectDb(const fs::path& folder, const std::string& hash) |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 51 | : m_lastUsed(time::steady_clock::now()) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 52 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | fs::path actualFolder = folder / "objects" / hash.substr(0, 2); |
| 54 | fs::create_directories(actualFolder); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | _LOG_DEBUG("Open " << (actualFolder / hash.substr(2, hash.size() - 2))); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &m_db); |
| 59 | if (res != SQLITE_OK) { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 60 | BOOST_THROW_EXCEPTION(Error("Cannot open/create database: [" + |
| 61 | (actualFolder / hash.substr(2, hash.size() - 2)).string() + "]")); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 64 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 65 | // for now, just attempt to create everything |
| 66 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | char* errmsg = 0; |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 68 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), nullptr, nullptr, &errmsg); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | if (res != SQLITE_OK && errmsg != 0) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | sqlite3_free(errmsg); |
| 71 | } |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | willStartSave(); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 76 | ObjectDb::~ObjectDb() |
| 77 | { |
| 78 | didStopSave(); |
| 79 | |
| 80 | int res = sqlite3_close(m_db); |
| 81 | if (res != SQLITE_OK) { |
| 82 | // complain |
| 83 | } |
| 84 | } |
| 85 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 86 | bool |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 87 | ObjectDb::doesExist(const boost::filesystem::path& folder, const Name& deviceName, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | const std::string& hash) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 89 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 90 | BOOST_ASSERT(hash.size() > 2); |
| 91 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 92 | fs::path actualFolder = folder / "objects" / hash.substr(0, 2); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 93 | bool retval = false; |
| 94 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | sqlite3* db; |
| 96 | int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &db); |
| 97 | if (res == SQLITE_OK) { |
| 98 | sqlite3_stmt* stmt; |
| 99 | sqlite3_prepare_v2(db, |
| 100 | "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", |
| 101 | -1, &stmt, 0); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 102 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 103 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 104 | SQLITE_TRANSIENT); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 106 | int res = sqlite3_step(stmt); |
| 107 | if (res == SQLITE_ROW) { |
| 108 | int countAll = sqlite3_column_int(stmt, 0); |
| 109 | int countNonNull = sqlite3_column_int(stmt, 1); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 111 | _LOG_TRACE("Total segments: " << countAll << ", non-empty segments: " << countNonNull); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | if (countAll > 0 && countAll == countNonNull) { |
| 114 | retval = true; |
| 115 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | sqlite3_finalize(stmt); |
| 119 | } |
| 120 | |
| 121 | sqlite3_close(db); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 122 | return retval; |
| 123 | } |
| 124 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 125 | void |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 126 | ObjectDb::saveContentObject(const Name& deviceName, sqlite3_int64 segment, const Data& data) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 127 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 128 | // update last used time |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 129 | m_lastUsed = time::steady_clock::now(); |
| 130 | |
| 131 | Sqlite3Statement stmt(m_db, "INSERT INTO File " |
| 132 | "(device_name, segment, content_object) " |
| 133 | "VALUES (?, ?, ?)"); |
| 134 | |
| 135 | _LOG_DEBUG("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " |
| 136 | << data.wireEncode().size() |
| 137 | << "]"); |
| 138 | stmt.bind(1, deviceName.wireEncode(), SQLITE_STATIC); |
| 139 | stmt.bind(2, segment); |
| 140 | stmt.bind(3, data.wireEncode(), SQLITE_STATIC); |
| 141 | stmt.step(); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 144 | shared_ptr<Data> |
| 145 | ObjectDb::fetchSegment(const Name& deviceName, sqlite3_int64 segment) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 146 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 147 | // update last used time |
| 148 | m_lastUsed = time::steady_clock::now(); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 149 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 150 | Sqlite3Statement stmt(m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?"); |
| 151 | stmt.bind(1, deviceName.wireEncode(), SQLITE_STATIC); |
| 152 | stmt.bind(2, segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 153 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 154 | if (stmt.step() == SQLITE_ROW) { |
| 155 | return make_shared<Data>(stmt.getBlock(0)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 156 | } |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 157 | else { |
| 158 | return nullptr; |
| 159 | } |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 162 | const time::steady_clock::TimePoint& |
| 163 | ObjectDb::getLastUsed() const |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 164 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 165 | return m_lastUsed; |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 166 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 168 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 169 | ObjectDb::willStartSave() |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 170 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 171 | sqlite3_exec(m_db, "BEGIN TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 175 | ObjectDb::didStopSave() |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 177 | sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 178 | } |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame] | 179 | |
| 180 | } // namespace chronoshare |
| 181 | } // namespace ndn |