blob: 3b1a0f2b98eca50736482679deab80901c83d6c1 [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"
22#include <iostream>
23#include <boost/make_shared.hpp>
24#include "db-helper.h"
25#include <sys/stat.h>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080026#include "logging.h"
27
28INIT_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
47ObjectDb::ObjectDb (const fs::path &folder, const std::string &hash)
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080048 : m_lastUsed (time(NULL))
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080049{
50 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
51 fs::create_directories (actualFolder);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080052
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080053 _LOG_DEBUG ("Open " << (actualFolder / hash.substr (2, hash.size () - 2)));
54
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080055 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 Afanasyeva35756b2013-01-22 16:59:11 -080062
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080063 // 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 Afanasyev9ca444e2013-01-25 16:29:35 -080070 // _LOG_TRACE ("Init \"error\": " << errmsg);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080071 sqlite3_free (errmsg);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080072 }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080073
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080074 // _LOG_DEBUG ("open db");
75
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080076 willStartSave ();
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080077}
78
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080079bool
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070080ObjectDb::DoesExist (const boost::filesystem::path &folder, const Ndnx::Name &deviceName, const std::string &hash)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080081{
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 Afanasyev1dd37ed2013-08-14 18:08:09 -070092 NdnxCharbufPtr buf = deviceName.toNdnxCharbuf ();
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080093 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 Afanasyev9ca444e2013-01-25 16:29:35 -0800101 _LOG_TRACE ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800102
103 if (countAll > 0 && countAll==countNonNull)
104 {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800105 retval = true;
106 }
107 }
108
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800109 sqlite3_finalize (stmt);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800110 }
111
112 sqlite3_close (db);
113 return retval;
114}
115
116
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800117ObjectDb::~ObjectDb ()
118{
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800119 didStopSave ();
120
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800121 // _LOG_DEBUG ("close db");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800122 int res = sqlite3_close (m_db);
123 if (res != SQLITE_OK)
124 {
125 // complain
126 }
127}
128
129void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700130ObjectDb::saveContentObject (const Ndnx::Name &deviceName, sqlite3_int64 segment, const Ndnx::Bytes &data)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800131{
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 Zhu81907f22013-01-23 17:32:34 -0800137 //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800138
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700139 NdnxCharbufPtr buf = deviceName.toNdnxCharbuf ();
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800140 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_STATIC);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800141 sqlite3_bind_int64 (stmt, 2, segment);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800142 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_STATIC);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800143
144 sqlite3_step (stmt);
Zhenkai Zhub74e1e92013-01-25 14:36:18 -0800145 //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db));
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800146 sqlite3_finalize (stmt);
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800147
148 // update last used time
149 m_lastUsed = time(NULL);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800150}
151
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700152Ndnx::BytesPtr
153ObjectDb::fetchSegment (const Ndnx::Name &deviceName, sqlite3_int64 segment)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800154{
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 Afanasyev1dd37ed2013-08-14 18:08:09 -0700158 NdnxCharbufPtr buf = deviceName.toNdnxCharbuf ();
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800159 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
160 sqlite3_bind_int64 (stmt, 2, segment);
161
162 BytesPtr ret;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800163
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800164 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 Zhu92bb6952013-02-06 16:43:30 -0800175 // update last used time
176 m_lastUsed = time(NULL);
177
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800178 return ret;
179}
180
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800181time_t
182ObjectDb::secondsSinceLastUse()
183{
184 return (time(NULL) - m_lastUsed);
185}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800186
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800187// sqlite3_int64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700188// ObjectDb::getNumberOfSegments (const Ndnx::Name &deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800189// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800190// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800191// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800192
193// bool retval = false;
194// int res = sqlite3_step (stmt);
195// if (res == SQLITE_ROW)
196// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800197// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800198// }
199// sqlite3_finalize (stmt);
200
201// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800202// }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800203
204void
205ObjectDb::willStartSave ()
206{
207 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800208 // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800209}
210
211void
212ObjectDb::didStopSave ()
213{
214 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800215 // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800216}