summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/util/testConfigValues/testConfigValues.cpp
blob: b887808c62eab69d6e383ea1741bc7fbf3d18be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#include <ConfigValues.hpp>
#include <NdbOut.hpp>
#include <stdlib.h>
#include <string.h>

#define CF_NODES     1
#define CF_LOG_PAGES 2
#define CF_MEM_PAGES 3
#define CF_START_TO  4
#define CF_STOP_TO   5

void print(Uint32 i, ConfigValues::ConstIterator & cf){
  ndbout_c("---");
  for(Uint32 j = 2; j<=7; j++){
    switch(cf.getTypeOf(j)){
    case ConfigValues::IntType:
      ndbout_c("Node %d : CFG(%d) : %d", 
	       i, j, cf.get(j, 999));
      break;
    case ConfigValues::Int64Type:
      ndbout_c("Node %d : CFG(%d) : %lld (64)", 
	       i, j, cf.get64(j, 999));
      break;
    case ConfigValues::StringType:
      ndbout_c("Node %d : CFG(%d) : %s",
	       i, j, cf.get(j, "<NOT FOUND>"));
      break;
    default:
      ndbout_c("Node %d : CFG(%d) : TYPE: %d",
	       i, j, cf.getTypeOf(j));
    }
  }
}

void print(Uint32 i, ConfigValues & _cf){
  ConfigValues::ConstIterator cf(_cf);
  print(i, cf);
}

void
print(ConfigValues & _cf){
  ConfigValues::ConstIterator cf(_cf);
  Uint32 i = 0;
  while(cf.openSection(CF_NODES, i)){
    print(i, cf);
    cf.closeSection();
    i++;
  }
}

inline
void
require(bool b){
  if(!b)
    abort();
}

int
main(void){

  {
    ConfigValuesFactory cvf(10, 20);
    cvf.openSection(1, 0);
    cvf.put(2, 12);
    cvf.put64(3, 13);
    cvf.put(4, 14);
    cvf.put64(5, 15);
    cvf.put(6, "Keso");
    cvf.put(7, "Kent");
    cvf.closeSection();

    cvf.openSection(1, 1);
    cvf.put(2, 22);
    cvf.put64(3, 23);
    cvf.put(4, 24);
    cvf.put64(5, 25);
    cvf.put(6, "Kalle");
    cvf.put(7, "Anka");
    cvf.closeSection();
  
    ndbout_c("-- print --");
    print(* cvf.m_cfg);

    cvf.shrink();
    ndbout_c("shrink\n-- print --");
    print(* cvf.m_cfg);
    cvf.expand(10, 10);
    ndbout_c("expand\n-- print --");
    print(* cvf.m_cfg);

    ndbout_c("packed size: %d", cvf.m_cfg->getPackedSize());

    ConfigValues::ConstIterator iter(* cvf.m_cfg);
    iter.openSection(CF_NODES, 0);
    ConfigValues * cfg2 = ConfigValuesFactory::extractCurrentSection(iter);
    print(99, * cfg2);
  
    cvf.shrink();
    ndbout_c("packed size: %d", cfg2->getPackedSize());

    UtilBuffer buf;
    Uint32 l1 = cvf.m_cfg->pack(buf);
    Uint32 l2 = cvf.m_cfg->getPackedSize();
    require(l1 == l2);
  
    ConfigValuesFactory cvf2;
    require(cvf2.unpack(buf));
    UtilBuffer buf2;
    cvf2.shrink();
    Uint32 l3 = cvf2.m_cfg->pack(buf2);
    require(l1 == l3);
  
    ndbout_c("unpack\n-- print --");
    print(* cvf2.m_cfg);

    cfg2->~ConfigValues();;
    cvf.m_cfg->~ConfigValues();
    free(cfg2);
    free(cvf.m_cfg);
  }
  return 0;
}