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