blob: 8cadf7e478b8fb5d222906b75dd958ea9c75c54f [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\
42";
43
44ObjectDb::ObjectDb (const fs::path &folder, const std::string &hash)
45{
46 fs::path actualFolder = folder / "objects" / hash.substr (0, 2);
47 fs::create_directories (actualFolder);
48
49 int res = sqlite3_open((actualFolder / hash.substr (2, hash.size () - 2)).c_str (), &m_db);
50 if (res != SQLITE_OK)
51 {
52 BOOST_THROW_EXCEPTION (Error::Db ()
53 << errmsg_info_str ("Cannot open/create dabatabase: [" +
54 (actualFolder / hash.substr (2, hash.size () - 2)).string () + "]"));
55 }
56
57 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
58 // for now, just attempt to create everything
59
60 char *errmsg = 0;
61 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
62 if (res != SQLITE_OK && errmsg != 0)
63 {
64 std::cerr << "DEBUG: " << errmsg << std::endl;
65 sqlite3_free (errmsg);
66 }
67}
68
69ObjectDb::~ObjectDb ()
70{
71 int res = sqlite3_close (m_db);
72 if (res != SQLITE_OK)
73 {
74 // complain
75 }
76}
77
78void
79ObjectDb::saveContentObject (const Ccnx::Name &deviceName, sqlite3_int64 segment, const Ccnx::Bytes &data)
80{
81 sqlite3_stmt *stmt;
82 sqlite3_prepare_v2 (m_db, "INSERT INTO File "
83 "(device_name, segment, content_object) "
84 "VALUES (?, ?, ?)", -1, &stmt, 0);
85
86 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
87 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
88 sqlite3_bind_int64 (stmt, 2, segment);
89 sqlite3_bind_blob (stmt, 3, &data[0], data.size (), SQLITE_TRANSIENT);
90
91 sqlite3_step (stmt);
92 sqlite3_finalize (stmt);
93}
94
95Ccnx::BytesPtr
96ObjectDb::fetchSegment (const Ccnx::Name &deviceName, sqlite3_int64 segment)
97{
98 sqlite3_stmt *stmt;
99 sqlite3_prepare_v2 (m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0);
100
101 CcnxCharbufPtr buf = deviceName.toCcnxCharbuf ();
102 sqlite3_bind_blob (stmt, 1, buf->buf (), buf->length (), SQLITE_TRANSIENT);
103 sqlite3_bind_int64 (stmt, 2, segment);
104
105 BytesPtr ret;
106
107 int res = sqlite3_step (stmt);
108 if (res == SQLITE_ROW)
109 {
110 const unsigned char *buf = reinterpret_cast<const unsigned char*> (sqlite3_column_blob (stmt, 0));
111 int bufBytes = sqlite3_column_bytes (stmt, 0);
112
113 ret = make_shared<Bytes> (buf, buf+bufBytes);
114 }
115
116 sqlite3_finalize (stmt);
117
118 return ret;
119}
120
121// sqlite3_int64
122// ObjectDb::getNumberOfSegments (const Ccnx::Name &deviceName)
123// {
124// }