00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef _API_DELTA_H_
00031 #define _API_DELTA_H_
00032
00033 #include <api_deltabase.h>
00034 #include <search_node.h>
00035 #include <dbtools.h>
00036 #include <counted_ptr.h>
00037 #include <db_method>
00038
00039 static std::string ___empty_string;
00040 class delta_get_action : public vdbl::method<std::string>
00041 {
00042 private:
00043 typedef vdbl::method<std::string> _Base;
00044 vdbl::colid cid;
00045 const vdbl::row* _r;
00046
00047 public:
00048 typedef vdbl::context context;
00049 typedef std::string return_type;
00050
00051 delta_get_action(vdbl::colid _c) : _Base(), cid(_c), _r() {}
00052 delta_get_action(const delta_get_action& _i) : _Base(_i), cid(_i.cid),
00053 _r(_i._r) {}
00054 virtual ~delta_get_action() {}
00055
00056 const std::string& operator() () const;
00057 const std::string& def() const { return operator()(); }
00058 void setcontext(const context* c, const vdbl::row* r) { _r = r; }
00059 };
00060
00061
00062
00063 inline bool delta_base::apply3(work_node& _x, const work_node& _y,
00064 undelta_base*& _u) const
00065 { _x = _y; return apply(_x, _u); }
00066
00067
00068
00069 inline bool undelta_base::unapply3(work_node& _x,
00070 const work_node& _y) const
00071 { _x = _y; return unapply(_x); }
00072
00073 inline const std::string& delta_get_action::operator() () const
00074 {
00075 if(!_r)
00076 return ___empty_string;
00077 bool error;
00078 const vdbl::col& _c(_r->get_col(cid, error));
00079 if(error)
00080 return ___empty_string;
00081 const delta* _d;
00082 _c.get_ptr(_d);
00083 if(_d == NULL)
00084 return ___empty_string;
00085 return _d->get_action();
00086 }
00087 inline delta::delta(const delta_base& __d)
00088 { _d = __d.new_copy(); }
00089 inline delta::delta(const delta& __d)
00090 { _d = __d._d->new_copy();
00091 #if DEBUG_DELTA
00092 std::cerr << "Called delta copy constructor" << std::endl;
00093 #endif
00094 }
00095 inline delta::~delta()
00096 { if(_d) delete _d;
00097 #if DEBUG_DELTA
00098 std::cerr << "Called delta destructor" << std::endl;
00099 #endif
00100 }
00101 inline delta& delta::operator=(const delta& __d)
00102 {
00103 if(_d) delete _d;
00104 _d = __d._d->new_copy();
00105 #if DEBUG_DELTA
00106 std::cerr << "Called delta assignment operator" << std::endl;
00107 #endif
00108 return *this;
00109 }
00110
00111 inline const std::string& delta::get_action() const { return _d->get_action(); }
00112 inline const delta_base* delta::get_base() const { return _d; }
00113
00114 inline bool delta::apply(work_node& _x, const delta_id& _i) const
00115 { undelta_base* _u(NULL);
00116 if(_d->apply(_x, _u))
00117 {
00118 _x.undeltas.insert(std::make_pair(_i,undelta(_u)));
00119 _x.deltas.push_back(_i);
00120 return true;
00121 }
00122 else
00123 return false;
00124 }
00125
00126 inline bool delta::apply3(work_node& _x, const work_node& _y,
00127 const delta_id& _i) const
00128 { undelta_base* _u(NULL);
00129 if(_d->apply3(_x, _y, _u))
00130 {
00131 _x.undeltas.insert(std::make_pair(_i,undelta(_u)));
00132 _x.deltas.push_back(_i);
00133 return true;
00134 }
00135 else
00136 return false;
00137 }
00138
00139 inline void delta::convert(work_node& _x)
00140 {
00141 delta_base* _do = _d;
00142 _do->convert(_x, _d);
00143 if(_do != _d)
00144 delete _do;
00145 }
00146
00147 inline delta_id delta::store(work_node& wn)
00148 {
00149 std::vector<vdbl::col_spec> _v;
00150 this->convert(wn);
00151 _v.push_back(vdbl::col_spec("delta", vdbl::typed_col<delta>(*this)));
00152 delta_id _r;
00153 if(!wn.dtable->insert_row(_v, _r))
00154 {
00155 std::cerr << "Storing delta in the deltas table failed!" << std::endl;
00156 throw "Programming error";
00157 }
00158 return _r;
00159 }
00160
00161 inline undelta::undelta(const undelta& __d)
00162 { _d = __d._d->new_copy();
00163 #if DEBUG_DELTA
00164 std::cerr << "Called undelta copy constructor" << std::endl;
00165 #endif
00166 }
00167 inline undelta::~undelta()
00168 { if(_d) delete _d;
00169 #if DEBUG_DELTA
00170 std::cerr << "Called undelta destructor" << std::endl;
00171 #endif
00172 }
00173 inline undelta& undelta::operator=(const undelta& __u)
00174 {
00175 if(_d) delete _d;
00176 _d = __u._d->new_copy();
00177 #if DEBUG_DELTA
00178 std::cerr << "Called undelta assignment operator" << std::endl;
00179 #endif
00180 return *this;
00181 }
00182 inline const undelta_base* undelta::get_base() const { return _d; }
00183
00184 inline bool undelta::unapply(work_node& _x) const
00185 { return _d->unapply(_x); }
00186
00187 inline bool undelta::unapply3(work_node& _x, const work_node& _y) const
00188 { return _d->unapply3(_x, _y); }
00189
00190 #endif