Remoto - VFS: VFS_mongo Class Reference
Remoto - VFS

A VFS_datastore subclass for accessing data in Mongo. More...

#include <VFS_mongo.h>

Inheritance diagram for VFS_mongo:
VFS_datastore VFS_node

Public Member Functions

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. More...
 
virtual ~VFS_mongo ()
 
virtual QString reportDetails ()
 Report the current cache usage. More...
 
- Public Member Functions inherited from VFS_datastore
 VFS_datastore (quint64 size, bool debug=false)
 
virtual ~VFS_datastore ()
 
virtual VFS_nodefind (VFS_request *r)
 
virtual bool isContainer ()
 All datastore subclasses are containers. More...
 
- Public Member Functions inherited from VFS_node
Q_INVOKABLE VFS_node ()
 The VFS_node constructor will add its instance to the VFS_node::__allNodes global node registry, observing thread safety rules. More...
 
virtual ~VFS_node ()
 VFS_node destructor. More...
 
virtual VFS_nodeappend (QString name, VFS_node *node, bool containerCheck=true, QString user="server")
 Append a VFS_node as a child of this node. More...
 
QString className ()
 Return the class name of a node. More...
 
virtual VFS_requestcreateRequest (VFS_request::requestType type, QString path, QString user="unknown", QJsonDocument data=QJsonDocument(), QJsonObject metadata=QJsonObject(), bool dontDelete=false)
 Create a new VFS_request with this object as _origin. More...
 
VFS_nodefind (QString path)
 Find a node by string path. More...
 
VFS_nodefindChildWithName (QString name)
 Check if a child with a given name exists. More...
 
virtual VFS_nodemount ()
 Mount this node. More...
 
QString uniqueChildName (QString name)
 Generate a unique child name. More...
 
virtual VFS_nodeunmount ()
 Unmount this node. More...
 
virtual bool validChildName (QString name)
 Check if a node name is valid. More...
 

Protected Member Functions

bool createCollection (QString name)
 Create a collection. More...
 
bool createDocument (QString path, QJsonObject o, quint64 &size, bool container=false)
 Create a new document in the database if it doesn't exist. More...
 
virtual QByteArray icon ()
 The "disk" icon found in the VFS_icons library. More...
 
virtual void ls (VFS_request *r)
 List the contents of a collection using a Mongo find() on the path. More...
 
virtual void metadata (VFS_request *r)
 Fetch the metadata for a database document. More...
 
virtual void read (VFS_request *r)
 Read the contents of a database document. More...
 
QJsonObject readDocument (QString path, quint64 &size, bool &container)
 Search for a document by "_path" in the database. More...
 
bool removeDocument (QString path)
 Remove a document from the database. More...
 
virtual void rm (VFS_request *r)
 Attempt to delete a file. More...
 
virtual void submit (VFS_request *r)
 Submit data to a file, applying the data as a diff. More...
 
virtual void subscribe (VFS_request *r)
 Subscribe to changes on a database document. More...
 
virtual void write (VFS_request *r)
 Write data to a database document. More...
 
quint64 writeDocument (QString path, QJsonObject o, bool container=false)
 Update a document in the database. More...
 
- Protected Member Functions inherited from VFS_node
virtual void aclDefaults (VFS_request *r)
 Return default values and features associated wth this node. More...
 
void addACLDefault (QJsonObject &acl, bool value, QString description="")
 Add a default value to the acl object. More...
 
void addACLFeature (QJsonObject &acl, QString feature, bool value, QString description="")
 Add a feature to the acl object. More...
 
void addACLFeatureGroup (QJsonObject &acl, QString feature, QString group, bool value)
 Add a feature group to the acl object. More...
 
void addACLFeatureUser (QJsonObject &acl, QString feature, QString user, bool value)
 Add a feature user to the acl object. More...
 
void addACLGroup (QJsonObject &acl, QString group, bool value)
 Add a group to the acl object. More...
 
void addACLUser (QJsonObject &acl, QString user, bool value)
 Add a user to the acl object. More...
 
virtual void issueRequest (VFS_node *target, VFS_request *t)
 Issue a VFS_request to its target. More...
 
virtual void issueRequest (VFS_request *t)
 A convenience function. More...
 
virtual void issueResponse (VFS_request *t)
 Once a request has been completed, issue a response. More...
 
virtual void releaseLock (VFS_request *r)
 Release a lock on this node. More...
 
virtual void report (VFS_request *r)
 Report debugging information about the current state of this node. More...
 
virtual void requestLock (VFS_request *r)
 Request a lock on this node. More...
 
virtual void unsubscribe (VFS_request *r)
 Remove an entry from this node's _subscription list. More...
 
virtual void unsubscribePath (QString path)
 Unsubscribe all references to a subpath. More...
 

Protected Attributes

int _expireInterval
 Interval for cache entries to expire, in milliseconds. More...
 
int _flushInterval
 Interval for cache entries to flush, in milliseconds. More...
 
- Protected Attributes inherited from VFS_datastore
VFS_datastore_cache _cache
 The data cache. More...
 
bool _debug
 Debug mode. More...
 
- Protected Attributes inherited from VFS_node
VFS_children _children
 This node's children. More...
 
QMutex _lock
 A recursive mutex that is local to this node. More...
 
VFS_subscriptionType _subscribers
 This node's subscribers. These subscribers will receive diff notifications. More...
 

Private Attributes

client _client
 The Mongo (mongocxx) client object. More...
 
QString _collection
 The collection to use. More...
 
bool _createCollection
 If a reference is made to a non-existent collection, create it, or throw an error. More...
 
database _database
 The Mongo (mongocxx) database object. More...
 
QString _databaseName
 The name of the database to use. More...
 
QString _url
 The database url for this node to connect to, for instance mongodb://localhost:27017. More...
 

Static Private Attributes

static instance _instance = std::move(mongocxx::instance::current())
 The global mongocxx instance. There should only ever be one of these. More...
 

Friends

class VFS_mongo_cache_entry
 

Additional Inherited Members

- Public Slots inherited from VFS_node
virtual void applyDiff (VFS_request *r)
 Apply a diff received via subscription. More...
 
virtual void executeRequest (VFS_request *t)
 Based on the VFS_request::requestType, execute the function associated with an operation. More...
 
void notifySubscribers (VFS_node *origin, VFS_request *t)
 Propagate a diff to subscribers. More...
 
virtual void receiveResponse (VFS_request *t)
 Once a VFS_request has been completed, a response will be issued back to its _origin. More...
 
void remove (bool andDelete)
 Remove a child node. More...
 
virtual void remove (VFS_node *node=nullptr, QString *name=nullptr, QString user="server")
 Remove a child node from this node. More...
 
virtual void subtreeRequest (VFS_request *t)
 find() the target of a VFS_request, and execute the request More...
 
virtual void unsubscribeAll (VFS_node *n)
 Remove all references to a subscriber from this node. More...
 
- Signals inherited from VFS_node
void diff (VFS_node *origin, VFS_request *t)
 Emit a diff, which will trigger notifySubscribers() for a mounted node. More...
 
void finished (bool andDelete=false)
 Emitted if a thread fails to create its node, or a node is rm()'d, or any other reason a node has completed its lifecycle. It is deleted if andDelete==true. More...
 
void mounted ()
 Emitted when a node is mount()ed. More...
 
void unmounted (VFS_node *self)
 Emitted when a node is unmount()ed. More...
 
- Static Public Member Functions inherited from VFS_datastore
static QString applyDeltaDiff (QString data, QJsonObject delta, QString trace="", QString user="server")
 Apply a delta diff to a json object. More...
 
static QJsonObject applyJsonDiff (QJsonObject obj, QJsonObject diff, QString trace="", QString user="server")
 Apply a json diff to a json object. More...
 
- Static Public Member Functions inherited from VFS_node
static bool __isNode (VFS_node *)
 Check to see if a node is in the global registry. More...
 
static QString code (QString nodename, QString libname, QString &error)
 Fetch code or any other resource from a node. More...
 

Detailed Description

A VFS_datastore subclass for accessing data in Mongo.

This plugin requires the mongocxx driver.

This plugin is a drop-in replacement for VFS_HD. It will append a _path field to documents and use that field to emulate a filesystem. Basically _id fields are ignored, and a regex is performed on path values to create listable directories.

The _path field will not be visible in returned documents, but it will appear if a mongo query happens outside of the VFS.

It is recommended to create an index on the _path field. The first time the node is used on a collection, if the collection does not exist it will create the collection and the index if the createCollection flag is set.

More complex queries are possible using the VFS_mongoRDBM plugin, which is in a different repository.

See also
Installing the mongocxx driver

Definition at line 42 of file VFS_mongo.h.

Constructor & Destructor Documentation

◆ VFS_mongo()

VFS_mongo::VFS_mongo ( QString  url,
QString  db,
QString  collection,
quint64  size = VFS_datastore_cache::DEFAULT_DATACACHE_SIZE,
bool  debug = false,
int  flushInterval = 5000,
int  expireInterval = 60000,
bool  createCollection = false 
)
explicit

Create a VFS_Mongo node.

Parameters
urlThe Mongo database uri, for instance "mongodb://localhost:27017"
dbThe database name
collectionThe collection to use
sizeThe cache size
debugVerbose debugging
flushIntervalInterval to flush to the database, in milliseconds.
expireIntervalInterval to expire cache entries, in milliseconds.
createCollectionIf a reference is made to a non-existent collection, create it, or throw an error.

Definition at line 261 of file VFS_mongo.cpp.

◆ ~VFS_mongo()

VFS_mongo::~VFS_mongo ( )
virtual

Definition at line 286 of file VFS_mongo.cpp.

Member Function Documentation

◆ createCollection()

bool VFS_mongo::createCollection ( QString  name)
protected

Create a collection.

Parameters
nameThe collection name.
Returns
Success or failure of the creation request.

Definition at line 488 of file VFS_mongo.cpp.

◆ createDocument()

bool VFS_mongo::createDocument ( QString  path,
QJsonObject  o,
quint64 &  size,
bool  container = false 
)
protected

Create a new document in the database if it doesn't exist.

Parameters
pathThe "path" to write to, which will become a field in the document.
oThe new data to write, as a QJsonObject
sizeThe size of the created document (when converted to a json object).
containerThe container flag (false for files, true for directories)
Exceptions
VFS_mongo_directoryExceptionThe path is empty.
VFS_mongo_collectionExceptionThe collection referenced in the path does not exist.
mongocxx::exceptionThe mongocxx driver has reported a problem.
Returns
Boolean succes of creating the document

If the document already exists, return false and don't update the existing one.

Definition at line 305 of file VFS_mongo.cpp.

◆ icon()

QByteArray VFS_mongo::icon ( )
protectedvirtual

The "disk" icon found in the VFS_icons library.

Returns
The "disk" icon
See also
VFS_node::icon()

Reimplemented from VFS_node.

Definition at line 553 of file VFS_mongo.cpp.

◆ ls()

void VFS_mongo::ls ( VFS_request r)
protectedvirtual

List the contents of a collection using a Mongo find() on the path.

Parameters
rThe VFS_request object

The resulting list will be of the form:

{
"entry1":true,
"entry2":false,
... etc.
}

Where the true and false values correspond to whether an entry is a file or directory, which is determined by the existence of an extension on the filename of the path.

The values are sorted alphabetically by the json serializer.

A file entry itself can be used for ls(), but it will return an empty set.

VFS_mongo::ls() does not use the _cache. It will read the underlying database and block during that time.

Optionally, if r->_metadata["sequence"]==true, the resulting list will be collapsed into a sequence view using rutils::sequenceListing().

See also
VFS_node::ls()
Todo:
Use an aggregator for listing directory contents. For instance, listing the root of a collection will query and return the whole collection and waste a lot of resources.

Reimplemented from VFS_node.

Definition at line 604 of file VFS_mongo.cpp.

◆ metadata()

void VFS_mongo::metadata ( VFS_request r)
protectedvirtual

Fetch the metadata for a database document.

Parameters
rThe VFS_request object

Uses VFS_node_type::getType() to determine a type for a path.

Reimplemented from VFS_node.

Definition at line 883 of file VFS_mongo.cpp.

◆ read()

void VFS_mongo::read ( VFS_request r)
protectedvirtual

Read the contents of a database document.

Parameters
rThe VFS_request object

If the entry is already in the cache, return the cached value. Otherwise, create a new VFS_mongo_cache_entry and load data into it.

If r->_metadata["createIfMissing"]==true, and the entry does not exist, the read() will be converted to a write() with a VFS_request::create type.

See also
VFS_node::read()
VFS_HD::read()

Reimplemented from VFS_node.

Definition at line 739 of file VFS_mongo.cpp.

◆ readDocument()

QJsonObject VFS_mongo::readDocument ( QString  path,
quint64 &  size,
bool &  container 
)
protected

Search for a document by "_path" in the database.

Parameters
pathThe path to read from / search for
sizeThe size of the document... written back to the caller
containerThe container-ness of the document... written back to the caller
Exceptions
VFS_mongo_collectionExceptionThe collection referenced in the path does not exist.
mongocxx::exceptionThe mongocxx driver has reported a problem.
Returns
The contents of the document, unless an exception was thrown

On a failure, this will throw an exception.

Definition at line 445 of file VFS_mongo.cpp.

◆ removeDocument()

bool VFS_mongo::removeDocument ( QString  path)
protected

Remove a document from the database.

Parameters
pathThe "path" to remove, which is queried by mongo
Exceptions
VFS_mongo_directoryExceptionThe path does not have two components.
VFS_mongo_collectionExceptionThe collection referenced in the path does not exist.
mongocxx::exceptionThe mongocxx driver has reported a problem.

On a failure, this will throw an exception. If a parentField is present, this method will execute a mongocxx::collection::find_one_and_delete() instead of the default mongocxx::collection::delete_one().

Definition at line 413 of file VFS_mongo.cpp.

◆ reportDetails()

QString VFS_mongo::reportDetails ( )
virtual

Report the current cache usage.

Returns
Return cache usage and cache entry count.

Reimplemented from VFS_datastore.

Definition at line 563 of file VFS_mongo.cpp.

◆ rm()

void VFS_mongo::rm ( VFS_request r)
protectedvirtual

Attempt to delete a file.

Parameters
rThe VFS_request object

Attempt to remove (delete) the file from the database, and set r->_success=true if it was possible. The parent directory has changed, so emit a diff() on the parent to notify any subscribers.

An rm() request with an empty r->_path, following all other VFS conventions, removes the VFS_mongo node from the VFS.

In all cases, remove the cache entry for this path.

See also
VFS_node::rm()

Reimplemented from VFS_node.

Definition at line 980 of file VFS_mongo.cpp.

◆ submit()

void VFS_mongo::submit ( VFS_request r)
protectedvirtual

Submit data to a file, applying the data as a diff.

Parameters
rThe VFS_request object

Fetch or create a cache entry for this file, and call VFS_mongo_cache_entry::getData(). Apply the r->_data as a diff using VFS_datastore::applyJsonDiff().

If r->_success==true, emit a diff() of the applied data to subscribers.

See also
VFS_mongo::write()
VFS_datastore::applyJsonDiff()

Reimplemented from VFS_node.

Definition at line 913 of file VFS_mongo.cpp.

◆ subscribe()

void VFS_mongo::subscribe ( VFS_request r)
protectedvirtual

Subscribe to changes on a database document.

Parameters
rThe VFS_request object

VFS_node::subscribe() is called, which will pass metadata through to metadata() and read(), and create an empty file if the entry doesn't exist and r->_metadata["createIfMissing"]==true.

See also
VFS_node::subscribe()

Reimplemented from VFS_node.

Definition at line 1044 of file VFS_mongo.cpp.

◆ write()

void VFS_mongo::write ( VFS_request r)
protectedvirtual

Write data to a database document.

Parameters
rThe VFS_request object

The contents of the data portion of the document will be entirely replaced by r->_data, converted to a bson object.

Because this is a write() operation, a diff() is emitted.

See also
VFS_mongo::read()

Reimplemented from VFS_node.

Definition at line 815 of file VFS_mongo.cpp.

◆ writeDocument()

quint64 VFS_mongo::writeDocument ( QString  path,
QJsonObject  o,
bool  container = false 
)
protected

Update a document in the database.

Parameters
pathThe "path" to write to, which will become a field in the document.
oThe new data to write, as a QJsonObject
containerThe container flag (false for files, true for directories)
Exceptions
VFS_mongo_directoryExceptionThe path is empty.
VFS_mongo_collectionExceptionThe collection referenced in the path does not exist.
mongocxx::exceptionThe mongocxx driver has reported a problem.
Returns
The number of bytes written (when converted to a json object)

On a failure, this will throw an exception.

Definition at line 369 of file VFS_mongo.cpp.

Friends And Related Function Documentation

◆ VFS_mongo_cache_entry

friend class VFS_mongo_cache_entry
friend

Definition at line 46 of file VFS_mongo.h.

Member Data Documentation

◆ _client

client VFS_mongo::_client
private

The Mongo (mongocxx) client object.

Definition at line 85 of file VFS_mongo.h.

◆ _collection

QString VFS_mongo::_collection
private

The collection to use.

Definition at line 81 of file VFS_mongo.h.

◆ _createCollection

bool VFS_mongo::_createCollection
private

If a reference is made to a non-existent collection, create it, or throw an error.

Definition at line 88 of file VFS_mongo.h.

◆ _database

database VFS_mongo::_database
private

The Mongo (mongocxx) database object.

Definition at line 86 of file VFS_mongo.h.

◆ _databaseName

QString VFS_mongo::_databaseName
private

The name of the database to use.

Definition at line 80 of file VFS_mongo.h.

◆ _expireInterval

int VFS_mongo::_expireInterval
protected

Interval for cache entries to expire, in milliseconds.

Definition at line 56 of file VFS_mongo.h.

◆ _flushInterval

int VFS_mongo::_flushInterval
protected

Interval for cache entries to flush, in milliseconds.

Definition at line 55 of file VFS_mongo.h.

◆ _instance

mongocxx::instance VFS_mongo::_instance = std::move(mongocxx::instance::current())
staticprivate

The global mongocxx instance. There should only ever be one of these.

Definition at line 83 of file VFS_mongo.h.

◆ _url

QString VFS_mongo::_url
private

The database url for this node to connect to, for instance mongodb://localhost:27017.

Definition at line 79 of file VFS_mongo.h.


The documentation for this class was generated from the following files: