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