Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
chunk_info_writer.cc
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "graphar/chunk_info_writer.h"
21 #include "graphar/filesystem.h"
22 #include "graphar/graph_info.h"
23 #include "graphar/result.h"
24 #include "graphar/types.h"
25 #include "graphar/util.h"
26 
27 namespace graphar {
28 
30  const std::shared_ptr<VertexInfo>& vertex_info, const std::string& prefix,
31  const ValidateLevel& validate_level)
32  : vertex_info_(vertex_info),
33  prefix_(prefix),
34  validate_level_(validate_level) {
35  if (validate_level_ == ValidateLevel::default_validate) {
36  throw std::runtime_error(
37  "default_validate is not allowed to be set as the global validate "
38  "level for VertexPropertyWriter");
39  }
40  GAR_ASSIGN_OR_RAISE_ERROR(fs_, FileSystemFromUriOrPath(prefix, &prefix_));
41 }
42 
43 // Check if the operation of copying a file as a chunk is allowed.
44 Status VertexChunkInfoWriter::validate(
45  const std::shared_ptr<PropertyGroup>& property_group, IdType chunk_index,
46  ValidateLevel validate_level) const {
47  // use the writer's validate level
48  if (validate_level == ValidateLevel::default_validate)
49  validate_level = validate_level_;
50  // no validate
51  if (validate_level == ValidateLevel::no_validate)
52  return Status::OK();
53  // weak & strong validate
54  if (!vertex_info_->HasPropertyGroup(property_group)) {
55  return Status::KeyError("The property group", " does not exist in ",
56  vertex_info_->GetLabel(), " vertex info.");
57  }
58  if (chunk_index < 0) {
59  return Status::IndexError("Negative chunk index ", chunk_index, ".");
60  }
61  return Status::OK();
62 }
63 
64 Status VertexChunkInfoWriter::WriteChunk(
65  const std::string& file_name,
66  const std::shared_ptr<PropertyGroup>& property_group, IdType chunk_index,
67  ValidateLevel validate_level) const {
68  GAR_RETURN_NOT_OK(validate(property_group, chunk_index, validate_level));
69  GAR_ASSIGN_OR_RAISE(auto suffix,
70  vertex_info_->GetFilePath(property_group, chunk_index));
71  std::string path = prefix_ + suffix;
72  return fs_->CopyFile(file_name, path);
73 }
74 
75 EdgeChunkInfoWriter::EdgeChunkInfoWriter(
76  const std::shared_ptr<EdgeInfo>& edge_info, const std::string& prefix,
77  AdjListType adj_list_type, const ValidateLevel& validate_level)
78  : edge_info_(edge_info),
79  adj_list_type_(adj_list_type),
80  validate_level_(validate_level) {
81  if (validate_level_ == ValidateLevel::default_validate) {
82  throw std::runtime_error(
83  "default_validate is not allowed to be set as the global validate "
84  "level for EdgeChunkWriter");
85  }
86  GAR_ASSIGN_OR_RAISE_ERROR(fs_, FileSystemFromUriOrPath(prefix, &prefix_));
87  chunk_size_ = edge_info_->GetChunkSize();
88  switch (adj_list_type) {
89  case AdjListType::unordered_by_source:
90  vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
91  break;
92  case AdjListType::ordered_by_source:
93  vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
94  break;
95  case AdjListType::unordered_by_dest:
96  vertex_chunk_size_ = edge_info_->GetDstChunkSize();
97  break;
98  case AdjListType::ordered_by_dest:
99  vertex_chunk_size_ = edge_info_->GetDstChunkSize();
100  break;
101  default:
102  vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
103  }
104 }
105 
106 // Check if the operation of writing number or copying a file is allowed.
107 Status EdgeChunkInfoWriter::validate(IdType count_or_index1,
108  IdType count_or_index2,
109  ValidateLevel validate_level) const {
110  // use the writer's validate level
111  if (validate_level == ValidateLevel::default_validate)
112  validate_level = validate_level_;
113  // no validate
114  if (validate_level == ValidateLevel::no_validate)
115  return Status::OK();
116  // weak & strong validate for adj list type
117  if (!edge_info_->HasAdjacentListType(adj_list_type_)) {
118  return Status::KeyError(
119  "Adj list type ", AdjListTypeToString(adj_list_type_),
120  " does not exist in the ", edge_info_->GetEdgeLabel(), " edge info.");
121  }
122  // weak & strong validate for count or index
123  if (count_or_index1 < 0 || count_or_index2 < 0) {
124  return Status::IndexError(
125  "The count or index must be non-negative, but got ", count_or_index1,
126  " and ", count_or_index2, ".");
127  }
128  return Status::OK();
129 }
130 
131 // Check if the operation of copying a file as a property chunk is allowed.
132 Status EdgeChunkInfoWriter::validate(
133  const std::shared_ptr<PropertyGroup>& property_group,
134  IdType vertex_chunk_index, IdType chunk_index,
135  ValidateLevel validate_level) const {
136  // use the writer's validate level
137  if (validate_level == ValidateLevel::default_validate)
138  validate_level = validate_level_;
139  // no validate
140  if (validate_level == ValidateLevel::no_validate)
141  return Status::OK();
142  // validate for adj list type & index
143  GAR_RETURN_NOT_OK(validate(vertex_chunk_index, chunk_index, validate_level));
144  // weak & strong validate for property group
145  if (!edge_info_->HasPropertyGroup(property_group)) {
146  return Status::KeyError("Property group", " does not exist in the ",
147  edge_info_->GetEdgeLabel(), " edge info.");
148  }
149  return Status::OK();
150 }
151 
152 Status EdgeChunkInfoWriter::WriteAdjListChunk(
153  const std::string& file_name, IdType vertex_chunk_index, IdType chunk_index,
154  ValidateLevel validate_level) const {
155  GAR_RETURN_NOT_OK(validate(vertex_chunk_index, chunk_index, validate_level));
156  GAR_ASSIGN_OR_RAISE(
157  auto suffix, edge_info_->GetAdjListFilePath(vertex_chunk_index,
158  chunk_index, adj_list_type_));
159  std::string path = prefix_ + suffix;
160  return fs_->CopyFile(file_name, path);
161 }
162 
163 Status EdgeChunkInfoWriter::WriteOffsetChunk(
164  const std::string& file_name, IdType vertex_chunk_index,
165  ValidateLevel validate_level) const {
166  GAR_RETURN_NOT_OK(validate(vertex_chunk_index, 0, validate_level));
167  GAR_ASSIGN_OR_RAISE(auto suffix, edge_info_->GetAdjListOffsetFilePath(
168  vertex_chunk_index, adj_list_type_));
169  std::string path = prefix_ + suffix;
170  return fs_->CopyFile(file_name, path);
171 }
172 
173 Status EdgeChunkInfoWriter::WritePropertyChunk(
174  const std::string& file_name,
175  const std::shared_ptr<PropertyGroup>& property_group,
176  IdType vertex_chunk_index, IdType chunk_index,
177  ValidateLevel validate_level) const {
178  GAR_RETURN_NOT_OK(validate(property_group, vertex_chunk_index, chunk_index,
179  validate_level));
180  GAR_ASSIGN_OR_RAISE(auto suffix, edge_info_->GetPropertyFilePath(
181  property_group, adj_list_type_,
182  vertex_chunk_index, chunk_index));
183  std::string path = prefix_ + suffix;
184  return fs_->CopyFile(file_name, path);
185 }
186 } // namespace graphar
Status outcome object (success or error)
Definition: status.h:123
static Status IndexError(Args &&... args)
Definition: status.h:197
static Status KeyError(Args &&... args)
Definition: status.h:172
static Status OK()
Definition: status.h:157
VertexChunkInfoWriter(const std::shared_ptr< VertexInfo > &vertex_info, const std::string &prefix, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Copy a file as a vertex property group chunk.