aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-27 16:54:55 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-27 16:54:55 +0000
commit00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6 (patch)
treef5e127d2e169a0eb93c66d5c76852303d4a10f0c
parent9098237f5bb16b204a5ea999b702e5eb170f68ac (diff)
downloadlibbu++-00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6.tar.gz
libbu++-00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6.tar.bz2
libbu++-00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6.tar.xz
libbu++-00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6.zip
Corrected the Bu::File::canRead() and Bu::File::canWrite() functions, they work
now. It helps to read the system docs. Anyway, nids is all fixed up, it seems to work great now, and I guess I got all the corner cases we'll hit for a while, fishtrax really did a number on them :) I also cleaned up all the debugging output, now you can see your program run instead of libbu++ internals. There could still be a good amount of improvement made in nids, it really shouldn't re-write whole blocks every time you write to a stream, but that will be an easy change down the line that won't effect any of the existing code.
Diffstat (limited to '')
-rw-r--r--src/cachestorenids.h7
-rw-r--r--src/file.cpp10
-rw-r--r--src/nids.cpp12
-rw-r--r--src/nidsstream.cpp14
4 files changed, 25 insertions, 18 deletions
diff --git a/src/cachestorenids.h b/src/cachestorenids.h
index bd0fcc7..9288008 100644
--- a/src/cachestorenids.h
+++ b/src/cachestorenids.h
@@ -48,12 +48,6 @@ namespace Bu
48 NidsStream ns = nStore.openStream( 0 ); 48 NidsStream ns = nStore.openStream( 0 );
49 Bu::Archive ar( ns, Bu::Archive::save ); 49 Bu::Archive ar( ns, Bu::Archive::save );
50 ar << hId; 50 ar << hId;
51
52 Bu::FString sName;
53 sName.format("hash-%d.%02d", time( NULL ), iCnt++ );
54 Bu::File sTmp( sName, Bu::File::Write|Bu::File::Create );
55 Bu::Archive artmp( sTmp, Bu::Archive::save );
56 artmp << hId;
57 } 51 }
58 52
59 virtual obtype *load( const keytype &key ) 53 virtual obtype *load( const keytype &key )
@@ -80,7 +74,6 @@ namespace Bu
80 keytype key = __cacheGetKey<obtype,keytype>( pSrc ); 74 keytype key = __cacheGetKey<obtype,keytype>( pSrc );
81 int iStream = nStore.createStream(); 75 int iStream = nStore.createStream();
82 hId.insert( key, iStream ); 76 hId.insert( key, iStream );
83 printf("Creating stream: %d\n", iStream );
84 NidsStream ns = nStore.openStream( iStream ); 77 NidsStream ns = nStore.openStream( iStream );
85 Bu::Archive ar( ns, Bu::Archive::save ); 78 Bu::Archive ar( ns, Bu::Archive::save );
86 obtype *pOb = new obtype(); 79 obtype *pOb = new obtype();
diff --git a/src/file.cpp b/src/file.cpp
index 20ff5c9..aa0ea32 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -113,7 +113,10 @@ bool Bu::File::canRead()
113#ifdef WIN32 113#ifdef WIN32
114 return true; 114 return true;
115#else 115#else
116 return (fcntl( fd, F_GETFL, 0 )&O_RDONLY) == O_RDONLY; 116 int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE;
117 if( iMode == O_RDONLY || iMode == O_RDWR )
118 return true;
119 return false;
117#endif 120#endif
118} 121}
119 122
@@ -122,7 +125,10 @@ bool Bu::File::canWrite()
122#ifdef WIN32 125#ifdef WIN32
123 return true; 126 return true;
124#else 127#else
125 return (fcntl( fd, F_GETFL, 0 )&O_WRONLY) == O_WRONLY; 128 int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE;
129 if( iMode == O_WRONLY || iMode == O_RDWR )
130 return true;
131 return false;
126#endif 132#endif
127} 133}
128 134
diff --git a/src/nids.cpp b/src/nids.cpp
index 00d5f2b..23c11b0 100644
--- a/src/nids.cpp
+++ b/src/nids.cpp
@@ -114,8 +114,8 @@ void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
114 114
115void Bu::Nids::updateHeader() 115void Bu::Nids::updateHeader()
116{ 116{
117// if( !sStore.canWrite() ) 117 if( !sStore.canWrite() )
118// return; 118 return;
119 sStore.setPos( 10 ); // Skip the magic number, version, bpi, block size 119 sStore.setPos( 10 ); // Skip the magic number, version, bpi, block size
120 sStore.write( &iBlocks, 4 ); 120 sStore.write( &iBlocks, 4 );
121 sStore.write( &iUsed, 4 ); 121 sStore.write( &iUsed, 4 );
@@ -222,8 +222,8 @@ void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
222 222
223void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize ) 223void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize )
224{ 224{
225// if( !sStore.canWrite() ) 225 if( !sStore.canWrite() )
226// return; 226 return;
227 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 ); 227 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 );
228 sStore.write( &uSize, 4 ); 228 sStore.write( &uSize, 4 );
229} 229}
@@ -240,8 +240,8 @@ uint32_t Bu::Nids::getNextBlock( uint32_t uIndex,
240 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 ); 240 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 );
241 sStore.write( &uNew, 4 ); 241 sStore.write( &uNew, 4 );
242 getBlock( uNew, pBlock ); 242 getBlock( uNew, pBlock );
243 printf("Allocated new block (%u) for stream %u.\n", 243 //printf("Allocated new block (%u) for stream %u.\n",
244 uNew, pBlock->uFirstBlock ); 244 // uNew, pBlock->uFirstBlock );
245 } 245 }
246 else 246 else
247 { 247 {
diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp
index fff73f6..31e1293 100644
--- a/src/nidsstream.cpp
+++ b/src/nidsstream.cpp
@@ -55,7 +55,7 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
55 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) ) 55 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
56 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); 56 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
57 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); 57 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead );
58 //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)", 58 //printf("read buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)",
59 // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock ); 59 // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock );
60 uPos += iRead; 60 uPos += iRead;
61 //printf(" -- %u\n", uPos%uBlockSize ); 61 //printf(" -- %u\n", uPos%uBlockSize );
@@ -80,7 +80,7 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
80 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); 80 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
81 memcpy( ((char *)pBuf)+nTotal, 81 memcpy( ((char *)pBuf)+nTotal,
82 pCurBlock->pData+(uPos%uBlockSize), iRead ); 82 pCurBlock->pData+(uPos%uBlockSize), iRead );
83 //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n", 83 //printf(" read buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n",
84 // iRead, uPos, uPos+iRead-1, uSize, 84 // iRead, uPos, uPos+iRead-1, uSize,
85 // nTotal, nTotal+nBytes-1, nBytes, uCurBlock ); 85 // nTotal, nTotal+nBytes-1, nBytes, uCurBlock );
86 uPos += iRead; 86 uPos += iRead;
@@ -111,6 +111,10 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
111 { 111 {
112 //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed ); 112 //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed );
113 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes ); 113 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes );
114 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u:%u)\n",
115 // nBytes, 0, nBytes-1, nBytes,
116 // uPos, uPos+nBytes-1, uSize, uCurBlock,
117 // pCurBlock->uBytesUsed );
114 if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed ) 118 if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed )
115 pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes; 119 pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes;
116 rNids.setBlock( uCurBlock, pCurBlock ); 120 rNids.setBlock( uCurBlock, pCurBlock );
@@ -132,6 +136,9 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
132 uNow = nBytes; 136 uNow = nBytes;
133 memcpy( pCurBlock->pData+(uPos%uBlockSize), 137 memcpy( pCurBlock->pData+(uPos%uBlockSize),
134 &((char *)pBuf)[nTotal], uNow ); 138 &((char *)pBuf)[nTotal], uNow );
139 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u:%u)\n",
140 // uNow, nTotal, nTotal+uNow-1, nBytes,
141 // uPos, uPos+uNow-1, uSize, uCurBlock, pCurBlock->uBytesUsed );
135 if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed ) 142 if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed )
136 pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow; 143 pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow;
137 rNids.setBlock( uCurBlock, pCurBlock ); 144 rNids.setBlock( uCurBlock, pCurBlock );
@@ -142,7 +149,8 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
142 nBytes -= uNow; 149 nBytes -= uNow;
143 //printf("wb: block %u = %ub (%ub total)\n", 150 //printf("wb: block %u = %ub (%ub total)\n",
144 // uCurBlock, pCurBlock->uBytesUsed, uSize ); 151 // uCurBlock, pCurBlock->uBytesUsed, uSize );
145 if( pCurBlock->uBytesUsed == uBlockSize ) 152 //if( pCurBlock->uBytesUsed == uBlockSize )
153 if( uPos%uBlockSize == 0 )
146 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock ); 154 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock );
147 if( nBytes == 0 ) 155 if( nBytes == 0 )
148 return nTotal; 156 return nTotal;