source: tags/0.91/test_pool.cpp @ 9

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

willem

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