1#include "duckdb/storage/buffer/buffer_handle.hpp"
2#include "duckdb/storage/buffer_manager.hpp"
3#include "duckdb/storage/buffer/block_handle.hpp"
4
5namespace duckdb {
6
7BufferHandle::BufferHandle() : handle(nullptr), node(nullptr) {
8}
9
10BufferHandle::BufferHandle(shared_ptr<BlockHandle> handle_p, FileBuffer *node_p)
11 : handle(std::move(handle_p)), node(node_p) {
12}
13
14BufferHandle::BufferHandle(BufferHandle &&other) noexcept {
15 std::swap(a&: node, b&: other.node);
16 std::swap(a&: handle, b&: other.handle);
17}
18
19BufferHandle &BufferHandle::operator=(BufferHandle &&other) noexcept {
20 std::swap(a&: node, b&: other.node);
21 std::swap(a&: handle, b&: other.handle);
22 return *this;
23}
24
25BufferHandle::~BufferHandle() {
26 Destroy();
27}
28
29bool BufferHandle::IsValid() const {
30 return node != nullptr;
31}
32
33void BufferHandle::Destroy() {
34 if (!handle || !IsValid()) {
35 return;
36 }
37 handle->block_manager.buffer_manager.Unpin(handle);
38 handle.reset();
39 node = nullptr;
40}
41
42FileBuffer &BufferHandle::GetFileBuffer() {
43 D_ASSERT(node);
44 return *node;
45}
46
47} // namespace duckdb
48