1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/storage/table/update_segment.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/storage/table/row_group.hpp"
12#include "duckdb/storage/storage_lock.hpp"
13#include "duckdb/storage/statistics/segment_statistics.hpp"
14#include "duckdb/common/types/string_heap.hpp"
15
16namespace duckdb {
17class ColumnData;
18class DataTable;
19class Vector;
20struct UpdateInfo;
21struct UpdateNode;
22
23class UpdateSegment {
24public:
25 UpdateSegment(ColumnData &column_data);
26 ~UpdateSegment();
27
28 ColumnData &column_data;
29
30public:
31 bool HasUpdates() const;
32 bool HasUncommittedUpdates(idx_t vector_index);
33 bool HasUpdates(idx_t vector_index) const;
34 bool HasUpdates(idx_t start_row_idx, idx_t end_row_idx);
35
36 void FetchUpdates(TransactionData transaction, idx_t vector_index, Vector &result);
37 void FetchCommitted(idx_t vector_index, Vector &result);
38 void FetchCommittedRange(idx_t start_row, idx_t count, Vector &result);
39 void Update(TransactionData transaction, idx_t column_index, Vector &update, row_t *ids, idx_t count,
40 Vector &base_data);
41 void FetchRow(TransactionData transaction, idx_t row_id, Vector &result, idx_t result_idx);
42
43 void RollbackUpdate(UpdateInfo &info);
44 void CleanupUpdateInternal(const StorageLockKey &lock, UpdateInfo &info);
45 void CleanupUpdate(UpdateInfo &info);
46
47 unique_ptr<BaseStatistics> GetStatistics();
48 StringHeap &GetStringHeap() {
49 return heap;
50 }
51
52private:
53 //! The lock for the update segment
54 StorageLock lock;
55 //! The root node (if any)
56 unique_ptr<UpdateNode> root;
57 //! Update statistics
58 SegmentStatistics stats;
59 //! Stats lock
60 mutex stats_lock;
61 //! Internal type size
62 idx_t type_size;
63 //! String heap, only used for strings
64 StringHeap heap;
65
66public:
67 typedef void (*initialize_update_function_t)(UpdateInfo *base_info, Vector &base_data, UpdateInfo *update_info,
68 Vector &update, const SelectionVector &sel);
69 typedef void (*merge_update_function_t)(UpdateInfo *base_info, Vector &base_data, UpdateInfo *update_info,
70 Vector &update, row_t *ids, idx_t count, const SelectionVector &sel);
71 typedef void (*fetch_update_function_t)(transaction_t start_time, transaction_t transaction_id, UpdateInfo *info,
72 Vector &result);
73 typedef void (*fetch_committed_function_t)(UpdateInfo *info, Vector &result);
74 typedef void (*fetch_committed_range_function_t)(UpdateInfo *info, idx_t start, idx_t end, idx_t result_offset,
75 Vector &result);
76 typedef void (*fetch_row_function_t)(transaction_t start_time, transaction_t transaction_id, UpdateInfo *info,
77 idx_t row_idx, Vector &result, idx_t result_idx);
78 typedef void (*rollback_update_function_t)(UpdateInfo &base_info, UpdateInfo &rollback_info);
79 typedef idx_t (*statistics_update_function_t)(UpdateSegment *segment, SegmentStatistics &stats, Vector &update,
80 idx_t count, SelectionVector &sel);
81
82private:
83 initialize_update_function_t initialize_update_function;
84 merge_update_function_t merge_update_function;
85 fetch_update_function_t fetch_update_function;
86 fetch_committed_function_t fetch_committed_function;
87 fetch_committed_range_function_t fetch_committed_range;
88 fetch_row_function_t fetch_row_function;
89 rollback_update_function_t rollback_update_function;
90 statistics_update_function_t statistics_update_function;
91
92private:
93 void InitializeUpdateInfo(UpdateInfo &info, row_t *ids, const SelectionVector &sel, idx_t count, idx_t vector_index,
94 idx_t vector_offset);
95};
96
97struct UpdateNodeData {
98 unique_ptr<UpdateInfo> info;
99 unsafe_unique_array<sel_t> tuples;
100 unsafe_unique_array<data_t> tuple_data;
101};
102
103struct UpdateNode {
104 unique_ptr<UpdateNodeData> info[RowGroup::ROW_GROUP_VECTOR_COUNT];
105};
106
107} // namespace duckdb
108