-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathRDGPartHeader.h
More file actions
364 lines (307 loc) · 11.7 KB
/
RDGPartHeader.h
File metadata and controls
364 lines (307 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#ifndef KATANA_LIBTSUBA_RDGPARTHEADER_H_
#define KATANA_LIBTSUBA_RDGPARTHEADER_H_
#include <cassert>
#include <optional>
#include <vector>
#include <arrow/api.h>
#include "katana/JSON.h"
#include "katana/Result.h"
#include "katana/URI.h"
#include "tsuba/Errors.h"
#include "tsuba/PartitionMetadata.h"
#include "tsuba/RDG.h"
#include "tsuba/WriteGroup.h"
#include "tsuba/tsuba.h"
namespace tsuba {
/// PropStorageInfo objects track the state of properties, and sanity check their
/// transitions. N.b., It does not "DO" the transitions, this structure is purely
/// for bookkeeping
///
/// Properties have 3 states:
/// * Absent - exists in storage but is not in memory
/// * Clean - in memory and matches what is in storage
/// * Dirty - in memory but does not match what is in storage
///
/// The state machine looks like this:
///
/// EXISTING NEW
/// PROPERTY modify PROPERTY
/// | +---------------------------------+ |
/// | | +-------+ modify | |
/// | | +------->| Clean +------+ | |
/// | | load| +-+-----+ | | |
/// | | | | ^ v v |
/// | +------+-+ | | +-------+ |
/// +->| Absent |<-------+ +-------+ Dirty |<-+
/// +--------+ unload write +-------+
///
/// Properties either start out in storage as part of an RDG on disk
/// (EXISTING PROPERTY) or start out in memory as part of an RDG in
/// memory (NEW PROPERTY)
class PropStorageInfo {
enum class State {
kAbsent,
kClean,
kDirty,
};
public:
PropStorageInfo(std::string name, std::shared_ptr<arrow::DataType> type)
: name_(std::move(name)),
path_(),
type_(std::move(type)),
state_(State::kDirty) {}
PropStorageInfo(std::string name, std::string path)
: name_(std::move(name)),
path_(std::move(path)),
state_(State::kAbsent) {}
void WasLoaded(const std::shared_ptr<arrow::DataType>& type) {
KATANA_LOG_ASSERT(state_ == State::kAbsent);
state_ = State::kClean;
type_ = type;
}
void WasModified(const std::shared_ptr<arrow::DataType>& type) {
path_.clear();
state_ = State::kDirty;
type_ = type;
}
void WasWritten(std::string_view new_path) {
KATANA_LOG_ASSERT(state_ == State::kDirty);
path_ = new_path;
state_ = State::kClean;
}
void WasUnloaded() {
KATANA_LOG_ASSERT(state_ == State::kClean);
state_ = State::kAbsent;
}
bool IsAbsent() const { return state_ == State::kAbsent; }
bool IsClean() const { return state_ == State::kClean; }
bool IsDirty() const { return state_ == State::kDirty; }
const std::string& name() const { return name_; }
const std::string& path() const { return path_; }
const std::shared_ptr<arrow::DataType>& type() const { return type_; }
// since we don't have type info in the header don't know the
// type when this would have been constructed. Allow others to
// fix up the type in this case, required until we can get the type
// from PartHeader files
void set_type(std::shared_ptr<arrow::DataType> type) {
KATANA_LOG_ASSERT(state_ == State::kAbsent);
type_ = std::move(type);
}
friend void to_json(nlohmann::json& j, const PropStorageInfo& propmd);
friend void from_json(const nlohmann::json& j, PropStorageInfo& propmd);
// required for json
PropStorageInfo() = default;
private:
std::string name_;
std::string path_;
std::shared_ptr<arrow::DataType> type_;
State state_;
};
class KATANA_EXPORT RDGPartHeader {
public:
static katana::Result<RDGPartHeader> Make(const katana::Uri& partition_path);
katana::Result<void> Validate() const;
katana::Result<void> Write(
RDGHandle handle, WriteGroup* writes,
RDG::RDGVersioningPolicy retain_version) const;
/// Mark all in-memory properties dirty so that they can be written
/// out, copy out-of-memory properties
katana::Result<void> ChangeStorageLocation(
const katana::Uri& old_location, const katana::Uri& new_location);
//
// Property manipulation
//
katana::Result<std::vector<PropStorageInfo*>> SelectNodeProperties(
const std::optional<std::vector<std::string>>& names = std::nullopt) {
return SelectProperties(&node_prop_info_list_, names);
}
katana::Result<std::vector<PropStorageInfo*>> SelectEdgeProperties(
const std::optional<std::vector<std::string>>& names = std::nullopt) {
return SelectProperties(&edge_prop_info_list_, names);
}
katana::Result<std::vector<PropStorageInfo*>> SelectPartitionProperties() {
return SelectProperties(&part_prop_info_list_, std::nullopt);
}
void UpsertNodePropStorageInfo(PropStorageInfo&& pmd) {
auto pmd_it = std::find_if(
node_prop_info_list_.begin(), node_prop_info_list_.end(),
[&](const PropStorageInfo& my_pmd) {
return my_pmd.name() == pmd.name();
});
if (pmd_it == node_prop_info_list_.end()) {
node_prop_info_list_.emplace_back(std::move(pmd));
} else {
pmd_it->IsDirty();
}
}
void UpsertEdgePropStorageInfo(PropStorageInfo&& pmd) {
auto pmd_it = std::find_if(
edge_prop_info_list_.begin(), edge_prop_info_list_.end(),
[&](const PropStorageInfo& my_pmd) {
return my_pmd.name() == pmd.name();
});
if (pmd_it == edge_prop_info_list_.end()) {
edge_prop_info_list_.emplace_back(std::move(pmd));
} else {
pmd_it->IsDirty();
}
}
void RemoveNodeProperty(uint32_t i) {
auto& p = node_prop_info_list_;
KATANA_LOG_DEBUG_ASSERT(i < p.size());
p.erase(p.begin() + i);
}
katana::Result<void> RemoveNodeProperty(const std::string& name) {
auto& p = node_prop_info_list_;
auto it = std::find_if(p.begin(), p.end(), [&](const PropStorageInfo& psi) {
return psi.name() == name;
});
if (it == p.end()) {
return KATANA_ERROR(
tsuba::ErrorCode::PropertyNotFound, "no such node property");
}
p.erase(it);
return katana::ResultSuccess();
}
void RemoveEdgeProperty(uint32_t i) {
auto& p = edge_prop_info_list_;
KATANA_LOG_DEBUG_ASSERT(i < p.size());
p.erase(p.begin() + i);
}
katana::Result<void> RemoveEdgeProperty(const std::string& name) {
auto& p = edge_prop_info_list_;
auto it = std::find_if(p.begin(), p.end(), [&](const PropStorageInfo& psi) {
return psi.name() == name;
});
if (it == p.end()) {
return KATANA_ERROR(
tsuba::ErrorCode::PropertyNotFound, "no such edge property");
}
p.erase(it);
return katana::ResultSuccess();
}
//
// Accessors/Mutators
//
const std::string& topology_path() const { return topology_path_; }
void set_topology_path(std::string path) { topology_path_ = std::move(path); }
const std::vector<PropStorageInfo>& node_prop_info_list() const {
return node_prop_info_list_;
}
std::vector<PropStorageInfo>& node_prop_info_list() {
return node_prop_info_list_;
}
void set_node_prop_info_list(
std::vector<PropStorageInfo>&& node_prop_info_list) {
node_prop_info_list_ = std::move(node_prop_info_list);
}
const std::vector<PropStorageInfo>& edge_prop_info_list() const {
return edge_prop_info_list_;
}
std::vector<PropStorageInfo>& edge_prop_info_list() {
return edge_prop_info_list_;
}
void set_edge_prop_info_list(
std::vector<PropStorageInfo>&& edge_prop_info_list) {
edge_prop_info_list_ = std::move(edge_prop_info_list);
}
const std::vector<PropStorageInfo>& part_prop_info_list() const {
return part_prop_info_list_;
}
void set_part_properties(std::vector<PropStorageInfo>&& part_prop_info_list) {
part_prop_info_list_ = std::move(part_prop_info_list);
}
const std::vector<std::string>& node_property_indexes_column_name() const {
return node_property_indexes_column_name_;
}
std::vector<std::string>& node_property_indexes_column_name() {
return node_property_indexes_column_name_;
}
void set_node_property_indexes_column_name(
std::vector<std::string>& node_property_indexes_column_name) {
node_property_indexes_column_name_ =
std::move(node_property_indexes_column_name);
}
const std::vector<std::string>& edge_property_indexes_column_name() const {
return edge_property_indexes_column_name_;
}
std::vector<std::string>& edge_property_indexes_column_name() {
return edge_property_indexes_column_name_;
}
void set_edge_property_indexes_column_name(
std::vector<std::string>& edge_property_indexes_column_name) {
edge_property_indexes_column_name_ =
std::move(edge_property_indexes_column_name);
}
const PartitionMetadata& metadata() const { return metadata_; }
void set_metadata(const PartitionMetadata& metadata) { metadata_ = metadata; }
friend void to_json(nlohmann::json& j, const RDGPartHeader& header);
friend void from_json(const nlohmann::json& j, RDGPartHeader& header);
private:
static katana::Result<std::vector<PropStorageInfo*>> DoSelectProperties(
std::vector<PropStorageInfo>* storage_info,
const std::vector<std::string>& names) {
std::unordered_map<std::string, std::pair<PropStorageInfo*, bool>>
name_to_slot;
for (auto& prop : *storage_info) {
name_to_slot.emplace(prop.name(), std::make_pair(&prop, false));
}
std::vector<PropStorageInfo*> properties;
for (const auto& name : names) {
auto it = name_to_slot.find(name);
if (it == name_to_slot.end()) {
return KATANA_ERROR(
ErrorCode::PropertyNotFound, "no property named {}",
std::quoted(name));
}
if (it->second.second) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "property cannot be loaded twice ({})",
std::quoted(name));
}
it->second.second = true;
properties.emplace_back(it->second.first);
}
return properties;
}
static katana::Result<std::vector<PropStorageInfo*>> DoSelectProperties(
std::vector<PropStorageInfo>* storage_info) {
// all of the properties
std::vector<PropStorageInfo*> properties;
for (auto& prop : *storage_info) {
properties.emplace_back(&prop);
}
return properties;
}
static katana::Result<std::vector<PropStorageInfo*>> SelectProperties(
std::vector<PropStorageInfo>* storage_info,
const std::optional<std::vector<std::string>>& names) {
if (names) {
return DoSelectProperties(storage_info, names.value());
}
return DoSelectProperties(storage_info);
}
static katana::Result<RDGPartHeader> MakeJson(
const katana::Uri& partition_path);
std::vector<PropStorageInfo> part_prop_info_list_;
std::vector<PropStorageInfo> node_prop_info_list_;
std::vector<PropStorageInfo> edge_prop_info_list_;
/// Column Names to create property index from on startup
std::vector<std::string>
node_property_indexes_column_name_; //nhomann serializes this automagically. to/from json required if column name type is (in the future) changed from string to a custom one
std::vector<std::string>
edge_property_indexes_column_name_; //nhomann serializes this automagically. to/from json required if column name type is (in the future) changed from string to a custom one
/// Metadata filled in by CuSP, or from storage (meta partition file)
PartitionMetadata metadata_;
std::string topology_path_;
};
void to_json(nlohmann::json& j, const RDGPartHeader& header);
void from_json(const nlohmann::json& j, RDGPartHeader& header);
void to_json(nlohmann::json& j, const PropStorageInfo& propmd);
void from_json(const nlohmann::json& j, PropStorageInfo& propmd);
void to_json(nlohmann::json& j, const PartitionMetadata& propmd);
void from_json(const nlohmann::json& j, PartitionMetadata& propmd);
void to_json(
nlohmann::json& j, const std::vector<tsuba::PropStorageInfo>& vec_pmd);
} // namespace tsuba
#endif