Turi Create  4.0
sarray_v2_encoded_block.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SFRAME_ENCODED_BLOCK_HPP
7 #define TURI_SFRAME_ENCODED_BLOCK_HPP
8 
9 #include <boost/circular_buffer.hpp>
10 #include <vector>
11 #include <memory>
12 #include <core/data/flexible_type/flexible_type.hpp>
13 #include <core/storage/sframe_data/sarray_v2_block_types.hpp>
14 namespace turi {
15 
16 
17 /**
18  * \internal
19  * \ingroup sframe_physical
20  * \addtogroup sframe_internal SFrame Internal
21  * \{
22  */
23 
24 /**
25  * SFrame v2 Format Implementation Detail
26  */
27 namespace v2_block_impl {
28 
29 class encoded_block_range;
30 struct typed_decode_stream;
31 /**
32  * This class provides accessors into a typed v2
33  * sarray<flexible_type> encoded column block. It maintains the
34  * block in a compressed state, and stream decodes it.
35  *
36  * The encoded block object is copyable, move constructable,
37  * copy assignable, move assignable. Copies are cheap and free
38  * as it only needs to copy a single shared pointer.
39  */
41  public:
42  /// Default constructor. Does nothing.
43  encoded_block();
44 
45  /// Default Copy constructor
46  encoded_block(const encoded_block&) = default;
47  /// Default Move constructor
48  encoded_block(encoded_block&&) = default;
49  /// Default Copy assignment
50  encoded_block& operator=(const encoded_block&) = default;
51  /// Default Move assignment
53 
54  /// block constructor from data contents; simply calls init().
55  encoded_block(block_info info, std::vector<char>&& data) {
56  init(info, std::move(data));
57  }
58 
59 
60  /// block constructor from data contents; simply calls init().
61  encoded_block(block_info info, std::shared_ptr<std::vector<char> > data) {
62  init(info, data);
63  }
64 
65  /**
66  * Initializes this block to point to new data.
67  *
68  * Existing ranges are NOT invalidated.
69  * They will continue to point to what they used to point to.
70  * \param info The block information structure
71  * \param data The binary data
72  */
73  void init(block_info info, std::vector<char>&& data);
74 
75 
76  /**
77  * Initializes this block to point to new data.
78  *
79  * Existing ranges are NOT invalidated.
80  * They will continue to point to what they used to point to.
81  * \param info The block information structure
82  * \param data The binary data
83  */
84  void init(block_info info, std::shared_ptr<std::vector<char> > data);
85 
86  /**
87  * Returns an accessor to the contents of the block.
88  *
89  * The range is *not* concurrent. But independent ranges can be accessed
90  * in parallel safely.
91  */
93 
94  /**
95  * Release the block object. All acquired ranges are stil valid.
96  */
97  void release();
98 
99  inline size_t size() const {
100  return m_size;
101  }
102 
103  const block_info get_block_info() const {
104  return m_block.m_block_info;
105  }
106 
107  std::shared_ptr<std::vector<char> > get_block_data() const {
108  return m_block.m_data;
109  }
110 
111  friend class encoded_block_range;
112 
113  private:
114 
115 
116  struct block {
117  /// The block information. Needed for the decode.
118  block_info m_block_info;
119  /// The actual block data.
120  std::shared_ptr<std::vector<char> > m_data;
121  };
122 
123  block m_block;
124  size_t m_size = 0;
125 }; // class encoded_block
126 
127 
128 /**
129  * The range returned by \ref encoded_block::get_range().
130  * It provides 2 basic methods. \ref decode(target, size) and \ref skip(n)
131  *
132  * The encoded_block_range provides a one pass reader to the data.
133  *
134  * The encoded_block_range holds its own pointers to the data and hence
135  * is not invalidated by destruction or reassignment of the originating
136  * encoded_block object.
137  *
138  * The range is *not* concurrent.
139  */
141  private:
142  public:
143  encoded_block_range() = default;
144  explicit encoded_block_range(const encoded_block& block);
145 
146  encoded_block_range(const encoded_block_range&) = delete;
150 
151  /**
152  * Decodes the next num_elem elements into the decode_target.
153  * Returns the number of elements read, i.e.
154  * while(num_elem) {
155  * (*decode_target) = next_value;
156  * ++decode_target;
157  * --num_elem;
158  * }
159  */
160  size_t decode_to(flexible_type* decode_target, size_t num_elem);
161  void skip(size_t n);
162  void release();
163 
165 
166  private:
167 
168  encoded_block::block m_block;
169  std::unique_ptr<typed_decode_stream> decoder;
170 
171 }; // encoded_block_range
172 
173 
174 
175 } // v2_block_impl
176 
177 /// \}
178 } // namespace turi
179 #endif
void init(block_info info, std::vector< char > &&data)
encoded_block & operator=(const encoded_block &)=default
Default Copy assignment.
encoded_block(block_info info, std::vector< char > &&data)
block constructor from data contents; simply calls init().
encoded_block(block_info info, std::shared_ptr< std::vector< char > > data)
block constructor from data contents; simply calls init().
encoded_block_range get_range()
encoded_block()
Default constructor. Does nothing.