Skip to content

Commit

Permalink
readonly support
Browse files Browse the repository at this point in the history
  • Loading branch information
albertz committed Mar 7, 2011
1 parent 659060a commit c09be41
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions Db.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct DbDirEntry {

struct DbIntf {
DbStats stats;
virtual Return setReadOnly(bool ro) { return "Db::setReadOnly: not implemented"; }
virtual Return init() = 0;
virtual Return push(/*out*/ DbEntryId& id, const DbEntry& entry) = 0;
virtual Return get(/*out*/ DbEntry& entry, const DbEntryId& id) = 0;
Expand Down
3 changes: 2 additions & 1 deletion DbKyotoBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ using namespace std;
using namespace kyotocabinet;

DbKyotoBackend::~DbKyotoBackend() {
db.close();
}

Return DbKyotoBackend::init() {
if(!db.open(filename, PolyDB::OWRITER | PolyDB::OCREATE))
if(!db.open(filename, readonly ? PolyDB::OREADER : (PolyDB::OWRITER | PolyDB::OCREATE)))
return std::string() + "failed to open KyotoCabinet DB: " + db.error().name();
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion DbKyotoBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
struct DbKyotoBackend : DbIntf {
kyotocabinet::PolyDB db;
std::string filename;
bool readonly;

DbKyotoBackend(const std::string& dbfilename = "db.kch") : filename(dbfilename) {}
DbKyotoBackend(const std::string& dbfilename = "db.kch", bool ro = false) : filename(dbfilename), readonly(ro) {}
~DbKyotoBackend();
Return setReadOnly(bool ro) { readonly = ro; return true; }
Return init();
Return push(/*out*/ DbEntryId& id, const DbEntry& entry);
Return get(/*out*/ DbEntry& entry, const DbEntryId& id);
Expand Down
1 change: 1 addition & 0 deletions db-list-dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static Return listDir(const std::string& path) {
static Return _main() {
DbDefBackend dbInst;
db = &dbInst;
db->setReadOnly(true);
ASSERT( db->init() );
ASSERT( listDir("") );
return true;
Expand Down

0 comments on commit c09be41

Please sign in to comment.