blob: 538e137a7db9ca85c826a8ae2aae5bf8760d22cb [file] [log] [blame]
Alexander Afanasyev71b43e72012-12-27 01:03:43 -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
Alexander Afanasyev242f8772013-01-01 23:26:31 -080022#include "db-helper.h"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080023#include "logging.h"
24
Alexander Afanasyevae43c502012-12-29 17:26:37 -080025#include <boost/make_shared.hpp>
26#include <boost/ref.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080027#include <boost/throw_exception.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080028
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080029INIT_LOGGER ("DbHelper");
30
Alexander Afanasyevae43c502012-12-29 17:26:37 -080031using namespace boost;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080032namespace fs = boost::filesystem;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080033
34const std::string INIT_DATABASE = "\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080035 PRAGMA foreign_keys = ON; \
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080036";
37
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080038DbHelper::DbHelper (const fs::path &path, const std::string &dbname)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080039{
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080040 fs::create_directories (path);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080041
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080042 int res = sqlite3_open((path / dbname).c_str (), &m_db);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080043 if (res != SQLITE_OK)
44 {
45 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080046 << errmsg_info_str ("Cannot open/create dabatabase: [" + (path / dbname).string () + "]"));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080047 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080048
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080049 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -080050 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080051 if (res != SQLITE_OK)
52 {
53 BOOST_THROW_EXCEPTION (Error::Db ()
54 << errmsg_info_str ("Cannot create function ``hash''"));
55 }
56
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080057 res = sqlite3_create_function (m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0);
58 if (res != SQLITE_OK)
59 {
60 BOOST_THROW_EXCEPTION (Error::Db ()
61 << errmsg_info_str ("Cannot create function ``is_prefix''"));
62 }
63
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080064 res = sqlite3_create_function (m_db, "directory_name", -1, SQLITE_ANY, 0, DbHelper::directory_name_xFun, 0, 0);
65 if (res != SQLITE_OK)
66 {
67 BOOST_THROW_EXCEPTION (Error::Db ()
68 << errmsg_info_str ("Cannot create function ``directory_name''"));
69 }
70
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080071 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
72 // for now, just attempt to create everything
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080073 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
74 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080075}
76
77DbHelper::~DbHelper ()
78{
79 int res = sqlite3_close (m_db);
80 if (res != SQLITE_OK)
81 {
82 // complain
83 }
84}
85
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080086void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080087DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
88{
89 if (argc != 2)
90 {
91 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
92 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
93 return;
94 }
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080095 if (sqlite3_value_type (argv[0]) != SQLITE_BLOB ||
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080096 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
97 {
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080098 // _LOG_ERROR ("Hash expects (blob,integer) parameters");
99 sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800100 return;
101 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800102
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800103 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
104
105 if (hash_context == 0)
106 {
107 sqlite3_result_error_nomem (context);
108 return;
109 }
110
111 if (*hash_context == 0)
112 {
113 *hash_context = EVP_MD_CTX_create ();
114 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
115 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800116
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800117 int nameBytes = sqlite3_value_bytes (argv[0]);
118 const void *name = sqlite3_value_blob (argv[0]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800119 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
120
121 EVP_DigestUpdate (*hash_context, name, nameBytes);
122 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
123}
124
125void
126DbHelper::hash_xFinal (sqlite3_context *context)
127{
128 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
129
130 if (hash_context == 0)
131 {
132 sqlite3_result_error_nomem (context);
133 return;
134 }
135
136 if (*hash_context == 0) // no rows
137 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800138 char charNullResult = 0;
139 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800140 return;
141 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800142
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800143 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
144 unsigned int hashLength = 0;
145
146 int ok = EVP_DigestFinal_ex (*hash_context,
147 hash, &hashLength);
148
149 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
150 delete [] hash;
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800151
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800152 EVP_MD_CTX_destroy (*hash_context);
153}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800154
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800155void
156DbHelper::is_prefix_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
157{
158 int len1 = sqlite3_value_bytes (argv[0]);
159 int len2 = sqlite3_value_bytes (argv[1]);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800160
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800161 if (len1 == 0)
162 {
163 sqlite3_result_int (context, 1);
164 return;
165 }
166
167 if (len1 > len2) // first parameter should be at most equal in length to the second one
168 {
169 sqlite3_result_int (context, 0);
170 return;
171 }
172
173 if (memcmp (sqlite3_value_blob (argv[0]), sqlite3_value_blob (argv[1]), len1) == 0)
174 {
175 sqlite3_result_int (context, 1);
176 }
177 else
178 {
179 sqlite3_result_int (context, 0);
180 }
181}
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800182
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800183void
184DbHelper::directory_name_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
185{
186 if (argc != 1)
187 {
188 sqlite3_result_error (context, "``directory_name'' expects 1 text argument", -1);
189 sqlite3_result_null (context);
190 return;
191 }
192
193 if (sqlite3_value_bytes (argv[0]) == 0)
194 {
195 sqlite3_result_null (context);
196 return;
197 }
198
199 boost::filesystem::path filePath (std::string (reinterpret_cast<const char*> (sqlite3_value_text (argv[0])), sqlite3_value_bytes (argv[0])));
200 std::string dirPath = filePath.parent_path ().generic_string ();
201 // _LOG_DEBUG ("directory_name FUN: " << dirPath);
202 if (dirPath.size () == 0)
203 {
204 sqlite3_result_null (context);
205 }
206 else
207 {
208 sqlite3_result_text (context, dirPath.c_str (), dirPath.size (), SQLITE_TRANSIENT);
209 }
210}