2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Atomic.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_FDBCLIENT_ATOMIC_H
|
|
|
|
#define FLOW_FDBCLIENT_ATOMIC_H
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/CommitTransaction.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doLittleEndianAdd(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if(!existingValue.size()) return otherOperand;
|
|
|
|
if(!otherOperand.size()) return otherOperand;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
uint8_t* buf = new (ar) uint8_t [otherOperand.size()];
|
|
|
|
int i = 0;
|
|
|
|
int carry = 0;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
for(i = 0; i<std::min(existingValue.size(), otherOperand.size()); i++) {
|
|
|
|
int sum = existingValue[i] + otherOperand[i] + carry;
|
|
|
|
buf[i] = sum;
|
|
|
|
carry = sum >> 8;
|
|
|
|
}
|
|
|
|
for (; i<otherOperand.size(); i++) {
|
|
|
|
int sum = otherOperand[i] + carry;
|
|
|
|
buf[i] = sum;
|
|
|
|
carry = sum >> 8;
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:18 +08:00
|
|
|
return StringRef(buf, i);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doAnd(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if(!otherOperand.size()) return otherOperand;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
uint8_t* buf = new (ar) uint8_t [otherOperand.size()];
|
|
|
|
int i = 0;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
for(i = 0; i<std::min(existingValue.size(), otherOperand.size()); i++)
|
|
|
|
buf[i] = existingValue[i] & otherOperand[i];
|
|
|
|
for(; i<otherOperand.size(); i++)
|
|
|
|
buf[i] = 0x0;
|
|
|
|
|
|
|
|
return StringRef(buf, i);
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doAndV2(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
if (!existingValueOptional.present())
|
2017-10-11 04:02:22 +08:00
|
|
|
return otherOperand;
|
|
|
|
|
2017-10-24 07:48:13 +08:00
|
|
|
return doAnd(existingValueOptional, otherOperand, ar);
|
2017-10-11 04:02:22 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doOr(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if(!existingValue.size()) return otherOperand;
|
|
|
|
if(!otherOperand.size()) return otherOperand;
|
|
|
|
|
|
|
|
uint8_t* buf = new (ar) uint8_t [otherOperand.size()];
|
|
|
|
int i = 0;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
for(i = 0; i<std::min(existingValue.size(), otherOperand.size()); i++)
|
|
|
|
buf[i] = existingValue[i] | otherOperand[i];
|
|
|
|
for(; i<otherOperand.size(); i++)
|
|
|
|
buf[i] = otherOperand[i];
|
|
|
|
|
|
|
|
return StringRef(buf, i);
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doXor(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if(!existingValue.size()) return otherOperand;
|
|
|
|
if(!otherOperand.size()) return otherOperand;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
uint8_t* buf = new (ar) uint8_t [otherOperand.size()];
|
|
|
|
int i = 0;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
for(i = 0; i<std::min(existingValue.size(), otherOperand.size()); i++)
|
|
|
|
buf[i] = existingValue[i] ^ otherOperand[i];
|
|
|
|
|
|
|
|
for(; i<otherOperand.size(); i++)
|
|
|
|
buf[i] = otherOperand[i];
|
|
|
|
|
|
|
|
return StringRef(buf, i);
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doAppendIfFits(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if(!existingValue.size()) return otherOperand;
|
|
|
|
if(!otherOperand.size()) return existingValue;
|
|
|
|
if(existingValue.size() + otherOperand.size() > CLIENT_KNOBS->VALUE_SIZE_LIMIT) {
|
|
|
|
TEST( true ) //AppendIfFIts resulted in truncation
|
|
|
|
return existingValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* buf = new (ar) uint8_t [existingValue.size() + otherOperand.size()];
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for(i = 0; i<existingValue.size(); i++)
|
|
|
|
buf[i] = existingValue[i];
|
|
|
|
|
|
|
|
for(j = 0; j<otherOperand.size(); j++)
|
|
|
|
buf[i+j] = otherOperand[j];
|
|
|
|
|
|
|
|
return StringRef(buf, i+j);
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doMax(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
if (!existingValue.size()) return otherOperand;
|
|
|
|
if (!otherOperand.size()) return otherOperand;
|
|
|
|
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for (i = otherOperand.size() - 1; i >= existingValue.size(); i--) {
|
|
|
|
if (otherOperand[i] != 0) {
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i >= 0; i--) {
|
|
|
|
if (otherOperand[i] > existingValue[i]) {
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
else if (otherOperand[i] < existingValue[i]) {
|
|
|
|
uint8_t* buf = new (ar) uint8_t [otherOperand.size()];
|
|
|
|
for (j = 0; j < std::min(existingValue.size(), otherOperand.size()); j++) {
|
|
|
|
buf[j] = existingValue[j];
|
|
|
|
}
|
|
|
|
for (; j < otherOperand.size(); j++) {
|
|
|
|
buf[j] = 0x0;
|
|
|
|
}
|
|
|
|
return StringRef(buf, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doByteMax(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
if (!existingValueOptional.present()) return otherOperand;
|
2017-10-11 04:02:22 +08:00
|
|
|
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.get();
|
2017-10-11 04:02:22 +08:00
|
|
|
if (existingValue > otherOperand)
|
|
|
|
return existingValue;
|
|
|
|
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doMin(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (!otherOperand.size()) return otherOperand;
|
|
|
|
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.present() ? existingValueOptional.get() : StringRef();
|
2017-05-26 04:48:44 +08:00
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for (i = otherOperand.size() - 1; i >= existingValue.size(); i--) {
|
|
|
|
if (otherOperand[i] != 0) {
|
|
|
|
uint8_t* buf = new (ar)uint8_t[otherOperand.size()];
|
|
|
|
for (j = 0; j < std::min(existingValue.size(), otherOperand.size()); j++) {
|
|
|
|
buf[j] = existingValue[j];
|
|
|
|
}
|
|
|
|
for (; j < otherOperand.size(); j++) {
|
|
|
|
buf[j] = 0x0;
|
|
|
|
}
|
|
|
|
return StringRef(buf, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i >= 0; i--) {
|
|
|
|
if (otherOperand[i] > existingValue[i]) {
|
|
|
|
uint8_t* buf = new (ar)uint8_t[otherOperand.size()];
|
|
|
|
for (j = 0; j < std::min(existingValue.size(), otherOperand.size()); j++) {
|
|
|
|
buf[j] = existingValue[j];
|
|
|
|
}
|
|
|
|
for (; j < otherOperand.size(); j++) {
|
|
|
|
buf[j] = 0x0;
|
|
|
|
}
|
|
|
|
return StringRef(buf, j);
|
|
|
|
}
|
|
|
|
else if (otherOperand[i] < existingValue[i]) {
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return otherOperand;
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doMinV2(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
if (!existingValueOptional.present())
|
2017-10-11 04:02:22 +08:00
|
|
|
return otherOperand;
|
|
|
|
|
2017-10-24 07:48:13 +08:00
|
|
|
return doMin(existingValueOptional, otherOperand, ar);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline ValueRef doByteMin(const Optional<ValueRef>& existingValueOptional, const ValueRef& otherOperand, Arena& ar) {
|
2017-10-24 07:48:13 +08:00
|
|
|
if (!existingValueOptional.present()) return otherOperand;
|
2020-02-05 02:26:18 +08:00
|
|
|
|
2017-10-24 07:48:13 +08:00
|
|
|
const ValueRef& existingValue = existingValueOptional.get();
|
2017-10-11 04:02:22 +08:00
|
|
|
if (existingValue < otherOperand)
|
|
|
|
return existingValue;
|
|
|
|
|
|
|
|
return otherOperand;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline Optional<ValueRef> doCompareAndClear(const Optional<ValueRef>& existingValueOptional,
|
2019-01-31 17:23:32 +08:00
|
|
|
const ValueRef& otherOperand, Arena& ar) {
|
|
|
|
if (!existingValueOptional.present() || existingValueOptional.get() == otherOperand) {
|
|
|
|
// Clear the value.
|
|
|
|
return Optional<ValueRef>();
|
|
|
|
}
|
|
|
|
return existingValueOptional; // No change required.
|
|
|
|
}
|
|
|
|
|
2020-01-16 11:15:35 +08:00
|
|
|
static void placeVersionstamp( uint8_t* destination, Version version, uint16_t transactionNumber ) {
|
|
|
|
version = bigEndian64(version);
|
|
|
|
transactionNumber = bigEndian16(transactionNumber);
|
|
|
|
static_assert( sizeof(version) == 8, "version size mismatch" );
|
|
|
|
memcpy( destination, &version, sizeof(version) );
|
|
|
|
static_assert( sizeof(transactionNumber) == 2, "txn num size mismatch");
|
|
|
|
memcpy( destination + sizeof(version), &transactionNumber, sizeof(transactionNumber) );
|
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Returns the range corresponding to the specified versionstamp key.
|
|
|
|
*/
|
2020-02-05 02:26:18 +08:00
|
|
|
inline KeyRangeRef getVersionstampKeyRange(Arena& arena, const KeyRef &key, Version minVersion, const KeyRef &maxKey) {
|
2017-05-26 04:48:44 +08:00
|
|
|
KeyRef begin(arena, key);
|
|
|
|
KeyRef end(arena, key);
|
|
|
|
|
2018-03-22 09:58:19 +08:00
|
|
|
if (begin.size() < 4)
|
2017-05-26 04:48:44 +08:00
|
|
|
throw client_invalid_operation();
|
|
|
|
|
2018-03-22 09:58:19 +08:00
|
|
|
int32_t pos;
|
|
|
|
memcpy(&pos, begin.end() - sizeof(int32_t), sizeof(int32_t));
|
|
|
|
pos = littleEndian32(pos);
|
|
|
|
begin = begin.substr(0, begin.size() - 4);
|
|
|
|
end = end.substr(0, end.size() - 3);
|
2017-05-26 04:48:44 +08:00
|
|
|
mutateString(end)[end.size()-1] = 0;
|
|
|
|
|
|
|
|
if (pos < 0 || pos + 10 > begin.size())
|
|
|
|
throw client_invalid_operation();
|
|
|
|
|
2020-01-16 11:15:35 +08:00
|
|
|
placeVersionstamp(mutateString(begin)+pos, minVersion, 0);
|
2017-05-26 04:48:44 +08:00
|
|
|
memset(mutateString(end) + pos, '\xff', 10);
|
|
|
|
|
|
|
|
return KeyRangeRef(begin, std::min(end, maxKey));
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:18 +08:00
|
|
|
inline void transformVersionstampKey( StringRef& key, Version version, uint16_t transactionNumber ) {
|
2020-01-22 07:35:27 +08:00
|
|
|
if (key.size() < 4)
|
|
|
|
throw client_invalid_operation();
|
|
|
|
|
2020-01-22 07:24:57 +08:00
|
|
|
int32_t pos;
|
|
|
|
memcpy(&pos, key.end() - sizeof(int32_t), sizeof(int32_t));
|
|
|
|
pos = littleEndian32(pos);
|
|
|
|
if (pos < 0 || pos + 10 > key.size())
|
|
|
|
throw client_invalid_operation();
|
|
|
|
|
|
|
|
placeVersionstamp( mutateString(key) + pos, version, transactionNumber );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:29:01 +08:00
|
|
|
inline void transformVersionstampMutation( MutationRef& mutation, StringRef MutationRef::* param, Version version, uint16_t transactionNumber ) {
|
2018-03-22 09:58:19 +08:00
|
|
|
if ((mutation.*param).size() >= 4) {
|
2018-02-16 13:35:00 +08:00
|
|
|
int32_t pos;
|
2018-03-22 09:58:19 +08:00
|
|
|
memcpy(&pos, (mutation.*param).end() - sizeof(int32_t), sizeof(int32_t));
|
2018-02-16 13:35:00 +08:00
|
|
|
pos = littleEndian32(pos);
|
2018-03-22 09:58:19 +08:00
|
|
|
mutation.*param = (mutation.*param).substr(0, (mutation.*param).size() - 4);
|
2018-02-16 13:35:00 +08:00
|
|
|
|
2018-03-22 09:58:19 +08:00
|
|
|
if (pos >= 0 && pos + 10 <= (mutation.*param).size()) {
|
|
|
|
placeVersionstamp( mutateString(mutation.*param) + pos, version, transactionNumber );
|
2018-02-16 13:35:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutation.type = MutationRef::SetValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|