19 #include <ndb_global.h>
20 #include <SimpleProperties.hpp>
23 #include <UtilBuffer.hpp>
26 SimpleProperties::Writer::first(){
31 SimpleProperties::Writer::add(Uint16 key, Uint32 value){
32 Uint32 head = Uint32Value;
35 if(!putWord(htonl(head)))
38 return putWord(htonl(value));
42 SimpleProperties::Writer::add(
const char * value,
int len){
43 const Uint32 valLen = (len + 3) / 4;
46 return putWords((Uint32*)value, valLen);
48 const Uint32 putLen= valLen - 1;
49 if (!putWords((Uint32*)value, putLen))
61 return putWord(tmp.lastWord);
65 SimpleProperties::Writer::add(Uint16 key,
const char * value){
66 Uint32 head = StringValue;
69 if(!putWord(htonl(head)))
71 Uint32 strLen = Uint32(strlen(value) + 1);
72 if(!putWord(htonl(strLen)))
75 return add(value, (
int)strLen);
80 SimpleProperties::Writer::add(Uint16 key,
const void* value,
int len){
81 Uint32 head = BinaryValue;
84 if(!putWord(htonl(head)))
86 if(!putWord(htonl(len)))
89 return add((
const char*)value, len);
92 SimpleProperties::Reader::Reader(){
110 return m_type != InvalidValue;
143 SimpleProperties::Reader::getString(
char * dst)
const {
144 if(peekWords((Uint32*)dst, m_itemLen))
150 SimpleProperties::Reader::readValue(){
151 if(!step(m_itemLen)){
152 m_type = InvalidValue;
158 m_type = InvalidValue;
163 m_key = tmp & 0xFFFF;
168 if(!peekWord(&m_ui32_value))
170 m_ui32_value = ntohl(m_ui32_value);
176 m_strLen = ntohl(tmp);
177 m_itemLen = (m_strLen + 3)/4;
181 m_type = InvalidValue;
187 SimpleProperties::unpack(Reader & it,
void * dst,
188 const SP2StructMapping _map[], Uint32 mapSz,
190 bool ignoreUnknownKeys){
196 Uint16 key = it.getKey();
197 for(Uint32
i = 0;
i<mapSz;
i++){
198 if(key == _map[
i].
Key){
200 if(_map[
i].Type == InvalidValue)
202 if(_map[
i].Type != it.getValueType())
205 char * _dst = (
char *)dst;
206 _dst += _map[
i].Offset;
208 switch(it.getValueType()){
210 const Uint32 val = it.getUint32();
212 if(val < _map[
i].minValue)
214 if(val > _map[
i].maxValue)
217 * ((Uint32 *)_dst) = val;
222 unsigned len = it.getValueLen();
223 if(len < _map[
i].minValue)
225 if(len > _map[
i].maxValue)
236 if(!found && !ignoreUnknownKeys)
244 SimpleProperties::pack(Writer & it,
const void * __src,
245 const SP2StructMapping _map[], Uint32 mapSz,
248 const char * _src = (
const char *)__src;
250 for(Uint32
i = 0;
i<mapSz;
i++){
252 const char * src = _src + _map[
i].Offset;
253 switch(_map[
i].Type){
254 case SimpleProperties::InvalidValue:
257 case SimpleProperties::Uint32Value:{
258 Uint32 val = * ((Uint32*)src);
260 if(val < _map[
i].minValue)
262 if(val > _map[
i].maxValue)
265 ok = it.add(_map[
i].Key, val);
268 case SimpleProperties::BinaryValue:{
269 const char * src_len = _src + _map[
i].Length_Offset;
270 Uint32 len = *((Uint32*)src_len);
272 if(len > _map[
i].maxValue)
275 ok = it.add(_map[
i].Key, src, len);
278 case SimpleProperties::StringValue:
280 size_t len = strlen(src);
281 if(len > _map[
i].maxValue)
284 ok = it.add(_map[
i].Key, src);
297 for(first(); valid(); next()){
298 switch(getValueType()){
299 case SimpleProperties::Uint32Value:
300 ndbout <<
"Key: " << getKey()
301 <<
" value(" << getValueLen() <<
") : "
302 << getUint32() << endl;
304 case SimpleProperties::BinaryValue:
305 case SimpleProperties::StringValue:
306 if(getValueLen() < 1024){
308 ndbout <<
"Key: " << getKey()
309 <<
" value(" << getValueLen() <<
") : "
310 <<
"\"" << tmp <<
"\"" << endl;
312 ndbout <<
"Key: " << getKey()
313 <<
" value(" << getValueLen() <<
") : "
314 <<
"\"" <<
"<TOO LONG>" <<
"\"" << endl;
319 ndbout <<
"Unknown type for key: " << getKey()
320 <<
" type: " << (Uint32)getValueType() << endl;
325 SimplePropertiesLinearReader::SimplePropertiesLinearReader
326 (
const Uint32 * src, Uint32 len){
334 SimplePropertiesLinearReader::reset() {
339 SimplePropertiesLinearReader::step(Uint32 len){
341 return m_pos < m_len;
345 SimplePropertiesLinearReader::getWord(Uint32 * dst) {
347 * dst = m_src[m_pos++];
354 SimplePropertiesLinearReader::peekWord(Uint32 * dst)
const {
356 * dst = m_src[m_pos];
363 SimplePropertiesLinearReader::peekWords(Uint32 * dst, Uint32 len)
const {
364 if(m_pos + len <= m_len){
365 memcpy(dst, &m_src[m_pos], 4 * len);
371 LinearWriter::LinearWriter(Uint32 * src, Uint32 len){
377 bool LinearWriter::reset() { m_pos = 0;
return m_len > 0;}
380 LinearWriter::putWord(Uint32 val){
382 m_src[m_pos++] = val;
389 LinearWriter::putWords(
const Uint32 * src, Uint32 len){
390 if(m_pos + len <= m_len){
391 memcpy(&m_src[m_pos], src, 4 * len);
399 LinearWriter::getWordsUsed()
const {
return m_pos;}
401 UtilBufferWriter::UtilBufferWriter(
UtilBuffer & b)
407 bool UtilBufferWriter::reset() { m_buf.clear();
return true;}
410 UtilBufferWriter::putWord(Uint32 val){
411 return (m_buf.append(&val, 4) == 0);
415 UtilBufferWriter::putWords(
const Uint32 * src, Uint32 len){
416 return (m_buf.append(src, 4 * len) == 0);
421 UtilBufferWriter::getWordsUsed()
const {
return m_buf.length() / 4;}
424 LinearPagesReader::LinearPagesReader(
const Uint32 * base,
431 m_noOfPages = noOfPages;
432 m_pageHeaderSz = headerSize;
438 LinearPagesReader::reset() { m_pos = 0;}
441 LinearPagesReader::step(Uint32 len){
443 return m_pos < m_len;
447 LinearPagesReader::getWord(Uint32 * dst) {
449 * dst = m_base[getPos(m_pos++)];
456 LinearPagesReader::peekWord(Uint32 * dst)
const {
458 * dst = m_base[getPos(m_pos)];
465 LinearPagesReader::peekWords(Uint32 * dst, Uint32 len)
const {
466 if(m_pos + len <= m_len){
467 for(Uint32
i = 0;
i<len;
i++)
468 * (dst +
i) = m_base[getPos(m_pos +
i)];
475 LinearPagesReader::getPos(Uint32 pos)
const {
476 const Uint32 sz = (m_pageSz - m_pageHeaderSz);
477 Uint32 no = pos / sz;
478 Uint32 in = pos % sz;
479 return no * m_pageSz + m_pageHeaderSz + in;
482 LinearPagesWriter::LinearPagesWriter(Uint32 * base,
488 m_noOfPages = noOfPages;
489 m_pageHeaderSz = headerSize;
490 m_len = noOfPages * (pageSize - headerSize);
495 LinearPagesWriter::putWord(Uint32 val){
497 m_base[getPos(m_pos++)] = val;
504 LinearPagesWriter::putWords(
const Uint32 * src, Uint32 len){
505 if(m_pos + len <= m_len){
506 for(Uint32
i = 0;
i<len;
i++)
507 m_base[getPos(m_pos++)] = src[
i];
515 LinearPagesWriter::getWordsUsed()
const {
516 return getPos(m_pos);
521 LinearPagesWriter::getPagesUsed()
const {
522 return m_pos / (m_pageSz - m_pageHeaderSz);
526 LinearPagesWriter::getPos(Uint32 pos)
const {
527 const Uint32 sz = (m_pageSz - m_pageHeaderSz);
528 Uint32 no = pos / sz;
529 Uint32 in = pos % sz;
530 return no * m_pageSz + m_pageHeaderSz + in;