blob: 2243b997510e6c9a44a2b7a391679d8da58449c5 [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 = "\
37CREATE TABLE \n\
38 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 Afanasyev68f2a952013-01-08 14:34:16 -080071}
72
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080073bool
74ObjectDb::DoesExist (const boost::filesystem::path &folder, const Ccnx::Name &deviceName, const std::string &hash)
75{
76 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
77 bool retval = false;
78
79 sqlite3 *db;
80 int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &db);
81 if (res == SQLITE_OK)
82 {
83 sqlite3_stmt *stmt;
84 sqlite3_prepare_v2 (db, "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", -1, &stmt, 0);
85
86 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
87 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
88
89 int res = sqlite3_step (stmt);
90 if (res == SQLITE_ROW)
91 {
92 int countAll = sqlite3_column_int (stmt, 0);
93 int countNonNull = sqlite3_column_int (stmt, 1);
94
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080095 _LOG_DEBUG ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080096
97 if (countAll > 0 && countAll==countNonNull)
98 {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080099 retval = true;
100 }
101 }
102
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800103 sqlite3_finalize (stmt);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800104 }
105
106 sqlite3_close (db);
107 return retval;
108}
109
110
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800111ObjectDb::~ObjectDb ()
112{
113 int res = sqlite3_close (m_db);
114 if (res != SQLITE_OK)
115 {
116 // complain
117 }
118}
119
120void
121ObjectDb::saveContentObject (const Ccnx::Name &deviceName, sqlite3_int64 segment, const Ccnx::Bytes &data)
122{
123 sqlite3_stmt *stmt;
124 sqlite3_prepare_v2 (m_db, "INSERT INTO File "
125 "(device_name, segment, content_object) "
126 "VALUES (?, ?, ?)", -1, &stmt, 0);
127
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800128 _LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]");
129
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800130 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
131 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
132 sqlite3_bind_int64 (stmt, 2, segment);
133 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_TRANSIENT);
134
135 sqlite3_step (stmt);
136 sqlite3_finalize (stmt);
137}
138
139Ccnx::BytesPtr
140ObjectDb::fetchSegment (const Ccnx::Name &deviceName, sqlite3_int64 segment)
141{
142 sqlite3_stmt *stmt;
143 sqlite3_prepare_v2 (m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0);
144
145 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
146 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
147 sqlite3_bind_int64 (stmt, 2, segment);
148
149 BytesPtr ret;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800150
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800151 int res = sqlite3_step (stmt);
152 if (res == SQLITE_ROW)
153 {
154 const unsigned char *buf = reinterpret_cast<const unsigned char*> (sqlite3_column_blob (stmt, 0));
155 int bufBytes = sqlite3_column_bytes (stmt, 0);
156
157 ret = make_shared<Bytes> (buf, buf+bufBytes);
158 }
159
160 sqlite3_finalize (stmt);
161
162 return ret;
163}
164
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800165
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800166// sqlite3_int64
167// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
168// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800169// sqlite3_stmt *stmt;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800170// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800171
172// bool retval = false;
173// int res = sqlite3_step (stmt);
174// if (res == SQLITE_ROW)
175// {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800176// retval = true;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800177// }
178// sqlite3_finalize (stmt);
179
180// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800181// }