<commit_before>#include <assert.h>
#include <limits.h>
#include <stdlib.h>

#include <algorithm>
#include <sstream>
#include <vector>

#include <leveldb/db.h>
#include <node.h>
#include <node_buffer.h>
#include <v8.h>

#include "batch.h"
#include "handle.h"
#include "helpers.h"
#include "iterator.h"
#include "options.h"

using namespace node;
using namespace v8;

namespace node_leveldb {

Persistent<FunctionTemplate> JHandle::constructor;

static inline Handle<Value> Error(const leveldb::Status& status) {
  if (status.IsNotFound()) {
    return Undefined();
  } else {
    return ThrowError(status.ToString().c_str());
  }
}





JHandle::JHandle(leveldb::DB* db)
  : ObjectWrap()
  , db_(db)
{
}

JHandle::~JHandle() {
  assert(db_ != NULL);
  delete db_;
  db_ = NULL;
  comparator_.Dispose();
};





/**

    Async support

 */

class JHandle::OpAsync {
 public:
  OpAsync(const Handle<Value>& callback)
    : status_(leveldb::Status())
  {
    assert(callback->IsFunction());
    Handle<Function> cb = Handle<Function>::Cast(callback);
    callback_ = Persistent<Function>::New(cb);
  }

  virtual ~OpAsync() {
    callback_.Dispose();
  }

  template <class T> static Handle<Value> AsyncEnqueue(T* op) {
    return AsyncQueue(op, AsyncWorker<T>, AsyncCallback<T>);
  }

  template <class T> static void AsyncWorker(uv_work_t* req) {
    T* op = static_cast<T*>(req->data);
    op->Run();
  }

  template <class T> static void AsyncCallback(uv_work_t* req) {
    HandleScope scope;
    T* op = static_cast<T*>(req->data);
    assert(!op->callback_.IsEmpty());

    Handle<Value> error = Null();
    Handle<Value> result = Null();

    op->Result(error, result);

    if ((error.IsEmpty()  || error->IsNull())  &&
        (result.IsEmpty() || result->IsNull()) &&
        !op->status_.ok()) {
      error = Exception::Error(String::New(op->status_.ToString().c_str()));
    }

    Handle<Value> args[] = { error, result };

    TryCatch tryCatch;
    op->callback_->Call(Context::GetCurrent()->Global(), 2, args);
    if (tryCatch.HasCaught()) FatalException(tryCatch);

    delete op;
    delete req;
  }
  leveldb::Status status_;
  Persistent<Function> callback_;
};





/**<commit_msg>Ignore NotFound errors in automatic error handling.<commit_after>#include <assert.h>
#include <limits.h>
#include <stdlib.h>

#include <algorithm>
#include <sstream>
#include <vector>

#include <leveldb/db.h>
#include <node.h>
#include <node_buffer.h>
#include <v8.h>

#include "batch.h"
#include "handle.h"
#include "helpers.h"
#include "iterator.h"
#include "options.h"

using namespace node;
using namespace v8;

namespace node_leveldb {

Persistent<FunctionTemplate> JHandle::constructor;

static inline Handle<Value> Error(const leveldb::Status& status) {
  if (status.IsNotFound()) {
    return Undefined();
  } else {
    return ThrowError(status.ToString().c_str());
  }
}





JHandle::JHandle(leveldb::DB* db)
  : ObjectWrap()
  , db_(db)
{
}

JHandle::~JHandle() {
  assert(db_ != NULL);
  delete db_;
  db_ = NULL;
  comparator_.Dispose();
};





/**

    Async support

 */

class JHandle::OpAsync {
 public:
  OpAsync(const Handle<Value>& callback)
    : status_(leveldb::Status())
  {
    assert(callback->IsFunction());
    Handle<Function> cb = Handle<Function>::Cast(callback);
    callback_ = Persistent<Function>::New(cb);
  }

  virtual ~OpAsync() {
    callback_.Dispose();
  }

  template <class T> static Handle<Value> AsyncEnqueue(T* op) {
    return AsyncQueue(op, AsyncWorker<T>, AsyncCallback<T>);
  }

  template <class T> static void AsyncWorker(uv_work_t* req) {
    T* op = static_cast<T*>(req->data);
    op->Run();
  }

  template <class T> static void AsyncCallback(uv_work_t* req) {
    HandleScope scope;
    T* op = static_cast<T*>(req->data);
    assert(!op->callback_.IsEmpty());

    Handle<Value> error = Null();
    Handle<Value> result = Null();

    op->Result(error, result);

    if ((error.IsEmpty()  || error->IsNull())  &&
        (result.IsEmpty() || result->IsNull()) &&
        !op->status_.ok() &&
        !op->status_.IsNotFound()) {
      error = Exception::Error(String::New(op->status_.ToString().c_str()));
    }

    Handle<Value> args[] = { error, result };

    TryCatch tryCatch;
    op->callback_->Call(Context::GetCurrent()->Global(), 2, args);
    if (tryCatch.HasCaught()) FatalException(tryCatch);

    delete op;
    delete req;
  }
  leveldb::Status status_;
  Persistent<Function> callback_;
};





/**
{'fraction_non_alphanumeric': 0.17094972067039105, 'fraction_numerical': 0.001340782122905028, 'mean_word_length': 3.8971553610503284, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 86, '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': '64743', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1502, 'n_words': 315}