summaryrefslogtreecommitdiff
path: root/src/queuebuf.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2010-05-10 07:15:05 +0000
committerMike Buland <eichlan@xagasoft.com>2010-05-10 07:15:05 +0000
commit9731dc86fa9b12adc064b99910dddb58932c71cf (patch)
tree00a358ff397e2b7b0fc3377564288f5c86b850c5 /src/queuebuf.cpp
parent8baf7e1e75a185c742dc6d5b27e50058635e5522 (diff)
downloadlibbu++-9731dc86fa9b12adc064b99910dddb58932c71cf.tar.gz
libbu++-9731dc86fa9b12adc064b99910dddb58932c71cf.tar.bz2
libbu++-9731dc86fa9b12adc064b99910dddb58932c71cf.tar.xz
libbu++-9731dc86fa9b12adc064b99910dddb58932c71cf.zip
Added the new Bu::CacheStoreFiles, it's an uber-simple cache storage system that
maybe would be better to call an example than a fully fledged storage strategy. It just names files based on your keys. It's very slow, and very wasteful, and shouldn't be used long-term in most normal cache systems.
Diffstat (limited to 'src/queuebuf.cpp')
-rw-r--r--src/queuebuf.cpp60
1 files changed, 41 insertions, 19 deletions
diff --git a/src/queuebuf.cpp b/src/queuebuf.cpp
index 9404164..9577793 100644
--- a/src/queuebuf.cpp
+++ b/src/queuebuf.cpp
@@ -70,13 +70,13 @@ size_t Bu::QueueBuf::read( void *pRawBuf, size_t nBytes )
70 iLeft -= iCopy; 70 iLeft -= iCopy;
71 pBuf += iCopy; 71 pBuf += iCopy;
72 iTotalSize -= iCopy; 72 iTotalSize -= iCopy;
73 sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl; 73// sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl;
74 } 74 }
75 75
76 return nBytes - iLeft; 76 return nBytes - iLeft;
77} 77}
78 78
79size_t QueueBuf::peek( void *pBuf, size_t nBytes ) 79size_t Bu::QueueBuf::peek( void *pRawBuf, size_t nBytes )
80{ 80{
81 if( nBytes <= 0 ) 81 if( nBytes <= 0 )
82 return 0; 82 return 0;
@@ -88,31 +88,33 @@ size_t QueueBuf::peek( void *pBuf, size_t nBytes )
88 char *pBuf = (char *)pRawBuf; 88 char *pBuf = (char *)pRawBuf;
89 89
90 int iTmpReadOffset = iReadOffset; 90 int iTmpReadOffset = iReadOffset;
91 int iTmpRemSize = iTotalSize; 91 size_t iTmpRemSize = iTotalSize;
92 BlockList::iterator iBlock = lBlocks.begin();
92 while( iLeft > 0 && iTmpRemSize > 0 ) 93 while( iLeft > 0 && iTmpRemSize > 0 )
93 { 94 {
94
95 // Switching to use temp variables instead of iReadOffset and iTotalSize 95 // Switching to use temp variables instead of iReadOffset and iTotalSize
96 if( iReadOffset == iBlockSize ) 96 if( iTmpReadOffset == iBlockSize )
97 { 97 {
98 if( lBlocks.isEmpty() ) 98 iBlock++;
99 if( iBlock == lBlocks.end() )
99 { 100 {
100 return nBytes-iLeft; 101 return nBytes-iLeft;
101 } 102 }
102 iReadOffset = 0; 103 iTmpReadOffset = 0;
103 } 104 }
104 char *pBlock = lBlocks.first(); 105 char *pBlock = *iBlock;
105 size_t iCopy = iBlockSize-iReadOffset; 106 size_t iCopy = iBlockSize-iTmpReadOffset;
106 if( iLeft < iCopy ) 107 if( iLeft < iCopy )
107 iCopy = iLeft; 108 iCopy = iLeft;
108 if( iTotalSize < iCopy ) 109 if( iTmpRemSize < iCopy )
109 iCopy = iTotalSize; 110 iCopy = iTmpRemSize;
110 memcpy( pBuf, pBlock+iReadOffset, iCopy ); 111 memcpy( pBuf, pBlock+iTmpReadOffset, iCopy );
111 iReadOffset += iCopy; 112 iTmpReadOffset += iCopy;
112 iLeft -= iCopy; 113 iLeft -= iCopy;
113 pBuf += iCopy; 114 pBuf += iCopy;
114 iTotalSize -= iCopy; 115 iTmpRemSize -= iCopy;
115 sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl; 116// sio << "Read (peek) " << iCopy << " bytes, new temp size: "
117// << iTmpRemSize << sio.nl;
116 } 118 }
117 119
118 return nBytes - iLeft; 120 return nBytes - iLeft;
@@ -147,7 +149,8 @@ size_t Bu::QueueBuf::write( const void *pRawBuf, size_t nBytes )
147 iLeft -= iCopy; 149 iLeft -= iCopy;
148 pBuf += iCopy; 150 pBuf += iCopy;
149 iTotalSize += iCopy; 151 iTotalSize += iCopy;
150 sio << "Wrote " << iCopy << " bytes, new size: " << iTotalSize << sio.nl; 152// sio << "Wrote " << iCopy << " bytes, new size: " << iTotalSize
153// << sio.nl;
151 } 154 }
152 155
153 return nBytes; 156 return nBytes;
@@ -158,8 +161,27 @@ long Bu::QueueBuf::tell()
158 return -1; 161 return -1;
159} 162}
160 163
161void Bu::QueueBuf::seek( long ) 164void Bu::QueueBuf::seek( long iAmnt )
162{ 165{
166 if( iAmnt <= 0 )
167 return;
168
169 if( iAmnt >= iTotalSize )
170 {
171// sio << "seek: clear all data (" << iAmnt << ">=" << iTotalSize
172// << ")." << sio.nl;
173 close();
174 return;
175 }
176
177 iReadOffset += iAmnt;
178 iTotalSize -= iAmnt;
179 while( iReadOffset >= iBlockSize )
180 {
181 removeBlock();
182 iReadOffset -= iBlockSize;
183// sio << "seek: removal step (" << iReadOffset << ")" << sio.nl;
184 }
163} 185}
164 186
165void Bu::QueueBuf::setPos( long ) 187void Bu::QueueBuf::setPos( long )
@@ -221,13 +243,13 @@ void Bu::QueueBuf::setBlocking( bool )
221void Bu::QueueBuf::addBlock() 243void Bu::QueueBuf::addBlock()
222{ 244{
223 lBlocks.append( new char[iBlockSize] ); 245 lBlocks.append( new char[iBlockSize] );
224 sio << "Added new block." << sio.nl; 246// sio << "Added new block." << sio.nl;
225} 247}
226 248
227void Bu::QueueBuf::removeBlock() 249void Bu::QueueBuf::removeBlock()
228{ 250{
229 delete[] lBlocks.first(); 251 delete[] lBlocks.first();
230 lBlocks.erase( lBlocks.begin() ); 252 lBlocks.erase( lBlocks.begin() );
231 sio << "Removed block." << sio.nl; 253// sio << "Removed block." << sio.nl;
232} 254}
233 255