blob: fd4223ea41a9c70cda4afbcc6585ecd071febb13 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
21#include "object-db.h"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080022#include "db-helper.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080023#include "logging.h"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include <boost/make_shared.hpp>
25#include <iostream>
26#include <sys/stat.h>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080027
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028INIT_LOGGER("Object.Db");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029
30using namespace std;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070031using namespace Ndnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080032using namespace boost;
33namespace fs = boost::filesystem;
34
35const std::string INIT_DATABASE = "\
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080036CREATE TABLE \n \
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080037 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 Afanasyevdbc06712013-01-08 18:30:28 -080044CREATE INDEX device ON File(device_name); \n\
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080045";
46
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047ObjectDb::ObjectDb(const fs::path& folder, const std::string& hash)
48 : m_lastUsed(time(NULL))
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080049{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080050 fs::path actualFolder = folder / "objects" / hash.substr(0, 2);
51 fs::create_directories(actualFolder);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080052
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053 _LOG_DEBUG("Open " << (actualFolder / hash.substr(2, hash.size() - 2)));
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080054
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080055 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 Afanasyeva35756b2013-01-22 16:59:11 -080061
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080062 // 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080065 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 Afanasyevb2e608d2013-01-23 20:00:53 -080071
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080072 // _LOG_DEBUG ("open db");
73
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 willStartSave();
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080075}
76
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080077bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078ObjectDb::DoesExist(const boost::filesystem::path& folder, const Ccnx::Name& deviceName,
79 const std::string& hash)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080080{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 fs::path actualFolder = folder / "objects" / hash.substr(0, 2);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080082 bool retval = false;
83
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 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 Afanasyevdbc06712013-01-08 18:30:28 -080091
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf();
93 sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080094
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 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 Afanasyevdbc06712013-01-08 18:30:28 -080099
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 _LOG_TRACE("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800101
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800102 if (countAll > 0 && countAll == countNonNull) {
103 retval = true;
104 }
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800105 }
106
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 sqlite3_finalize(stmt);
108 }
109
110 sqlite3_close(db);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800111 return retval;
112}
113
114
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115ObjectDb::~ObjectDb()
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800116{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 didStopSave();
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800118
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800119 // _LOG_DEBUG ("close db");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 int res = sqlite3_close(m_db);
121 if (res != SQLITE_OK) {
122 // complain
123 }
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800124}
125
126void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127ObjectDb::saveContentObject(const Ccnx::Name& deviceName, sqlite3_int64 segment,
128 const Ccnx::Bytes& data)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800129{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 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 Afanasyev68f2a952013-01-08 14:34:16 -0800135
Zhenkai Zhu81907f22013-01-23 17:32:34 -0800136 //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800137
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 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 Afanasyev68f2a952013-01-08 14:34:16 -0800142
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800143 sqlite3_step(stmt);
Zhenkai Zhub74e1e92013-01-25 14:36:18 -0800144 //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145 sqlite3_finalize(stmt);
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800146
147 // update last used time
148 m_lastUsed = time(NULL);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800149}
150
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151Ccnx::BytesPtr
152ObjectDb::fetchSegment(const Ccnx::Name& deviceName, sqlite3_int64 segment)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800153{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 sqlite3_stmt* stmt;
155 sqlite3_prepare_v2(m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1,
156 &stmt, 0);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800157
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf();
159 sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT);
160 sqlite3_bind_int64(stmt, 2, segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800161
162 BytesPtr ret;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800163
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800164 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 Afanasyev68f2a952013-01-08 14:34:16 -0800168
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800169 ret = make_shared<Bytes>(buf, buf + bufBytes);
170 }
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800171
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800172 sqlite3_finalize(stmt);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800173
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800174 // update last used time
175 m_lastUsed = time(NULL);
176
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800177 return ret;
178}
179
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800180time_t
181ObjectDb::secondsSinceLastUse()
182{
183 return (time(NULL) - m_lastUsed);
184}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800185
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800186// sqlite3_int64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700187// ObjectDb::getNumberOfSegments (const Ndnx::Name &deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800188// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800189// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800190// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800191
192// bool retval = false;
193// int res = sqlite3_step (stmt);
194// if (res == SQLITE_ROW)
195// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800196// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800197// }
198// sqlite3_finalize (stmt);
199
200// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800201// }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800202
203void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800204ObjectDb::willStartSave()
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800205{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800206 sqlite3_exec(m_db, "BEGIN TRANSACTION;", 0, 0, 0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800207 // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800208}
209
210void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800211ObjectDb::didStopSave()
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800212{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800213 sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800214 // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800215}