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_CONTAINERS
24 #define _IGNITE_BINARY_CONTAINERS
25 
26 #include <stdint.h>
27 
28 #include "ignite/impl/binary/binary_writer_impl.h"
29 #include "ignite/impl/binary/binary_reader_impl.h"
30 #include "ignite/impl/utils.h"
32 
33 namespace ignite
34 {
35  namespace binary
36  {
40  class IGNITE_IMPORT_EXPORT BinaryStringArrayWriter
41  {
42  public:
49  BinaryStringArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id);
50 
56  void Write(const char* val);
57 
64  void Write(const char* val, int32_t len);
65 
71  void Write(const std::string& val)
72  {
73  Write(val.c_str());
74  }
75 
79  void Close();
80  private:
82  impl::binary::BinaryWriterImpl* impl;
83 
85  const int32_t id;
86  };
87 
91  template<typename T>
92  class IGNITE_IMPORT_EXPORT BinaryArrayWriter
93  {
94  public:
101  BinaryArrayWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) : impl(impl), id(id)
102  {
103  // No-op.
104  }
105 
111  void Write(const T& val)
112  {
113  impl->WriteElement<T>(id, val);
114  }
115 
119  void Close()
120  {
121  impl->CommitContainer(id);
122  }
123  private:
125  impl::binary::BinaryWriterImpl* impl;
126 
128  const int32_t id;
129  };
130 
134  template<typename T>
135  class IGNITE_IMPORT_EXPORT BinaryCollectionWriter
136  {
137  public:
144  BinaryCollectionWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) : impl(impl), id(id)
145  {
146  // No-op.
147  }
148 
154  void Write(const T& val)
155  {
156  impl->WriteElement<T>(id, val);
157  }
158 
162  void Close()
163  {
164  impl->CommitContainer(id);
165  }
166  private:
168  impl::binary::BinaryWriterImpl* impl;
169 
171  const int32_t id;
172  };
173 
177  template<typename K, typename V>
178  class IGNITE_IMPORT_EXPORT BinaryMapWriter
179  {
180  public:
186  BinaryMapWriter(impl::binary::BinaryWriterImpl* impl, int32_t id) : impl(impl), id(id)
187  {
188  // No-op.
189  }
190 
197  void Write(const K& key, const V& val)
198  {
199  impl->WriteElement<K, V>(id, key, val);
200  }
201 
205  void Close()
206  {
207  impl->CommitContainer(id);
208  }
209  private:
211  impl::binary::BinaryWriterImpl* impl;
212 
214  const int32_t id;
215  };
216 
220  class IGNITE_IMPORT_EXPORT BinaryStringArrayReader
221  {
222  public:
230  BinaryStringArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size);
231 
237  bool HasNext();
238 
250  int32_t GetNext(char* res, int32_t len);
251 
257  std::string GetNext()
258  {
259  int32_t len = GetNext(NULL, 0);
260 
261  if (len != -1)
262  {
263  impl::utils::SafeArray<char> arr(len + 1);
264 
265  GetNext(arr.target, len + 1);
266 
267  return std::string(arr.target);
268  }
269  else
270  return std::string();
271  }
272 
278  int32_t GetSize() const;
279 
283  bool IsNull() const;
284  private:
286  impl::binary::BinaryReaderImpl* impl;
287 
289  const int32_t id;
290 
292  const int32_t size;
293  };
294 
298  template<typename T>
300  {
301  public:
309  BinaryArrayReader(impl::binary::BinaryReaderImpl* impl, int32_t id, int32_t size) :
310  impl(impl), id(id), size(size)
311  {
312  // No-op.
313  }
314 
320  bool HasNext()
321  {
322  return impl->HasNextElement(id);
323  }
324 
331  {
332  return impl->ReadElement<T>(id);
333  }
334 
340  int32_t GetSize()
341  {
342  return size;
343  }
344 
348  bool IsNull()
349  {
350  return size == -1;
351  }
352  private:
354  impl::binary::BinaryReaderImpl* impl;
355 
357  const int32_t id;
358 
360  const int32_t size;
361  };
362 
366  template<typename T>
368  {
369  public:
378  BinaryCollectionReader(impl::binary::BinaryReaderImpl* impl, int32_t id,
379  const CollectionType type, int32_t size) : impl(impl), id(id), type(type), size(size)
380  {
381  // No-op.
382  }
383 
389  bool HasNext()
390  {
391  return impl->HasNextElement(id);
392  }
393 
400  {
401  return impl->ReadElement<T>(id);
402  }
403 
410  {
411  return type;
412  }
413 
419  int32_t GetSize()
420  {
421  return size;
422  }
423 
427  bool IsNull()
428  {
429  return size == -1;
430  }
431  private:
433  impl::binary::BinaryReaderImpl* impl;
434 
436  const int32_t id;
437 
439  const CollectionType type;
440 
442  const int32_t size;
443  };
444 
448  template<typename K, typename V>
450  {
451  public:
460  BinaryMapReader(impl::binary::BinaryReaderImpl* impl, int32_t id, MapType type,
461  int32_t size) : impl(impl), id(id), type(type), size(size)
462  {
463  // No-op.
464  }
465 
471  bool HasNext()
472  {
473  return impl->HasNextElement(id);
474  }
475 
482  void GetNext(K* key, V* val)
483  {
484  return impl->ReadElement<K, V>(id, key, val);
485  }
486 
493  {
494  return type;
495  }
496 
502  int32_t GetSize()
503  {
504  return size;
505  }
506 
510  bool IsNull()
511  {
512  return size == -1;
513  }
514  private:
516  impl::binary::BinaryReaderImpl* impl;
517 
519  const int32_t id;
520 
522  const MapType type;
523 
525  const int32_t size;
526  };
527  }
528 }
529 
530 #endif
BinaryMapWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:186
void Write(const T &val)
Write a value.
Definition: binary_containers.h:111
T GetNext()
Read next element.
Definition: binary_containers.h:330
Declares specific binary constatants.
CollectionType GetType()
Get collection type.
Definition: binary_containers.h:409
bool IsNull()
Whether array is NULL.
Definition: binary_containers.h:348
Binary string array reader.
Definition: binary_containers.h:220
BinaryCollectionWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:144
Binary map reader.
Definition: binary_containers.h:449
Binary collection reader.
Definition: binary_containers.h:367
void Close()
Close the writer.
Definition: binary_containers.h:162
std::string GetNext()
Get next element.
Definition: binary_containers.h:257
void Close()
Close the writer.
Definition: binary_containers.h:205
void Write(const T &val)
Write a value.
Definition: binary_containers.h:154
BinaryCollectionReader(impl::binary::BinaryReaderImpl *impl, int32_t id, const CollectionType type, int32_t size)
Constructor.
Definition: binary_containers.h:378
CollectionType
Binary collection types.
Definition: binary_consts.h:35
int32_t GetSize()
Get collection size.
Definition: binary_containers.h:419
MapType GetType()
Get map type.
Definition: binary_containers.h:492
void GetNext(K *key, V *val)
Read next element.
Definition: binary_containers.h:482
Binary collection writer.
Definition: binary_containers.h:135
bool IsNull()
Whether collection is NULL.
Definition: binary_containers.h:427
void Close()
Close the writer.
Definition: binary_containers.h:119
bool IsNull()
Whether map is NULL.
Definition: binary_containers.h:510
Binary collection writer.
Definition: binary_containers.h:92
Binary array reader.
Definition: binary_containers.h:299
T GetNext()
Read next element.
Definition: binary_containers.h:399
Binary string array writer.
Definition: binary_containers.h:40
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:471
void Write(const std::string &val)
Write string.
Definition: binary_containers.h:71
BinaryMapReader(impl::binary::BinaryReaderImpl *impl, int32_t id, MapType type, int32_t size)
Constructor.
Definition: binary_containers.h:460
BinaryArrayWriter(impl::binary::BinaryWriterImpl *impl, int32_t id)
Constructor.
Definition: binary_containers.h:101
int32_t GetSize()
Get array size.
Definition: binary_containers.h:340
int32_t GetSize()
Get map size.
Definition: binary_containers.h:502
Binary map writer.
Definition: binary_containers.h:178
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:320
void Write(const K &key, const V &val)
Write a value.
Definition: binary_containers.h:197
BinaryArrayReader(impl::binary::BinaryReaderImpl *impl, int32_t id, int32_t size)
Constructor.
Definition: binary_containers.h:309
Apache Ignite API.
Definition: binary_consts.h:28
bool HasNext()
Check whether next element is available for read.
Definition: binary_containers.h:389
MapType
Binary map types.
Definition: binary_consts.h:66