<commit_before>
#include <node.h> 
#include <node_buffer.h>
#include "database.h"

namespace bangdown {
static v8::Persistent<v8::FunctionTemplate> database_constructor;

Database::Database (char* name) : name(name) {
  this->OpenDatabase(name);
};

Database::~Database () {
  if (bangdb != NULL)
    bangdb->closedatabase();
    delete bangdb;
  delete[] name;
};

database* Database::OpenDatabase (
  char* location
    ) {
  bangdb = new database((char*)location);
  printf("%s Created\n", bangdb->getdbname());
  return bangdb;
}

int Database::CloseDatabase () {
  bangdb->closedatabase();
  delete bangdb;
}

void Database::Init () {
  v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(Database::New);
  NanAssignPersistent(v8::FunctionTemplate, database_constructor, tpl);
  tpl->SetClassName(NanSymbol("Database"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  NODE_SET_PROTOTYPE_METHOD(tpl, "open", Database::Open);
  //NODE_SET_PROTOTYPE_METHOD(tpl, "new", Database::New);
  NODE_SET_PROTOTYPE_METHOD(tpl, "put", Database::Put);
  NODE_SET_PROTOTYPE_METHOD(tpl, "get", Database::Get);
  NODE_SET_PROTOTYPE_METHOD(tpl, "close", Database::Close);
  NODE_SET_PROTOTYPE_METHOD(tpl, "free", Database::Free);
}

/*
TODO: Convert from using single table
To a list of tables.
*/
int Database::OpenTable (char* tablename) {
  bangtable = bangdb->gettable(tablename);
  bangconnection = bangtable->getconnection();
  return 1;
}

int Database::CloseTable (char* tablename) {
  if (bangconnection != NULL) {
    bangconnection->closeconnection();
    bangconnection = NULL;
  }

  if (bangtable != NULL) {
    bangtable->closetable();
    bangtable = NULL;
  }

}

int Database::PutValue(char* key, char* val) {

  FDT ikey, ival;

  ikey.data = key;
  ikey.length = strlen(key);
  ival.data = (void*)val;
  ival.length = strlen(val);

  int flag = bangconnection->put(&ikey, &ival, INSERT_UPDATE); 
  return flag;
}

FDT* Database::GetValue(char* key) { 

  FDT ikey;

  ikey.data = key;
  ikey.length = strlen(key);

  FDT* result = bangconnection->get(&ikey);
 

  return result;
}

NAN_METHOD(Database::New) {
  NanScope();

  if (args.Length() == 0)
    return NanThrowError("constructor requires at least a location argument");

  if (!args[0]->IsString())
    return NanThrowError("constructor requires a location string argument");

  char* name = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  Database* obj = new Database(name);
  obj->Wrap(args.This());
  printf("NAME: %s \n", name);
//  NanReturnUndefined();
  NanReturnValue(args.This());
}
NAN_METHOD(Database::Open) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());
 
  char* tablename = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  _db->OpenTable(tablename);

  
  NanReturnUndefined();
}

NAN_METHOD(Database::Close) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());
 
  char* tablename = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  _db->CloseTable(tablename);

  NanReturnUndefined();
}

NAN_METHOD(Database::Free) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());

  _db->CloseDatabase(); <commit_msg>Added returns<commit_after>
#include <node.h> 
#include <node_buffer.h>
#include "database.h"

namespace bangdown {
static v8::Persistent<v8::FunctionTemplate> database_constructor;

Database::Database (char* name) : name(name) {
  this->OpenDatabase(name);
};

Database::~Database () {
  if (bangdb != NULL)
    bangdb->closedatabase();
    delete bangdb;
  delete[] name;
};

database* Database::OpenDatabase (
  char* location
    ) {
  bangdb = new database((char*)location);
  printf("%s Created\n", bangdb->getdbname());
  return bangdb;
}

int Database::CloseDatabase () {
  bangdb->closedatabase();
  delete bangdb;
  
  return 1;
}

void Database::Init () {
  v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(Database::New);
  NanAssignPersistent(v8::FunctionTemplate, database_constructor, tpl);
  tpl->SetClassName(NanSymbol("Database"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  NODE_SET_PROTOTYPE_METHOD(tpl, "open", Database::Open);
  //NODE_SET_PROTOTYPE_METHOD(tpl, "new", Database::New);
  NODE_SET_PROTOTYPE_METHOD(tpl, "put", Database::Put);
  NODE_SET_PROTOTYPE_METHOD(tpl, "get", Database::Get);
  NODE_SET_PROTOTYPE_METHOD(tpl, "close", Database::Close);
  NODE_SET_PROTOTYPE_METHOD(tpl, "free", Database::Free);
}

/*
TODO: Convert from using single table
To a list of tables.
*/
int Database::OpenTable (char* tablename) {
  bangtable = bangdb->gettable(tablename);
  bangconnection = bangtable->getconnection();
  return 1;
}

int Database::CloseTable (char* tablename) {
  if (bangconnection != NULL) {
    bangconnection->closeconnection();
    bangconnection = NULL;
  }

  if (bangtable != NULL) {
    bangtable->closetable();
    bangtable = NULL;
  }
  return 1;
}

int Database::PutValue(char* key, char* val) {

  FDT ikey, ival;

  ikey.data = key;
  ikey.length = strlen(key);
  ival.data = (void*)val;
  ival.length = strlen(val);

  int flag = bangconnection->put(&ikey, &ival, INSERT_UPDATE); 
  return flag;
}

FDT* Database::GetValue(char* key) { 

  FDT ikey;

  ikey.data = key;
  ikey.length = strlen(key);

  FDT* result = bangconnection->get(&ikey);
 

  return result;
}

NAN_METHOD(Database::New) {
  NanScope();

  if (args.Length() == 0)
    return NanThrowError("constructor requires at least a location argument");

  if (!args[0]->IsString())
    return NanThrowError("constructor requires a location string argument");

  char* name = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  Database* obj = new Database(name);
  obj->Wrap(args.This());
  printf("NAME: %s \n", name);
//  NanReturnUndefined();
  NanReturnValue(args.This());
}
NAN_METHOD(Database::Open) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());
 
  char* tablename = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  _db->OpenTable(tablename);

  
  NanReturnUndefined();
}

NAN_METHOD(Database::Close) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());
 
  char* tablename = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);

  _db->CloseTable(tablename);

  NanReturnUndefined();
}

NAN_METHOD(Database::Free) {
  NanScope();
 
  Database* _db = ObjectWrap::Unwrap<Database>(args.This());

  _db->CloseDatabase(); 

  NanReturnUndefined();
{'fraction_non_alphanumeric': 0.16599672472830132, 'fraction_numerical': 0.008634807205597736, 'mean_word_length': 5.186003683241252, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '604836', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2337, 'n_words': 450}