-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHybridNitroSQLiteQueryResult.cpp
More file actions
93 lines (72 loc) · 2.44 KB
/
HybridNitroSQLiteQueryResult.cpp
File metadata and controls
93 lines (72 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "HybridNitroSQLiteQueryResult.hpp"
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/Null.hpp>
#include <NitroModules/Promise.hpp>
#include <unordered_map>
#include <variant>
#include <vector>
namespace margelo::nitro::rnnitrosqlite {
namespace {
/**
* Compute the approximate external memory size of a single result row.
* This includes:
* - Column name string capacities,
* - Heap usage for the actual SQLiteValue contents.
*/
size_t getRowExternalMemorySize(const SQLiteQueryResultRow& row) {
size_t bucketMemory = row.bucket_count() * sizeof(void*);
constexpr size_t nodePadding = 24;
size_t nodesMemory = row.size() * (sizeof(std::pair<std::string, SQLiteValue>) * nodePadding);
return bucketMemory + nodesMemory;
}
/**
* Compute the approximate external memory size of the full result set.
* We add:
* - The vector's backing storage,
* - All rows (column names + values).
*/
size_t getResultsExternalMemorySize(const SQLiteQueryResults& results) {
size_t size = sizeof(SQLiteQueryResults);
const auto resultCapacity = results.capacity();
size += resultCapacity * sizeof(SQLiteQueryResultRow);
for (const auto& row : results) {
size += getRowExternalMemorySize(row);
}
return size;
}
/**
* Compute the approximate external memory size of the table metadata.
* We include:
* - Column name string capacities (map keys),
* - Metadata contents, especially the `name` string on each metadata entry.
*/
size_t getMetadataExternalMemorySize(const SQLiteQueryTableMetadata& metadata) {
size_t size = 0;
for (const auto& [columnName, columnMeta] : metadata) {
size += columnName.capacity();
size += columnMeta.name.capacity();
}
return size;
}
} // namespace
std::optional<double> HybridNitroSQLiteQueryResult::getInsertId() {
return _insertId;
}
double HybridNitroSQLiteQueryResult::getRowsAffected() {
return _rowsAffected;
}
SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
return _results;
};
std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
return _metadata;
}
size_t HybridNitroSQLiteQueryResult::getExternalMemorySize() noexcept {
size_t size = sizeof(*this);
size += getResultsExternalMemorySize(_results);
if (_metadata) {
size += getMetadataExternalMemorySize(*_metadata);
}
return size;
}
} // namespace margelo::nitro::rnnitrosqlite