| Apache Ignite C++

binary_containers.h
Go to the documentation of this file.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. 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
23 #ifndef _IGNITE_BINARY_BINARY_CONTAINERS
24 #define _IGNITE_BINARY_BINARY_CONTAINERS
25
26 #include <stdint.h>
27
28 #include <ignite/common/utils.h>
29
30 #include "ignite/impl/binary/binary_writer_impl.h"
31 #include "ignite/impl/binary/binary_reader_impl.h"
32 #include "ignite/binary/binary_consts.h"
33
34 namespace ignite
35 {
36 namespace binary
37 {
48 class IGNITE_IMPORT_EXPORT BinaryStringArrayWriter
49 {
50 public:
58 BinaryStringArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id);
59
67 void Write(const char* val);
68
77 void Write(const char* val, int32_t len);
78
86 void Write(const std::string& val)
87 {
88 Write(val.c_str());
89 }
90
99 void Close();
100
101 private:
103 impl::binary::BinaryWriterImpl* impl;
104
106 const int32_t id;
107 };
108
120 template<typename T>
121 class IGNITE_IMPORT_EXPORT BinaryArrayWriter
122 {
123 public:
131 BinaryArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
132 impl(impl), id(id)
133 {
134 // No-op.
135 }
136
144 void Write(const T& val)
145 {
146 impl->WriteElement<T>(id, val);
147 }
148
157 void Close()
158 {
159 impl->CommitContainer(id);
160 }
161
162 private:
164 impl::binary::BinaryWriterImpl* impl;
165
167 const int32_t id;
168 };
169
181 template<typename T>
182 class IGNITE_IMPORT_EXPORT BinaryCollectionWriter
183 {
184 public:
192 BinaryCollectionWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
193 impl(impl), id(id)
194 {
195 // No-op.
196 }
197
205 void Write(const T& val)
206 {
207 impl->WriteElement<T>(id, val);
208 }
209
218 void Close()
219 {
220 impl->CommitContainer(id);
221 }
222 private:
224 impl::binary::BinaryWriterImpl* impl;
225
227 const int32_t id;
228 };
229
240 template<typename K, typename V>
241 class IGNITE_IMPORT_EXPORT BinaryMapWriter
242 {
243 public:
251 BinaryMapWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
252 impl(impl), id(id)
253 {
254 // No-op.
255 }
256
265 void Write(const K& key, const V& val)
266 {
267 impl->WriteElement<K, V>(id, key, val);
268 }
269
277 void Close()
278 {
279 impl->CommitContainer(id);
280 }
281 private:
283 impl::binary::BinaryWriterImpl* impl;
284
286 const int32_t id;
287 };
288
297 class IGNITE_IMPORT_EXPORT BinaryStringArrayReader
298 {
299 public:
308 BinaryStringArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size);
309
315 bool HasNext();
316
330 int32_t GetNext(char* res, int32_t len);
331
339 std::string GetNext()
340 {
341 int32_t len = GetNext(NULL, 0);
342
343 if (len != -1)
344 {
345 ignite::common::FixedSizeArray<char> arr(len + 1);
346
347 GetNext(arr.GetData(), static_cast<int32_t>(arr.GetSize()));
348
349 return std::string(arr.GetData());
350 }
351 else
352 return std::string();
353 }
354
360 int32_t GetSize() const;
361
367 bool IsNull() const;
368
369 private:
371 impl::binary::BinaryReaderImpl* impl;
372
374 const int32_t id;
375
377 const int32_t size;
378 };
379
388 template<typename T>
389 class BinaryArrayReader
390 {
391 public:
400 BinaryArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size) :
401 impl(impl), id(id), size(size)
402 {
403 // No-op.
404 }
405
411 bool HasNext()
412 {
413 return impl->HasNextElement(id);
414 }
415
423 T GetNext()
424 {
425 return impl->ReadElement<T>(id);
426 }
427
433 int32_t GetSize()
434 {
435 return size;
436 }
437
443 bool IsNull()
444 {
445 return size == -1;
446 }
447 private:
449 impl::binary::BinaryReaderImpl* impl;
450
452 const int32_t id;
453
455 const int32_t size;
456 };
457
467 template<typename T>
468 class BinaryCollectionReader
469 {
470 public:
480 BinaryCollectionReader(impl::binary::BinaryReaderImpl* impl, int32_t id,
481 const CollectionType::Type type, int32_t size) : impl(impl), id(id), type(type), size(size)
482 {
483 // No-op.
484 }
485
491 bool HasNext()
492 {
493 return impl->HasNextElement(id);
494 }
495
503 T GetNext()
504 {
505 return impl->ReadElement<T>(id);
506 }
507
514 CollectionType::Type GetType()
515 {
516 return type;
517 }
518
524 int32_t GetSize()
525 {
526 return size;
527 }
528
534 bool IsNull()
535 {
536 return size == -1;
537 }
538 private:
540 impl::binary::BinaryReaderImpl* impl;
541
543 const int32_t id;
544
546 const CollectionType::Type type;
547
549 const int32_t size;
550 };
551
560 template<typename K, typename V>
561 class BinaryMapReader
562 {
563 public:
573 BinaryMapReader(impl::binary::BinaryReaderImpl* impl, int32_t id, MapType::Type type,
574 int32_t size) : impl(impl), id(id), type(type), size(size)
575 {
576 // No-op.
577 }
578
584 bool HasNext()
585 {
586 return impl->HasNextElement(id);
587 }
588
599 void GetNext(K& key, V& val)
600 {
601 return impl->ReadElement<K, V>(id, key, val);
602 }
603
610 MapType::Type GetType()
611 {
612 return type;
613 }
614
620 int32_t GetSize()
621 {
622 return size;
623 }
624
630 bool IsNull()
631 {
632 return size == -1;
633 }
634 private:
636 impl::binary::BinaryReaderImpl* impl;
637
639 const int32_t id;
640
642 const MapType::Type type;
643
645 const int32_t size;
646 };
647 }
648 }
649
650 #endif //_IGNITE_BINARY_BINARY_CONTAINERS
ignite::binary::BinaryMapWriter::BinaryMapWriter
BinaryMapWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:251
ignite::binary::BinaryArrayWriter::Write
void Write(const T &val)
Write a value.
Definition: binary_containers.h:144
ignite::binary::BinaryArrayReader::GetNext
T GetNext()
Read next element.
Definition: binary_containers.h:423
binary_consts.h
Declares specific binary constatants.
ignite::binary::BinaryArrayReader::IsNull
bool IsNull()
Check whether array is NULL.
Definition: binary_containers.h:443
ignite::binary::BinaryStringArrayReader
Binary string array reader.
Definition: binary_containers.h:297
ignite::binary::BinaryCollectionWriter::BinaryCollectionWriter
BinaryCollectionWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:192
ignite::binary::BinaryMapReader
Binary map reader.
Definition: binary_containers.h:561
ignite::binary::BinaryCollectionReader::GetType
CollectionType::Type GetType()
Get collection type.
Definition: binary_containers.h:514
ignite::binary::BinaryCollectionReader
Binary collection reader.
Definition: binary_containers.h:468
ignite::binary::BinaryCollectionWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:218
ignite::binary::BinaryStringArrayReader::GetNext
std::string GetNext()
Get next element.
Definition: binary_containers.h:339
ignite::binary::BinaryMapWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:277
ignite::binary::BinaryCollectionWriter::Write
void Write(const T &val)
Write a value.
Definition: binary_containers.h:205
ignite::binary::BinaryCollectionReader::GetSize
int32_t GetSize()
Get collection size.
Definition: binary_containers.h:524
ignite::binary::BinaryCollectionWriter
Binary collection writer.
Definition: binary_containers.h:182
ignite::binary::BinaryCollectionReader::IsNull
bool IsNull()
Check whether collection is NULL.
Definition: binary_containers.h:534
ignite::binary::BinaryArrayWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:157
ignite::binary::BinaryMapReader::IsNull
bool IsNull()
Check whether map is NULL.
Definition: binary_containers.h:630
ignite::binary::BinaryArrayWriter
Binary array writer.
Definition: binary_containers.h:121
ignite::binary::BinaryArrayReader
Binary array reader.
Definition: binary_containers.h:389
ignite::binary::CollectionType::Type
Type
Definition: binary_consts.h:35
ignite::binary::MapType::Type
Type
Definition: binary_consts.h:69
ignite::binary::BinaryCollectionReader::GetNext
T GetNext()
Read next element.
Definition: binary_containers.h:503
ignite::binary::BinaryStringArrayWriter
Binary string array writer.
Definition: binary_containers.h:48
ignite::binary::BinaryMapReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:584
ignite::binary::BinaryStringArrayWriter::Write
void Write(const std::string &val)
Write string.
Definition: binary_containers.h:86
ignite::binary::BinaryArrayWriter::BinaryArrayWriter
BinaryArrayWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:131
ignite::binary::BinaryMapReader::BinaryMapReader
BinaryMapReader(impl::binary::BinaryReaderImpl *impl, int32_t id, MapType::Type type, int32_t size)
Constructor.
Definition: binary_containers.h:573
ignite::binary::BinaryArrayReader::GetSize
int32_t GetSize()
Get array size.
Definition: binary_containers.h:433
ignite::binary::BinaryMapReader::GetNext
void GetNext(K &key, V &val)
Read next element.
Definition: binary_containers.h:599
ignite::binary::BinaryMapReader::GetSize
int32_t GetSize()
Get map size.
Definition: binary_containers.h:620
ignite::binary::BinaryMapWriter
Binary map writer.
Definition: binary_containers.h:241
ignite::binary::BinaryArrayReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:411
ignite::binary::BinaryMapReader::GetType
MapType::Type GetType()
Get map type.
Definition: binary_containers.h:610
ignite::binary::BinaryMapWriter::Write
void Write(const K &key, const V &val)
Write a map entry.
Definition: binary_containers.h:265
ignite::binary::BinaryArrayReader::BinaryArrayReader
BinaryArrayReader(impl::binary::BinaryReaderImpl *impl, int32_t id, int32_t size)
Constructor.
Definition: binary_containers.h:400
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::binary::BinaryCollectionReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:491
ignite::binary::BinaryCollectionReader::BinaryCollectionReader
BinaryCollectionReader(impl::binary::BinaryReaderImpl *impl, int32_t id, const CollectionType::Type type, int32_t size)
Constructor.
Definition: binary_containers.h:480