Merge branch 'master' into dev
diff --git a/include/object-db-file.h b/include/object-db-file.h
index fb9ca91..daa2193 100644
--- a/include/object-db-file.h
+++ b/include/object-db-file.h
@@ -8,8 +8,10 @@
#include <ofstream>
#include <sstream>
#include <boost/thread/locks.hpp>
-#include <boost/thread/recursive_mutex.hpp>
#include <boost/lexical_cast.hpp>
+#include <boost/interprocess/sync/file_lock.hpp>
+#include <boost/interprocess/sync/sharable_lock.hpp>
+#include <boost/interprocess/sync/scoped_lock.hpp>
#define _OVERRIDE
#ifdef __GNUC__
@@ -38,9 +40,9 @@
class ObjectDBFile
{
public:
- typedef boost::shared_mutex Lock;
- typedef boost::unique_lock<Lock> WriteLock;
- typedef boost::shared_lock<Lock> ReadLock;
+ typedef boost::interprocess::file_lock Filelock;
+ typedef boost::interprocess::scoped_lock<Filelock> WriteLock;
+ typedef boost::interprocess::sharable_lock<Filelock> ReadLock;
ObjectDBFile(const string &filename);
virtual ~ObjectDBFile(){}
@@ -91,10 +93,6 @@
rewind();
protected:
- // write lock should have been grabbed already before the call
- void
- prepareWrite();
-
// read or write lock should have been grabbed already before the call
void
checkInit(const string &msg);
@@ -109,8 +107,7 @@
string m_filename;
ifstream m_istream;
ofstream m_ostream;
- Lock m_lock;
- bool m_writable;
+ Filelock m_filelock;
bool m_initialized;
// capacity in terms of number of COs
int m_cap;
diff --git a/src/object-db-file.cpp b/src/object-db-file.cpp
index ae13d91..d294b23 100644
--- a/src/object-db-file.cpp
+++ b/src/object-db-file.cpp
@@ -23,12 +23,15 @@
, m_cap(0)
, m_index(0)
, m_initialized(false)
- , m_writable(false)
, m_filename(filename)
+ // This ensures file with filename exists (assuming having write permission)
+ // This is needed as file_lock only works with existing file
+ , m_ostream(m_filename, ios_base::binary | ios_base::app)
+ , m_istream(m_filename, ios_base::binary | ios_base::in)
+ , m_filelock(m_filename)
{
- m_istream.open(m_filename, ios_base::binary);
int magic;
- ReadLock(m_lock);
+ ReadLock(m_filelock);
readInt(m_istream, magic);
if (magic == MAGIC_NUM)
{
@@ -42,26 +45,18 @@
ObjectDBFile::~ObjectDBFile()
{
m_istream.close();
- if (m_writable)
- {
- m_ostream.close();
- }
+ m_ostream.close();
}
void
ObjectDBFile::init(int capacity)
{
- WriteLock(m_lock);
+ WriteLock(m_filelock);
if (m_initialized)
{
throwException("Trying to init already initialized ObjectDBFile object" + m_filename);
}
- if (!m_writable)
- {
- prepareWrite();
- }
-
m_cap = capacity;
m_size = 0;
@@ -92,14 +87,9 @@
void
ObjectDBFile::append(const Bytes &co)
{
- WriteLock(m_lock);
+ WriteLock(m_filelock);
checkInit("Trying to append to un-initialized ObjectDBFile: " + m_filename);
- if (!m_writable)
- {
- prepareWrite();
- }
-
if (m_size >= m_cap)
{
throwException("Exceed Maximum capacity: " + boost::lexical_cast<string>(m_cap));
@@ -156,7 +146,7 @@
int
ObjectDBFile::fSize()
{
- ReadLock(m_lock);
+ ReadLock(m_filelock);
updateSize();
return m_size;
}
@@ -164,14 +154,14 @@
int
ObjectDBFile::index()
{
- ReadLock(m_lock);
+ ReadLock(m_filelock);
return m_index;
}
bool
ObjectDBFile::seek(int index)
{
- ReadLock(m_lock);
+ ReadLock(m_filelock);
updateSize();
if (m_size <= index)
{
@@ -188,7 +178,7 @@
void
rewind()
{
- ReadLock(m_lock);
+ ReadLock(m_filelock);
m_index = 0;
// point to the start of the CO fields
m_istream.seekg( (3 + m_cap) * sizeof(int), ios::beg);
@@ -202,21 +192,3 @@
throwException(msg);
}
}
-
-void
-ObjectDBFile::prepareWrite()
-{
- ios_base::openmode mode = ios_base::app | ios_base::binary;
- // discard any content if the object is considered uninitialized
- if (!m_initialized)
- {
- mode |= ios_base::trunc;
- }
-
- m_ostream.open(m_filename, mode);
- m_writable = m_ostream.is_open();
- if (!m_writable)
- {
- throwException("Unable to open file for write: " + m_filename);
- }
-}