00001 #ifndef HYBRID_STORAGE_MANAGER_H
00002 #define HYBRID_STORAGE_MANAGER_H
00003
00004 #include "storage_manager.h"
00005 #include "ondisk_storage_manager.h"
00006 #include "hybrid_storage_manager.h"
00007 #include "incremental_sgd.h"
00008 #include "postgres_util.h"
00009 #include "timer.h"
00010 #include "vectors.h"
00011 #include <vector>
00012 #include <map>
00013
00014
00018 template<class T>
00019 class Hybrid_Storage_Manager : Storage_Manager<T> {
00021 Ondisk_Storage_Manager<T> *_disk;
00023 Hazy_Database *_conn;
00025 struct _full_entity_entry {
00026 key k;
00027 T x;
00028 double eps;
00029 bool label;
00030 static int parse_tuple(PGresult *res, int index, _full_entity_entry &c) {
00031 c.k = atoi(PQgetvalue(res, index, 0));
00032 std::string f_vec = PQgetvalue(res, index, 1);
00033 c.eps = atof(PQgetvalue(res, index, 2));
00034 c.label = c.eps > 0;
00035 int r = c.x.fromPSQL(f_vec);
00036 return r;
00037 }
00038 static bool _full_entity_compare(const _full_entity_entry &x, const _full_entity_entry &y) {
00039 return x.eps < y.eps;
00040 }
00041 };
00042 typedef std::vector<struct _full_entity_entry> _tbuffer;
00043 typedef typename std::vector<struct _full_entity_entry>::iterator _tbuffer_it;
00044 _tbuffer _buffer;
00045
00046 typedef std::map<key, int> _tentitymap;
00047 _tentitymap _entity_map;
00048
00049
00050 bool _epsused;
00051 typedef std::map<key, double> _tepsmap;
00052
00053 typedef typename std::map<key,double>::iterator _tepsmap_iterator;
00054 _tepsmap _epsmap;
00055
00056 static int parse_eps_map(PGresult *res, int index, key &k, double &eps) {
00057 k = atoi(PQgetvalue(res, index, 0));
00058 eps = atof(PQgetvalue(res, index, 1));
00059 return 0;
00060 }
00061
00063 int _buffer_size;
00065 double eps_high, eps_low;
00067 _tbuffer_it low_water_it, high_water_it;
00068
00069 bool _lazy;
00074 bool isLazy() { return _lazy; }
00079 bool isEager() { return !isLazy(); }
00080
00083 int unique_id_for_ps;
00084
00086 typedef std::ostringstream prepared_stmt;
00088 prepared_stmt query_buffer_fill, query_eps_exec;
00089
00094 void calculateSpaceUsedByEpsMap(double &spaceUsed);
00099 void calculateSpaceUsedByBuffer(double &spaceUsed);
00104 void calculateSpaceUsedByEntityMap(double &spaceUsed);
00109 void calculateSpaceUsed(double &spaceUsed);
00110
00112 bool _force_monotone;
00113
00116 #ifdef __LOGGING
00117 int log_se_eps_hit, log_se_eps_miss;
00118 int log_se_buffer_hit, log_se_buffer_miss;
00119
00120 int log_iu_buffer_hit, log_iu_buffer_miss;
00121 #endif
00122
00123
00124 public:
00125
00134 Hybrid_Storage_Manager(Hazy_Database *db_conn, ondisk_storage_spec *spec, Ondisk_Storage_Manager<T> *disk, int nBuffer, bool bUseEps);
00139 void resort(struct hazy_model &m);
00145 void incrementalUpdate(struct hazy_model &m, double &waste_time);
00152 void insertEntity(struct hazy_model &m, key e, T f);
00159 void getEntityClass(key e, sClass &c, struct hazy_model &m);
00167 void getNumInClass(sClass c, int &nClass, struct hazy_model &m, double &waste_time);
00168 };
00169
00170 #include "hybrid_storage_manager.hxx"
00171
00172 #endif
00173