blob: 7567c830e30170d484f7f63a31c28db9879a17de [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)
49{
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
80ObjectDb::DoesExist (const boost::filesystem::path &folder, const Ccnx::Name &deviceName, const std::string &hash)
81{
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
92 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
93 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
130ObjectDb::saveContentObject (const Ccnx::Name &deviceName, sqlite3_int64 segment, const Ccnx::Bytes &data)
131{
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 Afanasyev68f2a952013-01-08 14:34:16 -0800139 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
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);
147}
148
149Ccnx::BytesPtr
150ObjectDb::fetchSegment (const Ccnx::Name &deviceName, sqlite3_int64 segment)
151{
152 sqlite3_stmt *stmt;
153 sqlite3_prepare_v2 (m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0);
154
155 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
156 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
157 sqlite3_bind_int64 (stmt, 2, segment);
158
159 BytesPtr ret;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800160
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800161 int res = sqlite3_step (stmt);
162 if (res == SQLITE_ROW)
163 {
164 const unsigned char *buf = reinterpret_cast<const unsigned char*> (sqlite3_column_blob (stmt, 0));
165 int bufBytes = sqlite3_column_bytes (stmt, 0);
166
167 ret = make_shared<Bytes> (buf, buf+bufBytes);
168 }
169
170 sqlite3_finalize (stmt);
171
172 return ret;
173}
174
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800175
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800176// sqlite3_int64
177// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
178// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800179// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800180// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800181
182// bool retval = false;
183// int res = sqlite3_step (stmt);
184// if (res == SQLITE_ROW)
185// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800186// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800187// }
188// sqlite3_finalize (stmt);
189
190// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800191// }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800192
193void
194ObjectDb::willStartSave ()
195{
196 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800197 // _LOG_DEBUG ("Open transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800198}
199
200void
201ObjectDb::didStopSave ()
202{
203 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800204 // _LOG_DEBUG ("Close transaction: " << sqlite3_errmsg (m_db));
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800205}