1 /* Copyright (C) 2004 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
16 
17 #include <ConfigValues.hpp>
18 #include <NdbOut.hpp>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #define CF_NODES     1
23 #define CF_LOG_PAGES 2
24 #define CF_MEM_PAGES 3
25 #define CF_START_TO  4
26 #define CF_STOP_TO   5
27 
print(Uint32 i,ConfigValues::ConstIterator & cf)28 void print(Uint32 i, ConfigValues::ConstIterator & cf){
29   ndbout_c("---");
30   for(Uint32 j = 2; j<=7; j++){
31     switch(cf.getTypeOf(j)){
32     case ConfigValues::IntType:
33       ndbout_c("Node %d : CFG(%d) : %d",
34 	       i, j, cf.get(j, 999));
35       break;
36     case ConfigValues::Int64Type:
37       ndbout_c("Node %d : CFG(%d) : %lld (64)",
38 	       i, j, cf.get64(j, 999));
39       break;
40     case ConfigValues::StringType:
41       ndbout_c("Node %d : CFG(%d) : %s",
42 	       i, j, cf.get(j, "<NOT FOUND>"));
43       break;
44     default:
45       ndbout_c("Node %d : CFG(%d) : TYPE: %d",
46 	       i, j, cf.getTypeOf(j));
47     }
48   }
49 }
50 
print(Uint32 i,ConfigValues & _cf)51 void print(Uint32 i, ConfigValues & _cf){
52   ConfigValues::ConstIterator cf(_cf);
53   print(i, cf);
54 }
55 
56 void
print(ConfigValues & _cf)57 print(ConfigValues & _cf){
58   ConfigValues::ConstIterator cf(_cf);
59   Uint32 i = 0;
60   while(cf.openSection(CF_NODES, i)){
61     print(i, cf);
62     cf.closeSection();
63     i++;
64   }
65 }
66 
67 inline
68 void
require(bool b)69 require(bool b){
70   if(!b)
71     abort();
72 }
73 
74 int
main(void)75 main(void){
76 
77   {
78     ConfigValuesFactory cvf(10, 20);
79     cvf.openSection(1, 0);
80     cvf.put(2, 12);
81     cvf.put64(3, 13);
82     cvf.put(4, 14);
83     cvf.put64(5, 15);
84     cvf.put(6, "Keso");
85     cvf.put(7, "Kent");
86     cvf.closeSection();
87 
88     cvf.openSection(1, 1);
89     cvf.put(2, 22);
90     cvf.put64(3, 23);
91     cvf.put(4, 24);
92     cvf.put64(5, 25);
93     cvf.put(6, "Kalle");
94     cvf.put(7, "Anka");
95     cvf.closeSection();
96 
97     ndbout_c("-- print --");
98     print(* cvf.m_cfg);
99 
100     cvf.shrink();
101     ndbout_c("shrink\n-- print --");
102     print(* cvf.m_cfg);
103     cvf.expand(10, 10);
104     ndbout_c("expand\n-- print --");
105     print(* cvf.m_cfg);
106 
107     ndbout_c("packed size: %d", cvf.m_cfg->getPackedSize());
108 
109     ConfigValues::ConstIterator iter(* cvf.m_cfg);
110     iter.openSection(CF_NODES, 0);
111     ConfigValues * cfg2 = ConfigValuesFactory::extractCurrentSection(iter);
112     print(99, * cfg2);
113 
114     cvf.shrink();
115     ndbout_c("packed size: %d", cfg2->getPackedSize());
116 
117     UtilBuffer buf;
118     Uint32 l1 = cvf.m_cfg->pack(buf);
119     Uint32 l2 = cvf.m_cfg->getPackedSize();
120     require(l1 == l2);
121 
122     ConfigValuesFactory cvf2;
123     require(cvf2.unpack(buf));
124     UtilBuffer buf2;
125     cvf2.shrink();
126     Uint32 l3 = cvf2.m_cfg->pack(buf2);
127     require(l1 == l3);
128 
129     ndbout_c("unpack\n-- print --");
130     print(* cvf2.m_cfg);
131 
132     cfg2->~ConfigValues();;
133     cvf.m_cfg->~ConfigValues();
134     free(cfg2);
135     free(cvf.m_cfg);
136   }
137   return 0;
138 }
139