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