From 6403224b6fe50dfc28d3c25725b6d0910b7eb6c3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 3 Oct 2024 11:28:44 -0700 Subject: Minor tweaks/additions. Now to fix MyriadFs --- src/stable/myriad.cpp | 30 ++++++++++++++++++++++++++-- src/stable/myriad.h | 49 +++++++++++++++++++++++++++++++++++++++++++--- src/unstable/myriadcache.h | 4 ++-- src/unstable/myriadfs.cpp | 19 +++++------------- src/unstable/myriadfs.h | 4 ++-- 5 files changed, 83 insertions(+), 23 deletions(-) diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index 5e511fd..492676e 100644 --- a/src/stable/myriad.cpp +++ b/src/stable/myriad.cpp @@ -129,6 +129,19 @@ Bu::MyriadStream Bu::Myriad::open( Bu::Myriad::StreamId iStream, return Bu::MyriadStream( *this, pStream, eMode ); } +Bu::Myriad::StreamId Bu::Myriad::allocate() +{ + Bu::MutexLocker l( mAccess ); + + Stream *pStream = new Stream( *this, ++iLastUsedIndex, 0 ); + mhStream.lock(); + hStream.insert( pStream->iStream, pStream ); + mhStream.unlock(); + bStructureChanged = true; + + return pStream->iStream; +} + void Bu::Myriad::erase( Bu::Myriad::StreamId iStream ) { // For now, let's prevent you from erasing a stream if it's open. @@ -252,6 +265,19 @@ int32_t Bu::Myriad::getTotalUnusedBytes(int32_t iAssumeBlockSize ) const return iTotal; } +Bu::Myriad::StreamIdList Bu::Myriad::getStreamList() const +{ + mhStream.lock(); + StreamIdList lIds = hStream.getKeys(); + mhStream.unlock(); + lIds.sort(); + if( lIds.first() == 0 ) + { + lIds.eraseFirst(); + } + return lIds; +} + Bu::BitString Bu::Myriad::buildBlockUseMap() const { Bu::MutexLocker l( mAccess ); @@ -264,10 +290,10 @@ Bu::BitString Bu::Myriad::buildBlockUseMap() const return bsMap; } -Bu::Array Bu::Myriad::buildBlockMap() const +Bu::Myriad::StreamIdArray Bu::Myriad::buildBlockMap() const { Bu::MutexLocker l( mAccess ); - Bu::Array bm( iBlockCount ); + StreamIdArray bm( iBlockCount ); for( int j = 0; j < iBlockCount; j++ ) { bm.append( -1 ); diff --git a/src/stable/myriad.h b/src/stable/myriad.h index 6168aa2..5accd1e 100644 --- a/src/stable/myriad.h +++ b/src/stable/myriad.h @@ -31,10 +31,20 @@ namespace Bu }; subExceptionDeclEnd(); + /** + * Myriad Stream Multiplexer. This is a system that allows you to store + * many streams within a single backing stream. This is great for databases, + * caching, etc. It's fairly lightweight, and allows all streams to grow + * dynamically using a block-allocation scheme. This is used extensively + * by the caching system and MyriadFs as well as other systems within + * libbu++. + */ class Myriad { public: typedef int32_t StreamId; + typedef Bu::Array StreamIdArray; + typedef Bu::List StreamIdList; enum Mode : int32_t { None = 0x00, @@ -55,8 +65,9 @@ namespace Bu public: /** - * Open existing Myriad stream, or initialize a new one if it doesn't - * exist. + * Open existing Myriad container, or initialize a new one if the + * backing stream is empty. If other data is already in the provided + * backing stream an error is thrown. * * Myriad format V0 * 0 - 3: Myriad_MAGIC_CODE (0ad3fa84) @@ -75,8 +86,32 @@ namespace Bu int32_t iPreallocateBlocks=-1 ); virtual ~Myriad(); + /** + * Creates a new stream open in the specified eMode and, optionally, + * preallocates the specificed amount of space. The stream is zero + * bytes even if space is preallocated. The open stream is returned, + * ready for use. Use this if you don't care what the id is of the + * newly created stream. + */ MyriadStream create( Mode eMode, int32_t iPreallocateBytes=-1 ); + + /** + * Open an existing stream or create a new stream with the specified + * id (iStream) with the specified eMode. This respects the normal file + * modes, see Bu::Myriad::Mode for details. + */ MyriadStream open( StreamId iStream, Mode eMode ); + + /** + * Allocate a new stream but do not open it, just ensure it exists and + * return the id of the newly allocated stream. + */ + StreamId allocate(); + + /** + * Erase the stream specified by iStream. This only can work when the + * stream is not open at the moment. + */ void erase( StreamId iStream ); void setSize( StreamId iStream, int32_t iNewSize ); int32_t getSize( StreamId iStream ) const; @@ -90,7 +125,15 @@ namespace Bu int32_t getTotalUsedBytes() const; int32_t getTotalUnusedBytes( int32_t iAssumeBlockSize=-1 ) const; Bu::BitString buildBlockUseMap() const; - Bu::Array buildBlockMap() const; + StreamIdArray buildBlockMap() const; + + /** + * Lists all stream ids that you are allowed to open. Technically there + * is always a zero stream, but it is used by Myriad for stream/block + * accounting. It works like a normal stream but you should not open + * it. + */ + StreamIdList getStreamList() const; /** * Flush all caches to the backing stream, write all structural and diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h index d6842a5..f71f9b5 100644 --- a/src/unstable/myriadcache.h +++ b/src/unstable/myriadcache.h @@ -86,8 +86,8 @@ namespace Bu { Bu::ReadWriteMutex::WriteLocker wl( rwStore ); { - Bu::MyriadStream ms = mStore.create( Bu::Myriad::Create ); - hIndex.insert( o->getKey(), ms.getId() ); + Bu::Myriad::StreamId id = mStore.allocate(); + hIndex.insert( o->getKey(), id ); } _save( o ); diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp index 2eda0be..a2386c2 100644 --- a/src/unstable/myriadfs.cpp +++ b/src/unstable/myriadfs.cpp @@ -8,6 +8,7 @@ #include "bu/config.h" #include "bu/myriadfs.h" #include "bu/myriadstream.h" +#include "bu/mutexlocker.h" #include #include @@ -572,27 +573,17 @@ int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) { case typeRegFile: case typeSymLink: - { - Bu::MyriadStream ms = mStore.create( - Bu::Myriad::Read - ); - rs.uStreamIndex = ms.getId(); - } + rs.uStreamIndex = mStore.allocate(); break; case typeDir: - { - Bu::MyriadStream ms = mStore.create( - Bu::Myriad::Read - ); - rs.uStreamIndex = ms.getId(); - } // sio << "Creating directory node, storage: " // << rs.uStreamIndex << sio.nl; { - Bu::MyriadStream msDir = mStore.open( - rs.uStreamIndex, Bu::Myriad::Write + Bu::MyriadStream msDir = mStore.create( + Bu::Myriad::Write ); + rs.uStreamIndex = msDir.getId(); uint32_t uSize = 0; msDir.write( &uSize, 4 ); } diff --git a/src/unstable/myriadfs.h b/src/unstable/myriadfs.h index eccac65..4e1749e 100644 --- a/src/unstable/myriadfs.h +++ b/src/unstable/myriadfs.h @@ -11,7 +11,7 @@ #include #include "bu/myriad.h" -#include "bu/readwritemutex.h" +#include "bu/mutex.h" namespace Bu { @@ -195,7 +195,7 @@ namespace Bu private: Bu::Stream &rStore; Bu::Myriad mStore; - Bu::ReadWriteMutex mNodeIndex; + Bu::Mutex mAccess; NodeIndex hNodeIndex; int32_t iUser; int32_t iGroup; -- cgit v1.2.3