Scanner C++ API
source.h
1 /* Copyright 2018 Carnegie Mellon University
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/api/kernel.h"
19 #include "scanner/api/enumerator.h"
20 #include "scanner/util/common.h"
21 #include "scanner/util/profiler.h"
22 #include "scanner/util/storehouse.h"
23 
24 #include <vector>
25 
26 namespace scanner {
27 
29 struct SourceConfig {
30  std::vector<std::string> output_columns;
31  std::vector<proto::ColumnType> output_column_types;
32  std::vector<u8> args;
33  i32 node_id;
34  storehouse::StorageConfig* storage_config;
35 };
36 
43 class Source {
44  public:
45  Source(const SourceConfig& config) {}
46 
47  virtual ~Source(){};
48 
54  virtual void validate(proto::Result* result) { result->set_success(true); }
55 
68  virtual void read(const std::vector<ElementArgs>& args,
69  BatchedElements& output_columns) = 0;
70 
74  virtual void set_profiler(Profiler* profiler) { profiler_ = profiler; }
75 
80  Profiler* profiler_ = nullptr;
81 };
82 
85 namespace internal {
86 
87 class SourceBuilder;
88 
89 using SourceConstructor =
90  std::function<Source*(const SourceConfig& config)>;
91 
93  public:
94  SourceRegistration(const SourceBuilder& builder);
95 };
96 
98  public:
99  friend class SourceRegistration;
100 
101  SourceBuilder(const std::string& name, SourceConstructor constructor)
102  : name_(name),
103  constructor_(constructor) {}
104 
105  SourceBuilder& output(const std::string& name,
106  ColumnType type = ColumnType::Bytes) {
107  if (output_columns_.size() > 0) {
108  LOG(FATAL) << "Sources can only have one output column.";
109  }
110  output_columns_.push_back(std::make_tuple(name, type));
111  return *this;
112  }
113 
114  SourceBuilder& frame_output(const std::string& name) {
115  return output(name, ColumnType::Video);
116  }
117 
118  SourceBuilder& protobuf_name(const std::string& name) {
119  protobuf_name_ = name;
120  return *this;
121  }
122 
123  private:
124  std::string name_;
125  SourceConstructor constructor_;
126  std::vector<std::tuple<std::string, ColumnType>> output_columns_;
127  std::string protobuf_name_;
128 };
129 }
130 
131 #define REGISTER_SOURCE(name__, source__) \
132  REGISTER_SOURCE_HELPER(__COUNTER__, name__, source__)
133 
134 #define REGISTER_SOURCE_HELPER(uid__, name__, source__) \
135  REGISTER_SOURCE_UID(uid__, name__, source__)
136 
137 #define REGISTER_SOURCE_UID(uid__, name__, source__) \
138  static ::scanner::internal::SourceRegistration source_registration_##uid__ \
139  __attribute__((unused)) = ::scanner::internal::SourceBuilder( \
140  #name__, [](const ::scanner::SourceConfig& config) { \
141  return new source__(config); \
142  })
143 }
Parameters provided at instantiation of Source node.
Definition: source.h:29
Interface for reading data in a computation graph.
Definition: source.h:43
i32 node_id
Byte-string of proto args if given.
Definition: source.h:33
Definition: source.h:97
Definition: profiler.h:40
virtual void set_profiler(Profiler *profiler)
For internal use.
Definition: source.h:74
virtual void validate(proto::Result *result)
Checks if Source arguments are valid.
Definition: source.h:54
Definition: database.cpp:36