aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/array.h12
-rw-r--r--src/myriad.cpp53
-rw-r--r--src/myriad.h1
-rw-r--r--src/myriadstream.cpp6
-rw-r--r--src/unit/myriad.unit64
5 files changed, 126 insertions, 10 deletions
diff --git a/src/array.h b/src/array.h
index 604f8a6..39efb9e 100644
--- a/src/array.h
+++ b/src/array.h
@@ -488,6 +488,18 @@ namespace Bu
488 core->erase( i.iPos ); 488 core->erase( i.iPos );
489 } 489 }
490 490
491 void eraseLast()
492 {
493 _hardCopy();
494 core->erase( core->iSize-1 );
495 }
496
497 void eraseFirst()
498 {
499 _hardCopy();
500 core->erase( 0 );
501 }
502
491 /** 503 /**
492 * In order to make swapErase faster, what it does is swap the given 504 * In order to make swapErase faster, what it does is swap the given
493 * item in the array with the last item, then make the array shorter 505 * item in the array with the last item, then make the array shorter
diff --git a/src/myriad.cpp b/src/myriad.cpp
index 044e35e..9e128d1 100644
--- a/src/myriad.cpp
+++ b/src/myriad.cpp
@@ -352,8 +352,22 @@ int Bu::Myriad::findEmptyBlock()
352 return iBlocks++; 352 return iBlocks++;
353} 353}
354 354
355void Bu::Myriad::deleteStream( int /*iID*/ ) 355void Bu::Myriad::deleteStream( int iId )
356{ 356{
357 for( StreamArray::iterator i = aStreams.begin(); i; i++ )
358 {
359 if( (*i)->iId == iId )
360 {
361 Stream *pStream = *i;
362 for( BlockArray::iterator j = pStream->aBlocks.begin(); j; j++ )
363 {
364 bsBlockUsed.setBit( *j, false );
365 }
366 aStreams.erase( i );
367 bHeaderChanged = true;
368 return;
369 }
370 }
357} 371}
358 372
359Bu::MyriadStream Bu::Myriad::openStream( int iId ) 373Bu::MyriadStream Bu::Myriad::openStream( int iId )
@@ -426,9 +440,42 @@ void Bu::Myriad::syncBlock( Block *pBlock )
426 } 440 }
427} 441}
428 442
443int Bu::Myriad::streamAddBlock( Stream *pStream )
444{
445 int iBlock = findEmptyBlock();
446 pStream->aBlocks.append( iBlock );
447 bsBlockUsed.setBit( iBlock );
448 return iBlock;
449}
450
429void Bu::Myriad::setStreamSize( Stream *pStream, long iSize ) 451void Bu::Myriad::setStreamSize( Stream *pStream, long iSize )
430{ 452{
431 sio << "Oh man, you have to implement Bu::Myriad::setStreamSize!!! (line " 453 if( pStream->iSize == iSize )
432 << __LINE__ << ")" << sio.nl; 454 {
455 return;
456 }
457 else if( pStream->iSize > iSize )
458 {
459 // Shrink
460 for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize;
461 iNewSize-64 > iSize; iNewSize -= iBlockSize )
462 {
463 bsBlockUsed.setBit( pStream->aBlocks.last(), false );
464 pStream->aBlocks.eraseLast();
465 }
466 pStream->iSize = iSize;
467 bHeaderChanged = true;
468 }
469 else
470 {
471 // Grow
472 for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize;
473 iNewSize < iSize; iNewSize += iBlockSize )
474 {
475 streamAddBlock( pStream );
476 }
477 pStream->iSize = iSize;
478 bHeaderChanged = true;
479 }
433} 480}
434 481
diff --git a/src/myriad.h b/src/myriad.h
index 8e7cfb1..900037b 100644
--- a/src/myriad.h
+++ b/src/myriad.h
@@ -150,6 +150,7 @@ namespace Bu
150 void releaseBlock( Block *pBlock ); 150 void releaseBlock( Block *pBlock );
151 void syncBlock( Block *pBlock ); 151 void syncBlock( Block *pBlock );
152 152
153 int streamAddBlock( Stream *pStream );
153 void setStreamSize( Stream *pStream, long iSize ); 154 void setStreamSize( Stream *pStream, long iSize );
154 155
155 private: 156 private:
diff --git a/src/myriadstream.cpp b/src/myriadstream.cpp
index 41c5b53..b0b1d05 100644
--- a/src/myriadstream.cpp
+++ b/src/myriadstream.cpp
@@ -139,9 +139,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
139 } 139 }
140 else 140 else
141 { 141 {
142 iCurBlock = rMyriad.findEmptyBlock(); 142 iCurBlock = rMyriad.streamAddBlock( pStream );
143 pStream->aBlocks.append( iCurBlock );
144 rMyriad.bsBlockUsed.setBit( iCurBlock );
145#ifdef MYRIAD_STREAM_DEBUG 143#ifdef MYRIAD_STREAM_DEBUG
146 sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: " 144 sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: "
147 << iCurBlock << "." << sio.nl; 145 << iCurBlock << "." << sio.nl;
@@ -283,6 +281,8 @@ void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ )
283 281
284void Bu::MyriadStream::setSize( long iSize ) 282void Bu::MyriadStream::setSize( long iSize )
285{ 283{
284 if( iSize < 0 )
285 iSize = 0;
286 rMyriad.setStreamSize( pStream, iSize ); 286 rMyriad.setStreamSize( pStream, iSize );
287 if( iPos > iSize ) 287 if( iPos > iSize )
288 iPos = iSize; 288 iPos = iSize;
diff --git a/src/unit/myriad.unit b/src/unit/myriad.unit
index ed121cc..d27c0dc 100644
--- a/src/unit/myriad.unit
+++ b/src/unit/myriad.unit
@@ -20,9 +20,33 @@ using namespace Bu;
20 20
21suite Myriad 21suite Myriad
22{ 22{
23 void addBlock( Stream &s ) 23 test setSize
24 { 24 {
25 s.setPosEnd( 0 ); 25 FString sFileName("myriad-XXXXXXX");
26
27 File fMyriad = tempFile( sFileName );
28 Myriad m( fMyriad );
29 m.initialize( 64 );
30
31 MyriadStream ms = m.openStream( m.createStream() );
32 ms.setSize( 150 );
33 ms.setPos( 145 );
34 char stuff[10];
35 unitTest( ms.read( stuff, 10 ) == 5 );
36
37 ms.setSize( 12 );
38 unitTest( ms.read( stuff, 10 ) == 0 );
39 unitTest( ms.write( "hello", 5 ) == 5 );
40 unitTest( ms.tell() == 17 );
41
42 ms.setSize( 500 );
43 unitTest( ms.tell() == 17 );
44 }
45
46 void addBlock( Stream &s, bool bAppend=true )
47 {
48 if( bAppend )
49 s.setPosEnd( 0 );
26 int iSize = (random()%1016)+8; 50 int iSize = (random()%1016)+8;
27 s.write( &iSize, 4 ); 51 s.write( &iSize, 4 );
28 char *buf = new char[iSize-8]; 52 char *buf = new char[iSize-8];
@@ -72,7 +96,7 @@ suite Myriad
72 verifyBlock( s ); 96 verifyBlock( s );
73 } 97 }
74 98
75 test grow1 99 test stressGrow
76 { 100 {
77 FString sFileName("myriad-XXXXXXX"); 101 FString sFileName("myriad-XXXXXXX");
78 102
@@ -90,7 +114,6 @@ suite Myriad
90 114
91 for( int j = 0; j < 2500; j++ ) 115 for( int j = 0; j < 2500; j++ )
92 { 116 {
93
94 switch( random()%5 ) 117 switch( random()%5 )
95 { 118 {
96 case 0: 119 case 0:
@@ -134,5 +157,38 @@ suite Myriad
134 verifyStream( ms ); 157 verifyStream( ms );
135 } 158 }
136 } 159 }
160
161 test stressTruncate
162 {
163 FString sFileName("myriad-XXXXXXX");
164
165 File fMyriad = tempFile( sFileName );
166 Myriad m( fMyriad );
167 m.initialize( 64 );
168
169 Array<int> aStream;
170
171 for( int j = 0; j < 5; j++ )
172 {
173 aStream.append( m.createStream() );
174 }
175
176 srandom( 1024 );
177
178 char b;
179 for( int iter = 0; iter < 2500; iter++ )
180 {
181 for( Array<int>::iterator i = aStream.begin(); i; i++ )
182 {
183 MyriadStream ms = m.openStream( *i );
184 addBlock( ms, false );
185 ms.setSize( ms.tell() );
186 unitTest( ms.read( &b, 1 ) == 0 );
187 ms.setPos( 0 );
188 verifyBlock( ms );
189 unitTest( ms.read( &b, 1 ) == 0 );
190 }
191 }
192 }
137} 193}
138 194