blob: 0d1a2a6e12058f7234d184c121c9497449c73411 [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>
27
28using namespace std;
29using namespace Ccnx;
30using namespace boost;
31namespace fs = boost::filesystem;
32
33const std::string INIT_DATABASE = "\
34CREATE TABLE \n\
35 File( \n\
36 device_name BLOB NOT NULL, \n\
37 segment INTEGER, \n\
38 content_object BLOB, \n\
39 \
40 PRIMARY KEY (device_name, segment) \n\
41 ); \n\
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080042CREATE INDEX device ON File(device_name); \n\
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080043";
44
45ObjectDb::ObjectDb (const fs::path &folder, const std::string &hash)
46{
47 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
48 fs::create_directories (actualFolder);
49
50 int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &m_db);
51 if (res != SQLITE_OK)
52 {
53 BOOST_THROW_EXCEPTION (Error::Db ()
54 << errmsg_info_str ("Cannot open/create dabatabase: [" +
55 (actualFolder / hash.substr (2, hash.size () - 2)).string () + "]"));
56 }
57
58 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
59 // for now, just attempt to create everything
60
61 char *errmsg = 0;
62 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
63 if (res != SQLITE_OK && errmsg != 0)
64 {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080065 // std::cerr << "DEBUG: " << errmsg << std::endl;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080066 sqlite3_free (errmsg);
67 }
68}
69
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080070bool
71ObjectDb::DoesExist (const boost::filesystem::path &folder, const Ccnx::Name &deviceName, const std::string &hash)
72{
73 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
74 bool retval = false;
75
76 sqlite3 *db;
77 int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &db);
78 if (res == SQLITE_OK)
79 {
80 sqlite3_stmt *stmt;
81 sqlite3_prepare_v2 (db, "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", -1, &stmt, 0);
82
83 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
84 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
85
86 int res = sqlite3_step (stmt);
87 if (res == SQLITE_ROW)
88 {
89 int countAll = sqlite3_column_int (stmt, 0);
90 int countNonNull = sqlite3_column_int (stmt, 1);
91
92 cout << countAll << ", " << countNonNull << endl;
93
94 if (countAll > 0 && countAll==countNonNull)
95 {
96 cout << "2" << endl;
97 retval = true;
98 }
99 }
100
101 sqlite3_finalize (stmt);
102 }
103
104 sqlite3_close (db);
105 return retval;
106}
107
108
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800109ObjectDb::~ObjectDb ()
110{
111 int res = sqlite3_close (m_db);
112 if (res != SQLITE_OK)
113 {
114 // complain
115 }
116}
117
118void
119ObjectDb::saveContentObject (const Ccnx::Name &deviceName, sqlite3_int64 segment, const Ccnx::Bytes &data)
120{
121 sqlite3_stmt *stmt;
122 sqlite3_prepare_v2 (m_db, "INSERT INTO File "
123 "(device_name, segment, content_object) "
124 "VALUES (?, ?, ?)", -1, &stmt, 0);
125
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800126 cout << deviceName << endl;
127
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800128 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
129 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
130 sqlite3_bind_int64 (stmt, 2, segment);
131 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_TRANSIENT);
132
133 sqlite3_step (stmt);
134 sqlite3_finalize (stmt);
135}
136
137Ccnx::BytesPtr
138ObjectDb::fetchSegment (const Ccnx::Name &deviceName, sqlite3_int64 segment)
139{
140 sqlite3_stmt *stmt;
141 sqlite3_prepare_v2 (m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0);
142
143 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
144 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
145 sqlite3_bind_int64 (stmt, 2, segment);
146
147 BytesPtr ret;
148
149 int res = sqlite3_step (stmt);
150 if (res == SQLITE_ROW)
151 {
152 const unsigned char *buf = reinterpret_cast<const unsigned char*> (sqlite3_column_blob (stmt, 0));
153 int bufBytes = sqlite3_column_bytes (stmt, 0);
154
155 ret = make_shared<Bytes> (buf, buf+bufBytes);
156 }
157
158 sqlite3_finalize (stmt);
159
160 return ret;
161}
162
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800163
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800164// sqlite3_int64
165// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
166// {
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800167// sqlite3_stmt *stmt;
168// sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM File WHERE device_name=?", -1, &stmt, 0);
169
170// bool retval = false;
171// int res = sqlite3_step (stmt);
172// if (res == SQLITE_ROW)
173// {
174// retval = true;
175// }
176// sqlite3_finalize (stmt);
177
178// return retval;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800179// }