1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 #include "plasma/common.h"
23 #include "arrow/util/ubsan.h"
25 #include "plasma/plasma_generated.h"
27 namespace fb
= plasma::flatbuf
;
33 const char kErrorDetailTypeId
[] = "plasma::PlasmaStatusDetail";
35 class PlasmaStatusDetail
: public arrow::StatusDetail
{
37 explicit PlasmaStatusDetail(PlasmaErrorCode code
) : code_(code
) {}
38 const char* type_id() const override
{ return kErrorDetailTypeId
; }
39 std::string
ToString() const override
{
42 case PlasmaErrorCode::PlasmaObjectExists
:
43 type
= "Plasma object is exists";
45 case PlasmaErrorCode::PlasmaObjectNotFound
:
46 type
= "Plasma object is not found";
48 case PlasmaErrorCode::PlasmaStoreFull
:
49 type
= "Plasma store is full";
51 case PlasmaErrorCode::PlasmaObjectAlreadySealed
:
52 type
= "Plasma object is already sealed";
55 type
= "Unknown plasma error";
58 return std::string(type
);
60 PlasmaErrorCode
code() const { return code_
; }
63 PlasmaErrorCode code_
;
66 bool IsPlasmaStatus(const arrow::Status
& status
, PlasmaErrorCode code
) {
70 auto* detail
= status
.detail().get();
71 return detail
!= nullptr && detail
->type_id() == kErrorDetailTypeId
&&
72 static_cast<PlasmaStatusDetail
*>(detail
)->code() == code
;
79 arrow::Status
MakePlasmaError(PlasmaErrorCode code
, std::string message
) {
80 arrow::StatusCode arrow_code
= arrow::StatusCode::UnknownError
;
82 case PlasmaErrorCode::PlasmaObjectExists
:
83 arrow_code
= arrow::StatusCode::AlreadyExists
;
85 case PlasmaErrorCode::PlasmaObjectNotFound
:
86 arrow_code
= arrow::StatusCode::KeyError
;
88 case PlasmaErrorCode::PlasmaStoreFull
:
89 arrow_code
= arrow::StatusCode::CapacityError
;
91 case PlasmaErrorCode::PlasmaObjectAlreadySealed
:
93 arrow_code
= arrow::StatusCode::TypeError
;
96 return arrow::Status(arrow_code
, std::move(message
),
97 std::make_shared
<PlasmaStatusDetail
>(code
));
100 bool IsPlasmaObjectExists(const arrow::Status
& status
) {
101 return IsPlasmaStatus(status
, PlasmaErrorCode::PlasmaObjectExists
);
103 bool IsPlasmaObjectNotFound(const arrow::Status
& status
) {
104 return IsPlasmaStatus(status
, PlasmaErrorCode::PlasmaObjectNotFound
);
106 bool IsPlasmaObjectAlreadySealed(const arrow::Status
& status
) {
107 return IsPlasmaStatus(status
, PlasmaErrorCode::PlasmaObjectAlreadySealed
);
109 bool IsPlasmaStoreFull(const arrow::Status
& status
) {
110 return IsPlasmaStatus(status
, PlasmaErrorCode::PlasmaStoreFull
);
113 UniqueID
UniqueID::from_binary(const std::string
& binary
) {
115 std::memcpy(&id
, binary
.data(), sizeof(id
));
119 const uint8_t* UniqueID::data() const { return id_
; }
121 uint8_t* UniqueID::mutable_data() { return id_
; }
123 std::string
UniqueID::binary() const {
124 return std::string(reinterpret_cast<const char*>(id_
), kUniqueIDSize
);
127 std::string
UniqueID::hex() const {
128 constexpr char hex
[] = "0123456789abcdef";
130 for (int i
= 0; i
< kUniqueIDSize
; i
++) {
131 unsigned int val
= id_
[i
];
132 result
.push_back(hex
[val
>> 4]);
133 result
.push_back(hex
[val
& 0xf]);
138 // This code is from https://sites.google.com/site/murmurhash/
139 // and is public domain.
140 uint64_t MurmurHash64A(const void* key
, int len
, unsigned int seed
) {
141 const uint64_t m
= 0xc6a4a7935bd1e995;
144 uint64_t h
= seed
^ (len
* m
);
146 const uint64_t* data
= reinterpret_cast<const uint64_t*>(key
);
147 const uint64_t* end
= data
+ (len
/ 8);
149 while (data
!= end
) {
150 uint64_t k
= arrow::util::SafeLoad(data
++);
160 const unsigned char* data2
= reinterpret_cast<const unsigned char*>(data
);
164 h
^= uint64_t(data2
[6]) << 48; // fall through
166 h
^= uint64_t(data2
[5]) << 40; // fall through
168 h
^= uint64_t(data2
[4]) << 32; // fall through
170 h
^= uint64_t(data2
[3]) << 24; // fall through
172 h
^= uint64_t(data2
[2]) << 16; // fall through
174 h
^= uint64_t(data2
[1]) << 8; // fall through
176 h
^= uint64_t(data2
[0]);
187 size_t UniqueID::hash() const { return MurmurHash64A(&id_
[0], kUniqueIDSize
, 0); }
189 bool UniqueID::operator==(const UniqueID
& rhs
) const {
190 return std::memcmp(data(), rhs
.data(), kUniqueIDSize
) == 0;
193 const PlasmaStoreInfo
* plasma_config
;
195 } // namespace plasma