forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cc
More file actions
195 lines (161 loc) · 5.5 KB
/
Copy pathcommon.cc
File metadata and controls
195 lines (161 loc) · 5.5 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "plasma/common.h"
#include <limits>
#include <utility>
#include "arrow/util/ubsan.h"
#include "plasma/plasma_generated.h"
namespace fb = plasma::flatbuf;
namespace plasma {
namespace {
const char kErrorDetailTypeId[] = "plasma::PlasmaStatusDetail";
class PlasmaStatusDetail : public arrow::StatusDetail {
public:
explicit PlasmaStatusDetail(PlasmaErrorCode code) : code_(code) {}
const char* type_id() const override { return kErrorDetailTypeId; }
std::string ToString() const override {
const char* type;
switch (code()) {
case PlasmaErrorCode::PlasmaObjectExists:
type = "Plasma object is exists";
break;
case PlasmaErrorCode::PlasmaObjectNotFound:
type = "Plasma object is not found";
break;
case PlasmaErrorCode::PlasmaStoreFull:
type = "Plasma store is full";
break;
case PlasmaErrorCode::PlasmaObjectAlreadySealed:
type = "Plasma object is already sealed";
break;
default:
type = "Unknown plasma error";
break;
}
return std::string(type);
}
PlasmaErrorCode code() const { return code_; }
private:
PlasmaErrorCode code_;
};
bool IsPlasmaStatus(const arrow::Status& status, PlasmaErrorCode code) {
if (status.ok()) {
return false;
}
auto* detail = status.detail().get();
return detail != nullptr && detail->type_id() == kErrorDetailTypeId &&
static_cast<PlasmaStatusDetail*>(detail)->code() == code;
}
} // namespace
using arrow::Status;
arrow::Status MakePlasmaError(PlasmaErrorCode code, std::string message) {
arrow::StatusCode arrow_code = arrow::StatusCode::UnknownError;
switch (code) {
case PlasmaErrorCode::PlasmaObjectExists:
arrow_code = arrow::StatusCode::AlreadyExists;
break;
case PlasmaErrorCode::PlasmaObjectNotFound:
arrow_code = arrow::StatusCode::KeyError;
break;
case PlasmaErrorCode::PlasmaStoreFull:
arrow_code = arrow::StatusCode::CapacityError;
break;
case PlasmaErrorCode::PlasmaObjectAlreadySealed:
// Maybe a stretch?
arrow_code = arrow::StatusCode::TypeError;
break;
}
return arrow::Status(arrow_code, std::move(message),
std::make_shared<PlasmaStatusDetail>(code));
}
bool IsPlasmaObjectExists(const arrow::Status& status) {
return IsPlasmaStatus(status, PlasmaErrorCode::PlasmaObjectExists);
}
bool IsPlasmaObjectNotFound(const arrow::Status& status) {
return IsPlasmaStatus(status, PlasmaErrorCode::PlasmaObjectNotFound);
}
bool IsPlasmaObjectAlreadySealed(const arrow::Status& status) {
return IsPlasmaStatus(status, PlasmaErrorCode::PlasmaObjectAlreadySealed);
}
bool IsPlasmaStoreFull(const arrow::Status& status) {
return IsPlasmaStatus(status, PlasmaErrorCode::PlasmaStoreFull);
}
UniqueID UniqueID::from_binary(const std::string& binary) {
UniqueID id;
std::memcpy(&id, binary.data(), sizeof(id));
return id;
}
const uint8_t* UniqueID::data() const { return id_; }
uint8_t* UniqueID::mutable_data() { return id_; }
std::string UniqueID::binary() const {
return std::string(reinterpret_cast<const char*>(id_), kUniqueIDSize);
}
std::string UniqueID::hex() const {
constexpr char hex[] = "0123456789abcdef";
std::string result;
for (int i = 0; i < kUniqueIDSize; i++) {
unsigned int val = id_[i];
result.push_back(hex[val >> 4]);
result.push_back(hex[val & 0xf]);
}
return result;
}
// This code is from https://sites.google.com/site/murmurhash/
// and is public domain.
uint64_t MurmurHash64A(const void* key, int len, unsigned int seed) {
const uint64_t m = 0xc6a4a7935bd1e995;
const int r = 47;
uint64_t h = seed ^ (len * m);
const uint64_t* data = reinterpret_cast<const uint64_t*>(key);
const uint64_t* end = data + (len / 8);
while (data != end) {
uint64_t k = arrow::util::SafeLoad(data++);
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const unsigned char* data2 = reinterpret_cast<const unsigned char*>(data);
switch (len & 7) {
case 7:
h ^= uint64_t(data2[6]) << 48; // fall through
case 6:
h ^= uint64_t(data2[5]) << 40; // fall through
case 5:
h ^= uint64_t(data2[4]) << 32; // fall through
case 4:
h ^= uint64_t(data2[3]) << 24; // fall through
case 3:
h ^= uint64_t(data2[2]) << 16; // fall through
case 2:
h ^= uint64_t(data2[1]) << 8; // fall through
case 1:
h ^= uint64_t(data2[0]);
h *= m;
}
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
size_t UniqueID::hash() const { return MurmurHash64A(&id_[0], kUniqueIDSize, 0); }
bool UniqueID::operator==(const UniqueID& rhs) const {
return std::memcmp(data(), rhs.data(), kUniqueIDSize) == 0;
}
const PlasmaStoreInfo* plasma_config;
} // namespace plasma