Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 22 | #ifndef OBJECT_DB_H |
| 23 | #define OBJECT_DB_H |
| 24 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <sqlite3.h> |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 27 | #include <ndnx-common.h> |
| 28 | #include <ndnx-name.h> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 29 | #include <boost/filesystem.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 30 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 31 | #include <ctime> |
| 32 | #include <vector> |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 34 | class ObjectDb |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 35 | { |
| 36 | public: |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 37 | // database will be create in <folder>/<first-pair-of-hash-bytes>/<rest-of-hash> |
| 38 | ObjectDb (const boost::filesystem::path &folder, const std::string &hash); |
| 39 | ~ObjectDb (); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 41 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 42 | saveContentObject (const Ndnx::Name &deviceName, sqlite3_int64 segment, const Ndnx::Bytes &data); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 44 | Ndnx::BytesPtr |
| 45 | fetchSegment (const Ndnx::Name &deviceName, sqlite3_int64 segment); |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 47 | // sqlite3_int64 |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 48 | // getNumberOfSegments (const Ndnx::Name &deviceName); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 49 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 50 | time_t |
| 51 | secondsSinceLastUse(); |
| 52 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 53 | static bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 54 | DoesExist (const boost::filesystem::path &folder, const Ndnx::Name &deviceName, const std::string &hash); |
Alexander Afanasyev | b2e608d | 2013-01-23 20:00:53 -0800 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | void |
| 58 | willStartSave (); |
| 59 | |
| 60 | void |
| 61 | didStopSave (); |
| 62 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 63 | private: |
| 64 | sqlite3 *m_db; |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 65 | time_t m_lastUsed; |
Zhenkai Zhu | 7717d20 | 2012-12-29 18:01:36 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 68 | typedef boost::shared_ptr<ObjectDb> ObjectDbPtr; |
| 69 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 70 | #endif // OBJECT_DB_H |