Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
| 21 | #include "object-db.h" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 22 | #include "db-helper.h" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 23 | #include "logging.h" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 24 | #include <boost/make_shared.hpp> |
| 25 | #include <iostream> |
| 26 | #include <sys/stat.h> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 28 | INIT_LOGGER("Object.Db"); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 31 | using namespace Ndnx; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 32 | using namespace boost; |
| 33 | namespace fs = boost::filesystem; |
| 34 | |
| 35 | const std::string INIT_DATABASE = "\ |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 36 | CREATE TABLE \n \ |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 37 | File( \n\ |
| 38 | device_name BLOB NOT NULL, \n\ |
| 39 | segment INTEGER, \n\ |
| 40 | content_object BLOB, \n\ |
| 41 | \ |
| 42 | PRIMARY KEY (device_name, segment) \n\ |
| 43 | ); \n\ |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 44 | CREATE INDEX device ON File(device_name); \n\ |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 45 | "; |
| 46 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 47 | ObjectDb::ObjectDb(const fs::path& folder, const std::string& hash) |
| 48 | : m_lastUsed(time(NULL)) |
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 | fs::path actualFolder = folder / "objects" / hash.substr(0, 2); |
| 51 | fs::create_directories(actualFolder); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 53 | _LOG_DEBUG("Open " << (actualFolder / hash.substr(2, hash.size() - 2))); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 55 | int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &m_db); |
| 56 | if (res != SQLITE_OK) { |
| 57 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str( |
| 58 | "Cannot open/create dabatabase: [" + |
| 59 | (actualFolder / hash.substr(2, hash.size() - 2)).string() + "]")); |
| 60 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 62 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 63 | // for now, just attempt to create everything |
| 64 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 65 | char* errmsg = 0; |
| 66 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg); |
| 67 | if (res != SQLITE_OK && errmsg != 0) { |
| 68 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
| 69 | sqlite3_free(errmsg); |
| 70 | } |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 72 | // _LOG_DEBUG ("open db"); |
| 73 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 74 | willStartSave(); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 77 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 78 | ObjectDb::DoesExist(const boost::filesystem::path& folder, const Ccnx::Name& deviceName, |
| 79 | const std::string& hash) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 80 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 81 | fs::path actualFolder = folder / "objects" / hash.substr(0, 2); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 82 | bool retval = false; |
| 83 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 84 | sqlite3* db; |
| 85 | int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &db); |
| 86 | if (res == SQLITE_OK) { |
| 87 | sqlite3_stmt* stmt; |
| 88 | sqlite3_prepare_v2(db, |
| 89 | "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", |
| 90 | -1, &stmt, 0); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 92 | CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); |
| 93 | sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 95 | int res = sqlite3_step(stmt); |
| 96 | if (res == SQLITE_ROW) { |
| 97 | int countAll = sqlite3_column_int(stmt, 0); |
| 98 | int countNonNull = sqlite3_column_int(stmt, 1); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 100 | _LOG_TRACE("Total segments: " << countAll << ", non-empty segments: " << countNonNull); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 102 | if (countAll > 0 && countAll == countNonNull) { |
| 103 | retval = true; |
| 104 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 107 | sqlite3_finalize(stmt); |
| 108 | } |
| 109 | |
| 110 | sqlite3_close(db); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 111 | return retval; |
| 112 | } |
| 113 | |
| 114 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 115 | ObjectDb::~ObjectDb() |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 116 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 117 | didStopSave(); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 119 | // _LOG_DEBUG ("close db"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 120 | int res = sqlite3_close(m_db); |
| 121 | if (res != SQLITE_OK) { |
| 122 | // complain |
| 123 | } |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 127 | ObjectDb::saveContentObject(const Ccnx::Name& deviceName, sqlite3_int64 segment, |
| 128 | const Ccnx::Bytes& data) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 129 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 130 | sqlite3_stmt* stmt; |
| 131 | sqlite3_prepare_v2(m_db, "INSERT INTO File " |
| 132 | "(device_name, segment, content_object) " |
| 133 | "VALUES (?, ?, ?)", |
| 134 | -1, &stmt, 0); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 135 | |
Zhenkai Zhu | 81907f2 | 2013-01-23 17:32:34 -0800 | [diff] [blame] | 136 | //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 138 | CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); |
| 139 | sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_STATIC); |
| 140 | sqlite3_bind_int64(stmt, 2, segment); |
| 141 | sqlite3_bind_blob(stmt, 3, &data[0], data.size(), SQLITE_STATIC); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 143 | sqlite3_step(stmt); |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 144 | //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 145 | sqlite3_finalize(stmt); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 146 | |
| 147 | // update last used time |
| 148 | m_lastUsed = time(NULL); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 151 | Ccnx::BytesPtr |
| 152 | ObjectDb::fetchSegment(const Ccnx::Name& deviceName, sqlite3_int64 segment) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 153 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 154 | sqlite3_stmt* stmt; |
| 155 | sqlite3_prepare_v2(m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, |
| 156 | &stmt, 0); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 158 | CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); |
| 159 | sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); |
| 160 | sqlite3_bind_int64(stmt, 2, segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 161 | |
| 162 | BytesPtr ret; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 163 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 164 | int res = sqlite3_step(stmt); |
| 165 | if (res == SQLITE_ROW) { |
| 166 | const unsigned char* buf = reinterpret_cast<const unsigned char*>(sqlite3_column_blob(stmt, 0)); |
| 167 | int bufBytes = sqlite3_column_bytes(stmt, 0); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 169 | ret = make_shared<Bytes>(buf, buf + bufBytes); |
| 170 | } |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 172 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 173 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 174 | // update last used time |
| 175 | m_lastUsed = time(NULL); |
| 176 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 180 | time_t |
| 181 | ObjectDb::secondsSinceLastUse() |
| 182 | { |
| 183 | return (time(NULL) - m_lastUsed); |
| 184 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 185 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 186 | // sqlite3_int64 |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 187 | // ObjectDb::getNumberOfSegments (const Ndnx::Name &deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 188 | // { |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 189 | // sqlite3_stmt *stmt; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 190 | // sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 191 | |
| 192 | // bool retval = false; |
| 193 | // int res = sqlite3_step (stmt); |
| 194 | // if (res == SQLITE_ROW) |
| 195 | // { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 196 | // retval = true; |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 197 | // } |
| 198 | // sqlite3_finalize (stmt); |
| 199 | |
| 200 | // return retval; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 201 | // } |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 202 | |
| 203 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 204 | ObjectDb::willStartSave() |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 205 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 206 | sqlite3_exec(m_db, "BEGIN TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 207 | // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 211 | ObjectDb::didStopSave() |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 212 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 213 | sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 214 | // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 215 | } |