Grassroots Infrastructure
The Grassroots Infrastructure is a suite of computing tools to help users and developers use scientific data infrastructure that can easily be interconnected.
mongodb_tool.h File Reference
#include "mongoc.h"
#include "typedefs.h"
#include "jansson.h"
#include "mongodb_library.h"
#include "operation.h"
Include dependency graph for mongodb_tool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  MongoTool
 A MongoTool is a datatype that allows access to the data stored within a MongoDB instance. More...
 

Macros

#define MONGO_OID_STRING_BUFFER_SIZE   (25)
 

Functions

bool AddBSONDocumentToJSONArray (const bson_t *document_p, void *data_p)
 Convert a BSON document and add it to a JSON array. More...
 
int PrintBSONToLog (const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s,...)
 Print the JSON representation of a BSON fragment to the log Stream. More...
 
int PrintBSONToErrors (const uint32 level, const char *filename_s, const int line_number, const bson_t *bson_p, const char *message_s,...)
 Print the JSON representation of a BSON fragment to the errors Stream. More...
 
void LogBSONOid (const bson_oid_t *bson_p, const int level, const char *const filename_s, const int line_number, const char *const prefix_s)
 Print a BSON id to the log Stream. More...
 
void LogAllBSON (const bson_t *bson_p, const int level, const char *const file_s, const int line, const char *const prefix_s)
 Print the JSON representation of a BSON object and all of its children and members to the log Stream. More...
 
bson_t * GenerateQuery (const json_t *json_p)
 Create a BSON query from a JSON fragment. More...
 
json_t * ConvertBSONValueToJSON (const bson_value_t *value_p)
 Convert a BSON object into its equivalent JSON type. More...
 
json_t * ConvertBSONToJSON (const bson_t *bson_p)
 Create a new JSON fragment from a given BSON one. More...
 
bool InsertMongoData (MongoTool *tool_p, const json_t *values_p, bson_t **reply_pp, bson_error_t *error_p)
 
bool InsertMongoDataAsBSON (MongoTool *tool_p, const bson_t *doc_p, bson_t **reply_pp, bson_error_t *error_p)
 
bool RunMongoCommand (MongoTool *tool_p, bson_t *command_p, bson_t **reply_pp)
 
bool RemoveMongoDocumentsByBSON (MongoTool *tool_p, const bson_t *selector_p, const bool remove_first_match_only_flag)
 
bson_t * ConvertJSONToBSON (const json_t *json_p)
 Create a new BSON fragment from a given JSON one. More...
 
char * GetBSONOidAsString (const bson_oid_t *id_p)
 
void FreeBSONOidString (char *id_s)
 
bson_oid_t * GetBSONOidFromString (const char *id_s)
 
bool SaveMongoData (MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, bson_t *selector_p)
 
bool SaveMongoDataWithTimestamp (MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, bson_t *selector_p, const char *timestamp_key_s)
 
bool SaveAndBackupMongoDataWithTimestamp (MongoTool *mongo_p, json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p, const char *timestamp_key_s)
 
bool SaveMongoDataFromBSON (MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, bson_t *selector_p)
 
bool SaveAndBackupMongoData (MongoTool *mongo_p, const json_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p)
 
bool SaveAndBackupMongoDataFromBSON (MongoTool *mongo_p, const bson_t *data_to_save_p, const char *collection_s, const char *backup_collection_s, const char *id_key_s, bson_t *selector_p)
 
bool SetMongoDataAsBSON (MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
 
bool UnsetMongoDataAsBSON (MongoTool *tool_p, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
 
bool UpdateMongoDataAsBSON (MongoTool *tool_p, const char *const update_s, bson_t *selector_p, const bson_t *doc_p, bson_t **reply_pp)
 
bool SetMongoDataAsBSONForGivenId (MongoTool *tool_p, bson_oid_t *id_p, bson_t *update_p, bson_t **reply_pp)
 
bool SetMongoData (MongoTool *tool_p, bson_t *selector_p, const json_t *values_p, bson_t **reply_pp)
 
bson_oid_t * GetNewBSONOid (void)
 
bson_oid_t * GetNewUnitialisedBSONOid (void)
 
bson_oid_t * CopyBSONOid (const bson_oid_t *src_p)
 
void FreeBSONOid (bson_oid_t *id_p)
 
bson_t * AllocateBSON (void)
 
void FreeBSON (bson_t *bson_p)
 
bool GetMongoIdFromJSON (const json_t *data_p, bson_oid_t *id_p)
 
bool GetNamedIdFromJSON (const json_t *data_p, const char *const key_s, bson_oid_t *id_p)
 
const char * GetNamedIdAsStringFromJSON (const json_t *data_p, const char *const key_s)
 
bool GetIdFromJSONKeyValuePair (const json_t *id_val_p, bson_oid_t *id_p)
 
bool AddCompoundIdToJSON (json_t *data_p, bson_oid_t *id_p)
 
bool AddNamedCompoundIdToJSON (json_t *data_p, bson_oid_t *id_p, const char *key_s)
 
bool AddCompoundIdToJSONArray (json_t *array_p, const bson_oid_t *id_p)
 
bool AddIdToJSON (json_t *data_p, bson_oid_t *id_p, const char *key_s)
 
bool AddQueryTerm (bson_t *query_p, const char *key_s, const char *value_s, bool regex_flag)
 
int64 GetNumberOfMongoResults (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p)
 
OperationStatus ProcessMongoResults (MongoTool *tool_p, bson_t *query_p, bson_t *extra_opts_p, bool(*process_bson_fn)(const bson_t *document_p, void *data_p), void *data_p)
 

Variables

const char * MONGO_OID_KEY_S = "$oid"
 
const char * MONGO_TIMESTAMP_S = "modified"
 The key for specifying the timestamp of a saved piece of data. More...
 

Macro Definition Documentation

◆ MONGO_OID_STRING_BUFFER_SIZE

#define MONGO_OID_STRING_BUFFER_SIZE   (25)

Function Documentation

◆ AddBSONDocumentToJSONArray()

bool AddBSONDocumentToJSONArray ( const bson_t *  document_p,
void *  data_p 
)

Convert a BSON document and add it to a JSON array.

Parameters
document_pThe BSON document to extract the values from.
data_pThe JSON array to add the values to
Returns
true if the values were added successfully, false otherwise.

◆ PrintBSONToLog()

int PrintBSONToLog ( const uint32  level,
const char *  filename_s,
const int  line_number,
const bson_t *  bson_p,
const char *  message_s,
  ... 
)

Print the JSON representation of a BSON fragment to the log Stream.

Parameters
levelThe Stream level to specify when printing this.
filename_sThe name of the file which will be specified with this message.
line_numberThe line number which will be specified with this message.
bson_pThe BSON fragment to print.
prefix_sAn optional string to print in the stream prior to the BSON representation.

◆ PrintBSONToErrors()

int PrintBSONToErrors ( const uint32  level,
const char *  filename_s,
const int  line_number,
const bson_t *  bson_p,
const char *  message_s,
  ... 
)

Print the JSON representation of a BSON fragment to the errors Stream.

Parameters
levelThe Stream level to specify when printing this.
filename_sThe name of the file which will be specified with this message.
line_numberThe line number which will be specified with this message.
bson_pThe BSON fragment to print.
prefix_sAn optional string to print in the stream prior to the BSON representation.

◆ LogBSONOid()

void LogBSONOid ( const bson_oid_t *  bson_p,
const int  level,
const char *const  filename_s,
const int  line_number,
const char *const  prefix_s 
)

Print a BSON id to the log Stream.

Parameters
bson_pThe BSON fragment to print.
levelThe Stream level to specify when printing this.
filename_sThe name of the file which will be specified with this message.
line_numberThe line number which will be specified with this message.
prefix_sAn optional string to print in the stream prior to the BSON representation.

◆ LogAllBSON()

void LogAllBSON ( const bson_t *  bson_p,
const int  level,
const char *const  file_s,
const int  line,
const char *const  prefix_s 
)

Print the JSON representation of a BSON object and all of its children and members to the log Stream.

Parameters
bson_pThe BSON fragment to print.
levelThe Stream level to specify when printing this.
file_sThe name of the file which will be specified with this message.
lineThe line number which will be specified with this message.
prefix_sAn optional string to print in the stream prior to the BSON representation.

◆ GenerateQuery()

bson_t* GenerateQuery ( const json_t *  json_p)

Create a BSON query from a JSON fragment.

Parameters
json_pThe JSON fragment to create the query from. If the given fragment is not an object or array, treat the statement as key = value. If it is an array, treat it as an "in" query. If it is an object, the value can have the following fields:

key: one of "=", "<", "<=", ">", ">=", "in", "range", "not" value: can be single value or array. For a "range" key, it will be an array of 2 elements that are the inclusive lower and upper bounds.

Returns
The BSON query or NULL upon error.

◆ ConvertBSONValueToJSON()

json_t* ConvertBSONValueToJSON ( const bson_value_t *  value_p)

Convert a BSON object into its equivalent JSON type.

Parameters
value_pThe value to convert.
Returns
The newly-created JSON object with the equivalent value or NULL upon error.

◆ ConvertBSONToJSON()

json_t* ConvertBSONToJSON ( const bson_t *  bson_p)

Create a new JSON fragment from a given BSON one.

Parameters
bson_pThe BSON fragment to convert to JSON.
Returns
The JSON fragment or NULL upon error.

◆ InsertMongoData()

bool InsertMongoData ( MongoTool tool_p,
const json_t *  values_p,
bson_t **  reply_pp,
bson_error_t *  error_p 
)

◆ InsertMongoDataAsBSON()

bool InsertMongoDataAsBSON ( MongoTool tool_p,
const bson_t *  doc_p,
bson_t **  reply_pp,
bson_error_t *  error_p 
)

◆ RunMongoCommand()

bool RunMongoCommand ( MongoTool tool_p,
bson_t *  command_p,
bson_t **  reply_pp 
)

◆ RemoveMongoDocumentsByBSON()

bool RemoveMongoDocumentsByBSON ( MongoTool tool_p,
const bson_t *  selector_p,
const bool  remove_first_match_only_flag 
)

◆ ConvertJSONToBSON()

bson_t* ConvertJSONToBSON ( const json_t *  json_p)

Create a new BSON fragment from a given JSON one.

Parameters
json_pThe JSON fragment to convert to BSON.
Returns
The BSON fragment or NULL upon error.

◆ GetBSONOidAsString()

char* GetBSONOidAsString ( const bson_oid_t *  id_p)

◆ FreeBSONOidString()

void FreeBSONOidString ( char *  id_s)

◆ GetBSONOidFromString()

bson_oid_t* GetBSONOidFromString ( const char *  id_s)

◆ SaveMongoData()

bool SaveMongoData ( MongoTool mongo_p,
const json_t *  data_to_save_p,
const char *  collection_s,
bson_t *  selector_p 
)

◆ SaveMongoDataWithTimestamp()

bool SaveMongoDataWithTimestamp ( MongoTool mongo_p,
json_t *  data_to_save_p,
const char *  collection_s,
bson_t *  selector_p,
const char *  timestamp_key_s 
)

◆ SaveAndBackupMongoDataWithTimestamp()

bool SaveAndBackupMongoDataWithTimestamp ( MongoTool mongo_p,
json_t *  data_to_save_p,
const char *  collection_s,
const char *  backup_collection_s,
const char *  id_key_s,
bson_t *  selector_p,
const char *  timestamp_key_s 
)

◆ SaveMongoDataFromBSON()

bool SaveMongoDataFromBSON ( MongoTool mongo_p,
const bson_t *  data_to_save_p,
const char *  collection_s,
bson_t *  selector_p 
)

◆ SaveAndBackupMongoData()

bool SaveAndBackupMongoData ( MongoTool mongo_p,
const json_t *  data_to_save_p,
const char *  collection_s,
const char *  backup_collection_s,
const char *  id_key_s,
bson_t *  selector_p 
)

◆ SaveAndBackupMongoDataFromBSON()

bool SaveAndBackupMongoDataFromBSON ( MongoTool mongo_p,
const bson_t *  data_to_save_p,
const char *  collection_s,
const char *  backup_collection_s,
const char *  id_key_s,
bson_t *  selector_p 
)

◆ SetMongoDataAsBSON()

bool SetMongoDataAsBSON ( MongoTool tool_p,
bson_t *  selector_p,
const bson_t *  doc_p,
bson_t **  reply_pp 
)

◆ UnsetMongoDataAsBSON()

bool UnsetMongoDataAsBSON ( MongoTool tool_p,
bson_t *  selector_p,
const bson_t *  doc_p,
bson_t **  reply_pp 
)

◆ UpdateMongoDataAsBSON()

bool UpdateMongoDataAsBSON ( MongoTool tool_p,
const char *const  update_s,
bson_t *  selector_p,
const bson_t *  doc_p,
bson_t **  reply_pp 
)

◆ SetMongoDataAsBSONForGivenId()

bool SetMongoDataAsBSONForGivenId ( MongoTool tool_p,
bson_oid_t *  id_p,
bson_t *  update_p,
bson_t **  reply_pp 
)

◆ SetMongoData()

bool SetMongoData ( MongoTool tool_p,
bson_t *  selector_p,
const json_t *  values_p,
bson_t **  reply_pp 
)

◆ GetNewBSONOid()

bson_oid_t* GetNewBSONOid ( void  )

◆ GetNewUnitialisedBSONOid()

bson_oid_t* GetNewUnitialisedBSONOid ( void  )

◆ CopyBSONOid()

bson_oid_t* CopyBSONOid ( const bson_oid_t *  src_p)

◆ FreeBSONOid()

void FreeBSONOid ( bson_oid_t *  id_p)

◆ AllocateBSON()

bson_t* AllocateBSON ( void  )

◆ FreeBSON()

void FreeBSON ( bson_t *  bson_p)

◆ GetMongoIdFromJSON()

bool GetMongoIdFromJSON ( const json_t *  data_p,
bson_oid_t *  id_p 
)

◆ GetNamedIdFromJSON()

bool GetNamedIdFromJSON ( const json_t *  data_p,
const char *const  key_s,
bson_oid_t *  id_p 
)

◆ GetNamedIdAsStringFromJSON()

const char* GetNamedIdAsStringFromJSON ( const json_t *  data_p,
const char *const  key_s 
)

◆ GetIdFromJSONKeyValuePair()

bool GetIdFromJSONKeyValuePair ( const json_t *  id_val_p,
bson_oid_t *  id_p 
)

◆ AddCompoundIdToJSON()

bool AddCompoundIdToJSON ( json_t *  data_p,
bson_oid_t *  id_p 
)

◆ AddNamedCompoundIdToJSON()

bool AddNamedCompoundIdToJSON ( json_t *  data_p,
bson_oid_t *  id_p,
const char *  key_s 
)

◆ AddCompoundIdToJSONArray()

bool AddCompoundIdToJSONArray ( json_t *  array_p,
const bson_oid_t *  id_p 
)

◆ AddIdToJSON()

bool AddIdToJSON ( json_t *  data_p,
bson_oid_t *  id_p,
const char *  key_s 
)

◆ AddQueryTerm()

bool AddQueryTerm ( bson_t *  query_p,
const char *  key_s,
const char *  value_s,
bool  regex_flag 
)

◆ GetNumberOfMongoResults()

int64 GetNumberOfMongoResults ( MongoTool tool_p,
bson_t *  query_p,
bson_t *  extra_opts_p 
)

◆ ProcessMongoResults()

OperationStatus ProcessMongoResults ( MongoTool tool_p,
bson_t *  query_p,
bson_t *  extra_opts_p,
bool(*)(const bson_t *document_p, void *data_p)  process_bson_fn,
void *  data_p 
)

Variable Documentation

◆ MONGO_OID_KEY_S

const char* MONGO_OID_KEY_S = "$oid"

◆ MONGO_TIMESTAMP_S

const char* MONGO_TIMESTAMP_S = "modified"

The key for specifying the timestamp of a saved piece of data.