blob: 6120dc0743f780cab8149e2e6c67179c7439d741 [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 Afanasyev68f2a952013-01-08 14:34:16 -080053 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 Afanasyeva35756b2013-01-22 16:59:11 -080060
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080061 // 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 Afanasyevdbc06712013-01-08 18:30:28 -080068 // std::cerr << "DEBUG: " << errmsg << std::endl;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080069 sqlite3_free (errmsg);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080070 }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080071
72 willStartSave ();
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080073}
74
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080075bool
76ObjectDb::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 Afanasyeva35756b2013-01-22 16:59:11 -080097 _LOG_DEBUG ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080098
99 if (countAll > 0 && countAll==countNonNull)
100 {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800101 retval = true;
102 }
103 }
104
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800105 sqlite3_finalize (stmt);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800106 }
107
108 sqlite3_close (db);
109 return retval;
110}
111
112
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800113ObjectDb::~ObjectDb ()
114{
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800115 didStopSave ();
116
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800117 int res = sqlite3_close (m_db);
118 if (res != SQLITE_OK)
119 {
120 // complain
121 }
122}
123
124void
125ObjectDb::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 Zhu81907f22013-01-23 17:32:34 -0800132 //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800133
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800134 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
135 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
136 sqlite3_bind_int64 (stmt, 2, segment);
137 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_TRANSIENT);
138
139 sqlite3_step (stmt);
140 sqlite3_finalize (stmt);
141}
142
143Ccnx::BytesPtr
144ObjectDb::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 Afanasyeva35756b2013-01-22 16:59:11 -0800154
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800155 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 Afanasyevdbc06712013-01-08 18:30:28 -0800169
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800170// sqlite3_int64
171// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
172// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800173// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800174// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800175
176// bool retval = false;
177// int res = sqlite3_step (stmt);
178// if (res == SQLITE_ROW)
179// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800180// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800181// }
182// sqlite3_finalize (stmt);
183
184// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800185// }
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -0800186
187void
188ObjectDb::willStartSave ()
189{
190 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
191}
192
193void
194ObjectDb::didStopSave ()
195{
196 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
197}