diff options
Diffstat (limited to 'src/stable')
-rw-r--r-- | src/stable/mutex.h | 8 | ||||
-rw-r--r-- | src/stable/myriad.cpp | 34 | ||||
-rw-r--r-- | src/stable/myriad.h | 49 |
3 files changed, 79 insertions, 12 deletions
diff --git a/src/stable/mutex.h b/src/stable/mutex.h index d9e8910..8034974 100644 --- a/src/stable/mutex.h +++ b/src/stable/mutex.h | |||
@@ -33,7 +33,7 @@ namespace Bu | |||
33 | * wait for the mutex to unlock, the odds of which are usually farily | 33 | * wait for the mutex to unlock, the odds of which are usually farily |
34 | * low at deconstruction time. | 34 | * low at deconstruction time. |
35 | */ | 35 | */ |
36 | ~Mutex(); | 36 | virtual ~Mutex(); |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * Lock the mutex. This causes all future calls to lock on this | 39 | * Lock the mutex. This causes all future calls to lock on this |
@@ -44,13 +44,13 @@ namespace Bu | |||
44 | * computation within a locked section. This can cause thread-deadlock | 44 | * computation within a locked section. This can cause thread-deadlock |
45 | * and your program may hang. | 45 | * and your program may hang. |
46 | */ | 46 | */ |
47 | int lock(); | 47 | virtual int lock(); |
48 | 48 | ||
49 | /** | 49 | /** |
50 | * Unlock the mutex. This allows the next thread that asked for a lock | 50 | * Unlock the mutex. This allows the next thread that asked for a lock |
51 | * to lock the mutex and continue with execution. | 51 | * to lock the mutex and continue with execution. |
52 | */ | 52 | */ |
53 | int unlock(); | 53 | virtual int unlock(); |
54 | 54 | ||
55 | /** | 55 | /** |
56 | * Try to lock the mutex. This is the option to go with if you cannot | 56 | * Try to lock the mutex. This is the option to go with if you cannot |
@@ -58,7 +58,7 @@ namespace Bu | |||
58 | * will attempt to lock the mutex, if the mutex is already locked this | 58 | * will attempt to lock the mutex, if the mutex is already locked this |
59 | * function returns immediately with an error code. | 59 | * function returns immediately with an error code. |
60 | */ | 60 | */ |
61 | int trylock(); | 61 | virtual int trylock(); |
62 | 62 | ||
63 | protected: | 63 | protected: |
64 | pthread_mutex_t mutex; /**< The internal mutex reference. */ | 64 | pthread_mutex_t mutex; /**< The internal mutex reference. */ |
diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index 03cffa9..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 ); |
@@ -768,9 +794,7 @@ int32_t Bu::Myriad::Stream::read( int32_t iStart, void *pTarget, | |||
768 | 794 | ||
769 | if( iStart+iSize >= this->iSize ) | 795 | if( iStart+iSize >= this->iSize ) |
770 | { | 796 | { |
771 | int32_t iDiff = (iStart+iSize)-this->iSize; | 797 | iSize = this->iSize-iStart; |
772 | iSize -= iDiff; | ||
773 | iStart += iDiff; | ||
774 | } | 798 | } |
775 | 799 | ||
776 | while( iSize > 0 ) | 800 | while( iSize > 0 ) |
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 |