pivotal-jbarrett commented on a change in pull request #909: URL: https://github.com/apache/geode-native/pull/909#discussion_r787997787
########## File path: c-bindings/src/data_serializable_raw.cpp ########## @@ -0,0 +1,80 @@ +/* + * 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 <iterator> + +// C++ client public headers +#include "geode/DataInput.hpp" +#include "geode/DataOutput.hpp" +#include "geode/internal/DataSerializablePrimitive.hpp" + +// C client public headers +#include "data_serializable_raw.hpp" + +// C client private headers + +using apache::geode::client::DataInput; +using apache::geode::client::DataOutput; + +namespace apache { +namespace geode { +namespace client { +namespace internal { + +DataSerializableRaw::DataSerializableRaw(const int8_t* data, size_t size) { + bytes_.reserve(size); + std::copy(data, data + size, std::back_inserter(bytes_)); +} + +std::shared_ptr<DataSerializableRaw> DataSerializableRaw::create( + const int8_t* data, size_t size) { + return std::make_shared<DataSerializableRaw>(data, size); +} + +void DataSerializableRaw::toData(DataOutput& dataOutput) const { + dataOutput.writeBytesOnly(bytes_.data() + dsCodeSize_, + bytes_.size() - dsCodeSize_); +} + +void DataSerializableRaw::fromData(DataInput& dataInput) { + dataInput.readBytesOnly(bytes_.data() + dsCodeSize_, + bytes_.size() - dsCodeSize_); +} + +DSCode DataSerializableRaw::getDsCode() const { + return static_cast<DSCode>(bytes_[0]); +} + +bool DataSerializableRaw::operator==(const CacheableKey& other) const { + if (auto otherKey = dynamic_cast<const DataSerializableRaw*>(&other)) { + return bytes_ == otherKey->bytes_; + } + + return false; +} + +int32_t DataSerializableRaw::hashcode() const { + if (hashCode_ == 0) { + hashCode_ = internal::geode_hash<std::vector<int8_t>>{}(bytes_); Review comment: The hash is used to route the keys to the correct server. If the client and server don't agree on the hashing algorithm and result then they will silently work but it results in suboptimal operations with multiple hops. Equals is only going to be used client side for local caching, if caching is enabled. Hash maps require both a hash and equality function. The equality on the client and server don't have to match but given the hash needs to match it silly not to have the equality match. One problem that could exist if you don't use constant equality is that the server and client could disagree on what keys are in the caches. Imagine an object that has a field that is serialized, like timestamp, that isn't part of the equality or hash because its ephemeral but we hash the serialized form then the server and client won't agree on the hash value. Then if caching was enabled and equality is also based on the serialized form then the sever would only have one Key('foo') with some timestamp value X and the client could have Key('foo') with timestamp Y and Key('foo') with timestamp X. Local cache lookups would fail as timestamp advances, which could cause massive heap exhaustion. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
