2011-12-22 04:46:35 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// wterl: Erlang Wrapper for WiredTiger
|
|
|
|
//
|
2012-04-09 20:49:26 +00:00
|
|
|
// Copyright (c) 2012 Basho Technologies, Inc. All Rights Reserved.
|
2011-12-22 04:46:35 +00:00
|
|
|
//
|
|
|
|
// This file is provided to you under the Apache License,
|
|
|
|
// Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
#include "erl_nif.h"
|
|
|
|
#include "erl_driver.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-02-15 14:59:28 +00:00
|
|
|
#include <string.h>
|
2011-12-22 04:46:35 +00:00
|
|
|
|
|
|
|
#include "wiredtiger.h"
|
|
|
|
|
|
|
|
static ErlNifResourceType* wterl_conn_RESOURCE;
|
|
|
|
static ErlNifResourceType* wterl_session_RESOURCE;
|
2012-02-15 19:31:40 +00:00
|
|
|
static ErlNifResourceType* wterl_cursor_RESOURCE;
|
2011-12-22 04:46:35 +00:00
|
|
|
|
2012-04-02 20:59:13 +00:00
|
|
|
typedef struct {
|
2011-12-22 04:46:35 +00:00
|
|
|
WT_CONNECTION* conn;
|
2012-04-02 20:59:13 +00:00
|
|
|
} WterlConnHandle;
|
2011-12-22 04:46:35 +00:00
|
|
|
|
2012-04-02 20:59:13 +00:00
|
|
|
typedef struct {
|
2011-12-22 04:46:35 +00:00
|
|
|
WT_SESSION* session;
|
2012-04-02 20:59:13 +00:00
|
|
|
} WterlSessionHandle;
|
2011-12-22 04:46:35 +00:00
|
|
|
|
2012-04-02 20:59:13 +00:00
|
|
|
typedef struct {
|
2012-02-16 18:22:03 +00:00
|
|
|
WT_CURSOR* cursor;
|
2012-04-02 20:59:13 +00:00
|
|
|
} WterlCursorHandle;
|
2012-02-16 18:22:03 +00:00
|
|
|
|
2012-02-17 00:24:18 +00:00
|
|
|
typedef char Uri[128]; // object names
|
2012-02-08 22:31:16 +00:00
|
|
|
|
2011-12-22 04:46:35 +00:00
|
|
|
// Atoms (initialized in on_load)
|
|
|
|
static ERL_NIF_TERM ATOM_ERROR;
|
2012-02-08 22:31:16 +00:00
|
|
|
static ERL_NIF_TERM ATOM_OK;
|
2011-12-22 04:46:35 +00:00
|
|
|
|
2012-02-20 01:20:38 +00:00
|
|
|
typedef ERL_NIF_TERM (*CursorRetFun)(ErlNifEnv* env, WT_CURSOR* cursor, int rc);
|
|
|
|
|
2011-12-22 04:46:35 +00:00
|
|
|
// Prototypes
|
2012-02-08 22:31:16 +00:00
|
|
|
static ERL_NIF_TERM wterl_conn_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-16 20:07:44 +00:00
|
|
|
static ERL_NIF_TERM wterl_conn_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-15 19:31:40 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-17 00:06:56 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_insert(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_key_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_kv_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc);
|
2012-02-16 19:54:05 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_next(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_next_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_next_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_np_worker(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[],
|
|
|
|
CursorRetFun cursor_ret_fun, int next);
|
2012-02-16 19:54:05 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_prev(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_prev_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_prev_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-17 00:06:56 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_remove(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-16 20:07:44 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_reset(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-16 19:54:05 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_search(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_search_near(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_cursor_search_worker(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], int near);
|
2012-02-17 00:06:56 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_value_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc);
|
2012-09-17 14:58:48 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_checkpoint(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-16 20:07:44 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_create(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_delete(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_drop(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_put(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_rename(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_salvage(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_truncate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_upgrade(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
|
|
|
static ERL_NIF_TERM wterl_session_verify(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
|
2011-12-22 04:46:35 +00:00
|
|
|
|
|
|
|
static ErlNifFunc nif_funcs[] =
|
|
|
|
{
|
2012-02-08 22:31:16 +00:00
|
|
|
{"conn_close", 1, wterl_conn_close},
|
2012-02-16 20:07:44 +00:00
|
|
|
{"conn_open", 2, wterl_conn_open},
|
2012-02-15 19:31:40 +00:00
|
|
|
{"cursor_close", 1, wterl_cursor_close},
|
2012-02-17 00:06:56 +00:00
|
|
|
{"cursor_insert", 3, wterl_cursor_insert},
|
2012-02-16 19:54:05 +00:00
|
|
|
{"cursor_next", 1, wterl_cursor_next},
|
2012-02-20 01:20:38 +00:00
|
|
|
{"cursor_next_key", 1, wterl_cursor_next_key},
|
|
|
|
{"cursor_next_value", 1, wterl_cursor_next_value},
|
2012-02-16 19:54:05 +00:00
|
|
|
{"cursor_open", 2, wterl_cursor_open},
|
|
|
|
{"cursor_prev", 1, wterl_cursor_prev},
|
2012-02-20 01:20:38 +00:00
|
|
|
{"cursor_prev_key", 1, wterl_cursor_prev_key},
|
|
|
|
{"cursor_prev_value", 1, wterl_cursor_prev_value},
|
2012-02-17 00:06:56 +00:00
|
|
|
{"cursor_remove", 3, wterl_cursor_remove},
|
2012-02-16 19:54:05 +00:00
|
|
|
{"cursor_reset", 1, wterl_cursor_reset},
|
|
|
|
{"cursor_search", 2, wterl_cursor_search},
|
|
|
|
{"cursor_search_near", 2, wterl_cursor_search_near},
|
2012-02-17 00:06:56 +00:00
|
|
|
{"cursor_update", 3, wterl_cursor_update},
|
2012-09-17 14:58:48 +00:00
|
|
|
{"session_checkpoint", 2, wterl_session_checkpoint},
|
2012-02-16 20:07:44 +00:00
|
|
|
{"session_close", 1, wterl_session_close},
|
|
|
|
{"session_create", 3, wterl_session_create},
|
|
|
|
{"session_delete", 3, wterl_session_delete},
|
|
|
|
{"session_drop", 3, wterl_session_drop},
|
|
|
|
{"session_get", 3, wterl_session_get},
|
|
|
|
{"session_open", 1, wterl_session_open},
|
|
|
|
{"session_put", 4, wterl_session_put},
|
2012-02-16 23:44:28 +00:00
|
|
|
{"session_rename", 4, wterl_session_rename},
|
|
|
|
{"session_salvage", 3, wterl_session_salvage},
|
|
|
|
{"session_truncate", 3, wterl_session_truncate},
|
|
|
|
{"session_upgrade", 3, wterl_session_upgrade},
|
|
|
|
{"session_verify", 3, wterl_session_verify},
|
2011-12-22 04:46:35 +00:00
|
|
|
};
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static inline ERL_NIF_TERM wterl_strerror(ErlNifEnv* env, int rc)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == WT_NOTFOUND ?
|
|
|
|
enif_make_atom(env, "not_found") :
|
|
|
|
enif_make_tuple2(env, ATOM_ERROR,
|
|
|
|
enif_make_string(env, wiredtiger_strerror(rc), ERL_NIF_LATIN1));
|
2012-02-16 23:44:28 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 04:46:35 +00:00
|
|
|
static ERL_NIF_TERM wterl_conn_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
2012-02-17 05:42:22 +00:00
|
|
|
ErlNifBinary config;
|
2011-12-22 04:46:35 +00:00
|
|
|
char homedir[4096];
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_string(env, argv[0], homedir, sizeof homedir, ERL_NIF_LATIN1) &&
|
2012-02-17 05:42:22 +00:00
|
|
|
enif_inspect_binary(env, argv[1], &config))
|
2011-12-22 04:46:35 +00:00
|
|
|
{
|
|
|
|
WT_CONNECTION* conn;
|
2012-02-17 05:42:22 +00:00
|
|
|
int rc = wiredtiger_open(homedir, NULL, (const char*)config.data, &conn);
|
2011-12-22 04:46:35 +00:00
|
|
|
if (rc == 0)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlConnHandle* conn_handle = enif_alloc_resource(wterl_conn_RESOURCE, sizeof(WterlConnHandle));
|
2012-02-16 23:44:28 +00:00
|
|
|
conn_handle->conn = conn;
|
|
|
|
ERL_NIF_TERM result = enif_make_resource(env, conn_handle);
|
|
|
|
enif_release_resource(conn_handle);
|
2012-02-08 22:31:16 +00:00
|
|
|
return enif_make_tuple2(env, ATOM_OK, result);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 23:44:28 +00:00
|
|
|
return enif_make_badarg(env);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
2011-12-22 04:46:35 +00:00
|
|
|
|
2012-02-08 22:31:16 +00:00
|
|
|
static ERL_NIF_TERM wterl_conn_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlConnHandle* conn_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&conn_handle))
|
|
|
|
{
|
|
|
|
WT_CONNECTION* conn = conn_handle->conn;
|
|
|
|
int rc = conn->close(conn, NULL);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-16 23:44:28 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WTERL_OP_CREATE 1
|
|
|
|
#define WTERL_OP_DROP 2
|
|
|
|
#define WTERL_OP_SALVAGE 3
|
2012-09-17 14:58:48 +00:00
|
|
|
#define WTERL_OP_TRUNCATE 4
|
|
|
|
#define WTERL_OP_UPGRADE 5
|
|
|
|
#define WTERL_OP_VERIFY 6
|
2012-02-16 23:44:28 +00:00
|
|
|
|
|
|
|
static inline ERL_NIF_TERM wterl_session_worker(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], int op)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-16 23:44:28 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-09-17 14:58:48 +00:00
|
|
|
int rc;
|
2012-02-17 00:24:18 +00:00
|
|
|
Uri uri;
|
2012-09-17 14:58:48 +00:00
|
|
|
ErlNifBinary config;
|
2012-02-16 23:44:28 +00:00
|
|
|
if (enif_get_string(env, argv[1], uri, sizeof uri, ERL_NIF_LATIN1) &&
|
2012-02-17 05:42:22 +00:00
|
|
|
enif_inspect_binary(env, argv[2], &config))
|
2012-02-16 23:44:28 +00:00
|
|
|
{
|
2012-09-17 14:58:48 +00:00
|
|
|
switch (op)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
2012-09-17 14:58:48 +00:00
|
|
|
case WTERL_OP_CREATE:
|
|
|
|
rc = session->create(session, uri, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_DROP:
|
|
|
|
rc = session->drop(session, uri, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_SALVAGE:
|
|
|
|
rc = session->salvage(session, uri, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_TRUNCATE:
|
|
|
|
// Ignore the cursor start/stop form of truncation for now,
|
|
|
|
// support only the full file truncation.
|
|
|
|
rc = session->truncate(session, uri, NULL, NULL, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_UPGRADE:
|
|
|
|
rc = session->upgrade(session, uri, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_VERIFY:
|
|
|
|
default:
|
|
|
|
rc = session->verify(session, uri, (const char*)config.data);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 18:22:03 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2011-12-22 04:46:35 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlConnHandle* conn_handle;
|
2011-12-22 04:46:35 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&conn_handle))
|
|
|
|
{
|
|
|
|
WT_CONNECTION* conn = conn_handle->conn;
|
|
|
|
WT_SESSION* session;
|
|
|
|
int rc = conn->open_session(conn, NULL, NULL, &session);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle =
|
|
|
|
enif_alloc_resource(wterl_session_RESOURCE, sizeof(WterlSessionHandle));
|
2012-02-16 23:44:28 +00:00
|
|
|
session_handle->session = session;
|
|
|
|
ERL_NIF_TERM result = enif_make_resource(env, session_handle);
|
2011-12-22 04:46:35 +00:00
|
|
|
enif_keep_resource(conn_handle);
|
2012-02-16 23:44:28 +00:00
|
|
|
enif_release_resource(session_handle);
|
2012-02-08 22:31:16 +00:00
|
|
|
return enif_make_tuple2(env, ATOM_OK, result);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 22:31:16 +00:00
|
|
|
return enif_make_badarg(env);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2011-12-22 04:46:35 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-02-16 23:44:28 +00:00
|
|
|
int rc = session->close(session, NULL);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_create(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_session_drop(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_DROP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_session_rename(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2011-12-22 04:46:35 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
2012-02-17 05:42:22 +00:00
|
|
|
ErlNifBinary config;
|
2012-02-16 23:44:28 +00:00
|
|
|
Uri oldname, newname;
|
|
|
|
if (enif_get_string(env, argv[1], oldname, sizeof oldname, ERL_NIF_LATIN1) &&
|
|
|
|
enif_get_string(env, argv[2], newname, sizeof newname, ERL_NIF_LATIN1) &&
|
2012-02-17 05:42:22 +00:00
|
|
|
enif_inspect_binary(env, argv[3], &config))
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WT_SESSION* session = session_handle->session;
|
|
|
|
int rc = session->rename(session, oldname, newname, (const char*)config.data);
|
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_salvage(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_SALVAGE);
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:58:48 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_checkpoint(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-16 23:44:28 +00:00
|
|
|
{
|
2012-09-17 14:58:48 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
|
|
|
ErlNifBinary config;
|
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle) &&
|
|
|
|
enif_inspect_binary(env, argv[1], &config))
|
|
|
|
{
|
|
|
|
WT_SESSION* session = session_handle->session;
|
|
|
|
int rc = session->checkpoint(session, (const char*)config.data);
|
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
2012-02-16 23:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_session_truncate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_TRUNCATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_session_upgrade(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_UPGRADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_session_verify(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_session_worker(env, argc, argv, WTERL_OP_VERIFY);
|
|
|
|
}
|
|
|
|
|
2011-12-22 04:46:35 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_delete(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
Uri uri;
|
2012-02-08 22:31:16 +00:00
|
|
|
ErlNifBinary key;
|
2012-02-16 23:44:28 +00:00
|
|
|
if (enif_get_string(env, argv[1], uri, sizeof uri, ERL_NIF_LATIN1) &&
|
2012-02-08 22:31:16 +00:00
|
|
|
enif_inspect_binary(env, argv[2], &key))
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-02-08 22:31:16 +00:00
|
|
|
WT_CURSOR* cursor;
|
2012-02-16 23:44:28 +00:00
|
|
|
int rc = session->open_cursor(session, uri, NULL, "raw", &cursor);
|
2012-02-08 22:31:16 +00:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
WT_ITEM raw_key;
|
|
|
|
raw_key.data = key.data;
|
|
|
|
raw_key.size = key.size;
|
|
|
|
cursor->set_key(cursor, &raw_key);
|
|
|
|
rc = cursor->remove(cursor);
|
2012-04-05 00:13:58 +00:00
|
|
|
cursor->close(cursor);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
Uri uri;
|
|
|
|
ErlNifBinary key;
|
|
|
|
if (enif_get_string(env, argv[1], uri, sizeof uri, ERL_NIF_LATIN1) &&
|
|
|
|
enif_inspect_binary(env, argv[2], &key))
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-02-16 23:44:28 +00:00
|
|
|
WT_CURSOR* cursor;
|
|
|
|
int rc = session->open_cursor(session, uri, NULL, "overwrite,raw", &cursor);
|
2012-02-16 19:54:05 +00:00
|
|
|
if (rc != 0)
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
|
|
|
}
|
|
|
|
WT_ITEM raw_key, raw_value;
|
|
|
|
raw_key.data = key.data;
|
|
|
|
raw_key.size = key.size;
|
|
|
|
cursor->set_key(cursor, &raw_key);
|
|
|
|
rc = cursor->search(cursor);
|
|
|
|
if (rc == 0)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
rc = cursor->get_value(cursor, &raw_value);
|
2012-04-05 00:13:58 +00:00
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
ERL_NIF_TERM value;
|
|
|
|
unsigned char* bin = enif_make_new_binary(env, raw_value.size, &value);
|
|
|
|
memcpy(bin, raw_value.data, raw_value.size);
|
|
|
|
cursor->close(cursor);
|
|
|
|
return enif_make_tuple2(env, ATOM_OK, value);
|
|
|
|
}
|
2012-04-02 20:59:13 +00:00
|
|
|
}
|
2012-04-05 00:13:58 +00:00
|
|
|
cursor->close(cursor);
|
|
|
|
return wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_session_put(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-08 22:31:16 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
Uri uri;
|
|
|
|
ErlNifBinary key, value;
|
|
|
|
if (enif_get_string(env, argv[1], uri, sizeof uri, ERL_NIF_LATIN1) &&
|
2012-02-17 00:06:56 +00:00
|
|
|
enif_inspect_binary(env, argv[2], &key) &&
|
|
|
|
enif_inspect_binary(env, argv[3], &value))
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-02-16 23:44:28 +00:00
|
|
|
WT_CURSOR* cursor;
|
|
|
|
int rc = session->open_cursor(session, uri, NULL, "overwrite,raw", &cursor);
|
2012-02-16 19:54:05 +00:00
|
|
|
if (rc != 0)
|
2012-02-08 22:31:16 +00:00
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
2012-02-16 23:44:28 +00:00
|
|
|
WT_ITEM raw_key, raw_value;
|
|
|
|
raw_key.data = key.data;
|
|
|
|
raw_key.size = key.size;
|
|
|
|
cursor->set_key(cursor, &raw_key);
|
|
|
|
raw_value.data = value.data;
|
|
|
|
raw_value.size = value.size;
|
|
|
|
cursor->set_value(cursor, &raw_value);
|
|
|
|
rc = cursor->insert(cursor);
|
2012-04-05 00:13:58 +00:00
|
|
|
cursor->close(cursor);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-08 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
2011-12-22 04:46:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 18:22:03 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-15 19:31:40 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlSessionHandle* session_handle;
|
2012-02-15 19:31:40 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_session_RESOURCE, (void**)&session_handle))
|
|
|
|
{
|
2012-02-16 18:22:03 +00:00
|
|
|
WT_CURSOR* cursor;
|
2012-02-16 23:44:28 +00:00
|
|
|
Uri uri;
|
|
|
|
if (enif_get_string(env, argv[1], uri, sizeof uri, ERL_NIF_LATIN1))
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
|
|
|
WT_SESSION* session = session_handle->session;
|
2012-02-16 23:44:28 +00:00
|
|
|
int rc = session->open_cursor(session, uri, NULL, "overwrite,raw", &cursor);
|
2012-02-16 18:22:03 +00:00
|
|
|
if (rc == 0)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
|
|
|
WterlCursorHandle* cursor_handle =
|
|
|
|
enif_alloc_resource(wterl_cursor_RESOURCE, sizeof(WterlCursorHandle));
|
2012-02-16 23:44:28 +00:00
|
|
|
cursor_handle->cursor = cursor;
|
|
|
|
ERL_NIF_TERM result = enif_make_resource(env, cursor_handle);
|
2012-02-16 18:22:03 +00:00
|
|
|
enif_keep_resource(session_handle);
|
2012-02-16 23:44:28 +00:00
|
|
|
enif_release_resource(cursor_handle);
|
|
|
|
return enif_make_tuple2(env, ATOM_OK, result);
|
2012-02-16 18:22:03 +00:00
|
|
|
}
|
2012-04-02 20:59:13 +00:00
|
|
|
else
|
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_strerror(env, rc);
|
2012-02-16 18:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-16 18:22:03 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlCursorHandle *cursor_handle;
|
2012-02-16 18:22:03 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_cursor_RESOURCE, (void**)&cursor_handle))
|
|
|
|
{
|
|
|
|
WT_CURSOR* cursor = cursor_handle->cursor;
|
2012-02-16 23:44:28 +00:00
|
|
|
int rc = cursor->close(cursor);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-16 23:44:28 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_key_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc)
|
|
|
|
{
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
WT_ITEM raw_key;
|
|
|
|
rc = cursor->get_key(cursor, &raw_key);
|
|
|
|
if (rc == 0)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
2012-02-20 18:30:09 +00:00
|
|
|
ERL_NIF_TERM key;
|
|
|
|
memcpy(enif_make_new_binary(env, raw_key.size, &key), raw_key.data, raw_key.size);
|
|
|
|
return enif_make_tuple2(env, ATOM_OK, key);
|
2012-02-20 01:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wterl_strerror(env, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_kv_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc)
|
|
|
|
{
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
WT_ITEM raw_key, raw_value;
|
|
|
|
rc = cursor->get_key(cursor, &raw_key);
|
|
|
|
if (rc == 0)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
2012-02-20 01:20:38 +00:00
|
|
|
rc = cursor->get_value(cursor, &raw_value);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
2012-02-20 18:30:09 +00:00
|
|
|
ERL_NIF_TERM key, value;
|
|
|
|
memcpy(enif_make_new_binary(env, raw_key.size, &key), raw_key.data, raw_key.size);
|
|
|
|
memcpy(enif_make_new_binary(env, raw_value.size, &value), raw_value.data, raw_value.size);
|
|
|
|
return enif_make_tuple3(env, ATOM_OK, key, value);
|
2012-02-20 01:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wterl_strerror(env, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_value_ret(ErlNifEnv* env, WT_CURSOR *cursor, int rc)
|
2012-02-16 23:44:28 +00:00
|
|
|
{
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
WT_ITEM raw_value;
|
|
|
|
rc = cursor->get_value(cursor, &raw_value);
|
2012-02-16 18:22:03 +00:00
|
|
|
if (rc == 0)
|
2012-04-02 20:59:13 +00:00
|
|
|
{
|
2012-02-20 18:30:09 +00:00
|
|
|
ERL_NIF_TERM value;
|
|
|
|
memcpy(enif_make_new_binary(env, raw_value.size, &value), raw_value.data, raw_value.size);
|
|
|
|
return enif_make_tuple2(env, ATOM_OK, value);
|
2012-02-16 18:22:03 +00:00
|
|
|
}
|
2012-02-16 23:44:28 +00:00
|
|
|
}
|
|
|
|
return wterl_strerror(env, rc);
|
|
|
|
}
|
|
|
|
|
2012-02-20 01:20:38 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_np_worker(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[],
|
|
|
|
CursorRetFun cursor_ret, int prev)
|
2012-02-16 23:44:28 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlCursorHandle *cursor_handle;
|
2012-02-16 23:44:28 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_cursor_RESOURCE, (void**)&cursor_handle))
|
|
|
|
{
|
|
|
|
WT_CURSOR* cursor = cursor_handle->cursor;
|
2012-02-20 01:20:38 +00:00
|
|
|
return cursor_ret(env, cursor, prev == 0 ? cursor->next(cursor) : cursor->prev(cursor));
|
2012-02-15 19:31:40 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_next(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-16 19:54:05 +00:00
|
|
|
{
|
2012-02-20 01:20:38 +00:00
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_kv_ret, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_next_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_key_ret, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_next_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_value_ret, 0);
|
2012-02-16 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_prev(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-16 19:54:05 +00:00
|
|
|
{
|
2012-02-20 01:20:38 +00:00
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_kv_ret, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_prev_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_key_ret, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_prev_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_np_worker(env, argc, argv, wterl_cursor_value_ret, 1);
|
2012-02-16 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_search_worker(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], int near)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlCursorHandle *cursor_handle;
|
2012-02-16 19:54:05 +00:00
|
|
|
ErlNifBinary key;
|
|
|
|
if (enif_get_resource(env, argv[0], wterl_cursor_RESOURCE, (void**)&cursor_handle) &&
|
|
|
|
enif_inspect_binary(env, argv[1], &key))
|
|
|
|
{
|
|
|
|
WT_CURSOR* cursor = cursor_handle->cursor;
|
|
|
|
WT_ITEM raw_key;
|
|
|
|
int exact;
|
|
|
|
raw_key.data = key.data;
|
|
|
|
raw_key.size = key.size;
|
|
|
|
cursor->set_key(cursor, &raw_key);
|
2012-02-16 23:44:28 +00:00
|
|
|
|
|
|
|
// We currently ignore the less-than, greater-than or equals-to return information
|
|
|
|
// from the cursor.search_near method.
|
2012-02-20 01:20:38 +00:00
|
|
|
return wterl_cursor_value_ret(env, cursor,
|
|
|
|
near == 1 ?
|
|
|
|
cursor->search_near(cursor, &exact) : cursor->search(cursor));
|
2012-02-16 19:54:05 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_search(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-16 19:54:05 +00:00
|
|
|
{
|
2012-02-16 23:44:28 +00:00
|
|
|
return wterl_cursor_search_worker(env, argc, argv, 0);
|
2012-02-16 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 23:44:28 +00:00
|
|
|
static ERL_NIF_TERM wterl_cursor_search_near(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_search_worker(env, argc, argv, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_reset(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
2012-02-15 19:31:40 +00:00
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlCursorHandle *cursor_handle;
|
2012-02-16 18:22:03 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_cursor_RESOURCE, (void**)&cursor_handle))
|
2012-02-15 19:31:40 +00:00
|
|
|
{
|
2012-02-16 18:22:03 +00:00
|
|
|
WT_CURSOR* cursor = cursor_handle->cursor;
|
2012-02-16 23:44:28 +00:00
|
|
|
int rc = cursor->reset(cursor);
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-15 19:31:40 +00:00
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:06:56 +00:00
|
|
|
#define WTERL_OP_CURSOR_INSERT 1
|
|
|
|
#define WTERL_OP_CURSOR_UPDATE 2
|
|
|
|
#define WTERL_OP_CURSOR_REMOVE 3
|
|
|
|
|
|
|
|
static inline ERL_NIF_TERM wterl_cursor_data_op(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], int op)
|
|
|
|
{
|
2012-04-02 20:59:13 +00:00
|
|
|
WterlCursorHandle *cursor_handle;
|
2012-02-17 00:06:56 +00:00
|
|
|
if (enif_get_resource(env, argv[0], wterl_cursor_RESOURCE, (void**)&cursor_handle))
|
|
|
|
{
|
|
|
|
ErlNifBinary key, value;
|
|
|
|
int rc;
|
2012-04-02 20:59:13 +00:00
|
|
|
if (enif_inspect_binary(env, argv[1], &key) && enif_inspect_binary(env, argv[2], &value))
|
|
|
|
{
|
|
|
|
WT_CURSOR* cursor = cursor_handle->cursor;
|
2012-02-17 00:06:56 +00:00
|
|
|
WT_ITEM raw_key, raw_value;
|
|
|
|
raw_key.data = key.data;
|
|
|
|
raw_key.size = key.size;
|
|
|
|
cursor->set_key(cursor, &raw_key);
|
|
|
|
raw_value.data = value.data;
|
|
|
|
raw_value.size = value.size;
|
|
|
|
cursor->set_value(cursor, &raw_value);
|
2012-04-02 20:59:13 +00:00
|
|
|
switch (op)
|
|
|
|
{
|
2012-02-17 00:06:56 +00:00
|
|
|
case WTERL_OP_CURSOR_INSERT:
|
|
|
|
rc = cursor->insert(cursor);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_CURSOR_UPDATE:
|
|
|
|
rc = cursor->update(cursor);
|
|
|
|
break;
|
|
|
|
case WTERL_OP_CURSOR_REMOVE:
|
2012-04-02 20:59:13 +00:00
|
|
|
default:
|
2012-02-17 00:06:56 +00:00
|
|
|
rc = cursor->remove(cursor);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-02 20:59:13 +00:00
|
|
|
return rc == 0 ? ATOM_OK : wterl_strerror(env, rc);
|
2012-02-17 00:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enif_make_badarg(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_insert(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_data_op(env, argc, argv, WTERL_OP_CURSOR_INSERT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_data_op(env, argc, argv, WTERL_OP_CURSOR_UPDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ERL_NIF_TERM wterl_cursor_remove(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|
|
|
{
|
|
|
|
return wterl_cursor_data_op(env, argc, argv, WTERL_OP_CURSOR_REMOVE);
|
|
|
|
}
|
|
|
|
|
2011-12-22 04:46:35 +00:00
|
|
|
static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
|
|
|
|
{
|
|
|
|
ErlNifResourceFlags flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
|
2012-04-02 20:59:13 +00:00
|
|
|
wterl_conn_RESOURCE = enif_open_resource_type(env, NULL, "wterl_conn_resource", NULL, flags, NULL);
|
|
|
|
wterl_session_RESOURCE = enif_open_resource_type(env, NULL, "wterl_session_resource", NULL, flags, NULL);
|
|
|
|
wterl_cursor_RESOURCE = enif_open_resource_type(env, NULL, "wterl_cursor_resource", NULL, flags, NULL);
|
2011-12-22 04:46:35 +00:00
|
|
|
ATOM_ERROR = enif_make_atom(env, "error");
|
2012-02-08 22:31:16 +00:00
|
|
|
ATOM_OK = enif_make_atom(env, "ok");
|
2011-12-22 04:46:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERL_NIF_INIT(wterl, nif_funcs, &on_load, NULL, NULL, NULL);
|