source: tags/0.55/test_pool.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 15.7 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <string>
4#include "gdbm_pool.h"
5#include "flatfile_pool.h"
6#include "files_pool.h"
7#include "mysql_pool.h"
8#include <vector>
9#include <stdlib.h>
10#include "wutils.h"
11#include "clocks.h"
12
13#include "stopos_key.h"
14int testkeysetget()
15{
16  stopos_key k;
17  longuint n = k.get<longuint>();
18  if (n != 0)
19  {
20    std::cerr << "Keytesterror new key get: "<< n <<std::endl;
21    return 1;
22  }
23
24  n = 1234567890123456;
25  k.set(n);
26  longuint l = k.get<longuint>();
27  if (l != n)
28  {
29    std::cerr << "Keytesterror expect: " << n<< ":" << " got:" << l << std::endl;
30    return 1;
31  }
32  return 0;
33}
34 
35
36int testpool(bool dumpdb,bool openclose,int which)
37{
38#define DUMPDB if (dumpdb) {std::cerr << __LINE__ <<"dump\n"; p->dump_db();}
39#define OPENCLOSE \
40  if (openclose) \
41  {\
42     p->close_db();\
43     rc = p->open_db();\
44     if(rc)\
45     {\
46        std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db:" << rc << std::endl;\
47        return rc;\
48     }\
49   }
50
51  stopos_pool *p;
52  std::string dbname;
53  switch (which)
54  {
55    case 1: dbname = "/tmp/testdb_gdbm";
56            p = new gdbm_pool();
57            break;
58    case 2: dbname = "/tmp/testdb_flat";
59            p = new flatfile_pool();
60            break;
61    case 3: dbname = "/tmp/testdb_dir";
62            p = new files_pool();
63            break;
64    case 4: dbname = "testdb";
65            p = new mysql_pool();
66            break;
67    default: std::cerr << __LINE__ << "INVALID WHICH" << std::endl;
68             return 6;
69  }
70
71  std::vector <std::string> lines;
72  std::vector <std::string> keys;
73
74  std::string linesfile = "/tmp/linesfile";
75  std::string command;
76  command="man bash 2>/dev/null | head -n 100  > "+linesfile;
77  int rc;
78  rc = system(command.c_str());
79  if (rc != 0)
80    std::cerr << "Iets niet helemaal goed met man ... \n";
81  std::ifstream infile;
82  infile.open(linesfile.c_str());
83
84  int ntest = 0;
85  while (infile)
86  {
87    ntest++;
88    std::string line;
89    getline(infile,line);
90    lines.push_back(NumberToString(ntest)+":"+line);
91  }
92
93  p->set_db_name(dbname);
94  rc = p->create_db();
95  if (rc !=0)
96  {
97    std::cerr << __FILE__<<":"<<__LINE__<<": cannot create db:" << rc << std::endl;
98    return rc;
99  }
100  if (which ==0)
101  {
102    std::cerr << "Ja NU!\n";
103    sleep (5);
104    std::cerr << "continuing \n";
105  }
106  rc = p->open_db();
107  if (rc !=0)
108  {
109    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
110    return 7;
111  }
112
113  for (int i=0; i<ntest; i++)
114  {
115    keys.resize(0);
116    int rc;
117    std::string key;
118    OPENCLOSE
119    rc = p->add_line(lines[i],key);
120    if (rc!=0) 
121    {
122      p->close_db();
123      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << ":" << rc << std::endl;
124      return rc;
125    }
126    keys.push_back(key);
127  }
128  p->close_db();
129  rc = p->open_db();
130  if (rc !=0)
131  {
132    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
133    return rc;
134  }
135  DUMPDB
136  p->remove_line("4");
137  DUMPDB
138  p->remove_line("2");
139  DUMPDB
140  p->remove_line("3");
141  DUMPDB
142  p->remove_line("1");
143  DUMPDB
144  std::string line;
145  longuint m;
146  std::string getkey;
147  p->get_line(line,m,getkey);
148
149  p->create_db();
150  rc = p->open_db();
151  if (rc !=0)
152  {
153    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
154    return rc;
155  }
156
157  keys.resize(0);
158  for (int i=0; i<ntest; i++)
159  {
160    int rc;
161    std::string key;
162    OPENCLOSE
163    rc = p->add_line(lines[i],key);
164    if (rc!=0) 
165    {
166      p->close_db();
167      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":add_line '"<<lines[i]<<"' number: " << i << std::endl;
168      return rc;
169    }
170    keys.push_back(key);
171  }
172  DUMPDB
173
174  // try to read all lines:
175  p->close_db();
176  delete p;
177  switch (which)
178  {
179    case 1: p = new gdbm_pool();
180            break;
181    case 2: p = new flatfile_pool();
182            break;
183    case 3: p = new files_pool();
184            break;
185    case 4: p = new mysql_pool();
186            break;
187    default: std::cerr << __LINE__ << "INVALID WHICH" << std::endl;
188             return 6;
189  }
190  p->set_db_name(dbname);
191  rc = p->open_db();
192  if (rc !=0)
193  {
194    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db:"<<rc<<"\n";
195    return rc;
196  }
197
198  for (int i=0; i<ntest; i++)
199  {
200    std::string line,getkey;
201    int rc;
202    longuint m;
203    OPENCLOSE
204    rc = p->get_line(line,m,getkey);
205    DUMPDB
206
207    if (rc!=0) 
208    {
209      p->close_db();
210      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
211      return rc;
212    }
213    if (m != 0)
214    {
215      p->close_db();
216      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
217      return 9;
218    }
219    if (line != lines[i])
220    {
221      p->close_db();
222      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
223      return 9;
224    }
225  }
226
227  DUMPDB
228  // test if no further lines can be read
229  for (int i=0; i<10; i++)
230  {
231    std::string line,getkey;
232    int rc;
233    longuint m;
234    OPENCLOSE
235    rc = p->get_line(line,m,getkey);
236    if (rc == 0)
237    {
238      p->close_db();
239      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc <<std::endl;
240      return 9;
241    }
242  }
243  p->close_db();
244  rc = p->open_db();
245  if (rc !=0)
246  {
247    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
248    return rc;
249  }
250
251  DUMPDB
252  for (int i=0; i<ntest; i++)
253  {
254    int rc;
255    OPENCLOSE
256    rc = p->remove_line(keys[i]);
257    DUMPDB
258    if (rc != 0)
259    {
260      p->close_db();
261      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
262      return 9;
263    }
264  }
265  p->close_db();
266  rc = p->open_db();
267  if (rc !=0)
268  {
269    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
270    return rc;
271  }
272  for (int i=0; i<ntest; i++)
273  {
274    int rc;
275    OPENCLOSE
276    rc = p->remove_line(keys[i]);
277    if (rc == 0)
278    {
279      p->close_db();
280      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":can remove removed key " << "'"<< keys[i]<<"'" <<std::endl;
281      return 9;
282    }
283  }
284  p->close_db();
285  p->open_db();
286  p->set_kvp(0);
287
288  // typical use: fill database with lines,
289  // read and remove
290
291  {
292    for (int i=0; i<ntest; i++)
293    {
294      int rc;
295      std::string addkey;
296      OPENCLOSE
297      rc = p->add_line(lines[i],addkey);
298      if (rc!=0) 
299      {
300        p->close_db();
301        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << ":"<<rc<<std::endl;
302        return rc;
303      }
304    }
305    for (int i=0; i<ntest; i++)
306    {
307      std::string line,getkey;
308      int rc;
309      longuint m;
310      OPENCLOSE
311      rc = p->get_line(line,m,getkey);
312      DUMPDB
313      if (rc!=0) 
314      {
315        p->close_db();
316        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
317        return rc;
318      }
319      if (m != 0)
320      {
321        p->close_db();
322        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
323        return 9;
324      }
325      if (line != lines[i])
326      {
327        p->close_db();
328        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
329        return 9;
330      }
331      OPENCLOSE
332      p->remove_line(getkey);
333      if (rc !=0)
334      {
335        p->close_db();
336        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
337        return 9;
338      }
339    }
340    DUMPDB
341    // test if no further lines can be read
342    for (int i=0; i<10; i++)
343    {
344      std::string line,getkey;
345      int rc;
346      longuint m;
347      OPENCLOSE
348      rc = p->get_line(line,m,getkey);
349      if (rc == 0)
350      {
351        p->close_db();
352        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc <<std::endl;
353        return 9;
354      }
355    }
356  }
357  p->close_db();
358  p->open_db();
359  p->set_kvp(1);
360  keys.resize(0);
361  // kvp test
362  for (int i=0; i<ntest; i++)
363  {
364    int rc;
365    std::string key;
366    OPENCLOSE
367    rc = p->add_line(lines[i],key);
368    if (rc!=0) 
369    {
370      p->close_db();
371      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << std::endl;
372      return rc;
373    }
374    keys.push_back(key);
375  }
376  for (int i=0; i<ntest*3; i++)
377  {
378    std::string line,getkey;
379    int rc;
380    longuint m;
381    OPENCLOSE
382    rc = p->get_line(line,m,getkey);
383    if (rc!=0) 
384    {
385      p->close_db();
386      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
387      return rc;
388    }
389    if (m != (longuint)i/ntest)
390    {
391      p->close_db();
392      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: "<<i/ntest<<" got:" << m <<std::endl;
393      return 9;
394    }
395    if (line != lines[i%ntest])
396    {
397      p->close_db();
398      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
399      return 9;
400    }
401  }
402
403  p->close_db();
404  p->create_db();
405  p->open_db();
406
407  for (int i=0; i<10; i++)
408  {
409    std::string line,getkey;
410    int rc;
411    longuint m;
412    OPENCLOSE
413    rc = p->get_line(line,m,getkey);
414    if (rc == 0)
415    {
416      p->close_db();
417      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc << " line: '" << line << "'" <<std::endl;
418      return 9;
419    }
420  }
421
422  {
423    // test remove lines
424    p->set_kvp(0);
425    keys.resize(0);
426    for (int i=0; i<ntest; i++)
427    {
428      int rc;
429      std::string key;
430      OPENCLOSE
431      rc = p->add_line(lines[i],key);
432      if (rc!=0) 
433      {
434        p->close_db();
435        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << std::endl;
436        return rc;
437      }
438      keys.push_back(key);
439    }
440    //remove half of lines
441    DUMPDB
442    for (int i=0; i<ntest; i+=2)
443    {
444      OPENCLOSE
445      int rc = p->remove_line(keys[i]);
446      if (rc !=0)
447      {
448        p->close_db();
449        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
450        return 9;
451      }
452    }
453    // try to get other half
454    DUMPDB
455    for (int i=1; i<ntest; i+=2)
456    {
457      int rc;
458      longuint m;
459      std::string line,getkey;
460      OPENCLOSE
461      rc = p->get_line(line,m,getkey);
462      if (rc!=0) 
463      {
464        p->close_db();
465        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
466        return rc;
467      }
468      if (m != 0)
469      {
470        p->close_db();
471        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
472        return 9;
473      }
474      if (line != lines[i])
475      {
476        p->close_db();
477        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
478        return 9;
479      }
480    }
481    // there should be no lines left, check:
482    int nrem = 0;
483    longuint m; 
484    std::string line,getkey;
485    while (p->get_line(line,m,getkey)==0)
486      nrem++;
487
488    if (nrem != 0)
489    {
490      p->close_db();
491      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << nrem << " line: '" << line << "'" <<std::endl;
492      return 9;
493    }
494  }
495  p->close_db();
496  p->create_db();
497  p->open_db();
498  keys.resize(0);
499
500  {
501    // test the following
502    // after an add_line, the next line to be retrieved should be
503    // that line, unless there were already other lines not
504    // retrieved yet
505    std::string line,key,getkey;
506    int rc;
507    for (int i=0; i<ntest/2; i++)
508    {
509      OPENCLOSE
510      p->add_line(lines[i],key);
511      keys.push_back(key);
512    }
513    longuint m;
514    int l=2;
515    for (int i=0; i<l; i++)
516    {
517      OPENCLOSE
518      p->get_line(line,m,getkey);
519    }
520    DUMPDB
521    // the next line to be retrieved should be lines[l]
522    // first, we add a new line
523      OPENCLOSE
524    p->add_line(lines[ntest/2],key);
525    DUMPDB
526    keys.push_back(key);
527    //check now which line we get:
528      OPENCLOSE
529    p->get_line(line,m,getkey);
530    if (line != lines[l])
531    {
532      p->close_db();
533      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[l]<<"' got:'" << line <<"'"<<std::endl;
534      return 9;
535    }
536    // now retrieve all lines and add a new one. Can we retrieve that:
537    while (p->get_line(line,m,getkey) == 0);
538    DUMPDB
539      OPENCLOSE
540    p->add_line(lines[0],key);
541    DUMPDB
542      OPENCLOSE
543    rc = p->get_line(line,m,getkey);
544    if (rc != 0)
545    {
546      p->close_db();
547      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
548      return rc;
549    }
550    if (line != lines[0])
551    {
552      p->close_db();
553      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
554      return 9;
555    }
556  }
557
558  p->create_db();
559  p->open_db();
560  {
561    std::string key,line,getkey;
562    longuint m;
563    int rc;
564    // some delete and insert tests
565    // put only one line in the database, and remove it
566    OPENCLOSE
567    rc = p->add_line(lines[0],key);
568    if (rc != 0)
569    {
570      p->close_db();
571      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":add_line\n";
572      return rc;
573    }
574
575    OPENCLOSE
576    rc = p->remove_line(key);
577    if (rc != 0)
578    {
579      p->close_db();
580      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
581      return rc;
582    }
583    OPENCLOSE
584    rc = p->get_line(line,m,getkey);
585    if (rc == 0)
586    {
587      p->close_db();
588      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line" <<
589        "expected !0, got:"<<rc <<"\n";
590      return rc;
591    }
592    // add two lines, remove last one, we expect that get_line
593    // gets the first
594    OPENCLOSE
595    p->add_line(lines[0],key);
596    OPENCLOSE
597    p->add_line(lines[1],key);
598    OPENCLOSE
599    rc = p->remove_line(key);
600    if (rc != 0)
601    {
602      p->close_db();
603      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
604      return rc;
605    }
606    OPENCLOSE
607    rc = p->get_line(line,m,getkey);
608    if (rc != 0)
609    {
610      p->close_db();
611      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
612      return rc;
613    }
614    if (line != lines[0])
615    {
616      p->close_db();
617      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
618      return 9;
619    }
620    // add two lines, remove first one, we expect that get_line
621    // gets the last
622    p->close_db();
623    p->create_db();
624    p->open_db();
625    std::string key0;
626    OPENCLOSE
627    p->add_line(lines[0],key0);
628    OPENCLOSE
629    p->add_line(lines[1],key);
630    rc = p->remove_line(key0);
631    if (rc != 0)
632    {
633      p->close_db();
634      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
635      return rc;
636    }
637    OPENCLOSE
638    rc = p->get_line(line,m,getkey);
639    if (rc != 0)
640    {
641      p->close_db();
642      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
643      return rc;
644    }
645    if (line != lines[1])
646    {
647      p->close_db();
648      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
649      return 9;
650    }
651  }
652
653  p->close_db();
654  return 0;
655}
656int main()
657{
658  double t0;
659  bool dumpen = 0;
660  bool openclose = 1;
661  std::cerr << "running keytest .... ";
662  if (testkeysetget()==0)
663    std::cerr << "ok\n";
664  else
665  {
666    std::cerr << "NOT OK\n";
667  }
668
669  std::cerr << "running gdbm ....... ";
670  t0=wallclock();
671  if (testpool(dumpen,openclose,1) == 0)
672    std::cerr << "ok " << wallclock()-t0 << std::endl;
673  else
674  {
675    std::cerr << "NOT OK\n";
676  }
677  std::cerr << "running flatfile ... ";
678  t0=wallclock();
679  if (testpool(dumpen,openclose,2) == 0)
680    std::cerr << "ok " << wallclock()-t0 << std::endl;
681  else
682  {
683    std::cerr << "NOT OK\n";
684  }
685  std::cerr << "running files ...... ";
686  t0=wallclock();
687  if (testpool(dumpen,openclose,3) == 0)
688    std::cerr << "ok " << wallclock()-t0 << std::endl;
689  else
690  {
691    std::cerr << "NOT OK\n";
692  }
693  std::cerr << "running mysql ...... ";
694  t0=wallclock();
695  if (testpool(dumpen,openclose,4) == 0)
696    std::cerr << "ok " << wallclock() -t0 << std::endl;
697  else
698  {
699    std::cerr << "NOT OK\n";
700  }
701  std::cerr << "all is well that ends well" << std::endl;
702  return 0;
703}
704
Note: See TracBrowser for help on using the repository browser.