Skip to content

Commit

Permalink
Merge pull request #115 from AO-StreetArt/mongoBsonApi
Browse files Browse the repository at this point in the history
Mongo bson api
  • Loading branch information
AO-StreetArt committed Mar 11, 2018
2 parents 2826be7 + d8b4545 commit f4f8c4a
Show file tree
Hide file tree
Showing 8 changed files with 577 additions and 72 deletions.
17 changes: 10 additions & 7 deletions aossl/core/include/buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ THE SOFTWARE.

namespace AOSSL {

//! A base buffer
struct Buffer {
//! Success flag
bool success;

//! Error Message
std::string err_msg;
};

//! A Structure for holding a single value
struct StringBuffer {
struct StringBuffer: public Buffer {
//! Value stored
std::string val;

//! Success flag
bool success;

//! Error Message
std::string err_msg;
};

//! A Structure for storing a Key-Value pair
Expand Down
30 changes: 30 additions & 0 deletions aossl/mongo/include/mongo_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ THE SOFTWARE.
#include <exception>
#include <vector>
#include <mutex>
#include <iostream>

#include "mongo_interface.h"
#include "mongo_buffer_interface.h"

#include "aossl/core/include/slot_pool.h"
#include "aossl/core/include/connection.h"
Expand Down Expand Up @@ -183,6 +185,14 @@ class MongoClient: public MongoInterface {
void initialize(const char * url, const char * db, \
const char * collection_name, int size, int pstart_size, int pbatch);

bool create_document(bson_t *doc, \
bson_oid_t &oid, bson_error_t &error, char *out_str);

bool save_document(const char * key, \
bson_t *doc, bson_oid_t &oid, bson_error_t &error);

MongoIteratorInterface* query(bson_t *q, bson_t *o, mongoc_cursor_t *cursor);

public:
// Switch the current collection
void switch_collection(const char * collection_name);
Expand Down Expand Up @@ -259,6 +269,8 @@ class MongoClient: public MongoInterface {
return create_document(doc.c_str());
}

MongoResponseInterface* create_document(AOSSL::MongoBufferInterface *document);

inline MongoResponseInterface* create_document(const char * doc, \
const char * collection_name) {
switch_collection(collection_name);
Expand Down Expand Up @@ -310,6 +322,8 @@ class MongoClient: public MongoInterface {
// Otherwise it will be inserted
void save_document(const char * doc, const char * key);

void save_document(AOSSL::MongoBufferInterface *document, const char * key);

inline void save_document(std::string doc, std::string key) {
return save_document(doc.c_str(), key.c_str());
}
Expand All @@ -331,6 +345,9 @@ class MongoClient: public MongoInterface {
// May also include options in JSON format
MongoIteratorInterface* query(const char * query_str, const char * opts_str);

MongoIteratorInterface* query(AOSSL::MongoBufferInterface *query, \
AOSSL::MongoBufferInterface *opts);

inline MongoIteratorInterface* query(std::string query_str, \
std::string opts_str) {
return query(query_str.c_str(), opts_str.c_str());
Expand All @@ -355,6 +372,19 @@ class MongoClient: public MongoInterface {
inline MongoIteratorInterface* query(std::string query_str) {
return query(query_str.c_str());
}

//! Update by Query

//! Updates documents which match the provided query
//! If update_multiple is true, then update all of the documents that match
//! Otherwise, update only the first match
void update_by_query(AOSSL::MongoBufferInterface *query, \
AOSSL::MongoBufferInterface *update, bool update_multiple);

void update_by_query(AOSSL::MongoBufferInterface *query, \
AOSSL::MongoBufferInterface *update) {
update_by_query(query, update, true);
}
};

#endif // AOSSL_MONGO_INCLUDE_MONGO_ADMIN_H_
144 changes: 144 additions & 0 deletions aossl/mongo/include/mongo_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
MIT License Block
Copyright (c) 2016 Alex Barry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef AOSSL_MONGO_INCLUDE_MONGO_BUFFER_H_
#define AOSSL_MONGO_INCLUDE_MONGO_BUFFER_H_

#include <bson.h>
#include <mongoc.h>
#include <exception>
#include <string>
#include <unordered_map>

#include "aossl/core/include/buffers.h"
#include "mongo_buffer_interface.h"

namespace AOSSL {

//! A Structure for holding a BSON Document
class MongoBuffer: public Buffer, public MongoBufferInterface {
private:
std::vector<bson_t*> children;
int parent_index = 0;
int child_index = 0;
std::vector<int> array_indices;
public:
//! Build a new Mongo Buffer
MongoBuffer() {bson_t *b = new bson_t; bson_init (b); children.push_back(b);}
//! Destroy the Mongo Buffer
~MongoBuffer() {bson_destroy(children[0]); delete children[0]; for (int i = 1; i < children.size(); i++) {delete children[i];}}
//! Is a successful response
bool successful() {return Buffer::success;}
//! Error message
std::string get_error_message() {return Buffer::err_msg;}
//! Get the underlying bson_t document to pass to the mongoc driver
bson_t* get_bson() {return children[0];}
//! Add a string value to the buffer
void add_string(std::string key, std::string value) {add_string(key, value, MONGO_STRING_UTF8);}
//! Add a string value to the open array in the buffer
inline void add_string(std::string value, int encoding) {
char str[16];
const char *key;
bson_uint32_to_string(array_indices[child_index], &key, str, 16);
if (encoding == MONGO_STRING_UTF8) {
bson_append_utf8(children[child_index], key, -1, value.c_str(), -1);
}
array_indices[child_index]++;
}
//! Add a string value to the open array in the buffer
inline void add_string(std::string value) {add_string(value, MONGO_STRING_UTF8);}
//! Add a string value to the buffer
inline void add_string(std::string key, std::string value, int encoding) {
if (encoding == MONGO_STRING_UTF8) {
bson_append_utf8(children[child_index], key.c_str(), -1, value.c_str(), -1);
}
}
//! Add a boolean value to the buffer
inline void add_bool(std::string key, bool value) {
bson_append_bool(children[child_index], key.c_str(), -1, value);
}
//! Add a boolean value to the open array in the buffer
inline void add_bool(bool value) {
char str[16];
const char *key;
bson_uint32_to_string(array_indices[child_index], &key, str, 16);
bson_append_bool(children[child_index], key, -1, value);
array_indices[child_index]++;
}
//! Add an integer value to the buffer
inline void add_int(std::string key, int value) {
bson_append_int32(children[child_index], key.c_str(), -1, value);
}
//! Add an integer value to the open array in the buffer
inline void add_int(int value) {
char str[16];
const char *key;
bson_uint32_to_string(array_indices[child_index], &key, str, 16);
bson_append_int32(children[child_index], key, -1, value);
array_indices[child_index]++;
}
//! Add a double value to the buffer
void add_double(std::string key, double value) {bson_append_double(children[child_index], key.c_str(), -1, value);}
//! Add a double value to the open array in the buffer
inline void add_double(double value) {
char str[16];
const char *key;
bson_uint32_to_string(array_indices[child_index], &key, str, 16);
bson_append_double(children[child_index], key, -1, value);
array_indices[child_index]++;
}
//! Start an array in the buffer
inline void start_array(std::string key) {
bson_t *child = new bson_t;
children.push_back(child);
array_indices.push_back(0);
parent_index=child_index;
child_index++;
bson_append_array_begin(children[parent_index], key.c_str(), -1, children[child_index]);
}
//! End an array value in the buffer
inline void end_array() {
bson_append_array_end(children[parent_index], children[child_index]);
if (parent_index > 0) {parent_index--;}
if (child_index > 0) {child_index--;}
}
//! Start a subdocument in the buffer
inline void start_object(std::string key) {
bson_t *child = new bson_t;
children.push_back(child);
array_indices.push_back(0);
parent_index=child_index;
child_index++;
bson_append_document_begin (children[parent_index], key.c_str(), -1, children[child_index]);
}
//! End a subdocument in the buffer
inline void end_object() {
bson_append_document_end(children[parent_index], children[child_index]);
if (parent_index > 0) {parent_index--;}
if (child_index > 0) {child_index--;}
}
};

} // namespace AOSSL
#endif // AOSSL_MONGO_INCLUDE_MONGO_BUFFER_H_
102 changes: 102 additions & 0 deletions aossl/mongo/include/mongo_buffer_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
MIT License Block
Copyright (c) 2016 Alex Barry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef AOSSL_MONGO_INCLUDE_MONGO_BUFFER_INTERFACE_H_
#define AOSSL_MONGO_INCLUDE_MONGO_BUFFER_INTERFACE_H_

#include <bson.h>
#include <mongoc.h>
#include <exception>
#include <string>
#include <unordered_map>

#include "aossl/core/include/buffers.h"

namespace AOSSL {

const int MONGO_STRING_UTF8 = 0;

//! BSON Exception, used to store document-related errors
struct BsonException: public std::exception {
//! An error message passed on initialization
std::string int_msg;
const char * what_str;

//! Create a Mongo Exception, and store the given error message
inline BsonException(std::string msg) {
int_msg = "Error in Bson Document: " + msg;
what_str = int_msg.c_str();
}

BsonException() {}
~BsonException() throw() {}

//! Show the error message in readable format
const char * what() const throw() {
return what_str;
}
};

//! A Structure for holding a BSON Document
class MongoBufferInterface {
public:
virtual ~MongoBufferInterface() {}
//! Is a successful response
virtual bool successful() = 0;
//! Error message
virtual std::string get_error_message() = 0;
//! Get the underlying bson_t document to pass to the mongoc driver
virtual bson_t* get_bson() = 0;
//! Add a string value to the buffer
virtual void add_string(std::string key, std::string value) = 0;
//! Add a string value to the open array in the buffer
virtual void add_string(std::string value, int encoding) = 0;
//! Add a string value to the open array in the buffer
virtual void add_string(std::string value) = 0;
//! Add a string value to the buffer
virtual void add_string(std::string key, std::string value, int encoding) = 0;
//! Add a boolean value to the buffer
virtual void add_bool(std::string key, bool value) = 0;
//! Add a boolean value to the open array in the buffer
virtual void add_bool(bool value) = 0;
//! Add an integer value to the buffer
virtual void add_int(std::string key, int value) = 0;
//! Add an integer value to the open array in the buffer
virtual void add_int(int value) = 0;
//! Add a double value to the buffer
virtual void add_double(std::string key, double value) = 0;
//! Add a double value to the open array in the buffer
virtual void add_double(double value) = 0;
//! Start an array in the buffer
virtual void start_array(std::string key) = 0;
//! End an array value in the buffer
virtual void end_array() = 0;
//! Start a subdocument in the buffer
virtual void start_object(std::string key) = 0;
//! End a subdocument in the buffer
virtual void end_object() = 0;
};

} // namespace AOSSL
#endif // AOSSL_MONGO_INCLUDE_MONGO_BUFFER_INTERFACE_H_
21 changes: 21 additions & 0 deletions aossl/mongo/include/mongo_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ THE SOFTWARE.
#include <exception>
#include <vector>

#include "mongo_buffer_interface.h"

//! Mongo Exception, used to store errors passed from Mongo
struct MongoException: public std::exception {
//! An error message passed on initialization
Expand Down Expand Up @@ -87,6 +89,8 @@ class MongoInterface {
//! Create JSON Document, returns the document key
virtual MongoResponseInterface* create_document(std::string doc, \
std::string collection_name) = 0;
//! Create JSON Document, returns the document key
virtual MongoResponseInterface* create_document(AOSSL::MongoBufferInterface *document) = 0;

//! Delete a JSON Document, returns true if successful
virtual void delete_document(const char * key) = 0;
Expand Down Expand Up @@ -120,6 +124,9 @@ class MongoInterface {
//! Update an existing document, returns true if successful
virtual void save_document(std::string doc, std::string key, \
std::string collection_name) = 0;
//! Update an existing document
virtual void save_document(AOSSL::MongoBufferInterface *document, \
const char * key) = 0;

// Advanced Operations

Expand Down Expand Up @@ -157,6 +164,20 @@ class MongoInterface {
//! Accept the query in JSON format.
//! Return an iterator which can be used to access query results
virtual MongoIteratorInterface* query(std::string query_str) = 0;

//! Update by Query

//! Updates documents which match the provided query
//! If update_multiple is true, then update all of the documents that match
//! Otherwise, update only the first match
virtual void update_by_query(AOSSL::MongoBufferInterface *query, \
AOSSL::MongoBufferInterface *update, bool update_multiple) = 0;

//! Update by Query

//! Updates all documents which match the provided query
virtual void update_by_query(AOSSL::MongoBufferInterface *query, \
AOSSL::MongoBufferInterface *update) = 0;
};

#endif // AOSSL_MONGO_INCLUDE_MONGO_INTERFACE_H_

0 comments on commit f4f8c4a

Please sign in to comment.