diff options
-rw-r--r-- | src/stable/myriad.cpp | 30 | ||||
-rw-r--r-- | src/stable/myriad.h | 49 | ||||
-rw-r--r-- | src/unstable/myriadcache.h | 4 | ||||
-rw-r--r-- | src/unstable/myriadfs.cpp | 19 | ||||
-rw-r--r-- | 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, | |||
129 | return Bu::MyriadStream( *this, pStream, eMode ); | 129 | return Bu::MyriadStream( *this, pStream, eMode ); |
130 | } | 130 | } |
131 | 131 | ||
132 | Bu::Myriad::StreamId Bu::Myriad::allocate() | ||
133 | { | ||
134 | Bu::MutexLocker l( mAccess ); | ||
135 | |||
136 | Stream *pStream = new Stream( *this, ++iLastUsedIndex, 0 ); | ||
137 | mhStream.lock(); | ||
138 | hStream.insert( pStream->iStream, pStream ); | ||
139 | mhStream.unlock(); | ||
140 | bStructureChanged = true; | ||
141 | |||
142 | return pStream->iStream; | ||
143 | } | ||
144 | |||
132 | void Bu::Myriad::erase( Bu::Myriad::StreamId iStream ) | 145 | void Bu::Myriad::erase( Bu::Myriad::StreamId iStream ) |
133 | { | 146 | { |
134 | // For now, let's prevent you from erasing a stream if it's open. | 147 | // 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 | |||
252 | return iTotal; | 265 | return iTotal; |
253 | } | 266 | } |
254 | 267 | ||
268 | Bu::Myriad::StreamIdList Bu::Myriad::getStreamList() const | ||
269 | { | ||
270 | mhStream.lock(); | ||
271 | StreamIdList lIds = hStream.getKeys(); | ||
272 | mhStream.unlock(); | ||
273 | lIds.sort(); | ||
274 | if( lIds.first() == 0 ) | ||
275 | { | ||
276 | lIds.eraseFirst(); | ||
277 | } | ||
278 | return lIds; | ||
279 | } | ||
280 | |||
255 | Bu::BitString Bu::Myriad::buildBlockUseMap() const | 281 | Bu::BitString Bu::Myriad::buildBlockUseMap() const |
256 | { | 282 | { |
257 | Bu::MutexLocker l( mAccess ); | 283 | Bu::MutexLocker l( mAccess ); |
@@ -264,10 +290,10 @@ Bu::BitString Bu::Myriad::buildBlockUseMap() const | |||
264 | return bsMap; | 290 | return bsMap; |
265 | } | 291 | } |
266 | 292 | ||
267 | Bu::Array<int32_t> Bu::Myriad::buildBlockMap() const | 293 | Bu::Myriad::StreamIdArray Bu::Myriad::buildBlockMap() const |
268 | { | 294 | { |
269 | Bu::MutexLocker l( mAccess ); | 295 | Bu::MutexLocker l( mAccess ); |
270 | Bu::Array<int32_t> bm( iBlockCount ); | 296 | StreamIdArray bm( iBlockCount ); |
271 | for( int j = 0; j < iBlockCount; j++ ) | 297 | for( int j = 0; j < iBlockCount; j++ ) |
272 | { | 298 | { |
273 | bm.append( -1 ); | 299 | 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 | |||
31 | }; | 31 | }; |
32 | subExceptionDeclEnd(); | 32 | subExceptionDeclEnd(); |
33 | 33 | ||
34 | /** | ||
35 | * Myriad Stream Multiplexer. This is a system that allows you to store | ||
36 | * many streams within a single backing stream. This is great for databases, | ||
37 | * caching, etc. It's fairly lightweight, and allows all streams to grow | ||
38 | * dynamically using a block-allocation scheme. This is used extensively | ||
39 | * by the caching system and MyriadFs as well as other systems within | ||
40 | * libbu++. | ||
41 | */ | ||
34 | class Myriad | 42 | class Myriad |
35 | { | 43 | { |
36 | public: | 44 | public: |
37 | typedef int32_t StreamId; | 45 | typedef int32_t StreamId; |
46 | typedef Bu::Array<StreamId> StreamIdArray; | ||
47 | typedef Bu::List<StreamId> StreamIdList; | ||
38 | enum Mode : int32_t { | 48 | enum Mode : int32_t { |
39 | None = 0x00, | 49 | None = 0x00, |
40 | 50 | ||
@@ -55,8 +65,9 @@ namespace Bu | |||
55 | 65 | ||
56 | public: | 66 | public: |
57 | /** | 67 | /** |
58 | * Open existing Myriad stream, or initialize a new one if it doesn't | 68 | * Open existing Myriad container, or initialize a new one if the |
59 | * exist. | 69 | * backing stream is empty. If other data is already in the provided |
70 | * backing stream an error is thrown. | ||
60 | * | 71 | * |
61 | * Myriad format V0 | 72 | * Myriad format V0 |
62 | * 0 - 3: Myriad_MAGIC_CODE (0ad3fa84) | 73 | * 0 - 3: Myriad_MAGIC_CODE (0ad3fa84) |
@@ -75,8 +86,32 @@ namespace Bu | |||
75 | int32_t iPreallocateBlocks=-1 ); | 86 | int32_t iPreallocateBlocks=-1 ); |
76 | virtual ~Myriad(); | 87 | virtual ~Myriad(); |
77 | 88 | ||
89 | /** | ||
90 | * Creates a new stream open in the specified eMode and, optionally, | ||
91 | * preallocates the specificed amount of space. The stream is zero | ||
92 | * bytes even if space is preallocated. The open stream is returned, | ||
93 | * ready for use. Use this if you don't care what the id is of the | ||
94 | * newly created stream. | ||
95 | */ | ||
78 | MyriadStream create( Mode eMode, int32_t iPreallocateBytes=-1 ); | 96 | MyriadStream create( Mode eMode, int32_t iPreallocateBytes=-1 ); |
97 | |||
98 | /** | ||
99 | * Open an existing stream or create a new stream with the specified | ||
100 | * id (iStream) with the specified eMode. This respects the normal file | ||
101 | * modes, see Bu::Myriad::Mode for details. | ||
102 | */ | ||
79 | MyriadStream open( StreamId iStream, Mode eMode ); | 103 | MyriadStream open( StreamId iStream, Mode eMode ); |
104 | |||
105 | /** | ||
106 | * Allocate a new stream but do not open it, just ensure it exists and | ||
107 | * return the id of the newly allocated stream. | ||
108 | */ | ||
109 | StreamId allocate(); | ||
110 | |||
111 | /** | ||
112 | * Erase the stream specified by iStream. This only can work when the | ||
113 | * stream is not open at the moment. | ||
114 | */ | ||
80 | void erase( StreamId iStream ); | 115 | void erase( StreamId iStream ); |
81 | void setSize( StreamId iStream, int32_t iNewSize ); | 116 | void setSize( StreamId iStream, int32_t iNewSize ); |
82 | int32_t getSize( StreamId iStream ) const; | 117 | int32_t getSize( StreamId iStream ) const; |
@@ -90,7 +125,15 @@ namespace Bu | |||
90 | int32_t getTotalUsedBytes() const; | 125 | int32_t getTotalUsedBytes() const; |
91 | int32_t getTotalUnusedBytes( int32_t iAssumeBlockSize=-1 ) const; | 126 | int32_t getTotalUnusedBytes( int32_t iAssumeBlockSize=-1 ) const; |
92 | Bu::BitString buildBlockUseMap() const; | 127 | Bu::BitString buildBlockUseMap() const; |
93 | Bu::Array<int32_t> buildBlockMap() const; | 128 | StreamIdArray buildBlockMap() const; |
129 | |||
130 | /** | ||
131 | * Lists all stream ids that you are allowed to open. Technically there | ||
132 | * is always a zero stream, but it is used by Myriad for stream/block | ||
133 | * accounting. It works like a normal stream but you should not open | ||
134 | * it. | ||
135 | */ | ||
136 | StreamIdList getStreamList() const; | ||
94 | 137 | ||
95 | /** | 138 | /** |
96 | * Flush all caches to the backing stream, write all structural and | 139 | * 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 | |||
86 | { | 86 | { |
87 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | 87 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); |
88 | { | 88 | { |
89 | Bu::MyriadStream ms = mStore.create( Bu::Myriad::Create ); | 89 | Bu::Myriad::StreamId id = mStore.allocate(); |
90 | hIndex.insert( o->getKey(), ms.getId() ); | 90 | hIndex.insert( o->getKey(), id ); |
91 | } | 91 | } |
92 | _save( o ); | 92 | _save( o ); |
93 | 93 | ||
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 @@ | |||
8 | #include "bu/config.h" | 8 | #include "bu/config.h" |
9 | #include "bu/myriadfs.h" | 9 | #include "bu/myriadfs.h" |
10 | #include "bu/myriadstream.h" | 10 | #include "bu/myriadstream.h" |
11 | #include "bu/mutexlocker.h" | ||
11 | 12 | ||
12 | #include <string.h> | 13 | #include <string.h> |
13 | #include <unistd.h> | 14 | #include <unistd.h> |
@@ -572,27 +573,17 @@ int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) | |||
572 | { | 573 | { |
573 | case typeRegFile: | 574 | case typeRegFile: |
574 | case typeSymLink: | 575 | case typeSymLink: |
575 | { | 576 | rs.uStreamIndex = mStore.allocate(); |
576 | Bu::MyriadStream ms = mStore.create( | ||
577 | Bu::Myriad::Read | ||
578 | ); | ||
579 | rs.uStreamIndex = ms.getId(); | ||
580 | } | ||
581 | break; | 577 | break; |
582 | 578 | ||
583 | case typeDir: | 579 | case typeDir: |
584 | { | ||
585 | Bu::MyriadStream ms = mStore.create( | ||
586 | Bu::Myriad::Read | ||
587 | ); | ||
588 | rs.uStreamIndex = ms.getId(); | ||
589 | } | ||
590 | // sio << "Creating directory node, storage: " | 580 | // sio << "Creating directory node, storage: " |
591 | // << rs.uStreamIndex << sio.nl; | 581 | // << rs.uStreamIndex << sio.nl; |
592 | { | 582 | { |
593 | Bu::MyriadStream msDir = mStore.open( | 583 | Bu::MyriadStream msDir = mStore.create( |
594 | rs.uStreamIndex, Bu::Myriad::Write | 584 | Bu::Myriad::Write |
595 | ); | 585 | ); |
586 | rs.uStreamIndex = msDir.getId(); | ||
596 | uint32_t uSize = 0; | 587 | uint32_t uSize = 0; |
597 | msDir.write( &uSize, 4 ); | 588 | msDir.write( &uSize, 4 ); |
598 | } | 589 | } |
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 @@ | |||
11 | #include <sys/types.h> | 11 | #include <sys/types.h> |
12 | 12 | ||
13 | #include "bu/myriad.h" | 13 | #include "bu/myriad.h" |
14 | #include "bu/readwritemutex.h" | 14 | #include "bu/mutex.h" |
15 | 15 | ||
16 | namespace Bu | 16 | namespace Bu |
17 | { | 17 | { |
@@ -195,7 +195,7 @@ namespace Bu | |||
195 | private: | 195 | private: |
196 | Bu::Stream &rStore; | 196 | Bu::Stream &rStore; |
197 | Bu::Myriad mStore; | 197 | Bu::Myriad mStore; |
198 | Bu::ReadWriteMutex mNodeIndex; | 198 | Bu::Mutex mAccess; |
199 | NodeIndex hNodeIndex; | 199 | NodeIndex hNodeIndex; |
200 | int32_t iUser; | 200 | int32_t iUser; |
201 | int32_t iGroup; | 201 | int32_t iGroup; |