diff --git a/tensorflow_io/core/kernels/bigtable/serialization.cc b/tensorflow_io/core/kernels/bigtable/serialization.cc index 8a492e634..2a0bc8042 100644 --- a/tensorflow_io/core/kernels/bigtable/serialization.cc +++ b/tensorflow_io/core/kernels/bigtable/serialization.cc @@ -1,200 +1,200 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -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 - - 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 "tensorflow_io/core/kernels/bigtable/serialization.h" - -#include "tensorflow/core/platform/errors.h" -#include "tensorflow/core/platform/statusor.h" - -namespace tensorflow { -namespace io { -namespace { -#ifdef _WIN32 - -#include - -inline StatusOr BytesToBool(const std::string& bytes) { - union { - char byte; - bool res; - } u; - if (bytes.size() != 1U) { - return errors::InvalidArgument("Invalid bool representation."); - } - u.byte = bytes[0]; - return u.res; -} - -inline StatusOr BytesToInt32(const std::string& bytes) { - union { - char bytes[4]; - uint32_t res; - } u; - if (bytes.size() != 4U) { - return errors::InvalidArgument("Invalid int32 representation."); - } - memcpy(u.bytes, bytes.data(), 4); - return ntohl(u.res); -} - -inline StatusOr BytesToInt64(const std::string& bytes) { - union { - char bytes[8]; - uint32_t res; - } u; - if (bytes.size() != 8U) { - return errors::InvalidArgument("Invalid int64 representation."); - } - memcpy(u.bytes, bytes.data(), 8); - return ntohl(u.res); // <======= FIXME! BUG! HELP! -} - -inline StatusOr BytesToFloat(std::string const& s) { - auto const int_rep = BytesToInt32(s); - if (!int_rep.ok()) { - return int_rep; - } - union { - float res; - uint32_t int_rep; - } u; - u.int_rep = *int_rep; - return u.res; -} - -inline StatusOr BytesToDouble(std::string const& s) { - auto const int_rep = BytesToInt64(s); - if (!int_rep.ok()) { - return int_rep; - } - union { - double res; - uint64_t int_rep; - } u; - u.int_rep = *int_rep; - return u.res; -} -#else // _WIN32 - -#include "rpc/xdr.h" - -inline StatusOr BytesToFloat(std::string const& s) { - float v; - XDR xdrs; - xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); - if (!xdr_float(&xdrs, &v)) { - return errors::InvalidArgument("Error reading float from byte array."); - } - return v; -} - -inline StatusOr BytesToDouble(std::string const& s) { - double v; - XDR xdrs; - xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); - if (!xdr_double(&xdrs, &v)) { - return errors::InvalidArgument("Error reading double from byte array."); - } - return v; -} - -inline StatusOr BytesToInt64(std::string const& s) { - int64_t v; - XDR xdrs; - xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); - if (!xdr_int64_t(&xdrs, &v)) { - return errors::InvalidArgument("Error reading int64 from byte array."); - } - return v; -} - -inline StatusOr BytesToInt32(std::string const& s) { - int32_t v; - XDR xdrs; - xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); - if (!xdr_int32_t(&xdrs, &v)) { - return errors::InvalidArgument("Error reading int32 from byte array."); - } - return v; -} - -inline StatusOr BytesToBool(std::string const& s) { - bool_t v; - XDR xdrs; - xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); - if (!xdr_bool(&xdrs, &v)) { - return errors::InvalidArgument("Error reading bool from byte array."); - } - return v; -} - -#endif // _WIN32 -} // namespace -Status PutCellValueInTensor(Tensor& tensor, size_t index, DataType cell_type, - google::cloud::bigtable::Cell const& cell) { - switch (cell_type) { - case DT_STRING: { - auto tensor_data = tensor.tensor(); - tensor_data(index) = std::string(cell.value()); - } break; - case DT_BOOL: { - auto tensor_data = tensor.tensor(); - auto maybe_parsed_data = BytesToBool(cell.value()); - if (!maybe_parsed_data.ok()) { - return maybe_parsed_data.status(); - } - tensor_data(index) = maybe_parsed_data.ValueOrDie(); - } break; - case DT_INT32: { - auto tensor_data = tensor.tensor(); - auto maybe_parsed_data = BytesToInt32(cell.value()); - if (!maybe_parsed_data.ok()) { - return maybe_parsed_data.status(); - } - tensor_data(index) = maybe_parsed_data.ValueOrDie(); - } break; - case DT_INT64: { - auto tensor_data = tensor.tensor(); - auto maybe_parsed_data = BytesToInt64(cell.value()); - if (!maybe_parsed_data.ok()) { - return maybe_parsed_data.status(); - } - tensor_data(index) = maybe_parsed_data.ValueOrDie(); - } break; - case DT_FLOAT: { - auto tensor_data = tensor.tensor(); - auto maybe_parsed_data = BytesToFloat(cell.value()); - if (!maybe_parsed_data.ok()) { - return maybe_parsed_data.status(); - } - tensor_data(index) = maybe_parsed_data.ValueOrDie(); - } break; - case DT_DOUBLE: { - auto tensor_data = tensor.tensor(); - auto maybe_parsed_data = BytesToDouble(cell.value()); - if (!maybe_parsed_data.ok()) { - return maybe_parsed_data.status(); - } - tensor_data(index) = maybe_parsed_data.ValueOrDie(); - } break; - default: - return errors::Unimplemented("Data type not supported."); - } - return Status::OK(); -} - -} // namespace io -} // namespace tensorflow +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. + +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 + + 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 "tensorflow_io/core/kernels/bigtable/serialization.h" + +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/statusor.h" + +namespace tensorflow { +namespace io { +namespace { +#ifdef _WIN32 + +#include + +inline StatusOr BytesToBool(const std::string& bytes) { + union { + char byte; + bool res; + } u; + if (bytes.size() != 1U) { + return errors::InvalidArgument("Invalid bool representation."); + } + u.byte = bytes[0]; + return u.res; +} + +inline StatusOr BytesToInt32(const std::string& bytes) { + union { + char bytes[4]; + uint32_t res; + } u; + if (bytes.size() != 4U) { + return errors::InvalidArgument("Invalid int32 representation."); + } + memcpy(u.bytes, bytes.data(), 4); + return ntohl(u.res); +} + +inline StatusOr BytesToInt64(const std::string& bytes) { + union { + char bytes[8]; + uint32_t res; + } u; + if (bytes.size() != 8U) { + return errors::InvalidArgument("Invalid int64 representation."); + } + memcpy(u.bytes, bytes.data(), 8); + return ntohl(u.res); // <======= FIXME! BUG! HELP! +} + +inline StatusOr BytesToFloat(std::string const& s) { + auto const int_rep = BytesToInt32(s); + if (!int_rep.ok()) { + return int_rep; + } + union { + float res; + uint32_t int_rep; + } u; + u.int_rep = *int_rep; + return u.res; +} + +inline StatusOr BytesToDouble(std::string const& s) { + auto const int_rep = BytesToInt64(s); + if (!int_rep.ok()) { + return int_rep; + } + union { + double res; + uint64_t int_rep; + } u; + u.int_rep = *int_rep; + return u.res; +} +#else // _WIN32 + +#include "rpc/xdr.h" + +inline StatusOr BytesToFloat(std::string const& s) { + float v; + XDR xdrs; + xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); + if (!xdr_float(&xdrs, &v)) { + return errors::InvalidArgument("Error reading float from byte array."); + } + return v; +} + +inline StatusOr BytesToDouble(std::string const& s) { + double v; + XDR xdrs; + xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); + if (!xdr_double(&xdrs, &v)) { + return errors::InvalidArgument("Error reading double from byte array."); + } + return v; +} + +inline StatusOr BytesToInt64(std::string const& s) { + int64_t v; + XDR xdrs; + xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); + if (!xdr_int64_t(&xdrs, &v)) { + return errors::InvalidArgument("Error reading int64 from byte array."); + } + return v; +} + +inline StatusOr BytesToInt32(std::string const& s) { + int32_t v; + XDR xdrs; + xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); + if (!xdr_int32_t(&xdrs, &v)) { + return errors::InvalidArgument("Error reading int32 from byte array."); + } + return v; +} + +inline StatusOr BytesToBool(std::string const& s) { + bool_t v; + XDR xdrs; + xdrmem_create(&xdrs, const_cast(s.data()), sizeof(v), XDR_DECODE); + if (!xdr_bool(&xdrs, &v)) { + return errors::InvalidArgument("Error reading bool from byte array."); + } + return v; +} + +#endif // _WIN32 +} // namespace +Status PutCellValueInTensor(Tensor& tensor, size_t index, DataType cell_type, + google::cloud::bigtable::Cell const& cell) { + switch (cell_type) { + case DT_STRING: { + auto tensor_data = tensor.tensor(); + tensor_data(index) = std::string(cell.value()); + } break; + case DT_BOOL: { + auto tensor_data = tensor.tensor(); + auto maybe_parsed_data = BytesToBool(cell.value()); + if (!maybe_parsed_data.ok()) { + return maybe_parsed_data.status(); + } + tensor_data(index) = maybe_parsed_data.ValueOrDie(); + } break; + case DT_INT32: { + auto tensor_data = tensor.tensor(); + auto maybe_parsed_data = BytesToInt32(cell.value()); + if (!maybe_parsed_data.ok()) { + return maybe_parsed_data.status(); + } + tensor_data(index) = maybe_parsed_data.ValueOrDie(); + } break; + case DT_INT64: { + auto tensor_data = tensor.tensor(); + auto maybe_parsed_data = BytesToInt64(cell.value()); + if (!maybe_parsed_data.ok()) { + return maybe_parsed_data.status(); + } + tensor_data(index) = maybe_parsed_data.ValueOrDie(); + } break; + case DT_FLOAT: { + auto tensor_data = tensor.tensor(); + auto maybe_parsed_data = BytesToFloat(cell.value()); + if (!maybe_parsed_data.ok()) { + return maybe_parsed_data.status(); + } + tensor_data(index) = maybe_parsed_data.ValueOrDie(); + } break; + case DT_DOUBLE: { + auto tensor_data = tensor.tensor(); + auto maybe_parsed_data = BytesToDouble(cell.value()); + if (!maybe_parsed_data.ok()) { + return maybe_parsed_data.status(); + } + tensor_data(index) = maybe_parsed_data.ValueOrDie(); + } break; + default: + return errors::Unimplemented("Data type not supported."); + } + return Status::OK(); +} + +} // namespace io +} // namespace tensorflow