blob: 05aca3483d96a95dcb73da528d9c5b10d3e329a0 [file] [log] [blame]
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08001/* -*- 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 Afanasyeva35756b2013-01-22 16:59:11 -080027#include "logging.h"
28
29INIT_LOGGER ("Object.Db");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080030
31using namespace std;
32using namespace Ccnx;
33using namespace boost;
34namespace fs = boost::filesystem;
35
36const std::string INIT_DATABASE = "\
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080037CREATE TABLE \n \
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080038 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 Afanasyevdbc06712013-01-08 18:30:28 -080045CREATE INDEX device ON File(device_name); \n\
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080046";
47
48ObjectDb::ObjectDb (const fs::path &folder, const std::string &hash)
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080049 : m_lastUsed (time(NULL))
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080050{
51 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
52 fs::create_directories (actualFolder);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080053
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080054 _LOG_DEBUG ("Open " << (actualFolder / hash.substr (2, hash.size () - 2)));
55
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080056 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 Afanasyeva35756b2013-01-22 16:59:11 -080063
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080064 // 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 Afanasyev9ca444e2013-01-25 16:29:35 -080071 // _LOG_TRACE ("Init \"error\": " << errmsg);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080072 sqlite3_free (errmsg);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080073 }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080074
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -080075 // _LOG_DEBUG ("open db");
76
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080077 willStartSave ();
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080078}
79
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080080bool
81ObjectDb::DoesExist (const boost::filesystem::path &folder, const Ccnx::Name &deviceName, const std::string &hash)
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
93 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
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 Afanasyev9ca444e2013-01-25 16:29:35 -0800102 _LOG_TRACE ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800103
104 if (countAll > 0 && countAll==countNonNull)
105 {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800106 retval = true;
107 }
108 }
109
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800110 sqlite3_finalize (stmt);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800111 }
112
113 sqlite3_close (db);
114 return retval;
115}
116
117
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800118ObjectDb::~ObjectDb ()
119{
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800120 didStopSave ();
121
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800122 // _LOG_DEBUG ("close db");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800123 int res = sqlite3_close (m_db);
124 if (res != SQLITE_OK)
125 {
126 // complain
127 }
128}
129
130void
131ObjectDb::saveContentObject (const Ccnx::Name &deviceName, sqlite3_int64 segment, const Ccnx::Bytes &data)
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 Zhu81907f22013-01-23 17:32:34 -0800138 //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800139
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800140 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800141 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_STATIC);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800142 sqlite3_bind_int64 (stmt, 2, segment);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800143 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_STATIC);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800144
145 sqlite3_step (stmt);
Zhenkai Zhub74e1e92013-01-25 14:36:18 -0800146 //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db));
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800147 sqlite3_finalize (stmt);
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800148
149 // update last used time
150 m_lastUsed = time(NULL);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800151}
152
153Ccnx::BytesPtr
154ObjectDb::fetchSegment (const Ccnx::Name &deviceName, sqlite3_int64 segment)
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
159 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
160 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
161 sqlite3_bind_int64 (stmt, 2, segment);
162
163 BytesPtr ret;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800164
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800165 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 Zhu92bb6952013-02-06 16:43:30 -0800176 // update last used time
177 m_lastUsed = time(NULL);
178
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800179 return ret;
180}
181
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800182time_t
183ObjectDb::secondsSinceLastUse()
184{
185 return (time(NULL) - m_lastUsed);
186}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800187
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800188// sqlite3_int64
189// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
190// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800191// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800192// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800193
194// bool retval = false;
195// int res = sqlite3_step (stmt);
196// if (res == SQLITE_ROW)
197// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800198// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800199// }
200// sqlite3_finalize (stmt);
201
202// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800203// }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800204
205void
206ObjectDb::willStartSave ()
207{
208 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800209 // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800210}
211
212void
213ObjectDb::didStopSave ()
214{
215 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800216 // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800217}