|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the Apache 2.0 License. |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "ccf/kv/unit.h" |
| 6 | +#include "ccf/kv/untyped_map_handle.h" |
| 7 | + |
| 8 | +namespace ccf::kv |
| 9 | +{ |
| 10 | + /** Grants read access to a @c ccf::kv::UnitValue, as part of a @c |
| 11 | + * ccf::kv::Tx. |
| 12 | + */ |
| 13 | + template <typename Unit> |
| 14 | + class ReadableUnitValueHandle |
| 15 | + { |
| 16 | + protected: |
| 17 | + ccf::kv::untyped::MapHandle& read_handle; |
| 18 | + |
| 19 | + public: |
| 20 | + ReadableUnitValueHandle(ccf::kv::untyped::MapHandle& uh) : read_handle(uh) |
| 21 | + {} |
| 22 | + |
| 23 | + /** Test if the value has been touched. |
| 24 | + * |
| 25 | + * @return Boolean true iff the value is defined |
| 26 | + */ |
| 27 | + bool has() |
| 28 | + { |
| 29 | + return read_handle.has(Unit::get()); |
| 30 | + } |
| 31 | + |
| 32 | + /** Get version when this value was last written to, by a previous |
| 33 | + * transaction. |
| 34 | + * |
| 35 | + * @see ccf::kv::ReadableMapHandle::get_version_of_previous_write |
| 36 | + * |
| 37 | + * @return Optional containing version of applied transaction which last |
| 38 | + * wrote to this value, or nullopt if such a version does not exist |
| 39 | + */ |
| 40 | + std::optional<Version> get_version_of_previous_write() |
| 41 | + { |
| 42 | + return read_handle.get_version_of_previous_write(Unit::get()); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + /** Grants write access to a @c ccf::kv::UnitValue, as part of a @c |
| 47 | + * ccf::kv::Tx. |
| 48 | + */ |
| 49 | + template <typename Unit> |
| 50 | + class WriteableUnitValueHandle |
| 51 | + { |
| 52 | + protected: |
| 53 | + ccf::kv::untyped::MapHandle& write_handle; |
| 54 | + |
| 55 | + public: |
| 56 | + WriteableUnitValueHandle(ccf::kv::untyped::MapHandle& uh) : write_handle(uh) |
| 57 | + {} |
| 58 | + |
| 59 | + /** Touch this value. |
| 60 | + * |
| 61 | + * If this value was previously defined, it will be overwritten. Even if the |
| 62 | + * previous value was identical, this produces a serialised write in the |
| 63 | + * ledger. |
| 64 | + */ |
| 65 | + void touch() |
| 66 | + { |
| 67 | + write_handle.put(Unit::get(), Unit::get()); |
| 68 | + } |
| 69 | + |
| 70 | + /** Delete this value, restoring its original undefined state. |
| 71 | + */ |
| 72 | + void clear() |
| 73 | + { |
| 74 | + write_handle.clear(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + /** Grants read and write access to a @c ccf::kv::UnitValue, as part of a @c |
| 79 | + * ccf::kv::Tx. |
| 80 | + * |
| 81 | + * @see ccf::kv::ReadableUnitValueHandle |
| 82 | + * @see ccf::kv::WriteableUnitValueHandle |
| 83 | + */ |
| 84 | + template <typename Unit> |
| 85 | + class UnitValueHandle : public AbstractHandle, |
| 86 | + public ReadableUnitValueHandle<Unit>, |
| 87 | + public WriteableUnitValueHandle<Unit> |
| 88 | + { |
| 89 | + protected: |
| 90 | + ccf::kv::untyped::MapHandle untyped_handle; |
| 91 | + |
| 92 | + using ReadableBase = ReadableUnitValueHandle<Unit>; |
| 93 | + using WriteableBase = WriteableUnitValueHandle<Unit>; |
| 94 | + |
| 95 | + public: |
| 96 | + UnitValueHandle( |
| 97 | + ccf::kv::untyped::ChangeSet& changes, const std::string& map_name) : |
| 98 | + ReadableBase(untyped_handle), |
| 99 | + WriteableBase(untyped_handle), |
| 100 | + untyped_handle(changes, map_name) |
| 101 | + {} |
| 102 | + }; |
| 103 | +} |
0 commit comments