GridGain C++
binary_containers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #ifndef _IGNITE_BINARY_BINARY_CONTAINERS
23 #define _IGNITE_BINARY_BINARY_CONTAINERS
24 
25 #include <stdint.h>
26 
27 #include <ignite/common/utils.h>
28 
29 #include "ignite/impl/binary/binary_writer_impl.h"
30 #include "ignite/impl/binary/binary_reader_impl.h"
32 
33 namespace ignite
34 {
35  namespace binary
36  {
47  class IGNITE_IMPORT_EXPORT BinaryStringArrayWriter
48  {
49  public:
57  BinaryStringArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id);
58 
66  void Write(const char* val);
67 
76  void Write(const char* val, int32_t len);
77 
85  void Write(const std::string& val)
86  {
87  Write(val.c_str());
88  }
89 
98  void Close();
99 
100  private:
102  impl::binary::BinaryWriterImpl* impl;
103 
105  const int32_t id;
106  };
107 
119  template<typename T>
120  class IGNITE_IMPORT_EXPORT BinaryArrayWriter
121  {
122  public:
130  BinaryArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
131  impl(impl), id(id)
132  {
133  // No-op.
134  }
135 
143  void Write(const T& val)
144  {
145  impl->WriteElement<T>(id, val);
146  }
147 
156  void Close()
157  {
158  impl->CommitContainer(id);
159  }
160 
161  private:
163  impl::binary::BinaryWriterImpl* impl;
164 
166  const int32_t id;
167  };
168 
180  template<typename T>
181  class IGNITE_IMPORT_EXPORT BinaryCollectionWriter
182  {
183  public:
191  BinaryCollectionWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
192  impl(impl), id(id)
193  {
194  // No-op.
195  }
196 
204  void Write(const T& val)
205  {
206  impl->WriteElement<T>(id, val);
207  }
208 
217  void Close()
218  {
219  impl->CommitContainer(id);
220  }
221  private:
223  impl::binary::BinaryWriterImpl* impl;
224 
226  const int32_t id;
227  };
228 
239  template<typename K, typename V>
240  class IGNITE_IMPORT_EXPORT BinaryMapWriter
241  {
242  public:
250  BinaryMapWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) :
251  impl(impl), id(id)
252  {
253  // No-op.
254  }
255 
264  void Write(const K& key, const V& val)
265  {
266  impl->WriteElement<K, V>(id, key, val);
267  }
268 
276  void Close()
277  {
278  impl->CommitContainer(id);
279  }
280  private:
282  impl::binary::BinaryWriterImpl* impl;
283 
285  const int32_t id;
286  };
287 
296  class IGNITE_IMPORT_EXPORT BinaryStringArrayReader
297  {
298  public:
307  BinaryStringArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size);
308 
314  bool HasNext();
315 
329  int32_t GetNext(char* res, int32_t len);
330 
338  std::string GetNext()
339  {
340  int32_t len = GetNext(NULL, 0);
341 
342  if (len != -1)
343  {
344  ignite::common::FixedSizeArray<char> arr(len + 1);
345 
346  GetNext(arr.GetData(), static_cast<int32_t>(arr.GetSize()));
347 
348  return std::string(arr.GetData());
349  }
350  else
351  return std::string();
352  }
353 
359  int32_t GetSize() const;
360 
366  bool IsNull() const;
367 
368  private:
370  impl::binary::BinaryReaderImpl* impl;
371 
373  const int32_t id;
374 
376  const int32_t size;
377  };
378 
387  template<typename T>
389  {
390  public:
399  BinaryArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size) :
400  impl(impl), id(id), size(size)
401  {
402  // No-op.
403  }
404 
410  bool HasNext()
411  {
412  return impl->HasNextElement(id);
413  }
414 
423  {
424  return impl->ReadElement<T>(id);
425  }
426 
432  int32_t GetSize()
433  {
434  return size;
435  }
436 
442  bool IsNull()
443  {
444  return size == -1;
445  }
446  private:
448  impl::binary::BinaryReaderImpl* impl;
449 
451  const int32_t id;
452 
454  const int32_t size;
455  };
456 
466  template<typename T>
468  {
469  public:
479  BinaryCollectionReader(impl::binary::BinaryReaderImpl* impl, int32_t id,
480  const CollectionType::Type type, int32_t size) : impl(impl), id(id), type(type), size(size)
481  {
482  // No-op.
483  }
484 
490  bool HasNext()
491  {
492  return impl->HasNextElement(id);
493  }
494 
503  {
504  return impl->ReadElement<T>(id);
505  }
506 
514  {
515  return type;
516  }
517 
523  int32_t GetSize()
524  {
525  return size;
526  }
527 
533  bool IsNull()
534  {
535  return size == -1;
536  }
537  private:
539  impl::binary::BinaryReaderImpl* impl;
540 
542  const int32_t id;
543 
545  const CollectionType::Type type;
546 
548  const int32_t size;
549  };
550 
559  template<typename K, typename V>
561  {
562  public:
572  BinaryMapReader(impl::binary::BinaryReaderImpl* impl, int32_t id, MapType::Type type,
573  int32_t size) : impl(impl), id(id), type(type), size(size)
574  {
575  // No-op.
576  }
577 
583  bool HasNext()
584  {
585  return impl->HasNextElement(id);
586  }
587 
598  void GetNext(K& key, V& val)
599  {
600  return impl->ReadElement<K, V>(id, key, val);
601  }
602 
610  {
611  return type;
612  }
613 
619  int32_t GetSize()
620  {
621  return size;
622  }
623 
629  bool IsNull()
630  {
631  return size == -1;
632  }
633  private:
635  impl::binary::BinaryReaderImpl* impl;
636 
638  const int32_t id;
639 
641  const MapType::Type type;
642 
644  const int32_t size;
645  };
646  }
647 }
648 
649 #endif //_IGNITE_BINARY_BINARY_CONTAINERS
ignite::binary::BinaryCollectionWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:217
ignite
Ignite API.
Definition: cache.h:47
ignite::binary::BinaryMapWriter::BinaryMapWriter
BinaryMapWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:250
ignite::binary::BinaryStringArrayWriter
Binary string array writer.
Definition: binary_containers.h:47
ignite::binary::MapType::Type
Type
Definition: binary_consts.h:68
ignite::binary::BinaryCollectionWriter::Write
void Write(const T &val)
Write a value.
Definition: binary_containers.h:204
ignite::binary::BinaryStringArrayWriter::Write
void Write(const std::string &val)
Write string.
Definition: binary_containers.h:85
ignite::binary::BinaryCollectionReader::BinaryCollectionReader
BinaryCollectionReader(impl::binary::BinaryReaderImpl *impl, int32_t id, const CollectionType::Type type, int32_t size)
Constructor.
Definition: binary_containers.h:479
ignite::binary::BinaryArrayWriter::BinaryArrayWriter
BinaryArrayWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:130
ignite::binary::BinaryCollectionReader
Binary collection reader.
Definition: binary_containers.h:467
ignite::binary::BinaryArrayReader::GetNext
T GetNext()
Read next element.
Definition: binary_containers.h:422
ignite::binary::BinaryCollectionReader::GetSize
int32_t GetSize()
Get collection size.
Definition: binary_containers.h:523
binary_consts.h
ignite::binary::BinaryMapReader
Binary map reader.
Definition: binary_containers.h:560
ignite::binary::BinaryMapWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:276
ignite::binary::BinaryCollectionReader::IsNull
bool IsNull()
Check whether collection is NULL.
Definition: binary_containers.h:533
ignite::binary::BinaryCollectionWriter::BinaryCollectionWriter
BinaryCollectionWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:191
ignite::binary::BinaryArrayWriter::Close
void Close()
Close the writer.
Definition: binary_containers.h:156
ignite::binary::BinaryArrayReader::GetSize
int32_t GetSize()
Get array size.
Definition: binary_containers.h:432
ignite::binary::BinaryArrayWriter
Binary array writer.
Definition: binary_containers.h:120
ignite::binary::BinaryArrayReader::IsNull
bool IsNull()
Check whether array is NULL.
Definition: binary_containers.h:442
ignite::binary::BinaryArrayReader
Binary array reader.
Definition: binary_containers.h:388
ignite::binary::BinaryMapReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:583
ignite::binary::BinaryCollectionWriter
Binary collection writer.
Definition: binary_containers.h:181
ignite::binary::BinaryArrayWriter::Write
void Write(const T &val)
Write a value.
Definition: binary_containers.h:143
ignite::binary::BinaryMapReader::IsNull
bool IsNull()
Check whether map is NULL.
Definition: binary_containers.h:629
ignite::binary::BinaryStringArrayReader
Binary string array reader.
Definition: binary_containers.h:296
ignite::binary::BinaryMapWriter
Binary map writer.
Definition: binary_containers.h:240
ignite::binary::CollectionType::Type
Type
Definition: binary_consts.h:34
ignite::binary::BinaryCollectionReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:490
ignite::binary::BinaryArrayReader::BinaryArrayReader
BinaryArrayReader(impl::binary::BinaryReaderImpl *impl, int32_t id, int32_t size)
Constructor.
Definition: binary_containers.h:399
ignite::binary::BinaryStringArrayReader::GetNext
std::string GetNext()
Get next element.
Definition: binary_containers.h:338
ignite::binary::BinaryCollectionReader::GetNext
T GetNext()
Read next element.
Definition: binary_containers.h:502
ignite::binary::BinaryMapReader::BinaryMapReader
BinaryMapReader(impl::binary::BinaryReaderImpl *impl, int32_t id, MapType::Type type, int32_t size)
Constructor.
Definition: binary_containers.h:572
ignite::binary::BinaryMapReader::GetNext
void GetNext(K &key, V &val)
Read next element.
Definition: binary_containers.h:598
ignite::binary::BinaryArrayReader::HasNext
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:410
ignite::binary::BinaryMapReader::GetSize
int32_t GetSize()
Get map size.
Definition: binary_containers.h:619
ignite::binary::BinaryMapReader::GetType
MapType::Type GetType()
Get map type.
Definition: binary_containers.h:609
ignite::binary::BinaryMapWriter::Write
void Write(const K &key, const V &val)
Write a map entry.
Definition: binary_containers.h:264
ignite::binary::BinaryCollectionReader::GetType
CollectionType::Type GetType()
Get collection type.
Definition: binary_containers.h:513