Turi Create  4.0
unity_sarray_builder.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_UNITY_SARRAY_BUILDER_HPP
7 #define TURI_UNITY_SARRAY_BUILDER_HPP
8 
9 #include <vector>
10 #include <core/storage/sframe_data/sarray.hpp>
11 #include <boost/circular_buffer.hpp>
12 #include <model_server/lib/api/unity_sarray_builder_interface.hpp>
13 
14 namespace turi {
15 
16 // forward declarations
17 template <typename T>
18 class sarray;
19 
20 /**
21  * Provides a Python interface to incrementally build an SArray.
22  *
23  * Unlike most other unity objects, this is not a wrapper of another
24  * "sarray_builder" class, but provides the implementation. This is because it
25  * is a slightly embellished wrapper around the SArray's output iterator, so
26  * there is no further functionality that needs to be available for the C++
27  * side.
28  *
29  * The unity_sarray_builder is designed to append values until \ref close is
30  * called, which returns the SArray. No "reopening" is allowed, and no
31  * operations in that instance of unity_sarray_builder will work after close is
32  * called.
33  */
34 class unity_sarray_builder: public unity_sarray_builder_base {
35  public:
36  /**
37  * Default constructor. Does nothing
38  */
40 
41  /**
42  * Initialize the unity_sarray_buidler.
43  *
44  * This essentially opens the output iterator for writing.
45  *
46  */
47  void init(size_t num_segments, size_t history_size, flex_type_enum dtype);
48 
49  /**
50  * Add a single flexible_type value to the SArray.
51  *
52  * The segment number allows the user to use the parallel interface provided
53  * by the underlying output_iterator.
54  *
55  * Throws if:
56  * - init hasn't been called or close has been called
57  * - segment number is invalid
58  * - the type of \p val differs from the type given in \ref init
59  *
60  */
61  void append(const flexible_type &val, size_t segment);
62 
63  /**
64  * A wrapper around \ref append which adds multiple flexible_types to SArray.
65  *
66  * Throws if:
67  * - init hasn't been called or close has been called
68  * - segment number is invalid
69  * - the type of any values in \p vals differs from
70  * the type given in \ref init
71  */
72  void append_multiple(const std::vector<flexible_type> &vals, size_t segment);
73 
74  /**
75  * Return the current type of the SArray.
76  */
78 
79  /**
80  * Return the last \p num_elems elements appended.
81  */
82  std::vector<flexible_type> read_history(size_t num_elems, size_t segment);
83 
84  /**
85  * Finalize SArray and return it.
86  */
87  std::shared_ptr<unity_sarray_base> close();
88 
90  unity_sarray_builder& operator=(const unity_sarray_builder&) = delete;
91  private:
92  /// Methods
93 
94  /// Variables
95  bool m_inited = false;
96  bool m_closed = false;
97  std::shared_ptr<sarray<flexible_type>> m_sarray;
98  std::vector<sarray<flexible_type>::iterator> m_out_iters;
100  std::set<flex_type_enum> m_types_inserted;
101 
102  std::vector<std::shared_ptr<boost::circular_buffer<flexible_type>>> m_history;
103 };
104 
105 } // namespace turi
106 #endif // TURI_UNITY_SARRAY_BUILDER_HPP
void append(const flexible_type &val, size_t segment)
void append_multiple(const std::vector< flexible_type > &vals, size_t segment)
void init(size_t num_segments, size_t history_size, flex_type_enum dtype)
std::shared_ptr< unity_sarray_base > close()
std::vector< flexible_type > read_history(size_t num_elems, size_t segment)
flex_type_enum get_type()