Remoto - VFS: VFS_mongo.h Source File
Remoto - VFS
VFS_mongo.h
Go to the documentation of this file.
1 #ifndef VFS_MONGOHD_H
2 #define VFS_MONGOHD_H
3 
5 
6 #include <stdexcept>
7 
8 #ifndef Q_MOC_RUN
9 #include <mongocxx/instance.hpp>
10 #include <mongocxx/database.hpp>
11 #include <mongocxx/client.hpp>
12 #include <mongocxx/uri.hpp>
13 #endif
14 
15 using namespace mongocxx;
16 
17 class VFS_mongo;
18 
20 {
21  Q_OBJECT
22 
23  friend class VFS_mongo;
24 
25  public:
26  explicit VFS_mongo_cache_entry(VFS_mongo *mongo, QString path, bool debug, int flushInterval, int expireInterval, bool create=false, bool container=false, QJsonObject createData=QJsonObject());
27  virtual ~VFS_mongo_cache_entry();
28 
29  virtual bool valid(QString &reason);
30  virtual bool isContainer();
31 
32  protected:
34  QString _path;
35  bool _container;
36 
37  virtual void flush();
38 
39  public slots:
40 };
41 
42 class VFS_mongo : public VFS_datastore
43 {
44  Q_OBJECT
45 
46  friend class VFS_mongo_cache_entry; //for update_one and find_one
47 
48  public:
49  Q_INVOKABLE explicit VFS_mongo( QString uri, QString db, QString collection, quint64 size=VFS_datastore_cache::DEFAULT_DATACACHE_SIZE, bool debug=false, int flushInterval=5000, int expireInterval=60000, bool createCollection=false );
50  virtual ~VFS_mongo();
51 
52  virtual QString reportDetails();
53 
54  protected:
57 
58  virtual QByteArray icon();
59 
60  virtual void ls(VFS_request *r);
61  virtual void read(VFS_request *r);
62  virtual void write(VFS_request *r);
63  virtual void metadata(VFS_request *r);
64  //virtual void report(VFS_request *r);
65  virtual void submit(VFS_request *r);
66  virtual void rm(VFS_request *r);
67  virtual void subscribe(VFS_request *r);
68 
69  bool createDocument( QString path, QJsonObject o, quint64 &size, bool container=false );
70  bool removeDocument( QString path );
71  quint64 writeDocument( QString path, QJsonObject o, bool container=false );
72  QJsonObject readDocument( QString path, quint64 &size, bool &container );
73  bool createCollection( QString name );
74 
75  public slots:
76  // virtual void applyDiff(VFS_request *r);
77 
78  private:
79  QString _url;
80  QString _databaseName;
81  QString _collection;
82 
83  static instance _instance;
84 
85  client _client;
86  database _database;
87 
89 };
90 
91 
92 //https://stackoverflow.com/questions/8152720/correct-way-to-inherit-from-stdexception
93 class VFS_mongo_exception : public std::exception
94 {
95  public:
96  explicit VFS_mongo_exception(QString m);
97  virtual ~VFS_mongo_exception() throw();
98 
99  virtual const char *what() const throw();
100 
101  protected:
102  QString _message;
103 };
104 
106 {
107  public:
108  explicit VFS_mongo_entryException(QString m);
109  virtual ~VFS_mongo_entryException() throw();
110 };
111 
113 {
114  public:
115  explicit VFS_mongo_directoryException(QString m);
116  virtual ~VFS_mongo_directoryException() throw();
117 };
118 
120 {
121  public:
122  explicit VFS_mongo_collectionException(QString m);
123  virtual ~VFS_mongo_collectionException() throw();
124 };
125 
126 #endif // VFS_MONGO_PLUGIN_H
A pure virtual class which must be subclassed for data storage.
Definition: VFS_datastore.h:13
quint64 size()
static const quint64 DEFAULT_DATACACHE_SIZE
The default size to create a cache. Defaults to 100mb.
Definition: VFS_datastore.h:74
A base class for creating storage nodes in VFS.
Definition: VFS_datastore.h:84
A subclass of VFS_datastore_cache_entry, used for mongo database access.
Definition: VFS_mongo.h:20
virtual ~VFS_mongo_cache_entry()
VFS_mongo_cache_entry destructor.
Definition: VFS_mongo.cpp:143
QString _path
The path associated with this cache entry.
Definition: VFS_mongo.h:34
virtual bool isContainer()
Return the container-ness of this entry.
Definition: VFS_mongo.cpp:172
VFS_mongo * _mongo
The VFS_mongo instance that owns this item's cache.
Definition: VFS_mongo.h:33
virtual void flush()
Actually write the document to the database.
Definition: VFS_mongo.cpp:191
virtual bool valid(QString &reason)
If the file or directory does not exist or is not readable, _valid is set to false.
Definition: VFS_mongo.cpp:159
bool _container
The entry is a container.
Definition: VFS_mongo.h:35
VFS_mongo_cache_entry(VFS_mongo *mongo, QString path, bool debug, int flushInterval, int expireInterval, bool create=false, bool container=false, QJsonObject createData=QJsonObject())
Definition: VFS_mongo.cpp:47
A subclass of VFS_mongo_exception signalling a problem with the collection path of an operation.
Definition: VFS_mongo.h:120
VFS_mongo_collectionException(QString m)
VFS_mongo_collectionException constructor.
Definition: VFS_mongo.cpp:1147
A subclass of VFS_mongo_exception signalling a problem with the path of a read or write operation.
Definition: VFS_mongo.h:113
VFS_mongo_directoryException(QString m)
VFS_mongo_directoryException constructor.
Definition: VFS_mongo.cpp:1124
A subclass of VFS_mongo_exception signalling a problem with the path of a read or write operation.
Definition: VFS_mongo.h:106
virtual ~VFS_mongo_entryException()
Definition: VFS_mongo.cpp:1104
VFS_mongo_entryException(QString m)
VFS_mongo_entryException constructor.
Definition: VFS_mongo.cpp:1102
Base class for VFS_mongo_exception subclasses.
Definition: VFS_mongo.h:94
virtual const char * what() const
Fetch the exception message, in the style of std::exception.
Definition: VFS_mongo.cpp:1078
VFS_mongo_exception(QString m)
VFS_mongo_exception constructor.
Definition: VFS_mongo.cpp:1065
QString _message
This exception's message.
Definition: VFS_mongo.h:102
virtual ~VFS_mongo_exception()
Definition: VFS_mongo.cpp:1071
A VFS_datastore subclass for accessing data in Mongo.
Definition: VFS_mongo.h:43
virtual ~VFS_mongo()
Definition: VFS_mongo.cpp:286
virtual QByteArray icon()
The "disk" icon found in the VFS_icons library.
Definition: VFS_mongo.cpp:553
virtual void subscribe(VFS_request *r)
Subscribe to changes on a database document.
Definition: VFS_mongo.cpp:1044
client _client
The Mongo (mongocxx) client object.
Definition: VFS_mongo.h:85
static instance _instance
The global mongocxx instance. There should only ever be one of these.
Definition: VFS_mongo.h:83
bool createDocument(QString path, QJsonObject o, quint64 &size, bool container=false)
Create a new document in the database if it doesn't exist.
Definition: VFS_mongo.cpp:305
QJsonObject readDocument(QString path, quint64 &size, bool &container)
Search for a document by "_path" in the database.
Definition: VFS_mongo.cpp:445
int _flushInterval
Interval for cache entries to flush, in milliseconds.
Definition: VFS_mongo.h:55
virtual void submit(VFS_request *r)
Submit data to a file, applying the data as a diff.
Definition: VFS_mongo.cpp:913
virtual void metadata(VFS_request *r)
Fetch the metadata for a database document.
Definition: VFS_mongo.cpp:883
database _database
The Mongo (mongocxx) database object.
Definition: VFS_mongo.h:86
virtual QString reportDetails()
Report the current cache usage.
Definition: VFS_mongo.cpp:563
bool removeDocument(QString path)
Remove a document from the database.
Definition: VFS_mongo.cpp:413
virtual void write(VFS_request *r)
Write data to a database document.
Definition: VFS_mongo.cpp:815
QString _databaseName
The name of the database to use.
Definition: VFS_mongo.h:80
int _expireInterval
Interval for cache entries to expire, in milliseconds.
Definition: VFS_mongo.h:56
quint64 writeDocument(QString path, QJsonObject o, bool container=false)
Update a document in the database.
Definition: VFS_mongo.cpp:369
QString _url
The database url for this node to connect to, for instance mongodb://localhost:27017.
Definition: VFS_mongo.h:79
QString _collection
The collection to use.
Definition: VFS_mongo.h:81
bool _createCollection
If a reference is made to a non-existent collection, create it, or throw an error.
Definition: VFS_mongo.h:88
virtual void rm(VFS_request *r)
Attempt to delete a file.
Definition: VFS_mongo.cpp:980
virtual void ls(VFS_request *r)
List the contents of a collection using a Mongo find() on the path.
Definition: VFS_mongo.cpp:604
Q_INVOKABLE VFS_mongo(QString uri, QString db, QString collection, quint64 size=VFS_datastore_cache::DEFAULT_DATACACHE_SIZE, bool debug=false, int flushInterval=5000, int expireInterval=60000, bool createCollection=false)
Create a VFS_Mongo node.
Definition: VFS_mongo.cpp:261
virtual void read(VFS_request *r)
Read the contents of a database document.
Definition: VFS_mongo.cpp:739
bool createCollection(QString name)
Create a collection.
Definition: VFS_mongo.cpp:488
The base class for all requests between nodes.
Definition: VFS_node.h:54