00001 #include <fstream>
00002 #include <stdlib.h>
00003 #include <iostream>
00004 #include <string>
00005 #include <list>
00006 #include <utility>
00007 #include "diskdatabase.hpp"
00008
00009 using namespace std;
00010
00011 void error_return(const char* msg)
00012 {
00013 cerr << msg;
00014 exit(3);
00015 }
00016
00017 class mystring
00018 {
00019 public:
00020 string my;
00021
00022 mystring() {}
00023 mystring(const string& s) : my(s) {}
00024
00025 istream& read(istream& is)
00026 {
00027 size_t t;
00028 char *nstr;
00029
00030 is.read(&t, sizeof(t));
00031 nstr = new char[t];
00032 is.read(nstr,t);
00033 my = string(nstr);
00034
00035 delete [] nstr;
00036 }
00037
00038 size_t DataSize() const
00039 {
00040 return sizeof(size_t) + my.length() + 1;
00041 }
00042
00043 ostream& write(ostream& os)
00044 {
00045 size_t t;
00046
00047 t = my.length() + 1;
00048
00049 os.write(&t,sizeof(t));
00050 os.write(my.c_str(),t);
00051 }
00052 };
00053
00054 int main(int argc, char *argv[])
00055 {
00056 int i;
00057 ID id;
00058 list<ID> m;
00059 list<ID>::iterator p;
00060 DiskDatabase<mystring> ms;
00061 DiskDatabase<mystring> ps;
00062 pair<mystring,bool> ret;
00063 mystring s;
00064 fstream outfile;
00065 fstream infile;
00066
00067 if(!ms.Create("/h/11/trokhim/work/code/Hello")) error_return("Could not create database");
00068
00069 for(i=argc-1; i > 0; i--)
00070 {
00071 s = string(argv[i]);
00072 id = ms.Add(s);
00073
00074 cout << "ID: " << id << " ";
00075 cout << "DATA: " << s.my << endl;
00076
00077 m.push_back(id);
00078 }
00079
00080 for(p = m.begin(); p != m.end(); p++)
00081 {
00082 ret = ms[*p];
00083
00084 if(!ret.second)
00085 cout << "Couldn't retrieve the record from the database" << endl;
00086 else
00087 {
00088 cout << "ID " << *p << " ";
00089 cout << "string " << ret.first.my << endl;
00090 }
00091 }
00092
00093 cout << endl << "Here we delete all" << endl;
00094
00095 ms.RemoveAll();
00096
00097 for(p = m.begin(); p != m.end(); p++)
00098 {
00099 ret = ms[*p];
00100
00101 if(!ret.second)
00102 cout << "Couldn't retrieve the record from the database" << endl;
00103 else
00104 {
00105 cout << "ID " << *p << " ";
00106 cout << "string " << ret.first.my << endl;
00107 }
00108 }
00109 m.clear();
00110
00111 cout << endl << "Here we read them all again" << endl;
00112
00113 for(i=argc-1; i > 0; i--)
00114 {
00115 s = string(argv[i]);
00116 id = ms.Add(s);
00117
00118 cout << "ID: " << id << " ";
00119 cout << "DATA: " << s.my << endl;
00120
00121 m.push_back(id);
00122 }
00123
00124 for(p = m.begin(); p != m.end(); p++)
00125 {
00126 ret = ms[*p];
00127
00128 if(!ret.second)
00129 cout << "Couldn't retrieve the record from the database" << endl;
00130 else
00131 {
00132 cout << "ID " << *p << " ";
00133 cout << "string " << ret.first.my << endl;
00134 }
00135 }
00136
00137 cout << endl << "ANd the second time" << endl;
00138
00139 for(i=argc-1; i > 0; i--)
00140 {
00141 s = string(argv[i]);
00142 id = ms.Add(s);
00143
00144 cout << "ID: " << id << " ";
00145 cout << "DATA: " << s.my << endl;
00146
00147 m.push_back(id);
00148 }
00149
00150 for(p = m.begin(); p != m.end(); p++)
00151 {
00152 ret = ms[*p];
00153
00154 if(!ret.second)
00155 cout << "Couldn't retrieve the record from the database" << endl;
00156 else
00157 {
00158 cout << "ID " << *p << " ";
00159 cout << "string " << ret.first.my << endl;
00160 }
00161 }
00162
00163 cout << endl << "Here we delete all the second time" << endl;
00164
00165 p = m.begin();
00166 for(; p != m.end(); p++) ms.Remove(*p);
00167
00168 for(p = m.begin(); p != m.end(); p++)
00169 {
00170 ret = ms[*p];
00171
00172 if(!ret.second)
00173 cout << "Couldn't retrieve the record from the database" << endl;
00174 else
00175 {
00176 cout << "ID " << *p << " ";
00177 cout << "string " << ret.first.my << endl;
00178 }
00179 }
00180
00181 m.clear();
00182
00183 cout << endl << "Here we read them all again" << endl;
00184
00185 for(i=argc-1; i > 0; i--)
00186 {
00187 s = string(argv[i]);
00188 id = ms.Add(s);
00189
00190 cout << "ID: " << id << " ";
00191 cout << "DATA: " << s.my << endl;
00192
00193 m.push_back(id);
00194 }
00195
00196 for(p = m.begin(); p != m.end(); p++)
00197 {
00198 ret = ms[*p];
00199
00200 if(!ret.second)
00201 cout << "Couldn't retrieve the record from the database" << endl;
00202 else
00203 {
00204 cout << "ID " << *p << " ";
00205 cout << "string " << ret.first.my << endl;
00206 }
00207 }
00208
00209 cout << endl << "Here we write and read" << endl;
00210
00211 outfile.open("/h/11/trokhim/work/code/mdtest.dat", ios::out | ios::binary);
00212 if(!outfile) error_return("Couldn't open output file");
00213
00214 ms.Write(outfile);
00215 ms.Close();
00216 if(!outfile) error_return("couldn't write to the output file");
00217
00218 outfile.close();
00219
00220 infile.open("/h/11/trokhim/work/code/mdtest.dat", ios::in | ios::nocreate | ios::binary);
00221
00222 if(!infile) error_return("Couldn't open input file");
00223
00224 ps.Read(infile);
00225 if(!infile) error_return("couldn't read from input file");
00226
00227 cout << "hey7" << endl;
00228 infile.close();
00229 cout << "hey8" << endl;
00230
00231 for(p = m.begin(); p != m.end(); p++)
00232 {
00233 ret = ps[*p];
00234
00235 if(!ret.second)
00236 cout << "Couldn't retrieve the record from the database" << endl;
00237 else
00238 {
00239 cout << "ID " << *p << " ";
00240 cout << "string " << ret.first.my << endl;
00241 }
00242 }
00243
00244 return 0;
00245 }