|
| 1 | +/** Copyright 2020 Alibaba Group Holding Limited. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "neug/utils/property/property.h" |
| 20 | + |
| 21 | +namespace neug { |
| 22 | +namespace execution { |
| 23 | + |
| 24 | +// OwnedProperty wraps a Property together with optional owned memory that |
| 25 | +// backs the Property's string_view (used for List, VARCHAR or other non-pod |
| 26 | +// types). This is needed because Property is a pure-view type that never owns |
| 27 | +// memory, but Value->Property conversion for List types produces a temporary |
| 28 | +// blob that must outlive the Property until the storage layer copies the data. |
| 29 | +struct OwnedProperty { |
| 30 | + OwnedProperty() = default; |
| 31 | + |
| 32 | + explicit OwnedProperty(Property p) : prop_(std::move(p)) {} |
| 33 | + OwnedProperty(Property p, std::string owned) |
| 34 | + : owned_buffer_(std::move(owned)), prop_(std::move(p)) { |
| 35 | + reseat(); |
| 36 | + } |
| 37 | + OwnedProperty(OwnedProperty&& other) noexcept |
| 38 | + : owned_buffer_(std::move(other.owned_buffer_)), prop_(other.prop_) { |
| 39 | + reseat(); |
| 40 | + } |
| 41 | + |
| 42 | + OwnedProperty& operator=(OwnedProperty&& other) noexcept { |
| 43 | + if (this != &other) { |
| 44 | + owned_buffer_ = std::move(other.owned_buffer_); |
| 45 | + prop_ = other.prop_; |
| 46 | + reseat(); |
| 47 | + } |
| 48 | + return *this; |
| 49 | + } |
| 50 | + |
| 51 | + // Copy constructor: deep-copy the buffer and re-seat |
| 52 | + OwnedProperty(const OwnedProperty& other) |
| 53 | + : owned_buffer_(other.owned_buffer_), prop_(other.prop_) { |
| 54 | + reseat(); |
| 55 | + } |
| 56 | + |
| 57 | + OwnedProperty& operator=(const OwnedProperty& other) { |
| 58 | + if (this != &other) { |
| 59 | + owned_buffer_ = other.owned_buffer_; |
| 60 | + prop_ = other.prop_; |
| 61 | + reseat(); |
| 62 | + } |
| 63 | + return *this; |
| 64 | + } |
| 65 | + |
| 66 | + // Access the underlying Property (for passing to storage layer APIs). |
| 67 | + const Property& prop() const { return prop_; } |
| 68 | + Property& prop() { return prop_; } |
| 69 | + |
| 70 | + private: |
| 71 | + void reseat() { |
| 72 | + if (!owned_buffer_.empty()) { |
| 73 | + if (prop_.type() == DataTypeId::kList) { |
| 74 | + prop_.set_list_data(owned_buffer_); |
| 75 | + } else if (prop_.type() == DataTypeId::kVarchar) { |
| 76 | + prop_.set_string_view(owned_buffer_); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + std::string owned_buffer_; |
| 82 | + Property prop_; |
| 83 | +}; |
| 84 | + |
| 85 | +} // namespace execution |
| 86 | +} // namespace neug |
0 commit comments