OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SQLPersistSCM.cc
Go to the documentation of this file.
1 /*
2  * opencog/persist/sql/SQLPersistSCM.cc
3  *
4  * Copyright (c) 2008 by OpenCog Foundation
5  * Copyright (c) 2008, 2009, 2013, 2015 Linas Vepstas <linasvepstas@gmail.com>
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
27 
28 #include "SQLPersistSCM.h"
29 #include "AtomStorage.h"
30 
31 using namespace opencog;
32 
33 namespace opencog {
35 {
36  private:
38  public:
40  void set_store(AtomStorage *);
41 
42  virtual NodePtr getNode(Type, const char *) const;
43  virtual LinkPtr getLink(Type, const HandleSeq&) const;
44  virtual AtomPtr getAtom(Handle) const;
45  virtual HandleSeq getIncomingSet(Handle) const;
46  virtual void storeAtom(Handle);
47  virtual void loadType(AtomTable&, Type);
48  virtual void barrier();
49 };
50 };
51 
53 {
54  _store = NULL;
55 }
56 
58 {
59  _store = as;
60 }
61 
62 NodePtr SQLBackingStore::getNode(Type t, const char *name) const
63 {
64  return _store->getNode(t, name);
65 }
66 
67 LinkPtr SQLBackingStore::getLink(Type t, const std::vector<Handle>& oset) const
68 {
69  return _store->getLink(t, oset);
70 }
71 
73 {
74  return _store->getAtom(h);
75 }
76 
78 {
79  return _store->getIncomingSet(h);
80 }
81 
83 {
84  _store->storeAtom(h);
85 }
86 
88 {
89  _store->loadType(at, t);
90 }
91 
93 {
95 }
96 
97 // =================================================================
98 
100 {
101  _as = as;
102  _store = NULL;
103  _backing = new SQLBackingStore();
104 
105  // XXX FIXME Huge hack alert.
106  // As of 2013, no one uses this thing, except for NLP processing.
107  // Since I'm too lazy to find an elegant solution right now, I'm
108  // just going to hack this in. Fix this someday.
109  //
110  // Anyway, what the below does is to ignore these certain types,
111  // when they are to be fetched from the backing store. This can
112  // speed up document processing, since we know that word instances
113  // and documents and sentences will not be stored in the database.
114  // Thus, we don't even try to fetch these.
115 
116 #define NLP_HACK 0
117 #if NLP_HACK
118  _backing->_ignored_types.insert(VARIABLE_NODE);
119  _backing->_ignored_types.insert(TYPE_NODE);
120  _backing->_ignored_types.insert(TYPED_VARIABLE_LINK);
121  _backing->_ignored_types.insert(BIND_LINK);
122 
123  _backing->_ignored_types.insert(DOCUMENT_NODE);
124  _backing->_ignored_types.insert(SENTENCE_NODE);
125  _backing->_ignored_types.insert(PARSE_NODE);
126  _backing->_ignored_types.insert(PARSE_LINK);
127  _backing->_ignored_types.insert(WORD_INSTANCE_NODE);
128  _backing->_ignored_types.insert(WORD_INSTANCE_LINK);
129 #endif // NLP_HACK
130 
131 #ifdef HAVE_GUILE
132  static bool is_init = false;
133  if (is_init) return;
134  is_init = true;
135  scm_with_guile(init_in_guile, this);
136 #endif
137 }
138 
140 {
141 #ifdef HAVE_GUILE
142  scm_c_define_module("opencog persist-sql", init_in_module, self);
143  scm_c_use_module("opencog persist-sql");
144 #endif
145  return NULL;
146 }
147 
149 {
150  SQLPersistSCM* self = (SQLPersistSCM*) data;
151  self->init();
152 }
153 
155 {
156 #ifdef HAVE_GUILE
157  define_scheme_primitive("sql-open", &SQLPersistSCM::do_open, this, "persist-sql");
158  define_scheme_primitive("sql-close", &SQLPersistSCM::do_close, this, "persist-sql");
159  define_scheme_primitive("sql-load", &SQLPersistSCM::do_load, this, "persist-sql");
160  define_scheme_primitive("sql-store", &SQLPersistSCM::do_store, this, "persist-sql");
161 #endif
162 }
163 
165 {
166  delete _backing;
167 }
168 
169 void SQLPersistSCM::do_open(const std::string& dbname,
170  const std::string& username,
171  const std::string& auth)
172 {
173  _store = new AtomStorage(dbname, username, auth);
174  if (!_store)
175  throw RuntimeException(TRACE_INFO,
176  "sql-open: Error: Unable to open the database");
177 
178  if (!_store->connected())
179  {
180  delete _store;
181  _store = NULL;
182  throw RuntimeException(TRACE_INFO,
183  "sql-open: Error: Unable to connect to the database");
184  }
185 
186  // reserve() is critical here, to reserve UUID range.
187  _store->reserve();
189  AtomSpace *as = _as;
190 #ifdef HAVE_GUILE
191  if (NULL == as)
192  as = SchemeSmob::ss_get_env_as("sql-open");
193 #endif
195 }
196 
198 {
199  if (_store == NULL)
200  throw RuntimeException(TRACE_INFO,
201  "sql-close: Error: Database not open");
202 
203  AtomSpace *as = _as;
204 #ifdef HAVE_GUILE
205  if (NULL == as)
206  as = SchemeSmob::ss_get_env_as("sql-close");
207 #endif
209 
210  _backing->set_store(NULL);
211  delete _store;
212  _store = NULL;
213 }
214 
216 {
217  if (_store == NULL)
218  throw RuntimeException(TRACE_INFO,
219  "sql-load: Error: Database not open");
220 
221  AtomSpace *as = _as;
222 #ifdef HAVE_GUILE
223  if (NULL == as)
224  as = SchemeSmob::ss_get_env_as("sql-load");
225 #endif
226  // XXX TODO: this should probably be done in a separate thread.
227  _store->load(const_cast<AtomTable&>(as->get_atomtable()));
228 }
229 
230 
232 {
233  if (_store == NULL)
234  throw RuntimeException(TRACE_INFO,
235  "sql-store: Error: Database not open");
236 
237  AtomSpace *as = _as;
238 #ifdef HAVE_GUILE
239  if (NULL == as)
240  as = SchemeSmob::ss_get_env_as("sql-store");
241 #endif
242  // XXX TODO This should really be started in a new thread ...
243  _store->store(const_cast<AtomTable&>(as->get_atomtable()));
244 }
245 
247 {
248  static SQLPersistSCM patty(NULL);
249 }
AtomStorage * _store
Definition: SQLPersistSCM.h:49
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
void storeAtom(AtomPtr, bool synchronous=false)
virtual HandleSeq getIncomingSet(Handle) const
void load(AtomTable &)
void registerBackingStore(BackingStore *)
Definition: AtomSpace.cc:88
void set_store(AtomStorage *)
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
void unregisterBackingStore(BackingStore *)
Definition: AtomSpace.cc:93
virtual void storeAtom(Handle)
virtual LinkPtr getLink(Type, const HandleSeq &) const
AtomTable & get_atomtable(void)
Definition: AtomSpace.h:74
void opencog_persist_sql_init(void)
virtual AtomPtr getAtom(Handle) const
void loadType(AtomTable &, Type)
static void init_in_module(void *)
NodePtr getNode(Type, const char *)
void store(const AtomTable &)
AtomPtr getAtom(const char *, int)
static void * init_in_guile(void *)
SQLBackingStore * _backing
Definition: SQLPersistSCM.h:48
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
std::set< Type > _ignored_types
Definition: BackingStore.h:119
virtual void loadType(AtomTable &, Type)
std::vector< Handle > getIncomingSet(Handle)
static AtomSpace * ss_get_env_as(const char *)
LinkPtr getLink(Type, const std::vector< Handle > &)
void do_open(const std::string &, const std::string &, const std::string &)
virtual NodePtr getNode(Type, const char *) const
SQLPersistSCM(AtomSpace *)