Skip to content

Commit

Permalink
fix bad DB initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vrabaud committed May 11, 2012
1 parent e8650ad commit d1bf40f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
23 changes: 18 additions & 5 deletions include/object_recognition_core/db/db_parameters.h
Expand Up @@ -36,6 +36,8 @@
#ifndef DB_PARAMETERS_H_ #ifndef DB_PARAMETERS_H_
#define DB_PARAMETERS_H_ #define DB_PARAMETERS_H_


#include <object_recognition_core/common/json.hpp>

namespace object_recognition_core namespace object_recognition_core
{ {
namespace db namespace db
Expand Down Expand Up @@ -68,37 +70,47 @@ namespace object_recognition_core
* @param params A map between some db parameters and their value * @param params A map between some db parameters and their value
*/ */
explicit explicit
ObjectDbParameters(const or_json::mObject& params); ObjectDbParameters(const ObjectDbParametersRaw& params);


static ObjectDbType static ObjectDbType
StringToType(const std::string & type); StringToType(const std::string & type);


static std::string static std::string
TypeToString(const ObjectDbParameters::ObjectDbType & type); TypeToString(const ObjectDbParameters::ObjectDbType & type);


ObjectDbType inline ObjectDbType
type() const type() const
{ {
return type_; return type_;
} }


template<typename T> template<typename T>
void void
set_parameter(const std::string & key, const T & value) set_parameter(const std::string& key, const T& value)
{ {
if (key == "type") if (key == "type")
set_type(value); set_type(value);
else else
{
if ((type() != NONCORE) && (raw_.find(key) == raw_.end()))
throw std::runtime_error("Key \"" + key + "\" not a default key in db of type " + TypeToString(type()));

raw_[key] = or_json::mValue(value); raw_[key] = or_json::mValue(value);
}
} }


void void
set_parameter(const std::string & key, const or_json::mValue & value) set_parameter(const std::string& key, const or_json::mValue& value)
{ {
if (key == "type") if (key == "type")
set_type(value.get_str()); set_type(value.get_str());
else else
{
if ((type() != NONCORE) && (raw_.find(key) == raw_.end()))
throw std::runtime_error("Key \"" + key + "\" not a default key in db of type " + TypeToString(type()));

raw_[key] = value; raw_[key] = value;
}
} }


void void
Expand Down Expand Up @@ -131,10 +143,11 @@ namespace object_recognition_core
protected: protected:
/** The type of the collection 'CouchDB' ... */ /** The type of the collection 'CouchDB' ... */
ObjectDbType type_; ObjectDbType type_;
/** All the raw parameters: they are of integral types */ /** All the raw parameters: they are of integral types. 'type' is there */
or_json::mObject raw_; or_json::mObject raw_;
}; };
} }
} }


#endif /* DB_PARAMETERS_H_ */ #endif /* DB_PARAMETERS_H_ */

20 changes: 11 additions & 9 deletions src/db/db.cpp
Expand Up @@ -102,7 +102,7 @@ namespace object_recognition_core
} }
} }


ObjectDbParameters::ObjectDbParameters(const or_json::mObject& parameters) ObjectDbParameters::ObjectDbParameters(const ObjectDbParametersRaw& parameters)
{ {
if (parameters.find("type") == parameters.end()) if (parameters.find("type") == parameters.end())
{ {
Expand Down Expand Up @@ -157,23 +157,25 @@ namespace object_recognition_core
} }


void void
ObjectDb::set_parameters(const ObjectDbParameters &in_params) ObjectDb::set_parameters(const ObjectDbParameters& in_params)
{ {
ObjectDbParametersRaw params_raw = parameters_.raw(); ObjectDbParametersRaw params_raw = in_params.raw();
switch (parameters_.type())
switch (in_params.type())
{ {
case ObjectDbParameters::COUCHDB: case ObjectDbParameters::COUCHDB:
db_ = boost::shared_ptr<ObjectDbBase>(new ObjectDbCouch(params_raw)); db_ = boost::shared_ptr<ObjectDbBase>(new ObjectDbCouch(params_raw));
return; break;
case ObjectDbParameters::EMPTY: case ObjectDbParameters::EMPTY:
return; break;
case ObjectDbParameters::FILESYSTEM: case ObjectDbParameters::FILESYSTEM:
db_ = boost::shared_ptr<ObjectDbBase>(new ObjectDbFilesystem(params_raw)); db_ = boost::shared_ptr<ObjectDbBase>(new ObjectDbFilesystem(params_raw));
return; break;
default: default:
return; break;
} }
parameters_ = in_params;
parameters_ = ObjectDbParameters(params_raw);
} }


void void
Expand Down

0 comments on commit d1bf40f

Please sign in to comment.