Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ namespace margelo::nitro::rnnitrosqlite {

namespace {

constexpr size_t nodePadding = 24;

size_t getValueExternalMemorySize(const SQLiteValue& value) {
if (std::holds_alternative<std::string>(value)) {
return std::get<std::string>(value).capacity();
}

if (std::holds_alternative<std::shared_ptr<ArrayBuffer>>(value)) {
const auto& buffer = std::get<std::shared_ptr<ArrayBuffer>>(value);
if (buffer != nullptr && buffer->isOwner()) {
return buffer->size();
}
}

return 0;
}

/**
* Compute the approximate external memory size of a single result row.
* This includes:
Expand All @@ -18,9 +35,15 @@ namespace {
*/
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;
size_t nodesMemory = row.size() * (sizeof(SQLiteQueryResultRow::value_type) + nodePadding);
size_t contentsMemory = 0;

for (const auto& [columnName, value] : row) {
contentsMemory += columnName.capacity();
contentsMemory += getValueExternalMemorySize(value);
}

return bucketMemory + nodesMemory + contentsMemory;
}

/**
Expand Down Expand Up @@ -49,10 +72,10 @@ namespace {
* - Metadata contents, especially the `name` string on each metadata entry.
*/
size_t getMetadataExternalMemorySize(const SQLiteQueryTableMetadata& metadata) {
size_t size = 0;
size_t size = metadata.bucket_count() * sizeof(void*);
size += metadata.size() * (sizeof(SQLiteQueryTableMetadata::value_type) + nodePadding);

for (const auto& [columnName, columnMeta] : metadata) {

size += columnName.capacity();
size += columnMeta.name.capacity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
HybridNitroSQLiteQueryResult(SQLiteQueryResults results, std::optional<double> insertId, double rowsAffected,
std::optional<SQLiteQueryTableMetadata> metadata)
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(metadata) {}
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(std::move(metadata)) {}

private:
std::optional<double> _insertId;
Expand Down
47 changes: 43 additions & 4 deletions packages/react-native-nitro-sqlite/cpp/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ using namespace margelo::nitro::rnnitrosqlite;

namespace margelo::rnnitrosqlite {


static constexpr double kInt64MinAsDouble = static_cast<double>(std::numeric_limits<int64_t>::min());
static constexpr double kInt64UpperBoundAsDouble = -kInt64MinAsDouble;

Expand Down Expand Up @@ -128,8 +127,7 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
} else if (std::holds_alternative<double>(value)) {
// Bind whole numbers as INTEGER so vec0 rowid/pk/partition (which reject REAL) work; SQLite still coerces to REAL for REAL columns.
double doubleValue = std::get<double>(value);
if (std::trunc(doubleValue) == doubleValue && doubleValue >= kInt64MinAsDouble &&
doubleValue < kInt64UpperBoundAsDouble) {
if (std::trunc(doubleValue) == doubleValue && doubleValue >= kInt64MinAsDouble && doubleValue < kInt64UpperBoundAsDouble) {
sqlite3_bind_int64(statement, sqliteIndex, static_cast<sqlite3_int64>(doubleValue));
} else {
sqlite3_bind_double(statement, sqliteIndex, doubleValue);
Expand Down Expand Up @@ -258,7 +256,48 @@ std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& d

int rowsAffected = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return std::make_shared<HybridNitroSQLiteQueryResult>(results, static_cast<double>(latestInsertRowId), rowsAffected, metadata);
return std::make_shared<HybridNitroSQLiteQueryResult>(std::move(results), static_cast<double>(latestInsertRowId), rowsAffected,
std::move(metadata));
}

SQLiteOperationResult sqliteExecuteForRowsAffected(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
if (dbMap.count(dbName) == 0) {
throw NitroSQLiteException::DatabaseNotOpen(dbName);
}

auto db = dbMap[dbName];

sqlite3_stmt* statement;
int statementStatus = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);
if (statementStatus == SQLITE_OK) {
if (params) {
bindStatement(statement, *params);
}
} else {
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
}

bool isFailed = false;
while (true) {
int result = sqlite3_step(statement);
if (result == SQLITE_ROW) {
continue;
}
if (result != SQLITE_DONE) {
isFailed = true;
}
break;
}

sqlite3_finalize(statement);

if (isFailed) {
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
}

int rowsAffected = sqlite3_changes(db);
return {.rowsAffected = rowsAffected};
}

SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-nitro-sqlite/cpp/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void sqliteDetachDb(const std::string& mainDBName, const std::string& alias);
std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params);

SQLiteOperationResult sqliteExecuteForRowsAffected(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params);

SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query);

void sqliteCloseAll();
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-nitro-sqlite/cpp/sqliteExecuteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ SQLiteOperationResult sqliteExecuteBatch(const std::string& dbName, const std::v
int rowsAffected = 0;
sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
for (int i = 0; i < commandCount; i++) {
const auto command = commands.at(i);
const auto& command = commands.at(i);

// Batch only aggregates rowsAffected; per-command result rows are discarded.
auto result = sqliteExecute(dbName, command.sql, command.params);
rowsAffected += result->getRowsAffected();
auto result = sqliteExecuteForRowsAffected(dbName, command.sql, command.params);
rowsAffected += result.rowsAffected;
}
sqliteExecuteLiteral(dbName, "COMMIT");
return {
Expand Down
Loading