diff options
Diffstat (limited to '')
-rw-r--r-- | src/myriad.cpp | 69 |
1 files changed, 49 insertions, 20 deletions
diff --git a/src/myriad.cpp b/src/myriad.cpp index 58270c5..77b481d 100644 --- a/src/myriad.cpp +++ b/src/myriad.cpp | |||
@@ -34,7 +34,12 @@ Bu::Myriad::Myriad( Bu::Stream &sStore ) : | |||
34 | 34 | ||
35 | Bu::Myriad::~Myriad() | 35 | Bu::Myriad::~Myriad() |
36 | { | 36 | { |
37 | updateHeader(); | 37 | if( !hActiveBlocks.isEmpty() ) |
38 | { | ||
39 | sio << "Bu::Myriad::~Myriad(): Error: There are " | ||
40 | << hActiveBlocks.getSize() << " unsynced blocks!" << sio.nl; | ||
41 | } | ||
42 | sync(); | ||
38 | 43 | ||
39 | for( StreamArray::iterator i = aStreams.begin(); i; i++ ) | 44 | for( StreamArray::iterator i = aStreams.begin(); i; i++ ) |
40 | { | 45 | { |
@@ -46,7 +51,13 @@ void Bu::Myriad::sync() | |||
46 | { | 51 | { |
47 | updateHeader(); | 52 | updateHeader(); |
48 | 53 | ||
49 | // Later, also flush all caches. | 54 | for( BlockHash::iterator i = hActiveBlocks.begin(); i; i++ ) |
55 | { | ||
56 | if( (*i)->bChanged ) | ||
57 | { | ||
58 | syncBlock( *i ); | ||
59 | } | ||
60 | } | ||
50 | } | 61 | } |
51 | 62 | ||
52 | void Bu::Myriad::initialize() | 63 | void Bu::Myriad::initialize() |
@@ -206,16 +217,19 @@ void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate ) | |||
206 | this->iBlocks = iPreAllocate; | 217 | this->iBlocks = iPreAllocate; |
207 | 218 | ||
208 | pStr->iSize = sStore.tell(); | 219 | pStr->iSize = sStore.tell(); |
209 | sio << "Myriad: Actual end of header stream = " << pStr->iSize << sio.nl; | 220 | // sio << "Myriad: Actual end of header stream = " << pStr->iSize << sio.nl; |
210 | 221 | ||
211 | pStr->iSize = iHeaderSize; | 222 | pStr->iSize = iHeaderSize; |
212 | for( int j = 0; j < iHeaderBlocks; j++ ) | 223 | for( int j = 0; j < iHeaderBlocks; j++ ) |
213 | { | 224 | { |
214 | pStr->aBlocks.append( j ); | 225 | pStr->aBlocks.append( j ); |
226 | bsBlockUsed.setBit( j ); | ||
215 | } | 227 | } |
216 | 228 | ||
217 | aStreams.append( pStr ); | 229 | aStreams.append( pStr ); |
218 | 230 | ||
231 | sio << bsBlockUsed.toString() << sio.nl; | ||
232 | |||
219 | //hStreams.insert( 0, BlockArray( 0 ) ); | 233 | //hStreams.insert( 0, BlockArray( 0 ) ); |
220 | } | 234 | } |
221 | 235 | ||
@@ -227,31 +241,36 @@ void Bu::Myriad::updateHeader() | |||
227 | char cBuf; | 241 | char cBuf; |
228 | int iBuf; | 242 | int iBuf; |
229 | 243 | ||
244 | for( StreamArray::iterator i = aStreams.begin(); i; i++ ) | ||
245 | { | ||
246 | sio << "Myriad: Stream " << Fmt(4) << (*i)->iId << ": " << (*i)->aBlocks << sio.nl; | ||
247 | } | ||
248 | |||
230 | // Compute the new size of the header. | 249 | // Compute the new size of the header. |
231 | int iHeaderSize = 14 + 8*aStreams.getSize(); | 250 | int iHeaderSize = 14 + 8*aStreams.getSize(); |
232 | sio << "Myriad: updateHeader: aStreams.getSize() = " << aStreams.getSize() | 251 | // sio << "Myriad: updateHeader: aStreams.getSize() = " << aStreams.getSize() |
233 | << sio.nl; | 252 | // << sio.nl; |
234 | for( StreamArray::iterator i = aStreams.begin(); i; i++ ) | 253 | for( StreamArray::iterator i = aStreams.begin(); i; i++ ) |
235 | { | 254 | { |
236 | iHeaderSize += 4*(*i)->aBlocks.getSize(); | 255 | iHeaderSize += 4*(*i)->aBlocks.getSize(); |
237 | sio << "Myriad: updateHeader: (*i)->aBlocks.getSize() = " | 256 | // sio << "Myriad: updateHeader: (*i)->aBlocks.getSize() = " |
238 | << (*i)->aBlocks.getSize() << sio.nl; | 257 | // << (*i)->aBlocks.getSize() << sio.nl; |
239 | } | 258 | } |
240 | int iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); | 259 | int iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); |
241 | while( iNewBlocks > aStreams[0]->aBlocks.getSize() ) | 260 | while( iNewBlocks > aStreams[0]->aBlocks.getSize() ) |
242 | { | 261 | { |
243 | int iBlock = findEmptyBlock(); | 262 | int iBlock = findEmptyBlock(); |
244 | sio << "Myriad: updateHeader: Appending block " << iBlock | 263 | // sio << "Myriad: updateHeader: Appending block " << iBlock |
245 | << " to header." << sio.nl; | 264 | // << " to header." << sio.nl; |
246 | aStreams[0]->aBlocks.append( iBlock ); | 265 | aStreams[0]->aBlocks.append( iBlock ); |
247 | bsBlockUsed.setBit( iBlock ); | 266 | bsBlockUsed.setBit( iBlock ); |
248 | iHeaderSize += 4; | 267 | iHeaderSize += 4; |
249 | iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); | 268 | iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); |
250 | } | 269 | } |
251 | aStreams[0]->iSize = iHeaderSize; | 270 | aStreams[0]->iSize = iHeaderSize; |
252 | sio << "Myriad: updateHeader: iHeaderSize=" << iHeaderSize | 271 | // sio << "Myriad: updateHeader: iHeaderSize=" << iHeaderSize |
253 | << ", iNewBlocks=" << iNewBlocks << ", curBlocks=" | 272 | // << ", iNewBlocks=" << iNewBlocks << ", curBlocks=" |
254 | << aStreams[0]->aBlocks.getSize() << sio.nl; | 273 | // << aStreams[0]->aBlocks.getSize() << sio.nl; |
255 | 274 | ||
256 | MyriadStream sHdr( *this, aStreams[0] ); | 275 | MyriadStream sHdr( *this, aStreams[0] ); |
257 | sHdr.write( Myriad_MAGIC_CODE, 4 ); | 276 | sHdr.write( Myriad_MAGIC_CODE, 4 ); |
@@ -296,7 +315,7 @@ int Bu::Myriad::createStream( int iPreAllocate ) | |||
296 | for( int j = 0; j < iPreAllocate; j++ ) | 315 | for( int j = 0; j < iPreAllocate; j++ ) |
297 | { | 316 | { |
298 | int iFreeBlock = findEmptyBlock(); | 317 | int iFreeBlock = findEmptyBlock(); |
299 | sio << "Myriad: Adding block " << iFreeBlock << sio.nl; | 318 | // sio << "Myriad: Adding block " << iFreeBlock << sio.nl; |
300 | pStr->aBlocks.append( iFreeBlock ); | 319 | pStr->aBlocks.append( iFreeBlock ); |
301 | bsBlockUsed.setBit( iFreeBlock ); | 320 | bsBlockUsed.setBit( iFreeBlock ); |
302 | } | 321 | } |
@@ -311,7 +330,7 @@ int Bu::Myriad::findEmptyBlock() | |||
311 | if( bsBlockUsed.getBit( j ) == false ) | 330 | if( bsBlockUsed.getBit( j ) == false ) |
312 | return j; | 331 | return j; |
313 | } | 332 | } |
314 | sio << "Myriad: findEmptyBlock(): No empty blocks, adding new one." << sio.nl; | 333 | // sio << "Myriad: findEmptyBlock(): No empty blocks, adding new one." << sio.nl; |
315 | 334 | ||
316 | bsBlockUsed.setSize( bsBlockUsed.getSize()+1, false ); | 335 | bsBlockUsed.setSize( bsBlockUsed.getSize()+1, false ); |
317 | sStore.setPos( iBlockSize*iBlocks ); | 336 | sStore.setPos( iBlockSize*iBlocks ); |
@@ -330,6 +349,7 @@ void Bu::Myriad::deleteStream( int /*iID*/ ) | |||
330 | 349 | ||
331 | Bu::MyriadStream Bu::Myriad::openStream( int iId ) | 350 | Bu::MyriadStream Bu::Myriad::openStream( int iId ) |
332 | { | 351 | { |
352 | sio << "Myriad: Request to open stream: " << iId << sio.nl; | ||
333 | return MyriadStream( *this, findStream( iId ) ); | 353 | return MyriadStream( *this, findStream( iId ) ); |
334 | } | 354 | } |
335 | 355 | ||
@@ -361,8 +381,8 @@ Bu::Myriad::Stream *Bu::Myriad::findStream( int iId ) | |||
361 | 381 | ||
362 | Bu::Myriad::Block *Bu::Myriad::getBlock( int iBlock ) | 382 | Bu::Myriad::Block *Bu::Myriad::getBlock( int iBlock ) |
363 | { | 383 | { |
364 | sio << "Myriad: Reading block " << iBlock << ", bytes " | 384 | // sio << "Myriad: Reading block " << iBlock << ", bytes " |
365 | << iBlockSize*iBlock << "-" << iBlockSize*(iBlock+1) << sio.nl; | 385 | // << iBlockSize*iBlock << "-" << iBlockSize*(iBlock+1) << sio.nl; |
366 | Block *pBlock = new Block; | 386 | Block *pBlock = new Block; |
367 | pBlock->pData = new char[iBlockSize]; | 387 | pBlock->pData = new char[iBlockSize]; |
368 | sStore.setPos( iBlockSize * iBlock ); | 388 | sStore.setPos( iBlockSize * iBlock ); |
@@ -370,6 +390,8 @@ Bu::Myriad::Block *Bu::Myriad::getBlock( int iBlock ) | |||
370 | pBlock->bChanged = false; | 390 | pBlock->bChanged = false; |
371 | pBlock->iBlockIndex = iBlock; | 391 | pBlock->iBlockIndex = iBlock; |
372 | 392 | ||
393 | hActiveBlocks.insert( iBlock, pBlock ); | ||
394 | |||
373 | return pBlock; | 395 | return pBlock; |
374 | } | 396 | } |
375 | 397 | ||
@@ -377,14 +399,21 @@ void Bu::Myriad::releaseBlock( Bu::Myriad::Block *pBlock ) | |||
377 | { | 399 | { |
378 | if( pBlock == NULL ) | 400 | if( pBlock == NULL ) |
379 | return; | 401 | return; |
380 | sio << "Myriad: Releasing block " << pBlock->iBlockIndex << sio.nl; | 402 | // sio << "Myriad: Releasing block " << pBlock->iBlockIndex << sio.nl; |
403 | syncBlock( pBlock ); | ||
404 | hActiveBlocks.erase( pBlock->iBlockIndex ); | ||
405 | delete[] pBlock->pData; | ||
406 | delete pBlock; | ||
407 | } | ||
408 | |||
409 | void Bu::Myriad::syncBlock( Block *pBlock ) | ||
410 | { | ||
381 | if( pBlock->bChanged ) | 411 | if( pBlock->bChanged ) |
382 | { | 412 | { |
383 | sio << "Myriad: - Block changed, writing back to stream." << sio.nl; | 413 | // sio << "Myriad: - Block changed, writing back to stream." << sio.nl; |
384 | sStore.setPos( iBlockSize * pBlock->iBlockIndex ); | 414 | sStore.setPos( iBlockSize * pBlock->iBlockIndex ); |
385 | sStore.write( pBlock->pData, iBlockSize ); | 415 | sStore.write( pBlock->pData, iBlockSize ); |
416 | pBlock->bChanged = false; | ||
386 | } | 417 | } |
387 | delete[] pBlock->pData; | ||
388 | delete pBlock; | ||
389 | } | 418 | } |
390 | 419 | ||