Scanner C++ API
enumerator.h
1 /* Copyright 2017 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/util/common.h"
19 #include "scanner/util/profiler.h"
20 #include "scanner/util/storehouse.h"
21 
22 #include <vector>
23 
24 namespace scanner {
25 
30  std::vector<u8> args;
31  storehouse::StorageConfig* storage_config;
32 };
33 
34 struct ElementArgs {
35  i64 row_id;
36  std::vector<u8> args;
37 };
38 
42 class Enumerator {
43  public:
44  Enumerator(const EnumeratorConfig& config) {};
45  virtual ~Enumerator() {};
46 
47  virtual void validate(proto::Result* result) { result->set_success(true); }
48 
49  virtual i64 total_elements() = 0;
50 
55  virtual ElementArgs element_args_at(i64 element_idx) = 0;
56 };
57 
60 namespace internal {
61 
62 class EnumeratorBuilder;
63 
64 using EnumeratorConstructor =
65  std::function<Enumerator*(const EnumeratorConfig& config)>;
66 
68  public:
70 };
71 
73  public:
74  friend class EnumeratorRegistration;
75 
76  EnumeratorBuilder(const std::string& name, EnumeratorConstructor constructor)
77  : name_(name),
78  constructor_(constructor) {}
79 
80  EnumeratorBuilder& protobuf_name(std::string protobuf_name) {
81  protobuf_name_ = protobuf_name;
82  return *this;
83  }
84 
85  private:
86  std::string name_;
87  EnumeratorConstructor constructor_;
88  std::string protobuf_name_;
89 };
90 }
91 
92 #define REGISTER_ENUMERATOR(name__, enumerator__) \
93  REGISTER_ENUMERATOR_HELPER(__COUNTER__, name__, enumerator__)
94 
95 #define REGISTER_ENUMERATOR_HELPER(uid__, name__, enumerator__) \
96  REGISTER_ENUMERATOR_UID(uid__, name__, enumerator__)
97 
98 #define REGISTER_ENUMERATOR_UID(uid__, name__, enumerator__) \
99  static ::scanner::internal::EnumeratorRegistration \
100  enumerator_registration_##uid__ __attribute__((unused)) = \
101  ::scanner::internal::EnumeratorBuilder( \
102  #name__, [](const ::scanner::EnumeratorConfig& config) { \
103  return new enumerator__(config); \
104  })
105 }
Definition: enumerator.h:72
Interface for enumerating available data from a data source.
Definition: enumerator.h:42
Definition: enumerator.h:34
Parameters provided at instantiation of an enumerator.
Definition: enumerator.h:29
Definition: database.cpp:36
storehouse::StorageConfig * storage_config
Byte-string of proto args if given.
Definition: enumerator.h:31