00001 #include "storage_manager.h"
00002 #include "globals.h"
00003 #include "timer.h"
00004 #include <fstream>
00005
00006
00010 template<class T>
00011 Storage_Manager<T>::~Storage_Manager() { }
00012
00013
00014
00020 template<class T>
00021 void
00022 Storage_Manager<T>::
00023 loadFromFile(struct hazy_model& hm, const char* szFileName) {
00024 DEBUG_ONLY(Timer t1(true););
00025 std::ifstream our_file(szFileName);
00026 int nExamples = 0;
00027 if(our_file.is_open()) {
00028 int id = 0;
00029 std::string line;
00030 while(! our_file.eof() && getline(our_file, line) ) {
00031 std::istringstream f(line + "\n");
00032 T x;
00033 f >> id >> x;
00034 VERBOSE_ONLY(std::cout << "got: " << id << " - " << x << " " << line << std::endl;);
00035 insertEntity(hm, id, x);
00036 nExamples++;
00037 }
00038 }
00039 DEBUG_ONLY(std::cout << "Loaded " << nExamples << " tuples in " << t1.stop() << "s" << std::endl;);
00040 }
00041
00046 template<class T>
00047 int
00048 Storage_Manager<T>::loadFromDatabase(struct hazy_model &hm) {
00049 if(db_conn == NULL) {
00050 fprintf(stderr, "Error: Entities cannot be loaded from database. Hazy_Database is NULL");
00051 return -1;
00052 }
00053 std::vector< sParse_IDvec<T> > _out;
00054 std::string query = "SELECT id, feature_vector FROM " + entity_table_name + ";";
00055 int ret_val = db_conn->postgresTupleParser(query.c_str(), sParse_IDvec<T>::parse_tuple, _out);
00056 if(ret_val < 0) {
00057 fprintf(stderr, "error in load from database method in storage manager: %s\n", entity_table_name.c_str());
00058 return -1;
00059 }
00060
00061 for(unsigned int i = 0; i < _out.size(); i ++) {
00062 insertEntity(hm, _out.at(i).id, _out.at(i).f);
00063 }
00064
00065 return ret_val;
00066 }