From 6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 6 Nov 2024 16:45:09 -0800 Subject: Tests are back, minor fixes. There is a cache tracking bug exposed in cachedel test, it is unclear if this is a regression yet. --- src/stable/myriad.cpp | 22 ++-- src/tests/cache.cpp | 289 ++++++++++++++++++++++++++++++++++++++++++++ src/tests/cachedel.cpp | 291 +++++++++++++++++++++++++++++++++++++++++++++ src/tests/mfstest.cpp | 88 ++++++++++++++ src/unstable/myriadcache.h | 6 +- 5 files changed, 683 insertions(+), 13 deletions(-) create mode 100644 src/tests/cache.cpp create mode 100644 src/tests/cachedel.cpp create mode 100644 src/tests/mfstest.cpp (limited to 'src') diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index 492676e..53250c2 100644 --- a/src/stable/myriad.cpp +++ b/src/stable/myriad.cpp @@ -152,19 +152,21 @@ void Bu::Myriad::erase( Bu::Myriad::StreamId iStream ) "No such stream exists."); } Stream *pStream = hStream.get( iStream ); - Bu::MutexLocker sl( pStream->mAccess ); - if( pStream->iOpenCount > 0 ) { - throw Bu::MyriadException( Bu::MyriadException::streamOpen, - "Cannot currently erase a stream while it is open."); - } + Bu::MutexLocker sl( pStream->mAccess ); + if( pStream->iOpenCount > 0 ) + { + throw Bu::MyriadException( Bu::MyriadException::streamOpen, + "Cannot currently erase a stream while it is open."); + } - for( Bu::Array::iterator i = pStream->aBlocks.begin(); i; i++ ) - { - releaseBlock( *i, false ); + for( Bu::Array::iterator i = pStream->aBlocks.begin(); i; i++ ) + { + releaseBlock( *i, false ); + } + pStream->aBlocks.clear(); + hStream.erase( iStream ); } - pStream->aBlocks.clear(); - hStream.erase( iStream ); delete pStream; } diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp new file mode 100644 index 0000000..4751559 --- /dev/null +++ b/src/tests/cache.cpp @@ -0,0 +1,289 @@ +#include "bu/myriadcache.h" +#include "bu/uuid.h" +#include "bu/string.h" +#include "bu/sio.h" + +class Something : public Bu::CacheObject +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ); +public: + Something() + { + } + + Something( const Bu::String &sName ) : + uId( Bu::Uuid::generate() ), + sName( sName ) + { + } + + virtual ~Something() + { + } + + virtual Bu::Uuid getKey() const + { + return uId; + } + + Bu::String getName() const + { + return sName; + } + + void setName( const Bu::String &sNewName ) + { + sName = sNewName; + changed(); + } + + virtual Bu::String toString() const=0; + +private: + Bu::Uuid uId; + Bu::String sName; +}; + +class SubSomethingA : public Something +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s ); +public: + SubSomethingA() + { + } + + SubSomethingA( const Bu::String &sName, int iNumber ) : + Something( sName ), + iNumber( iNumber ) + { + } + + virtual Bu::String toString() const + { + return Bu::String("[typeA] %1 (%2)").arg( getName() ).arg( iNumber ); + } + +private: + int iNumber; +}; + +class SubSomethingB : public Something +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s ); +public: + SubSomethingB() + { + } + + SubSomethingB( const Bu::String &sName, const Bu::String &sString ) : + Something( sName ), + sString( sString ) + { + } + + virtual Bu::String toString() const + { + return Bu::String("[typeB] %1 (%2)").arg( getName() ).arg( sString ); + } + +private: + Bu::String sString; +}; + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ) +{ + return ar >> s.uId >> s.sName; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ) +{ + return ar << s.uId << s.sName; +} + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s ) +{ + return ar >> (Something &)s >> s.iNumber; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s ) +{ + return ar << (Something &)s << s.iNumber; +} + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s ) +{ + return ar >> (Something &)s >> s.sString; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s ) +{ + return ar << (Something &)s << s.sString; +} + +namespace Bu +{ + template<> + void _cacheObjectSave( Bu::Stream &s, const Something *pObject ) + { + Bu::Archive ar( s, Bu::Archive::save ); + if( typeid(*pObject) == typeid(SubSomethingA) ) + { + ar << (uint8_t)1 << (SubSomethingA &)*pObject; + } + else if( typeid(*pObject) == typeid(SubSomethingB) ) + { + ar << (uint8_t)2 << (SubSomethingB &)*pObject; + } + else + { + Bu::println("Not a recognized type!"); + throw Bu::ExceptionBase("Not recognized type!"); + } + } + + template<> + Something *_cacheObjectLoad( Bu::CacheObject::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s ) + { + Bu::Archive ar( s, Bu::Archive::load ); + uint8_t uType; + ar >> uType; + switch( uType ) + { + case 1: + { + SubSomethingA *ret = initObj(new SubSomethingA()); + ar >> *ret; + return ret; + } + break; + + case 2: + { + SubSomethingB *ret = initObj(new SubSomethingB()); + ar >> *ret; + return ret; + } + break; + + default: + throw Bu::ExceptionBase("Flagrant error! Invalid type!"); + } + + return NULL; + } +} + +typedef Bu::CachePtr SomethingPtr; +typedef Bu::CachePtr SomethingAPtr; +typedef Bu::CachePtr SomethingBPtr; +typedef Bu::MyriadCache SomethingCache; + +int main( int, char *[] ) +{ + Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite); + SomethingCache c( fStore ); + + SomethingPtr ptr; + if( time(NULL)%2 ) + ptr = c.insert( new SubSomethingA("Hello", 55) ).cast(); + else + ptr = c.insert( new SubSomethingB("Goodbye", "Things") ).cast(); + + Bu::println("Something[%1]: %2"). + arg( ptr.getKey() ). + arg( ptr->getName() ); + + SomethingCache::KeyList lKeys = c.getKeys(); + Bu::println("Count: %1").arg( lKeys.getSize() ); + for( SomethingCache::KeyList::iterator i = lKeys.begin(); i; i++ ) + { + Bu::println(" - %1: '%2'").arg( *i ).arg( c.get( *i )->toString() ); + } + Bu::println("Count: %1").arg( c.getSize() ); + + SomethingPtr p2 = c.get( ptr.getKey() ); + Bu::println("%1 == %2").arg( p2->getName() ).arg( ptr->getName() ); + SomethingPtr p3 = ptr.cast(); + Bu::println("%1: %2").arg( p3.getKey() ).arg( p3->getName() ); + + return 0; +} + +/* +int main( int argc, char *argv[] ) +{ + Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite); + SomethingCache c( fStore ); + + for( int j = 1; j < argc; j++ ) + { + SomethingPtr ptr = c.insert( new Something(argv[j]) ); + + Bu::println("Something[%1]: %2"). + arg( ptr.getKey() ). + arg( ptr->getName() ); + } + + SomethingCache::KeyList lKeys = c.getKeys(); + Bu::println("Count: %1").arg( lKeys.getSize() ); + int j = 0; + for( SomethingCache::KeyList::iterator i = lKeys.begin(); i; i++ ) + { + Bu::println(" - %1: '%2'").arg( *i ).arg( c.get( *i )->getName() ); + if( ((j++)%2) ) + c.erase( *i ); + } + Bu::println("Count: %1").arg( c.getSize() ); + + c._debug(); + + SomethingPtr p2; + SomethingPtr p1( c.get( lKeys.first() ) ); + + c._debug(); + + { + SomethingPtr p2( p1 ); + c._debug(); + } + + c._debug(); + + p2 = p1; + + c._debug(); + + p1.unbind(); + + c._debug(); + + Bu::println("Name: %1").arg( p1->getName() ); + + p1.unbind(); + p1.lock(); + p1.unlock(); + + c._debug(); + + SomethingPtr p3 = c.getLazy( lKeys.first() ); + + c._debug(); + + { + SomethingPtr::Locker l( p3 ); + + Bu::println("Name again: %1").arg( p3->getName() ); + + p3->setName( p3->getName() + " - again" ); + } + + c.sync(); + + c._debug(); + + return 0; +} +*/ diff --git a/src/tests/cachedel.cpp b/src/tests/cachedel.cpp new file mode 100644 index 0000000..3fa3e86 --- /dev/null +++ b/src/tests/cachedel.cpp @@ -0,0 +1,291 @@ +#include "bu/myriadcache.h" +#include "bu/uuid.h" +#include "bu/string.h" +#include "bu/sio.h" +#include "bu/membuf.h" + +class Something : public Bu::CacheObject +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ); +public: + Something() + { + } + + Something( const Bu::String &sName ) : + uId( Bu::Uuid::generate() ), + sName( sName ) + { + } + + virtual ~Something() + { + } + + virtual Bu::Uuid getKey() const + { + return uId; + } + + Bu::String getName() const + { + return sName; + } + + void setName( const Bu::String &sNewName ) + { + sName = sNewName; + changed(); + } + + virtual Bu::String toString() const=0; + +private: + Bu::Uuid uId; + Bu::String sName; +}; + +class SubSomethingA : public Something +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s ); +public: + SubSomethingA() + { + } + + SubSomethingA( const Bu::String &sName, int iNumber ) : + Something( sName ), + iNumber( iNumber ) + { + } + + virtual Bu::String toString() const + { + return Bu::String("[typeA] %1 (%2)").arg( getName() ).arg( iNumber ); + } + +private: + int iNumber; +}; + +class SubSomethingB : public Something +{ +friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s ); +friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s ); +public: + SubSomethingB() + { + } + + SubSomethingB( const Bu::String &sName, const Bu::String &sString ) : + Something( sName ), + sString( sString ) + { + } + + virtual Bu::String toString() const + { + return Bu::String("[typeB] %1 (%2)").arg( getName() ).arg( sString ); + } + +private: + Bu::String sString; +}; + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ) +{ + return ar >> s.uId >> s.sName; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ) +{ + return ar << s.uId << s.sName; +} + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s ) +{ + return ar >> (Something &)s >> s.iNumber; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s ) +{ + return ar << (Something &)s << s.iNumber; +} + +Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s ) +{ + return ar >> (Something &)s >> s.sString; +} + +Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s ) +{ + return ar << (Something &)s << s.sString; +} + +namespace Bu +{ + template<> + void _cacheObjectSave( Bu::Stream &s, const Something *pObject ) + { + Bu::Archive ar( s, Bu::Archive::save ); + if( typeid(*pObject) == typeid(SubSomethingA) ) + { + ar << (uint8_t)1 << (SubSomethingA &)*pObject; + } + else if( typeid(*pObject) == typeid(SubSomethingB) ) + { + ar << (uint8_t)2 << (SubSomethingB &)*pObject; + } + else + { + Bu::println("Not a recognized type!"); + throw Bu::ExceptionBase("Not recognized type!"); + } + } + + template<> + Something *_cacheObjectLoad( Bu::CacheObject::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s ) + { + Bu::Archive ar( s, Bu::Archive::load ); + uint8_t uType; + ar >> uType; + switch( uType ) + { + case 1: + { + SubSomethingA *ret = initObj(new SubSomethingA()); + ar >> *ret; + return ret; + } + break; + + case 2: + { + SubSomethingB *ret = initObj(new SubSomethingB()); + ar >> *ret; + return ret; + } + break; + + default: + throw Bu::ExceptionBase("Flagrant error! Invalid type!"); + } + + return NULL; + } +} + +typedef Bu::CachePtr SomethingPtr; +typedef Bu::CachePtr SomethingAPtr; +typedef Bu::CachePtr SomethingBPtr; +typedef Bu::MyriadCache SomethingCache; + +int main( int, char *[] ) +{ + Bu::MemBuf mbStore; + SomethingCache c( mbStore ); + + SomethingPtr ptr; + if( time(NULL)%2 ) + ptr = c.insert( new SubSomethingA("Hello", 55) ).cast(); + else + ptr = c.insert( new SubSomethingB("Goodbye", "Things") ).cast(); + + Bu::Uuid id = ptr.getKey(); + Bu::println("Something[%1]: %2"). + arg( ptr.getKey() ). + arg( ptr->getName() ); + + Bu::println("has %1: %2").arg( id ).arg( c.has( id ) ); + c.erase( ptr.getKey() ); + Bu::println("has %1: %2").arg( id ).arg( c.has( id ) ); + SomethingPtr b = ptr; + + SomethingPtr p2 = c.insert( new SubSomethingA("new test", 123) ).cast(); + id = p2.getKey(); + Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) ); + p2.unbind(); + Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) ); + c.erase( id ); + Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) ); + + return 0; +} + +/* +int main( int argc, char *argv[] ) +{ + Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite); + SomethingCache c( fStore ); + + for( int j = 1; j < argc; j++ ) + { + SomethingPtr ptr = c.insert( new Something(argv[j]) ); + + Bu::println("Something[%1]: %2"). + arg( ptr.getKey() ). + arg( ptr->getName() ); + } + + SomethingCache::KeyList lKeys = c.getKeys(); + Bu::println("Count: %1").arg( lKeys.getSize() ); + int j = 0; + for( SomethingCache::KeyList::iterator i = lKeys.begin(); i; i++ ) + { + Bu::println(" - %1: '%2'").arg( *i ).arg( c.get( *i )->getName() ); + if( ((j++)%2) ) + c.erase( *i ); + } + Bu::println("Count: %1").arg( c.getSize() ); + + c._debug(); + + SomethingPtr p2; + SomethingPtr p1( c.get( lKeys.first() ) ); + + c._debug(); + + { + SomethingPtr p2( p1 ); + c._debug(); + } + + c._debug(); + + p2 = p1; + + c._debug(); + + p1.unbind(); + + c._debug(); + + Bu::println("Name: %1").arg( p1->getName() ); + + p1.unbind(); + p1.lock(); + p1.unlock(); + + c._debug(); + + SomethingPtr p3 = c.getLazy( lKeys.first() ); + + c._debug(); + + { + SomethingPtr::Locker l( p3 ); + + Bu::println("Name again: %1").arg( p3->getName() ); + + p3->setName( p3->getName() + " - again" ); + } + + c.sync(); + + c._debug(); + + return 0; +} +*/ diff --git a/src/tests/mfstest.cpp b/src/tests/mfstest.cpp new file mode 100644 index 0000000..813ef8e --- /dev/null +++ b/src/tests/mfstest.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +class Obstore +{ +public: + Obstore() : + fStore("test.mfs", Bu::File::WriteNew|Bu::File::Read ), + fsOb( fStore, 1024 ) + { + } + + virtual ~Obstore() + { + } + + Bu::String getObject( const Bu::String &sPath ) + { + try + { + Bu::MyriadStream s = fsOb.open( + sPath, + Bu::MyriadFs::Read + ); + return s.readAll(); + } + catch( Bu::ExceptionBase &e ) + { + Bu::println("Exception opening file: %1").arg( e.what() ); + return Bu::String(); + } + } + + void storeObject( const Bu::String &sPath, const Bu::String &sData ) + { + for( Bu::String::const_iterator i = sPath.begin()+1; i; i++ ) + { + if( *i == '/' ) + { + Bu::String sChunk( sPath.begin(), i ); + fsOb.mkDir( sChunk, 0664 ); + } + } + try + { + Bu::MyriadStream s = fsOb.open( + sPath, + Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate + ); + s.write( sData ); + } + catch(Bu::ExceptionBase &e ) + { + Bu::println("Exception opening file: %1").arg( e.what() ); + } + } + + Bu::File fStore; + Bu::MyriadFs fsOb; +}; + +int main( int, char *[] ) +{ + Obstore os; + + os.storeObject("/killTeamState.json", "This is a json file."); + os.storeObject("/appCredentials/local", "App credentials"); + os.storeObject("/appCredentials/inckybot", "App credentials"); + os.storeObject("/appCredentials/playwith", "App credentials"); + os.storeObject("/restreamIo/users", "User data"); + os.storeObject("/youTube/users", "User data"); + os.storeObject("/topTippers.json", "{\"people\": [\"sam\", \"joe\", \"bob\"]}"); + os.storeObject("/wh40kDecks.json", ""); + + Bu::MyriadStream s = os.fsOb.open( + "/appCredentials/inckybot", + Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate + ); + s.write("Some new credentials, this is a bigger bit of data."); + + os.storeObject("/wh40kDecks.json", "{ \"op\": \"replaceDecks\", \"decks\": { \"lev\": { \"name\": \"Leviathan\", \"primary\": { \"pt\": {\"name\": \"Priority Targets\"}, \"vg\": {\"name\": \"Vital Ground\"}, \"se\": {\"name\": \"Scorched Earth\"}, \"pf\": {\"name\": \"Purge the Foe\"}, \"th\": {\"name\": \"Take and Hold\"}, \"sd\": {\"name\": \"Supply Drop\"}, \"tr\": {\"name\": \"The Ritual\"}, \"ds\": {\"name\": \"Deploy Servo-Skulls\"}, \"sp\": {\"name\": \"Sites of Power\"}, }, \"secondary\": { \"tt\": {\"name\": \"A Tempting Target\"}, \"ad\": {\"name\": \"Area Denial\"}, \"as\": {\"name\": \"Assassination\"}, \"be\": {\"name\": \"Behind Enemy Lines\"}, \"bd\": {\"name\": \"Bring It Down\"}, \"ce\": {\"name\": \"Capture Enemy Outpost\"}, \"cl\": {\"name\": \"Cleanse\"}, \"ds\": {\"name\": \"Defend Stronghold\"}, \"dt\": {\"name\": \"Deploy Teleport Homer\"}, \"ef\": {\"name\": \"Engage On All Fronts\"}, \"eb\": {\"name\": \"Extend Battle Lines\"}, \"is\": {\"name\": \"Investigate Signals\"}, \"np\": {\"name\": \"No Prisoners\"}, \"of\": {\"name\": \"Overwhelming Force\"}, \"sl\": {\"name\": \"Secure No Man’s Land\"}, \"sh\": {\"name\": \"Storm Hostile Objective\"}, }, \"gambit\": { \"dt\": {\"name\": \"Delaying Tactics\"}, \"ee\": {\"name\": \"Emergency Evacuation\"}, \"oc\": {\"name\": \"Orbital Strike Coordinates\"}, \"pp\": {\"name\": \"Proceed As Planned\"}, }, \"missionRule\": { \"cr\": {\"name\": \"Chilling Rain\"}, \"sc\": {\"name\": \"Sweep and Clear\"}, \"hs\": {\"name\": \"Hidden Supplies\"}, \"mi\": {\"name\": \"Minefields\"}, \"to\": {\"name\": \"Targets of Opportunity\"}, \"sf\": {\"name\": \"Scrambler Fields\"}, \"dr\": {\"name\": \"Delayed Reserves\"}, \"cb\": {\"name\": \"Chosen Battlefield\"}, \"mb\": {\"name\": \"Maelstrom of Battle\"}, \"sl\": {\"name\": \"Supply Lines\"}, \"si\": {\"name\": \"Secret Intel\"}, \"vs\": {\"name\": \"Vox Static\"}, }, }, } }"); + os.storeObject("/wh40kState.json", "{\"cards\": \"hello\"}"); + + return 0; +} + diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h index f71f9b5..289ab05 100644 --- a/src/unstable/myriadcache.h +++ b/src/unstable/myriadcache.h @@ -108,13 +108,13 @@ namespace Bu const keytype &k ) { - Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); + Bu::MyriadStream ms = mStore.open( hIndex.get( k ), Bu::Myriad::Read ); return _cacheObjectLoad( initObj, k, ms ); } virtual void _save( const obtype *o ) { - Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); + Bu::MyriadStream ms = mStore.open( hIndex.get( o->getKey() ), Bu::Myriad::WriteNew ); _cacheObjectSave( ms, o ); ms.setSize( ms.tell() ); @@ -127,7 +127,7 @@ namespace Bu if( !bStructureChanged ) return; - Bu::MyriadStream ms = mStore.openStream( 1 ); + Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::WriteNew ); Bu::Archive ar( ms, Bu::Archive::save ); ar << (uint8_t)0 << hIndex; ar.close(); -- cgit v1.2.3