source: tags/0.92/test_pool.cpp @ 19

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