Skip to content

Commit

Permalink
wip: add backup function
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 8, 2025
1 parent 76b80b1 commit 6c61b4c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "async_wrap-inl.h"
#include "node_sqlite.h"
#include <path.h>
#include "base_object-inl.h"
Expand All @@ -9,13 +10,15 @@
#include "node_mem-inl.h"
#include "sqlite3.h"
#include "util-inl.h"
#include "threadpoolwork-inl.h"

#include <cinttypes>

namespace node {
namespace sqlite {

using v8::Array;
using v8::HandleScope;
using v8::ArrayBuffer;
using v8::BigInt;
using v8::Boolean;
Expand All @@ -40,6 +43,7 @@ using v8::NewStringType;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::Promise;
using v8::SideEffectType;
using v8::String;
using v8::TryCatch;
Expand Down Expand Up @@ -128,6 +132,54 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
isolate->ThrowException(error);
}

class BackupJob: public ThreadPoolWork {
public:
explicit BackupJob(Environment* env,
DatabaseSync* source,
Local<Promise::Resolver> resolver,
const char* source_name,
const char* destination_name,
int pages,
int sleep) : ThreadPoolWork(env, "node_sqlite3.BackupJob"),
env_(env),
source_(source),
/* resolver_(env->isolate(), resolver), */
source_name_(source_name),
destination_name_(destination_name),
pages_(pages),
sleep_(sleep) {
resolver_.Reset(env->isolate(), resolver);
}

void DoThreadPoolWork() override {
std::cout << "Gotta do a backup here" << std::endl;
}

void AfterThreadPoolWork(int status) override {
HandleScope handle_scope(env()->isolate());

if (!resolver_.IsEmpty()) {
Local<Promise::Resolver> resolver = Local<Promise::Resolver>::New(env()->isolate(), resolver_);
Local<String> message = String::NewFromUtf8(env()->isolate(), "Backup completed", NewStringType::kNormal).ToLocalChecked();

// Resolve the promise with a value (Null in this case)
resolver->Resolve(env()->context(), message).ToChecked();
}
}

private:

Environment* env() const { return env_; }

Environment* env_;
DatabaseSync* source_;
Global<Promise::Resolver> resolver_;
std::string source_name_;
std::string destination_name_;
int pages_;
int sleep_;
};

class UserDefinedFunction {
public:
explicit UserDefinedFunction(Environment* env,
Expand Down Expand Up @@ -533,6 +585,20 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
}

void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Environment* env = Environment::GetCurrent(args);
Local<Promise::Resolver> resolver = Promise::Resolver::New(env->context())
.ToLocalChecked()
.As<Promise::Resolver>();

args.GetReturnValue().Set(resolver->GetPromise());

BackupJob* job = new BackupJob(env, db, resolver, "main", "main", 100, 250);
job->ScheduleWork();
}

void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Expand Down Expand Up @@ -1718,6 +1784,7 @@ static void Initialize(Local<Object> target,
SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close);
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
SetProtoMethod(isolate, db_tmpl, "backup", DatabaseSync::Backup);
SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction);
SetProtoMethod(
isolate, db_tmpl, "createSession", DatabaseSync::CreateSession);
Expand Down
2 changes: 2 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DatabaseSync : public BaseObject {
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Backup(const v8::FunctionCallbackInfo<v8::Value>& args);
static void CustomFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
static void CreateSession(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ApplyChangeset(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -81,6 +82,7 @@ class DatabaseSync : public BaseObject {
bool enable_load_extension_;
sqlite3* connection_;

std::set<sqlite3_backup*> backups_;
std::set<sqlite3_session*> sessions_;
std::unordered_set<StatementSync*> statements_;

Expand Down

0 comments on commit 6c61b4c

Please sign in to comment.