Commit 1ddccdb1 authored by Casualet's avatar Casualet

add comments, change the style

parent e74b2a97
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <algorithm>
#include <util/enum_text.hh> #include <util/enum_text.hh>
#include <main/serializers.hh> #include <main/serializers.hh>
// FIXME: Maybe should inherit from DBObject. // FIXME: Maybe should inherit from DBObject.
class AbstractMetaKey : public NormalAlloc { class AbstractMetaKey : public NormalAlloc {
public: public:
...@@ -104,9 +106,13 @@ private: ...@@ -104,9 +106,13 @@ private:
} }
}; };
/*
* DBObject intends to give each object of this type an
* id, which can be written into the embeeded database.
*/
class DBObject { class DBObject {
const unsigned int id; const unsigned int id;
public: public:
// 0 indicates that the object does not have a database id. // 0 indicates that the object does not have a database id.
// This is the state of the object before it is written to // This is the state of the object before it is written to
...@@ -145,14 +151,14 @@ public: ...@@ -145,14 +151,14 @@ public:
// FIXME: Use rtti. // FIXME: Use rtti.
virtual std::string typeName() const = 0; virtual std::string typeName() const = 0;
/* */
virtual std::vector<DBMeta *> virtual std::vector<DBMeta *>
fetchChildren(const std::unique_ptr<Connect> &e_conn) = 0; fetchChildren(const std::unique_ptr<Connect> &e_conn) = 0;
// Stops processing on error. /* */
virtual bool virtual bool
applyToChildren(std::function<bool(const DBMeta &)>) applyToChildren(std::function<bool(const DBMeta &)>) const = 0;
const = 0; /*traverse the map to get the key for the conresponding child(reference MappedDBMeta)*/
virtual AbstractMetaKey const &getKey(const DBMeta &child) virtual AbstractMetaKey const &getKey(const DBMeta &child) const = 0;
const = 0;
protected: protected:
std::vector<DBMeta*> std::vector<DBMeta*>
...@@ -169,19 +175,16 @@ public: ...@@ -169,19 +175,16 @@ public:
LeafDBMeta(unsigned int id) : DBMeta(id) {} LeafDBMeta(unsigned int id) : DBMeta(id) {}
std::vector<DBMeta *> std::vector<DBMeta *>
fetchChildren(const std::unique_ptr<Connect> &e_conn) fetchChildren(const std::unique_ptr<Connect> &e_conn) {
{
return std::vector<DBMeta *>(); return std::vector<DBMeta *>();
} }
bool applyToChildren(std::function<bool(const DBMeta &)> bool applyToChildren(std::function<bool(const DBMeta &)>
fn) const fn) const {
{
return true; return true;
} }
AbstractMetaKey const &getKey(const DBMeta &child) const AbstractMetaKey const &getKey(const DBMeta &child) const {
{
// FIXME: // FIXME:
assert(false); assert(false);
} }
...@@ -192,19 +195,29 @@ public: ...@@ -192,19 +195,29 @@ public:
// 'const' back on the members. // 'const' back on the members.
// > FIXME: The key in children is a pointer so this means our lookup is // > FIXME: The key in children is a pointer so this means our lookup is
// slow. Use std::reference_wrapper. // slow. Use std::reference_wrapper.
/*
* ChildType is an instance of EncLayer, and KeyType could be MetaKey.
*/
template <typename ChildType, typename KeyType> template <typename ChildType, typename KeyType>
class MappedDBMeta : public DBMeta { class MappedDBMeta : public DBMeta {
public: public:
MappedDBMeta() {} MappedDBMeta() {}
MappedDBMeta(unsigned int id) : DBMeta(id) {} MappedDBMeta(unsigned int id) : DBMeta(id) {}
virtual ~MappedDBMeta() {} virtual ~MappedDBMeta() {}
virtual bool addChild(KeyType key, std::unique_ptr<ChildType> meta); virtual bool addChild(KeyType key, std::unique_ptr<ChildType> meta);
virtual bool childExists(const KeyType &key) const; virtual bool childExists(const KeyType &key) const;
virtual ChildType * getChild(const KeyType &key) const; virtual ChildType * getChild(const KeyType &key) const;
/*the return type is different from that of DBMeta, what are the consequences?*/
KeyType const &getKey(const DBMeta &child) const; KeyType const &getKey(const DBMeta &child) const;
virtual std::vector<DBMeta *> virtual std::vector<DBMeta *>
fetchChildren(const std::unique_ptr<Connect> &e_conn); fetchChildren(const std::unique_ptr<Connect> &e_conn);
/*what if we remove this declaration?*/
bool applyToChildren(std::function<bool(const DBMeta &)> fn) const; bool applyToChildren(std::function<bool(const DBMeta &)> fn) const;
const std::map<KeyType, std::unique_ptr<ChildType> > & const std::map<KeyType, std::unique_ptr<ChildType> > &
getChildren() const { getChildren() const {
return children; return children;
...@@ -213,14 +226,17 @@ public: ...@@ -213,14 +226,17 @@ public:
getChildWithGChild(const DBMeta &gchild) const; getChildWithGChild(const DBMeta &gchild) const;
private: private:
/*store the hirechy of metadata as a map. For example, each database has sever tables,
*then in the databasemeta stores a map of tables. The keys are of type string(metakey),
*and the values are of type DBMeta.
*/
std::map<KeyType, std::unique_ptr<ChildType> > children; std::map<KeyType, std::unique_ptr<ChildType> > children;
}; };
//#include <main/dbobject.tt>
#include <algorithm>
/*template implemented in dbobject.hh*/
template <typename KeyType> template <typename KeyType>
bool MetaKey<KeyType>::operator <(const MetaKey<KeyType> &rhs) const bool MetaKey<KeyType>::operator <(const MetaKey<KeyType> &rhs) const
{ {
...@@ -342,11 +358,9 @@ getChildWithGChild(const DBMeta &gchild) const ...@@ -342,11 +358,9 @@ getChildWithGChild(const DBMeta &gchild) const
}; };
it.second->applyToChildren(misGet); it.second->applyToChildren(misGet);
if (true == match) { if (true == match) {
return it.second.get(); return it.second.get();
} }
} }
return nullptr; return nullptr;
} }
...@@ -62,8 +62,7 @@ OnionMeta::OnionMeta(onion o, std::vector<SECLEVEL> levels, ...@@ -62,8 +62,7 @@ OnionMeta::OnionMeta(onion o, std::vector<SECLEVEL> levels,
const Create_field &cf, unsigned long uniq_count, const Create_field &cf, unsigned long uniq_count,
SECLEVEL minimum_seclevel) SECLEVEL minimum_seclevel)
: onionname(getpRandomName() + TypeText<onion>::toText(o)), : onionname(getpRandomName() + TypeText<onion>::toText(o)),
uniq_count(uniq_count), minimum_seclevel(minimum_seclevel) uniq_count(uniq_count), minimum_seclevel(minimum_seclevel) {
{
assert(levels.size() >= 1); assert(levels.size() >= 1);
const Create_field * newcf = &cf; const Create_field * newcf = &cf;
...@@ -81,7 +80,6 @@ OnionMeta::OnionMeta(onion o, std::vector<SECLEVEL> levels, ...@@ -81,7 +80,6 @@ OnionMeta::OnionMeta(onion o, std::vector<SECLEVEL> levels,
this->layers.push_back(std::move(el)); this->layers.push_back(std::move(el));
} }
assert(this->layers.size() >= 1); assert(this->layers.size() >= 1);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment