summaryrefslogtreecommitdiff
path: root/src/myriad.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/myriad.cpp')
-rw-r--r--src/myriad.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/myriad.cpp b/src/myriad.cpp
index 2f5c14f..9d7a849 100644
--- a/src/myriad.cpp
+++ b/src/myriad.cpp
@@ -151,6 +151,7 @@ void Bu::Myriad::initialize()
151 // << ", pIn->tell()=" << pIn->tell() << sio.nl; 151 // << ", pIn->tell()=" << pIn->tell() << sio.nl;
152 s.aBlocks.append( iBId ); 152 s.aBlocks.append( iBId );
153 bsBlockUsed.setBit( iBId ); 153 bsBlockUsed.setBit( iBId );
154 iUsed++;
154 if( (j == 0 && k == iHeaderBlocks-1) ) 155 if( (j == 0 && k == iHeaderBlocks-1) )
155 { 156 {
156 // sio << "Myriad: - End of prepartition, unlocking skipping." 157 // sio << "Myriad: - End of prepartition, unlocking skipping."
@@ -172,6 +173,13 @@ void Bu::Myriad::initialize()
172 173
173void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate ) 174void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate )
174{ 175{
176 for( StreamArray::iterator i = aStreams.begin(); i; i++ )
177 {
178 delete *i;
179 }
180 aStreams.clear();
181 iUsed = 0;
182
175 int iHeaderSize = 14 + 8 + 4; 183 int iHeaderSize = 14 + 8 + 4;
176 int iHeaderBlocks = 0; //blkDiv( iHeaderSize+4, iBlockSize ); 184 int iHeaderBlocks = 0; //blkDiv( iHeaderSize+4, iBlockSize );
177 char cBuf = 1; 185 char cBuf = 1;
@@ -192,6 +200,7 @@ void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate )
192 // << iBlockSize << ", iHeaderBlocks=" << iHeaderBlocks << sio.nl; 200 // << iBlockSize << ", iHeaderBlocks=" << iHeaderBlocks << sio.nl;
193 201
194 bsBlockUsed.setSize( iPreAllocate, true ); 202 bsBlockUsed.setSize( iPreAllocate, true );
203 iUsed++;
195 204
196 char *pBlock = new char[iBlockSize]; 205 char *pBlock = new char[iBlockSize];
197 memset( pBlock, 0, iBlockSize ); 206 memset( pBlock, 0, iBlockSize );
@@ -240,6 +249,7 @@ void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate )
240 { 249 {
241 pStr->aBlocks.append( j ); 250 pStr->aBlocks.append( j );
242 bsBlockUsed.setBit( j ); 251 bsBlockUsed.setBit( j );
252 iUsed++;
243 } 253 }
244 254
245 aStreams.append( pStr ); 255 aStreams.append( pStr );
@@ -283,6 +293,7 @@ void Bu::Myriad::updateHeader()
283// << " to header." << sio.nl; 293// << " to header." << sio.nl;
284 aStreams[0]->aBlocks.append( iBlock ); 294 aStreams[0]->aBlocks.append( iBlock );
285 bsBlockUsed.setBit( iBlock ); 295 bsBlockUsed.setBit( iBlock );
296 iUsed++;
286 iHeaderSize += 4; 297 iHeaderSize += 4;
287 iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); 298 iNewBlocks = blkDiv( iHeaderSize, iBlockSize );
288 } 299 }
@@ -339,6 +350,7 @@ int Bu::Myriad::createStream( int iPreAllocate )
339// sio << "Myriad: Adding block " << iFreeBlock << sio.nl; 350// sio << "Myriad: Adding block " << iFreeBlock << sio.nl;
340 pStr->aBlocks.append( iFreeBlock ); 351 pStr->aBlocks.append( iFreeBlock );
341 bsBlockUsed.setBit( iFreeBlock ); 352 bsBlockUsed.setBit( iFreeBlock );
353 iUsed++;
342 } 354 }
343 355
344 bHeaderChanged = true; 356 bHeaderChanged = true;
@@ -346,6 +358,52 @@ int Bu::Myriad::createStream( int iPreAllocate )
346 return pStr->iId; 358 return pStr->iId;
347} 359}
348 360
361int Bu::Myriad::createStreamWithId( int iId, int iPreAllocate )
362{
363 try
364 {
365 findStream( iId );
366 throw MyriadException( MyriadException::streamExists,
367 "There is already a stream with the given id.");
368 }
369 catch( MyriadException &e )
370 {
371 Stream *pStr = new Stream();
372 pStr->iId = iId;
373 //sio << "Myriad: New stream id=" << pStr->iId << ", iPreAllocate="
374 // << iPreAllocate << sio.nl;
375 pStr->iSize = 0;
376 if( aStreams.last()->iId < iId )
377 {
378 aStreams.append( pStr );
379 }
380 else
381 {
382 for( StreamArray::iterator i = aStreams.begin(); i; i++ )
383 {
384 if( (*i)->iId > iId )
385 {
386 aStreams.insert( i, pStr );
387 break;
388 }
389 }
390 }
391
392 for( int j = 0; j < iPreAllocate; j++ )
393 {
394 int iFreeBlock = findEmptyBlock();
395 // sio << "Myriad: Adding block " << iFreeBlock << sio.nl;
396 pStr->aBlocks.append( iFreeBlock );
397 bsBlockUsed.setBit( iFreeBlock );
398 iUsed++;
399 }
400
401 bHeaderChanged = true;
402
403 return pStr->iId;
404 }
405}
406
349int Bu::Myriad::findEmptyBlock() 407int Bu::Myriad::findEmptyBlock()
350{ 408{
351 bHeaderChanged = true; 409 bHeaderChanged = true;
@@ -374,6 +432,12 @@ int Bu::Myriad::findEmptyBlock()
374 432
375void Bu::Myriad::deleteStream( int iId ) 433void Bu::Myriad::deleteStream( int iId )
376{ 434{
435 if( iId < 0 )
436 throw MyriadException( MyriadException::invalidStreamId,
437 "Invalid stream id.");
438 if( iId == 0 )
439 throw MyriadException( MyriadException::protectedStream,
440 "You cannot delete stream zero, it is protected.");
377 for( StreamArray::iterator i = aStreams.begin(); i; i++ ) 441 for( StreamArray::iterator i = aStreams.begin(); i; i++ )
378 { 442 {
379 if( (*i)->iId == iId ) 443 if( (*i)->iId == iId )
@@ -382,6 +446,7 @@ void Bu::Myriad::deleteStream( int iId )
382 for( BlockArray::iterator j = pStream->aBlocks.begin(); j; j++ ) 446 for( BlockArray::iterator j = pStream->aBlocks.begin(); j; j++ )
383 { 447 {
384 bsBlockUsed.setBit( *j, false ); 448 bsBlockUsed.setBit( *j, false );
449 iUsed--;
385 } 450 }
386 aStreams.erase( i ); 451 aStreams.erase( i );
387 bHeaderChanged = true; 452 bHeaderChanged = true;
@@ -391,12 +456,33 @@ void Bu::Myriad::deleteStream( int iId )
391 } 456 }
392} 457}
393 458
459Bu::Array<int> Bu::Myriad::getStreamIds()
460{
461 Bu::Array<int> aRet( aStreams.getSize() );
462 for( StreamArray::iterator i = aStreams.begin(); i; i++ )
463 {
464 aRet.append( (*i)->iId );
465 }
466
467 return aRet;
468}
469
470int Bu::Myriad::getStreamSize( int iId )
471{
472 return findStream( iId )->iSize;
473}
474
394Bu::MyriadStream Bu::Myriad::openStream( int iId ) 475Bu::MyriadStream Bu::Myriad::openStream( int iId )
395{ 476{
396 //sio << "Myriad: Request to open stream: " << iId << sio.nl; 477 //sio << "Myriad: Request to open stream: " << iId << sio.nl;
397 return MyriadStream( *this, findStream( iId ) ); 478 return MyriadStream( *this, findStream( iId ) );
398} 479}
399 480
481int Bu::Myriad::getNumStreams()
482{
483 return aStreams.getSize();
484}
485
400int Bu::Myriad::getBlockSize() 486int Bu::Myriad::getBlockSize()
401{ 487{
402 return iBlockSize; 488 return iBlockSize;
@@ -469,6 +555,7 @@ int Bu::Myriad::streamAddBlock( Stream *pStream )
469 int iBlock = findEmptyBlock(); 555 int iBlock = findEmptyBlock();
470 pStream->aBlocks.append( iBlock ); 556 pStream->aBlocks.append( iBlock );
471 bsBlockUsed.setBit( iBlock ); 557 bsBlockUsed.setBit( iBlock );
558 iUsed++;
472 return iBlock; 559 return iBlock;
473} 560}
474 561
@@ -484,6 +571,8 @@ void Bu::Myriad::setStreamSize( Stream *pStream, long iSize )
484 for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize; 571 for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize;
485 iNewSize-64 > iSize; iNewSize -= iBlockSize ) 572 iNewSize-64 > iSize; iNewSize -= iBlockSize )
486 { 573 {
574 if( bsBlockUsed.getBit( pStream->aBlocks.last() ) )
575 iUsed--;
487 bsBlockUsed.setBit( pStream->aBlocks.last(), false ); 576 bsBlockUsed.setBit( pStream->aBlocks.last(), false );
488 pStream->aBlocks.eraseLast(); 577 pStream->aBlocks.eraseLast();
489 } 578 }