From 9d1074612a1934a1c06e264b74e89e2141616381 Mon Sep 17 00:00:00 2001 From: Geoff Evans Date: Sat, 28 Apr 2018 16:40:50 -0700 Subject: [PATCH] Moved --- API.h | 23 -- LICENSE.md | 28 --- Mongo.cpp | 613 ------------------------------------------------- Mongo.h | 132 ----------- Mongo.inl | 48 ---- Precompile.cpp | 13 -- Precompile.h | 17 -- README.md | 18 +- 8 files changed, 1 insertion(+), 891 deletions(-) delete mode 100644 API.h delete mode 100644 LICENSE.md delete mode 100644 Mongo.cpp delete mode 100644 Mongo.h delete mode 100644 Mongo.inl delete mode 100644 Precompile.cpp delete mode 100644 Precompile.h diff --git a/API.h b/API.h deleted file mode 100644 index 739c601..0000000 --- a/API.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "Platform/System.h" - -#include "Foundation/Profile.h" - -#if HELIUM_SHARED -# ifdef HELIUM_MONGO_EXPORTS -# define HELIUM_MONGO_API HELIUM_API_EXPORT -# else -# define HELIUM_MONGO_API HELIUM_API_IMPORT -# endif -#else -#define HELIUM_MONGO_API -#endif - -#define HELIUM_MONGO_PROFILE 0 - -#if HELIUM_PROFILE_INSTRUMENT_ALL || HELIUM_MONGO_PROFILE -#define HELIUM_MONGO_SCOPE_TIMER( ... ) HELIUM_PROFILE_SCOPE_TIMER( __VA_ARGS__ ) -#else -#define HELIUM_MONGO_SCOPE_TIMER( ... ) -#endif diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index 54081fe..0000000 --- a/LICENSE.md +++ /dev/null @@ -1,28 +0,0 @@ -Helium Open License -------------------- - -Portions Copyright (c) 201x, WhiteMoon Dreams, Inc. -Portions Copyright (c) 200x, Insomniac Games, Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notices, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -Neither the name of Whitemoon Dreams, Inc. nor Insomniac Games, Inc. nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -Suggested Header for Source Files ---------------------------------- - -\ -\ -Portions Copyright (C) 201x Whitemoon Dreams, Inc. -Portions Copyright (C) 200x Insomniac Games, Inc. - -This library is free software; you can redistribute it and/or modify it under the terms of the Helium Open License. - -This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even an implied warranty. See the Helium Open License for more details. - -You should have received a copy of the Helium Open License along with this code; if not, visit: http://github.com/HeliumProject/Helium diff --git a/Mongo.cpp b/Mongo.cpp deleted file mode 100644 index 6f9476b..0000000 --- a/Mongo.cpp +++ /dev/null @@ -1,613 +0,0 @@ -#include "Precompile.h" -#include "Mongo/Mongo.h" - -#include "Foundation/Log.h" -#include "Foundation/MemoryStream.h" - -HELIUM_DEFINE_CLASS( Helium::Mongo::Model ); - -using namespace Helium; -using namespace Helium::Reflect; -using namespace Helium::Persist; -using namespace Helium::Mongo; - -static String GetNamespace( const String& dbName, const char* collection, const MetaClass* metaClass ) -{ - String ns ( dbName ); - ns += "."; - - if ( collection ) - { - ns += collection; - return ns; - } - - std::string defaultCollection; - if ( metaClass->GetProperty( "defaultCollection", defaultCollection ) ) - { - ns += defaultCollection.c_str(); - return ns; - } - - String metaClassName ( metaClass->m_Name ); - for ( size_t startIndex = 0, foundIndex = Invalid(); ( foundIndex = metaClassName.Find( ':', startIndex ) ) != Invalid(); ) - { - metaClassName.Remove( foundIndex, 1 ); - } - - ns += metaClassName; - return ns; -} - -void Model::PopulateMetaType( Helium::Reflect::MetaStruct& type ) -{ - // some db interactions require _id NOT be set (update), so discard the value but load it if we find it in data - type.AddField( &Model::id, "_id", Reflect::FieldFlags::Discard ); -} - -namespace Helium -{ - namespace Mongo - { - int initCount = 0; - int number = 0; - - static int Increment() - { - int result = Helium::AtomicIncrement( number ); - return result; - } - } -} - -void Mongo::Initialize() -{ - if ( ++Mongo::initCount == 1 ) - { - mongo_init_sockets(); - bson_set_oid_inc( &Increment ); - } -} - -void Mongo::Cleanup() -{ - if ( --Mongo::initCount == 0 ) - { - bson_set_oid_inc( NULL ); - } -} - -const char* Mongo::GetErrorString( int status ) -{ - switch( status ) - { - case MONGO_CONN_NO_SOCKET: return "Could not create a socket"; - case MONGO_CONN_FAIL: return "An error occured while calling connect()"; - case MONGO_CONN_ADDR_FAIL: return "An error occured while calling getaddrinfo()"; - case MONGO_CONN_NOT_MASTER: return "Warning: connected to a non-master node (read-only)"; - case MONGO_CONN_BAD_SET_NAME: return "Given rs name doesn't match this replica set"; - case MONGO_CONN_NO_PRIMARY: return "Can't find primary in replica set. Connection closed"; - case MONGO_IO_ERROR: return "An error occurred while reading or writing on the socket"; - case MONGO_SOCKET_ERROR: return "Other socket error"; - case MONGO_READ_SIZE_ERROR: return "The response is not the expected length"; - case MONGO_COMMAND_FAILED: return "The command returned with 'ok' value of 0"; - case MONGO_WRITE_ERROR: return "Write with given write_concern returned an error"; - case MONGO_NS_INVALID: return "The name for the ns (database or collection) is invalid"; - case MONGO_BSON_INVALID: return "BSON not valid for the specified op"; - case MONGO_BSON_NOT_FINISHED: return "BSON object has not been finished"; - case MONGO_BSON_TOO_LARGE: return "BSON object exceeds max BSON size"; - case MONGO_WRITE_CONCERN_INVALID: return "Supplied write concern object is invalid"; - } - - return "Unknown error"; -} - -Cursor::Cursor( Database* db, mongo_cursor* cursor ) - : db( db ) - , cursor( cursor ) -{ -} - -Cursor::Cursor( const Cursor& rhs ) - : db( rhs.db ) - , cursor( rhs.cursor ) -{ - rhs.db = NULL; - rhs.cursor = NULL; -} - -Cursor::~Cursor() -{ - if ( this->cursor ) - { - mongo_cursor_destroy( this->cursor ); - } -} - -Helium::StrongPtr< Model > Cursor::Next( const Reflect::MetaClass* defaultType ) -{ - if ( !HELIUM_VERIFY( db ) || !HELIUM_VERIFY( cursor ) || !HELIUM_VERIFY_MSG( db->IsCorrectThread(), "Database access from improper thread" ) ) - { - return NULL; - } - - Helium::StrongPtr< Model > object; - - while ( !object.ReferencesObject() && mongo_cursor_next( cursor ) == MONGO_OK ) - { - const Reflect::MetaClass* type = defaultType; - - // if we have type info encoded in the BSON, use it instead of the defaultType - bson_iterator i[1]; - if ( BSON_STRING == bson_find( i, &cursor->current, "_type" ) ) - { - const char* typeName = bson_iterator_string( i ); - HELIUM_ASSERT( typeName ); - const Reflect::MetaClass* storedType = Reflect::Registry::GetInstance()->GetMetaClass( typeName ); - if ( storedType ) - { - type = storedType; - } - } - - if ( type ) - { - object = Reflect::AssertCast< Model >( type->m_Creator() ); - } - - if ( object.ReferencesObject() ) - { - bson_iterator_init( i, &cursor->current ); - - try - { - Helium::Persist::ArchiveReaderBson::ReadFromBson( i, reinterpret_cast< Helium::Reflect::ObjectPtr& >( object ) ); - } - catch ( Helium::Exception& ex ) - { - Helium::Log::Error( "Failed to parse BSON in query result: %s\n", ex.What() ); - object = NULL; - } - } - } - - return object; -} - -bool Cursor::Next( const Helium::StrongPtr< Model >& object ) -{ - if ( !HELIUM_VERIFY( db ) || !HELIUM_VERIFY( cursor ) || !HELIUM_VERIFY_MSG( db->IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - bool result = true; - - if ( mongo_cursor_next( cursor ) == MONGO_OK ) - { - bson_iterator i[1]; - bson_iterator_init( i, &cursor->current ); - try - { - Helium::Persist::ArchiveReaderBson::ReadFromBson( i, reinterpret_cast< const Helium::Reflect::ObjectPtr& >( object ) ); - } - catch ( Helium::Exception& ex ) - { - Helium::Log::Error( "Failed to generate BSON for object: %s\n", ex.What() ); - result = false; - } - } - - return result; -} - -Database::Database( const char* name ) - : name( name ) - , isConnected( false ) - , threadId( Thread::GetCurrentId() ) -{ - mongo_init( conn ); -} - -Database::~Database() -{ - mongo_destroy( conn ); -} - -bool Database::Connect( const char* addr, uint16_t port ) -{ - this->threadId = Thread::GetCurrentId(); - - if ( MONGO_OK == mongo_client( conn, addr, port ) ) - { - return isConnected = true; - } - else - { - Log::Error( "mongo_client failed: %s\n", GetErrorString( conn->err ) ); - return isConnected = false; - } -} - -int64_t Database::GetServerTime( bool inMilliseconds ) -{ - bson b[1]; - HELIUM_VERIFY( BSON_OK == bson_init( b ) ); - HELIUM_VERIFY( BSON_OK == bson_append_int( b, "hostInfo", 1 ) ); - HELIUM_VERIFY( BSON_OK == bson_finish( b ) ); - - bson_date_t result = -1; - - bson out[1]; - bson_init_zero( out ); - if ( MONGO_OK == mongo_run_command( conn, "admin", b, out ) ) - { - bson_iterator i[1]; - if ( BSON_OBJECT == bson_find( i, out, "system" ) ) - { - bson systemBson[1]; - bson_init_zero( systemBson ); - bson_iterator_subobject_init( i, systemBson, false ); - if ( BSON_DATE == bson_find( i, systemBson, "currentTime" ) ) - { - result = inMilliseconds ? bson_iterator_date( i ) : bson_iterator_date( i ) / 1000; - } - bson_destroy( systemBson ); - } - } - - bson_destroy( b ); - bson_destroy( out ); - - return result; -} - -bool Database::Drop() -{ - if ( !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - if ( MONGO_OK != mongo_cmd_drop_db( conn, name.GetData() ) ) - { - Log::Error( "mongo_cmd_drop_db failed: %s\n", GetErrorString( conn->err ) ); - return false; - } - - return true; -} - -bool Database::DropCollection( const char* name ) -{ - if ( !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - if ( MONGO_OK != mongo_cmd_drop_collection( conn, this->name.GetData(), name, NULL ) ) - { - Log::Error( "mongo_cmd_drop_collection failed: %s\n", GetErrorString( conn->err ) ); - return false; - } - - return true; -} - -double Database::GetCollectionCount( const char* name ) -{ - return mongo_count( conn, this->name.GetData(), name, NULL ); -} - -bool Database::CreateCappedCollection( const char* name, int cappedSizeInBytes, int cappedMaxCount ) -{ - if ( !HELIUM_VERIFY( IsCorrectThread() ) ) - { - return false; - } - - if ( HELIUM_VERIFY( cappedSizeInBytes ) ) - { - int result = mongo_create_capped_collection( conn, this->name.GetData(), name, cappedSizeInBytes, cappedMaxCount, NULL ); - if ( MONGO_OK != result && MONGO_COMMAND_FAILED != conn->err ) - { - Log::Error( "mongo_create_capped_collection failed: %s\n", GetErrorString( conn->err ) ); - // call IsConnected to update the value of the isConnected flag. - IsConnected( true ); - return false; - } - } - - return true; -} - -bool Database::Insert( const StrongPtr< Model >& object, const char* collection ) -{ - if ( !HELIUM_VERIFY( object->id == BsonObjectId::Null ) || !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - bool result = true; - - bson_oid_t oid[1]; - bson_oid_gen( oid ); - MemoryCopy( object->id.bytes, oid->bytes, sizeof( bson_oid_t ) ); - - bson b[1]; - HELIUM_VERIFY( BSON_OK == bson_init( b ) ); - try - { - bson_oid_t oid[1]; - MemoryCopy( oid, object->id.bytes, sizeof( bson_oid_t ) ); - HELIUM_VERIFY( BSON_OK == bson_append_oid( b, "_id", oid ) ); - HELIUM_VERIFY( BSON_OK == bson_append_string( b, "_type", object->GetMetaClass()->m_Name ) ); - ArchiveWriterBson::WriteToBson( object, b ); - } - catch ( Helium::Exception& ) - { - result = false; - } - HELIUM_VERIFY( BSON_OK == bson_finish( b ) ); - - if ( result ) - { - String ns = GetNamespace( name, collection, object->GetMetaClass() ); - if ( MONGO_OK != mongo_insert( conn, ns.GetData(), b, NULL ) ) - { - Log::Error( "mongo_insert failed: %s\n", GetErrorString( conn->err ) ); - // call IsConnected to update the value of the isConnected flag. - IsConnected( true ); - result = false; - } - } - - bson_destroy( b ); - return result; -} - -bool Database::Update( const StrongPtr< Model >& object, const char* collection ) -{ - if ( !HELIUM_VERIFY_MSG( object->id != BsonObjectId::Null, "Cannot update object with null id" ) || !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - bson_oid_t oid[1]; - MemoryCopy( oid, object->id.bytes, sizeof( bson_oid_t ) ); -#if HELIUM_DEBUG - char str[ sizeof(bson_oid_t) * 2 + 1 ] = { 0 }; - bson_oid_to_string( oid, str ); -#endif - - bson cond[1]; - HELIUM_VERIFY( BSON_OK == bson_init( cond ) ); - HELIUM_VERIFY( BSON_OK == bson_append_oid( cond, "_id", oid ) ); - HELIUM_VERIFY( BSON_OK == bson_finish( cond ) ); - - bson op[1]; - HELIUM_VERIFY( BSON_OK == bson_init( op ) ); - bool result = true; - try - { - HELIUM_VERIFY( BSON_OK == bson_append_start_object( op, "$set" ) ); - ArchiveWriterBson::WriteToBson( object, op ); - HELIUM_VERIFY( BSON_OK == bson_append_finish_object( op ) ); - } - catch ( Helium::Exception& ) - { - Log::Error( "Failed to generate BSON for object\n" ); - result = false; - } - HELIUM_VERIFY( BSON_OK == bson_finish( op ) ); - - if ( result ) - { - String ns = GetNamespace( name, collection, object->GetMetaClass() ); - if ( MONGO_OK != mongo_update( conn, ns.GetData(), cond, op, MONGO_UPDATE_BASIC, NULL ) ) - { - Log::Error( "mongo_update failed: %s\n", GetErrorString( conn->err ) ); - // call IsConnected to update the value of the isConnected flag. - IsConnected( true ); - result = false; - } - } - - bson_destroy( cond ); - bson_destroy( op ); - return result; -} - -bool Database::Get( const StrongPtr< Model >& object, const char* collection ) -{ - if ( !HELIUM_VERIFY_MSG( object->id != BsonObjectId::Null, "Cannot update object with null id" ) || !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - bool result = true; - - bson_oid_t oid[1]; - MemoryCopy( oid, object->id.bytes, sizeof( bson_oid_t ) ); -#if HELIUM_DEBUG - char str[ sizeof(bson_oid_t) * 2 + 1 ] = { 0 }; - bson_oid_to_string( oid, str ); -#endif - - bson query[1]; - HELIUM_VERIFY( BSON_OK == bson_init( query ) ); - HELIUM_VERIFY( BSON_OK == bson_append_oid( query, "_id", oid ) ); - HELIUM_VERIFY( BSON_OK == bson_finish( query ) ); - - bson out[1]; - bson_init_zero( out ); - - { - String ns = GetNamespace( name, collection, object->GetMetaClass() ); - if ( MONGO_OK != mongo_find_one( conn, ns.GetData(), query, NULL, out ) ) - { - Log::Error( "mongo_find_one failed: %s\n", GetErrorString( conn->err ) ); - // call IsConnected to update the value of the isConnected flag. - IsConnected( true ); - result = false; - } - } - - if ( result ) - { - bson_iterator i[1]; - bson_iterator_init( i, out ); - try - { - ArchiveReaderBson::ReadFromBson( i, reinterpret_cast< Helium::StrongPtr< Object >& >( const_cast< StrongPtr< Model >& >( object ) ) ); - } - catch ( Helium::Exception& ) - { - Log::Error( "Failed to generate object for BSON\n" ); - result = false; - } - } - - bson_destroy( query ); - bson_destroy( out ); - return result; -} - -bool Database::Insert( StrongPtr< Model >* objects, size_t count, const char* collection ) -{ - if ( !HELIUM_VERIFY( objects ) || !HELIUM_VERIFY( count ) || !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - bool result = true; - const MetaClass* metaClass = (*objects)->GetMetaClass(); - Helium::DynamicArray< bson > bsons; - Helium::DynamicArray< bson* > pointers; - bsons.Reserve( count ); - pointers.Reserve( count ); - for ( size_t i=0; iGetMetaClass() ) ) - { - continue; - } - - if ( !HELIUM_VERIFY( objects[i]->id == BsonObjectId::Null ) ) - { - continue; - } - - bsons.Add( bson() ); - bson* b = &bsons.GetLast(); - HELIUM_VERIFY( BSON_OK == bson_init( b ) ); - pointers.Add( b ); - - bson_oid_gen( (bson_oid_t*)objects[i]->id.bytes ); - -#if HELIUM_DEBUG - char str[ sizeof(bson_oid_t) * 2 + 1 ] = { 0 }; - bson_oid_to_string( (bson_oid_t*)objects[i]->id.bytes, str ); -#endif - - try - { - bson_oid_t oid[1]; - MemoryCopy( oid, objects[i]->id.bytes, sizeof( bson_oid_t ) ); - HELIUM_VERIFY( BSON_OK == bson_append_oid( b, "_id", oid ) ); - HELIUM_VERIFY( BSON_OK == bson_append_string( b, "_type", objects[i]->GetMetaClass()->m_Name ) ); - ArchiveWriterBson::WriteToBson( reinterpret_cast< ObjectPtr& >( objects[i] ), b ); - } - catch ( Helium::Exception& ) - { - Log::Error( "Failed to generate BSON for object\n" ); - result = false; - } - - HELIUM_VERIFY( BSON_OK == bson_finish( b ) ); - } - - if ( result ) - { - String ns = GetNamespace( name, collection, metaClass ); - if ( MONGO_OK != mongo_insert_batch( conn, ns.GetData(), const_cast< const bson** >( pointers.GetData() ), static_cast< int >( pointers.GetSize() ), NULL, 0x0 ) ) - { - Log::Error( "mongo_insert_batch failed: %s\n", GetErrorString( conn->err ) ); - // call IsConnected to update the value of the isConnected flag. - IsConnected( true ); - result = false; - } - } - - for ( size_t i=0; iname ); - ns += "."; - ns += collection; - - if ( MONGO_OK != mongo_create_index( conn, ns.GetData(), key, name, options, NULL ) ) - { - Log::Error( "mongo_cmd_drop_collection failed: %s\n", GetErrorString( conn->err ) ); - return false; - } - - return true; -} - -Cursor Database::Find( const char* collection, const bson* query, int limit, int skip, int options ) -{ - if ( !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return Cursor(); - } - - Helium::String ns ( name ); - ns += "."; - ns += collection; - - // we don't currently support specifying fields because we need to ensure _type is sent back, and - // merging bson documents is difficult/impossible with the current version of the bson api - mongo_cursor* c = mongo_find( conn, ns.GetData(), query, NULL, limit, skip, options ); - if ( c ) - { - return Cursor( this, c ); - } - else - { - return Cursor(); - } -} - - -bool Database::Remove( const char* collection, const bson* query ) -{ - if ( !HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - return false; - } - - Helium::String ns ( name ); - ns += "."; - ns += collection; - - if ( MONGO_OK != mongo_remove( conn, ns.GetData(), query, NULL ) ) - { - Log::Error( "mongo_cmd_remove failed: %s\n", GetErrorString( conn->err ) ); - return false; - } - - return true; -} diff --git a/Mongo.h b/Mongo.h deleted file mode 100644 index b031106..0000000 --- a/Mongo.h +++ /dev/null @@ -1,132 +0,0 @@ -#pragma once - -#include "API.h" - -#include "Platform/Locks.h" -#include "Platform/Thread.h" - -#include "Foundation/Log.h" -#include "Foundation/String.h" -#include "Foundation/ReferenceCounting.h" - -#include "Reflect/Object.h" -#include "Reflect/TranslatorDeduction.h" - -#include "Persist/ArchiveBson.h" - -#include - -#include - -#define HELIUM_MONGO_DEFAULT_PORT ( 27017 ) - -namespace Helium -{ - namespace Mongo - { - HELIUM_MONGO_API void Initialize(); - HELIUM_MONGO_API void Cleanup(); - HELIUM_MONGO_API const char* GetErrorString( int status ); - - class HELIUM_MONGO_API Model : public Helium::Reflect::Object - { - public: - // the unique-id of the model instance, generated by the client just before insert - // you should not insert an object twice, instead null out the id before inserting another copy - Helium::Persist::BsonObjectId id; - - HELIUM_DECLARE_CLASS( Model, Helium::Reflect::Object ); - static void PopulateMetaType( Helium::Reflect::MetaStruct& type ); - }; - - class Database; - - class HELIUM_MONGO_API Cursor - { - public: - Cursor( Database* db = NULL, mongo_cursor* cursor = NULL ); - Cursor( const Cursor& rhs ); - ~Cursor(); - - // test if our find succeeded - inline bool IsValid(); - - // get a single result of an expected type - template< class DefaultType > - Helium::StrongPtr< DefaultType > Next(); - - // get a single result object from the cursor, specifying the type to allocate if one is not specified in the data - Helium::StrongPtr< Model > Next( const Reflect::MetaClass* defaultType ); - - // read a single object into an existing instance - bool Next( const Helium::StrongPtr< Model >& object ); - - private: - mutable Database* db; - mutable mongo_cursor* cursor; - }; - - class HELIUM_MONGO_API Database : public Helium::NonCopyable - { - public: - Database( const char* name = "" ); - ~Database(); - - // db ops/preferences - inline const char* GetName() const; - void SetName( const char* name ); - bool Connect( const char* addr, uint16_t port = HELIUM_MONGO_DEFAULT_PORT ); - inline bool IsConnected( bool pingServer ); - inline mongo* GetConnection(); - - // thread verification - inline void SetThread( Helium::ThreadId threadId = Thread::GetCurrentId() ); - inline bool IsCorrectThread() const; - - // server routines - int64_t GetServerTime( bool inMilliseconds = false ); - - // database routines - bool Drop(); - - // collection routines - bool DropCollection( const char* name ); - double GetCollectionCount( const char* name ); - bool CreateCappedCollection( const char* name, int cappedSizeInBytes, int cappedMaxCount = 0 ); - - // single insert - bool Insert( const Helium::StrongPtr< Model >& object, const char* collection = NULL ); - - // single update - bool Update( const Helium::StrongPtr< Model >& object, const char* collection = NULL ); - - // batch insert, default collection will be named for the type of collection objects - bool Insert( Helium::StrongPtr< Model >* objects, size_t count, const char* collection = NULL ); - - // single fetch - bool Get( const Helium::StrongPtr< Model >& object, const char* collection = NULL ); - - // index ops - bool EnsureIndex( const char* collection, const bson* key, const char* name = NULL, int options = 0x0 ); - - // find - // query == NULL will return all objects by default - // fields == NULL will populate all fields in result objects, populate to determine which fields are updated - // collection == NULL uses collection named for the type specified in the cursor object - Cursor Find( const char* collection, const bson* query = NULL, int limit = 0, int skip = 0, int options = 0 ); - - // remove - // collection == NULL uses collection named for the type specified in the cursor object - // query == NULL will remove all objects by default - bool Remove( const char* collection, const bson* query = NULL ); - - private: - Helium::String name; - bool isConnected; - mongo conn[1]; - Helium::ThreadId threadId; - }; - } -} - -#include "Mongo/Mongo.inl" \ No newline at end of file diff --git a/Mongo.inl b/Mongo.inl deleted file mode 100644 index b58651e..0000000 --- a/Mongo.inl +++ /dev/null @@ -1,48 +0,0 @@ -bool Helium::Mongo::Cursor::IsValid() -{ - return db != NULL && cursor != NULL; -} - -template< class DefaultType > -Helium::StrongPtr< DefaultType > Helium::Mongo::Cursor::Next() -{ - return Reflect::SafeCast< DefaultType >( Next( Reflect::GetMetaClass< DefaultType >() ) ); -} - -inline const char* Helium::Mongo::Database::GetName() const -{ - return this->name.GetData(); -} - -inline void Helium::Mongo::Database::SetName( const char* name ) -{ - this->name = name; -} - -bool Helium::Mongo::Database::IsConnected( bool pingServer ) -{ - if ( isConnected && pingServer && HELIUM_VERIFY_MSG( IsCorrectThread(), "Database access from improper thread" ) ) - { - isConnected = MONGO_OK == mongo_check_connection( this->conn ); - } - - return isConnected; -} - -mongo* Helium::Mongo::Database::GetConnection() -{ - return &conn[0]; -} - -void Helium::Mongo::Database::SetThread( Helium::ThreadId threadId ) -{ - if ( this->threadId != threadId ) - { - this->threadId = threadId; - } -} - -bool Helium::Mongo::Database::IsCorrectThread() const -{ - return this->threadId == Thread::GetCurrentId(); -} \ No newline at end of file diff --git a/Precompile.cpp b/Precompile.cpp deleted file mode 100644 index 24d1987..0000000 --- a/Precompile.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "Precompile.h" - -#include "Platform/MemoryHeap.h" - -#if HELIUM_HEAP - -HELIUM_DEFINE_DEFAULT_MODULE_HEAP( Reflect ); - -#if HELIUM_DEBUG -#include "Platform/NewDelete.h" -#endif - -#endif // HELIUM_HEAP diff --git a/Precompile.h b/Precompile.h deleted file mode 100644 index ae9073c..0000000 --- a/Precompile.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "Platform/Assert.h" -#include "Platform/Locks.h" -#include "Platform/MemoryHeap.h" -#include "Platform/System.h" -#include "Platform/Thread.h" -#include "Platform/Trace.h" - -#include "Foundation/Log.h" -#include "Foundation/ReferenceCounting.h" -#include "Foundation/String.h" - -#include "Reflect/Object.h" -#include "Reflect/TranslatorDeduction.h" - -#include "Persist/ArchiveBson.h" diff --git a/README.md b/README.md index 75d0c6d..2ee2382 100644 --- a/README.md +++ b/README.md @@ -1,17 +1 @@ -![Helium Game Engine](https://raw.githubusercontent.com/HeliumProject/Helium/master/Editor/Icons/Helium/Helium.png) - -Mongo completely automates the serialization of an object to and from a MongoDB database. The [Reflect](https://github.com/HeliumProject/Reflect) C++ reflection library provides information about the member variable layout of a class of object. Model objects contain only a single new member variable: id, which follows the _id variable that stores the internal unique id of an object in MongoDB. - -Design -====== - -This module was written to implement the database modelling design pattern in C++ using reflection information provided by Reflect. At the moment its focused on correctly serializing C++ classes to and from BSON within the context of a database connection. Queries and indexes must be implemented outside the scope of this module. Inserts are only valid using a model object with a null id, and Updates are only valid with a model object with a non-null id. - -Implementation -============== - -Mongo was written to not rely on locks when accessing the database. Mongo::Database merely tracks which thread is intended to use the database connection, and it will assert if the wrong thread causes the connection to be used. - -Location -======== -https://github.com/HeliumProject/Mongo +Moved: https://github.com/HeliumProject/Core