Flex  0.17.9
transaction_utils.h
Go to the documentation of this file.
1 
16 #ifndef GRAPHSCOPE_DATABASE_TRANSACTION_UTILS_H_
17 #define GRAPHSCOPE_DATABASE_TRANSACTION_UTILS_H_
18 
22 #include "glog/logging.h"
23 #include "grape/serialization/in_archive.h"
24 #include "grape/serialization/out_archive.h"
25 
26 namespace gs {
27 
28 inline void serialize_field(grape::InArchive& arc, const Any& prop) {
29  if (prop.type == PropertyType::Bool()) {
30  arc << prop.value.b;
31  } else if (prop.type == PropertyType::Int32()) {
32  arc << prop.value.i;
33  } else if (prop.type == PropertyType::UInt32()) {
34  arc << prop.value.ui;
35  } else if (prop.type == PropertyType::Date()) {
36  arc << prop.value.d.milli_second;
37  } else if (prop.type == PropertyType::Day()) {
38  arc << prop.value.day.to_u32();
39  } else if (prop.type.type_enum == impl::PropertyTypeImpl::kString) {
40  std::string_view s = *prop.value.s_ptr;
41  arc << s;
42  } else if (prop.type == PropertyType::StringView()) {
43  arc << prop.value.s;
44  } else if (prop.type == PropertyType::Int64()) {
45  arc << prop.value.l;
46  } else if (prop.type == PropertyType::UInt64()) {
47  arc << prop.value.ul;
48  } else if (prop.type == PropertyType::Double()) {
49  arc << prop.value.db;
50  } else if (prop.type == PropertyType::Float()) {
51  arc << prop.value.f;
52  } else if (prop.type == PropertyType::Empty()) {
53  } else if (prop.type == PropertyType::Record()) {
54  arc << prop.value.record.size();
55  for (auto& field : prop.value.record) {
56  serialize_field(arc, field);
57  }
58  } else {
59  LOG(FATAL) << "Unexpected property type" << int(prop.type.type_enum);
60  }
61 }
62 
63 inline void deserialize_field(grape::OutArchive& arc, Any& prop) {
64  if (prop.type == PropertyType::Bool()) {
65  arc >> prop.value.b;
66  } else if (prop.type == PropertyType::Int32()) {
67  arc >> prop.value.i;
68  } else if (prop.type == PropertyType::UInt32()) {
69  arc >> prop.value.ui;
70  } else if (prop.type == PropertyType::Date()) {
71  int64_t date_val;
72  arc >> date_val;
73  prop.value.d.milli_second = date_val;
74  } else if (prop.type == PropertyType::Day()) {
75  uint32_t val;
76  arc >> val;
77  prop.value.day.from_u32(val);
78  } else if (prop.type == PropertyType::StringView()) {
79  arc >> prop.value.s;
80  } else if (prop.type == PropertyType::Int64()) {
81  arc >> prop.value.l;
82  } else if (prop.type == PropertyType::UInt64()) {
83  arc >> prop.value.ul;
84  } else if (prop.type == PropertyType::Double()) {
85  arc >> prop.value.db;
86  } else if (prop.type == PropertyType::Float()) {
87  arc >> prop.value.f;
88  } else if (prop.type == PropertyType::Empty()) {
89  } else if (prop.type == PropertyType::Record()) {
90  size_t len;
91  arc >> len;
92  Record r(len);
93  for (size_t i = 0; i < r.len; ++i) {
94  deserialize_field(arc, r.props[i]);
95  }
96  prop.set_record(r);
97 
98  } else {
99  LOG(FATAL) << "Unexpected property type: "
100  << static_cast<int>(prop.type.type_enum);
101  }
102 }
103 
105  grape::OutArchive& arc, Any& oid) {
106  label_t label;
107  arc >> label;
108  oid.type = std::get<0>(graph.schema_.get_vertex_primary_key(label).at(0));
109  deserialize_field(arc, oid);
110  return label;
111 }
112 
113 class UpdateBatch {
114  public:
115  UpdateBatch() { arc_.Resize(sizeof(WalHeader)); }
116  UpdateBatch(const UpdateBatch& other) = delete;
117 
119  arc_.Clear();
120  update_vertices_.clear();
121  update_edges_.clear();
122  }
123  void clear() {
124  arc_.Clear();
125  update_vertices_.clear();
126  update_edges_.clear();
127  arc_.Resize(sizeof(WalHeader));
128  }
129  void AddVertex(label_t label, Any&& oid, std::vector<Any>&& props) {
130  arc_ << static_cast<uint8_t>(0) << label;
131  serialize_field(arc_, oid);
132  for (auto& prop : props) {
133  serialize_field(arc_, prop);
134  }
135  update_vertices_.emplace_back(label, std::move(oid), std::move(props));
136  }
137 
138  void AddEdge(label_t src_label, Any&& src, label_t dst_label, Any&& dst,
139  label_t edge_label, Any&& prop) {
140  arc_ << static_cast<uint8_t>(1) << src_label;
141  serialize_field(arc_, src);
142  arc_ << dst_label;
143  serialize_field(arc_, dst);
144  arc_ << edge_label;
145  serialize_field(arc_, prop);
146  update_edges_.emplace_back(src_label, std::move(src), dst_label,
147  std::move(dst), edge_label, std::move(prop));
148  }
149  const std::vector<std::tuple<label_t, Any, std::vector<Any>>>&
151  return update_vertices_;
152  }
153  const std::vector<std::tuple<label_t, Any, label_t, Any, label_t, Any>>&
154  GetUpdateEdges() const {
155  return update_edges_;
156  }
157  grape::InArchive& GetArc() { return arc_; }
158 
159  private:
160  std::vector<std::tuple<label_t, Any, std::vector<Any>>> update_vertices_;
161  std::vector<std::tuple<label_t, Any, label_t, Any, label_t, Any>>
163  grape::InArchive arc_;
164 };
165 
166 } // namespace gs
167 
168 #endif // GRAPHSCOPE_DATABASE_TRANSACTION_UTILS_H_
gs::AnyValue::db
double db
Definition: types.h:370
gs::UpdateBatch::GetUpdateVertices
const std::vector< std::tuple< label_t, Any, std::vector< Any > > > & GetUpdateVertices() const
Definition: transaction_utils.h:150
gs::PropertyType::UInt64
static PropertyType UInt64()
Definition: types.cc:302
gs::UpdateBatch::update_edges_
std::vector< std::tuple< label_t, Any, label_t, Any, label_t, Any > > update_edges_
Definition: transaction_utils.h:162
gs::MutablePropertyFragment::schema_
Schema schema_
Definition: mutable_property_fragment.h:115
gs::UpdateBatch::GetUpdateEdges
const std::vector< std::tuple< label_t, Any, label_t, Any, label_t, Any > > & GetUpdateEdges() const
Definition: transaction_utils.h:154
gs::AnyValue::s_ptr
StringPtr s_ptr
Definition: types.h:377
gs::Any
Definition: types.h:383
gs::AnyValue::l
int64_t l
Definition: types.h:362
gs::PropertyType::Empty
static PropertyType Empty()
Definition: types.cc:278
gs::AnyValue::record
Record record
Definition: types.h:376
types.h
gs::AnyValue::i
int32_t i
Definition: types.h:359
gs::UpdateBatch::UpdateBatch
UpdateBatch()
Definition: transaction_utils.h:115
gs::PropertyType::StringView
static PropertyType StringView()
Definition: types.cc:317
gs::PropertyType::UInt32
static PropertyType UInt32()
Definition: types.cc:293
gs::PropertyType::Bool
static PropertyType Bool()
Definition: types.cc:281
gs::Day::to_u32
uint32_t to_u32() const
Definition: types.cc:539
gs
Definition: adj_list.h:23
gs::PropertyType::type_enum
impl::PropertyTypeImpl type_enum
Definition: types.h:97
mutable_property_fragment.h
gs::Record::len
size_t len
Definition: types.h:314
gs::MutablePropertyFragment
Definition: mutable_property_fragment.h:37
gs::PropertyType::Date
static PropertyType Date()
Definition: types.cc:308
gs::Record::props
Any * props
Definition: types.h:315
gs::AnyValue::b
bool b
Definition: types.h:358
gs::PropertyType::Int64
static PropertyType Int64()
Definition: types.cc:299
gs::UpdateBatch::clear
void clear()
Definition: transaction_utils.h:123
gs::Record
Definition: types.h:300
gs::Any::value
AnyValue value
Definition: types.h:794
wal.h
gs::UpdateBatch::AddVertex
void AddVertex(label_t label, Any &&oid, std::vector< Any > &&props)
Definition: transaction_utils.h:129
gs::WalHeader
Definition: wal.h:33
gs::AnyValue::ui
uint32_t ui
Definition: types.h:360
gs::UpdateBatch::arc_
grape::InArchive arc_
Definition: transaction_utils.h:163
gs::PropertyType::Day
static PropertyType Day()
Definition: types.cc:311
gs::Any::set_record
void set_record(Record v)
Definition: types.h:552
gs::AnyValue::d
Date d
Definition: types.h:367
gs::AnyValue::s
std::string_view s
Definition: types.h:369
gs::PropertyType::Double
static PropertyType Double()
Definition: types.cc:305
gs::UpdateBatch::update_vertices_
std::vector< std::tuple< label_t, Any, std::vector< Any > > > update_vertices_
Definition: transaction_utils.h:160
gs::PropertyType::Record
static PropertyType Record()
Definition: types.cc:339
gs::Any::type
PropertyType type
Definition: types.h:793
gs::AnyValue::f
float f
Definition: types.h:361
gs::impl::PropertyTypeImpl::kString
@ kString
gs::deserialize_oid
label_t deserialize_oid(const MutablePropertyFragment &graph, grape::OutArchive &arc, Any &oid)
Definition: transaction_utils.h:104
gs::serialize_field
void serialize_field(grape::InArchive &arc, const Any &prop)
Definition: transaction_utils.h:28
gs::UpdateBatch::~UpdateBatch
~UpdateBatch()
Definition: transaction_utils.h:118
gs::AnyValue::ul
uint64_t ul
Definition: types.h:363
gs::Day::from_u32
void from_u32(uint32_t val)
Definition: types.cc:541
gs::UpdateBatch::AddEdge
void AddEdge(label_t src_label, Any &&src, label_t dst_label, Any &&dst, label_t edge_label, Any &&prop)
Definition: transaction_utils.h:138
gs::PropertyType::Int32
static PropertyType Int32()
Definition: types.cc:290
gs::label_t
uint8_t label_t
Definition: types.h:32
gs::deserialize_field
void deserialize_field(grape::OutArchive &arc, Any &prop)
Definition: transaction_utils.h:63
gs::PropertyType::Float
static PropertyType Float()
Definition: types.cc:296
gs::AnyValue::day
Day day
Definition: types.h:368
gs::UpdateBatch::GetArc
grape::InArchive & GetArc()
Definition: transaction_utils.h:157
gs::Schema::get_vertex_primary_key
const std::vector< std::tuple< PropertyType, std::string, size_t > > & get_vertex_primary_key(label_t index) const
Definition: schema.cc:376
gs::Record::size
size_t size() const
Definition: types.h:309
gs::UpdateBatch
Definition: transaction_utils.h:113