Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "object-db.h" |
| 23 | #include <iostream> |
| 24 | #include <boost/make_shared.hpp> |
| 25 | #include "db-helper.h" |
| 26 | #include <sys/stat.h> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 27 | #include "logging.h" |
| 28 | |
| 29 | INIT_LOGGER ("Object.Db"); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 32 | using namespace Ndnx; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 33 | using namespace boost; |
| 34 | namespace fs = boost::filesystem; |
| 35 | |
| 36 | const std::string INIT_DATABASE = "\ |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 37 | CREATE TABLE \n \ |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 38 | File( \n\ |
| 39 | device_name BLOB NOT NULL, \n\ |
| 40 | segment INTEGER, \n\ |
| 41 | content_object BLOB, \n\ |
| 42 | \ |
| 43 | PRIMARY KEY (device_name, segment) \n\ |
| 44 | ); \n\ |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 45 | CREATE INDEX device ON File(device_name); \n\ |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 46 | "; |
| 47 | |
| 48 | ObjectDb::ObjectDb (const fs::path &folder, const std::string &hash) |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 49 | : m_lastUsed (time(NULL)) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 50 | { |
| 51 | fs::path actualFolder = folder / "objects" / hash.substr (0, 2); |
| 52 | fs::create_directories (actualFolder); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 54 | _LOG_DEBUG ("Open " << (actualFolder / hash.substr (2, hash.size () - 2))); |
| 55 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 56 | int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &m_db); |
| 57 | if (res != SQLITE_OK) |
| 58 | { |
| 59 | BOOST_THROW_EXCEPTION (Error::Db () |
| 60 | << errmsg_info_str ("Cannot open/create dabatabase: [" + |
| 61 | (actualFolder / hash.substr (2, hash.size () - 2)).string () + "]")); |
| 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 | |
| 67 | char *errmsg = 0; |
| 68 | res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg); |
| 69 | if (res != SQLITE_OK && errmsg != 0) |
| 70 | { |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 71 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 72 | sqlite3_free (errmsg); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 73 | } |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 75 | // _LOG_DEBUG ("open db"); |
| 76 | |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 77 | willStartSave (); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 80 | bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 81 | 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] | 82 | { |
| 83 | fs::path actualFolder = folder / "objects" / hash.substr (0, 2); |
| 84 | bool retval = false; |
| 85 | |
| 86 | sqlite3 *db; |
| 87 | int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &db); |
| 88 | if (res == SQLITE_OK) |
| 89 | { |
| 90 | sqlite3_stmt *stmt; |
| 91 | sqlite3_prepare_v2 (db, "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", -1, &stmt, 0); |
| 92 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 93 | NdnxCharbufPtr buf = deviceName.toNdnxCharbuf (); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 94 | sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT); |
| 95 | |
| 96 | int res = sqlite3_step (stmt); |
| 97 | if (res == SQLITE_ROW) |
| 98 | { |
| 99 | int countAll = sqlite3_column_int (stmt, 0); |
| 100 | int countNonNull = sqlite3_column_int (stmt, 1); |
| 101 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 102 | _LOG_TRACE ("Total segments: " << countAll << ", non-empty segments: " << countNonNull); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 103 | |
| 104 | if (countAll > 0 && countAll==countNonNull) |
| 105 | { |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 106 | retval = true; |
| 107 | } |
| 108 | } |
| 109 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 110 | sqlite3_finalize (stmt); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | sqlite3_close (db); |
| 114 | return retval; |
| 115 | } |
| 116 | |
| 117 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 118 | ObjectDb::~ObjectDb () |
| 119 | { |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 120 | didStopSave (); |
| 121 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 122 | // _LOG_DEBUG ("close db"); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 123 | int res = sqlite3_close (m_db); |
| 124 | if (res != SQLITE_OK) |
| 125 | { |
| 126 | // complain |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | sqlite3_stmt *stmt; |
| 134 | sqlite3_prepare_v2 (m_db, "INSERT INTO File " |
| 135 | "(device_name, segment, content_object) " |
| 136 | "VALUES (?, ?, ?)", -1, &stmt, 0); |
| 137 | |
Zhenkai Zhu | 81907f2 | 2013-01-23 17:32:34 -0800 | [diff] [blame] | 138 | //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 140 | NdnxCharbufPtr buf = deviceName.toNdnxCharbuf (); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 141 | sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_STATIC); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 142 | sqlite3_bind_int64 (stmt, 2, segment); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 143 | sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_STATIC); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 144 | |
| 145 | sqlite3_step (stmt); |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 146 | //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 147 | sqlite3_finalize (stmt); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 148 | |
| 149 | // update last used time |
| 150 | m_lastUsed = time(NULL); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 153 | Ndnx::BytesPtr |
| 154 | ObjectDb::fetchSegment (const Ndnx::Name &deviceName, sqlite3_int64 segment) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 155 | { |
| 156 | sqlite3_stmt *stmt; |
| 157 | sqlite3_prepare_v2 (m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0); |
| 158 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 159 | NdnxCharbufPtr buf = deviceName.toNdnxCharbuf (); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 160 | sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT); |
| 161 | sqlite3_bind_int64 (stmt, 2, segment); |
| 162 | |
| 163 | BytesPtr ret; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 165 | int res = sqlite3_step (stmt); |
| 166 | if (res == SQLITE_ROW) |
| 167 | { |
| 168 | const unsigned char *buf = reinterpret_cast<const unsigned char*> (sqlite3_column_blob (stmt, 0)); |
| 169 | int bufBytes = sqlite3_column_bytes (stmt, 0); |
| 170 | |
| 171 | ret = make_shared<Bytes> (buf, buf+bufBytes); |
| 172 | } |
| 173 | |
| 174 | sqlite3_finalize (stmt); |
| 175 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 176 | // update last used time |
| 177 | m_lastUsed = time(NULL); |
| 178 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 182 | time_t |
| 183 | ObjectDb::secondsSinceLastUse() |
| 184 | { |
| 185 | return (time(NULL) - m_lastUsed); |
| 186 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 188 | // sqlite3_int64 |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 189 | // ObjectDb::getNumberOfSegments (const Ndnx::Name &deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 190 | // { |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 191 | // sqlite3_stmt *stmt; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 192 | // 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] | 193 | |
| 194 | // bool retval = false; |
| 195 | // int res = sqlite3_step (stmt); |
| 196 | // if (res == SQLITE_ROW) |
| 197 | // { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 198 | // retval = true; |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 199 | // } |
| 200 | // sqlite3_finalize (stmt); |
| 201 | |
| 202 | // return retval; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 203 | // } |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 204 | |
| 205 | void |
| 206 | ObjectDb::willStartSave () |
| 207 | { |
| 208 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 209 | // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
| 213 | ObjectDb::didStopSave () |
| 214 | { |
| 215 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 216 | // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db)); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 217 | } |