add dummy cache; remove read(n)
diff --git a/include/object-db-file.h b/include/object-db-file.h
index daa2193..b90bd91 100644
--- a/include/object-db-file.h
+++ b/include/object-db-file.h
@@ -7,6 +7,7 @@
#include <ifstream>
#include <ofstream>
#include <sstream>
+#include <deque>
#include <boost/thread/locks.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
@@ -63,10 +64,6 @@
virtual Bytes
next() _OVERRIDE;
- // get n COs; if the remaining number of COs < n, return all;
- virtual void
- read(vector<Bytes> &vco, int n) _OVERRIDE;
-
// size in terms of number of COs
// This is the lazy form of size, i.e. it returns the size cached in this object
// but that may not necessarily equal to the actual size kept in file
@@ -101,6 +98,10 @@
void
updateSize();
+ // read lock should have been grabbed already before the call
+ void
+ fillDummyCache();
+
#define MAGIC_NUM 0xAAAAAAAA
protected:
@@ -114,6 +115,12 @@
int m_size;
// the index (or seq) of the CO to be read
int m_index;
+
+ // A dummy Cache that holds the next 10 (or all remaining if less than 10)
+ // COs after a next() operation
+ // If needed and time allows, we can have more complex cache
+ #define CACHE_SIZE 10
+ map<int, Bytes> m_dummyCache;
};
void inline
diff --git a/include/object-db.h b/include/object-db.h
index 4ad8504..7daa37c 100644
--- a/include/object-db.h
+++ b/include/object-db.h
@@ -33,10 +33,6 @@
virtual Bytes
next() = 0;
- // get n COs; if the remaining number of COs < n, return all;
- virtual void
- read(vector<Bytes> &vco, int n) = 0;
-
// size in terms of number of COs
virtual int
size() = 0 const;
diff --git a/src/object-db-file.cpp b/src/object-db-file.cpp
index 9fe62de..02991a9 100644
--- a/src/object-db-file.cpp
+++ b/src/object-db-file.cpp
@@ -123,28 +123,45 @@
ObjectDBFile::next()
{
ReadLock(m_filelock);
+ // We are been lazy here; just use file lock as mutex
+ // for the access to the cache too
+ if (m_dummyCache.find(m_index) != map::end)
+ {
+ int index = m_index;
+ m_index++;
+ return m_dummyCache[index];
+ }
+
+ // m_index not found in cache
Bytes co;
if (m_index >= m_size)
{
+ // at the end of file, return empty
return co;
}
+
readBytes(m_istream, co);
m_index++;
+
+ // fill dummy cache with the next CACHE_SIZE COs
+ fillDummyCache();
+
return co;
}
-// Caching is not so useful here, as its sequentially reading anyway
void
-ObjectDBFile::read(vector<Bytes> &vco, int n)
+ObjectDBFile::fillDummyCache()
{
- ReadLock(m_filelock);
- int stop = (m_index + n < m_size ) ? m_index + n : m_size;
- while (m_index < stop)
+ m_dummyCache.clear();
+ int stop = (m_index + CACHE_SIZE < m_size) ? m_index + CACHE_SIZE : m_size;
+ // the m_index should not change
+ int index = m_index;
+ while (index < stop)
{
Bytes co;
readBytes(m_istream, co);
- vco.push_back(co);
- m_index++;
+ m_dummyCache.insert(make_pair(index, co));
+ index++;
}
}