OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AtomStorage.h
Go to the documentation of this file.
1 /*
2  * FUNCTION:
3  * Base class for SQL-backed persistent storage.
4  *
5  * HISTORY:
6  * Copyright (c) 2008,2009 Linas Vepstas <linasvepstas@gmail.com>
7  *
8  * LICENSE:
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef _OPENCOG_PERSITENT_ATOM_STORAGE_H
26 #define _OPENCOG_PERSITENT_ATOM_STORAGE_H
27 
28 #include <atomic>
29 #include <mutex>
30 #include <set>
31 #include <thread>
32 #include <vector>
33 
34 #include <opencog/util/async_method_caller.h>
35 #include <opencog/atomspace/Atom.h>
36 #include <opencog/atomspace/Link.h>
37 #include <opencog/atomspace/Node.h>
41 
42 namespace opencog
43 {
49 {
50  private:
51  // Pool of shared connections
53  void put_conn(ODBCConnection*);
54  concurrent_stack<ODBCConnection*> conn_pool;
55 
56  // Utility for handling responses on stack.
57  class Response;
58  class Outgoing;
59 
60  void init(const char *, const char *, const char *);
61  AtomPtr makeAtom (Response &, Handle);
62  AtomPtr getAtom (const char *, int);
63 
64  int get_height(AtomPtr);
66  void setMaxHeight(int);
67  int getMaxHeight(void);
68 
70  void vdo_store_atom(AtomPtr&);
71  void do_store_single_atom(AtomPtr, int);
72 
73  std::string oset_to_string(const HandleSeq&, int);
76  bool store_cb(AtomPtr);
77  std::atomic<unsigned long> load_count;
78  std::atomic<unsigned long> store_count;
79 
80  void rename_tables(void);
81  void create_tables(void);
82 
83  // Track UUID's that are in use.
84  std::mutex id_cache_mutex;
86  std::set<UUID> local_id_cache;
87  void add_id_to_cache(UUID);
88  void get_ids(void);
89 
90  std::mutex id_create_mutex;
91  std::set<UUID> id_create_cache;
92  std::unique_lock<std::mutex> maybe_create_id(UUID);
93 
95  int getMaxObservedHeight(void);
96  bool idExists(const char *);
97 
98  // The typemap translates between opencog type numbers and
99  // the database type numbers. Initially, they match up, but
100  // might get askew if new types are added or deleted.
101 
102  // TYPEMAP_SZ is defined as the maximum number of possible
103  // OpenCog Types (65536 as Type is currently a short int)
104  static_assert(2 == sizeof(Type),
105  "*** Typemap needs to be redesigned to handle larger types! ***");
106  #define TYPEMAP_SZ (1 << (8 * sizeof(Type)))
110 
112  void load_typemap(void);
113  void setup_typemap(void);
114  void set_typemap(int, const char *);
115 
116 #ifdef OUT_OF_LINE_TVS
117  bool tvExists(int);
118  int storeTruthValue(AtomPtr, Handle);
119  int TVID(const TruthValue &);
120  TruthValue * getTV(int);
121 #endif /* OUT_OF_LINE_TVS */
122 
123  // Provider of asynchronous store of atoms.
124  async_caller<AtomStorage, AtomPtr> _write_queue;
125 
126  public:
127  AtomStorage(const std::string& dbname,
128  const std::string& username,
129  const std::string& authentication);
130  AtomStorage(const char * dbname,
131  const char * username,
132  const char * authentication);
133  AtomStorage(const AtomStorage&) = delete; // disable copying
134  AtomStorage& operator=(const AtomStorage&) = delete; // disable assignment
135  ~AtomStorage();
136  bool connected(void); // connection to DB is alive
137 
138  void kill_data(void); // destroy DB contents
139 
140  // Store atoms to DB
141  void storeSingleAtom(AtomPtr);
142  void storeAtom(AtomPtr, bool synchronous = false);
143  void flushStoreQueue();
144 
145  // Fetch atoms from DB
146  bool atomExists(Handle);
148  std::vector<Handle> getIncomingSet(Handle);
149  NodePtr getNode(Type, const char *);
150  NodePtr getNode(const Node &n)
151  {
152  return getNode(n.getType(), n.getName().c_str());
153  }
154  LinkPtr getLink(Type, const std::vector<Handle>&);
155  LinkPtr getLink(const Link &l)
156  {
157  return getLink(l.getType(), l.getOutgoingSet());
158  }
159 
160  // Large-scale loads and saves
161  void loadType(AtomTable &, Type); // Load *all* atoms of type
162  void load(AtomTable &); // Load entire contents of DB
163  void store(const AtomTable &); // Store entire contents of AtomTable
164  void reserve(void); // reserve range of UUID's
165 };
166 
168 } // namespace opencog
169 
170 #endif // _OPENCOG_PERSITENT_ATOM_STORAGE_H
void storeSingleAtom(AtomPtr)
void init(const char *, const char *, const char *)
int get_height(AtomPtr)
std::atomic< unsigned long > store_count
Definition: AtomStorage.h:78
char * db_typename[TYPEMAP_SZ]
Definition: AtomStorage.h:109
std::string oset_to_string(const HandleSeq &, int)
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)
void create_tables(void)
void setup_typemap(void)
void load(AtomTable &)
ODBCConnection * get_conn()
Type loading_typemap[TYPEMAP_SZ]
Definition: AtomStorage.h:108
Type getType() const
Definition: Atom.h:197
NodePtr getNode(const Node &n)
Definition: AtomStorage.h:150
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
void vdo_store_atom(AtomPtr &)
void rename_tables(void)
void do_store_single_atom(AtomPtr, int)
void storeOutgoing(AtomPtr, Handle)
unsigned long UUID
UUID == Universally Unique Identifier.
Definition: Handle.h:46
std::mutex id_cache_mutex
Definition: AtomStorage.h:84
bool atomExists(Handle)
AtomPtr makeAtom(Response &, Handle)
const std::string & getName() const
Definition: Node.h:87
void loadType(AtomTable &, Type)
void getOutgoing(HandleSeq &, Handle)
std::set< UUID > id_create_cache
Definition: AtomStorage.h:91
NodePtr getNode(Type, const char *)
void store(const AtomTable &)
AtomStorage(const std::string &dbname, const std::string &username, const std::string &authentication)
AtomPtr getAtom(const char *, int)
int getMaxObservedHeight(void)
#define TYPEMAP_SZ
Definition: AtomStorage.h:106
std::atomic< unsigned long > load_count
Definition: AtomStorage.h:77
int do_store_atom(AtomPtr)
std::set< UUID > local_id_cache
Definition: AtomStorage.h:86
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
async_caller< AtomStorage, AtomPtr > _write_queue
Definition: AtomStorage.h:124
void load_typemap(void)
std::unique_lock< std::mutex > maybe_create_id(UUID)
UUID getMaxObservedUUID(void)
int storing_typemap[TYPEMAP_SZ]
Definition: AtomStorage.h:107
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
std::mutex id_create_mutex
Definition: AtomStorage.h:90
void set_typemap(int, const char *)
std::vector< Handle > getIncomingSet(Handle)
void add_id_to_cache(UUID)
LinkPtr getLink(Type, const std::vector< Handle > &)
bool idExists(const char *)
bool store_cb(AtomPtr)
void put_conn(ODBCConnection *)
concurrent_stack< ODBCConnection * > conn_pool
Definition: AtomStorage.h:54
LinkPtr getLink(const Link &l)
Definition: AtomStorage.h:155
AtomStorage & operator=(const AtomStorage &)=delete