Ai
1 Star 0 Fork 0

hantaohuang/php-rocksdb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rocksdb_db.cc 15.21 KB
一键复制 编辑 原始数据 按行查看 历史
哇咔咔 提交于 2020-05-22 14:49 +08:00 . improve: print extension info
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
/* rocksdb extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php_rocksdb.h"
#include "rocksdb/db.h"
#include "rocksdb/merge_operator.h"
#include "stringappend.h"
#include "error.h"
using namespace rocksdb;
typedef struct
{
DB *db;
zend_object std;
} rocksdb_db_t;
zend_class_entry *rocksdb_db_ce;
static zend_object_handlers rocksdb_db_handlers;
extern void php_rocksdb_iterator_set_ptr(zval *zobject, Iterator *iter);
extern WriteBatch *php_rocksdb_write_batch_get_ptr(zval *zobject);
extern void check_rocksdb_db_options(Options &op, HashTable *vht);
extern void check_rocksdb_db_write_options(WriteOptions &wop, HashTable *vht);
extern void check_rocksdb_db_read_options(ReadOptions &rop, HashTable *vht);
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_open, 0, 0, 2)
ZEND_ARG_INFO(0, dbName)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_openForReadOnly, 0, 0, 2)
ZEND_ARG_INFO(0, dbName)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_openAsSecondary, 0, 0, 3)
ZEND_ARG_INFO(0, dbName)
ZEND_ARG_INFO(0, options)
ZEND_ARG_INFO(0, secondaryPath)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_put, 0, 0, 3)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, writeOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_write, 0, 0, 2)
ZEND_ARG_INFO(0, batch)
ZEND_ARG_INFO(0, writeOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_get, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, readOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_del, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, writeOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_deleteRange, 0, 0, 3)
ZEND_ARG_INFO(0, beginKey)
ZEND_ARG_INFO(0, endKey)
ZEND_ARG_INFO(0, writeOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_newIterator, 0, 0, 2)
ZEND_ARG_INFO(0, beginKey)
ZEND_ARG_INFO(0, readOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_destroyDB, 0, 0, 2)
ZEND_ARG_INFO(0, dbName)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_keyMayExist, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, readOptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rocksdb_db_keyExist, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, readOptions)
ZEND_END_ARG_INFO()
static inline rocksdb_db_t *php_rocksdb_db_fetch_object(zend_object *obj)
{
return (rocksdb_db_t *) ((char *) obj - rocksdb_db_handlers.offset);
}
static zend_object *php_rocksdb_db_create_object(zend_class_entry *ce)
{
rocksdb_db_t *rocksdb_db = (rocksdb_db_t *) zend_object_alloc(sizeof(rocksdb_db_t), ce);
zend_object_std_init(&rocksdb_db->std, ce);
object_properties_init(&rocksdb_db->std, ce);
rocksdb_db->std.handlers = &rocksdb_db_handlers;
return &rocksdb_db->std;
}
static void php_rocksdb_db_free_object(zend_object *object)
{
rocksdb_db_t *rocksdb_db = (rocksdb_db_t *) php_rocksdb_db_fetch_object(object);
zend_object_std_dtor(&rocksdb_db->std);
}
static DB *php_rocksdb_db_get_ptr(zval *zobject)
{
return php_rocksdb_db_fetch_object(Z_OBJ_P(zobject))->db;
}
PHP_RINIT_FUNCTION(rocksdb)
{
return SUCCESS;
}
static PHP_METHOD(rocksdb, __construct)
{
}
static PHP_METHOD(rocksdb, open)
{
char *path;
size_t path_len;
zval *zoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(path, path_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
rocksdb_db_t *rocksdb_db = php_rocksdb_db_fetch_object(Z_OBJ_P(ZEND_THIS));
Options options;
options.IncreaseParallelism();
options.OptimizeLevelStyleCompaction();
if (zoptions)
{
check_rocksdb_db_options(options, Z_ARRVAL_P(zoptions));
}
Status s = DB::Open(options, path, &rocksdb_db->db);
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_OPEN_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, openForReadOnly)
{
char *path;
size_t path_len;
zval *zoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(path, path_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
rocksdb_db_t *rocksdb_db = php_rocksdb_db_fetch_object(Z_OBJ_P(ZEND_THIS));
Options options;
options.IncreaseParallelism();
options.OptimizeLevelStyleCompaction();
if (zoptions)
{
check_rocksdb_db_options(options, Z_ARRVAL_P(zoptions));
}
Status s = DB::OpenForReadOnly(options, path, &rocksdb_db->db);
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_OPEN_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, openAsSecondary)
{
char *path;
size_t path_len;
zval *zoptions = nullptr;
char *secondary_path;
size_t secondary_path_len;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STRING(path, path_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zoptions)
Z_PARAM_STRING(secondary_path, secondary_path_len)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
rocksdb_db_t *rocksdb_db = php_rocksdb_db_fetch_object(Z_OBJ_P(ZEND_THIS));
Options options;
options.IncreaseParallelism();
options.OptimizeLevelStyleCompaction();
if (zoptions)
{
check_rocksdb_db_options(options, Z_ARRVAL_P(zoptions));
}
Status s = DB::OpenAsSecondary(options, path, secondary_path, &rocksdb_db->db);
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_OPEN_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, put)
{
char *key;
size_t key_len;
char *value;
size_t value_len;
zval *zwriteoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STRING(key, key_len)
Z_PARAM_STRING(value, value_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zwriteoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
WriteOptions wop;
if (zwriteoptions)
{
check_rocksdb_db_write_options(wop, Z_ARRVAL_P(zwriteoptions));
}
Status s = db->Put(wop, std::string(key, key_len), std::string(value, value_len));
if (!s.ok()) {
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_PUT_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, write)
{
zval *zbatch;
zval *zwriteoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJECT(zbatch)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zwriteoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
WriteBatch *batch = php_rocksdb_write_batch_get_ptr(zbatch);
WriteOptions wop;
if (zwriteoptions)
{
check_rocksdb_db_write_options(wop, Z_ARRVAL_P(zwriteoptions));
}
Status s = db->Write(wop, batch);
if (!s.ok()) {
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_WRITE_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, get)
{
char *key;
size_t key_len;
zval *zreadoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(key, key_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zreadoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
ReadOptions rop;
if (zreadoptions)
{
check_rocksdb_db_read_options(rop, Z_ARRVAL_P(zreadoptions));
}
std::string value;
Status s = db->Get(rop, std::string(key, key_len), &value);
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_GET_ERROR);
}
RETURN_STRINGL(value.c_str(), value.length());
}
static PHP_METHOD(rocksdb, del)
{
char *key;
size_t key_len;
zval *zwriteoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(key, key_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zwriteoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
WriteOptions wop;
if (zwriteoptions)
{
check_rocksdb_db_write_options(wop, Z_ARRVAL_P(zwriteoptions));
}
Status s = db->Delete(wop, std::string(key, key_len));
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_DELETE_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, deleteRange)
{
char *begin_key;
size_t begin_key_len;
char *end_key;
size_t end_key_len;
zval *zwriteoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STRING(begin_key, begin_key_len)
Z_PARAM_STRING(end_key, end_key_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zwriteoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
WriteOptions wop;
if (zwriteoptions)
{
check_rocksdb_db_write_options(wop, Z_ARRVAL_P(zwriteoptions));
}
Status s = db->DeleteRange(wop, 0, std::string(begin_key, begin_key_len), std::string(end_key, end_key_len));
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_DELETE_RANGE_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, newIterator)
{
zval *begin_key;
zval *zreadoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(begin_key)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zreadoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
ReadOptions rop;
if (zreadoptions)
{
check_rocksdb_db_read_options(rop, Z_ARRVAL_P(zreadoptions));
}
zval ziter;
object_init_ex(&ziter, rocksdb_iterator_ce);
Iterator *iter = db->NewIterator(rop);
php_rocksdb_iterator_set_ptr(&ziter, iter);
zend_call_method_with_1_params(
&ziter,
rocksdb_iterator_ce,
&rocksdb_iterator_ce->constructor,
(const char *) "__construct",
NULL,
begin_key
);
RETVAL_OBJ(Z_OBJ_P(&ziter));
}
static PHP_METHOD(rocksdb, close)
{
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
Status s = db->Close();
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_CLOSE_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, destroyDB)
{
char *path;
size_t path_len;
zval *zoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(path, path_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Options options;
if (zoptions)
{
check_rocksdb_db_options(options, Z_ARRVAL_P(zoptions));
}
Status s = DestroyDB(path, options);
if (!s.ok())
{
zend_throw_exception(rocksdb_exception_ce, s.ToString().c_str(), ROCKSDB_DESTROY_ERROR);
}
RETURN_TRUE;
}
static PHP_METHOD(rocksdb, keyMayExist)
{
char *key;
size_t key_len;
zval *zreadoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(key, key_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zreadoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
ReadOptions rop;
if (zreadoptions)
{
check_rocksdb_db_read_options(rop, Z_ARRVAL_P(zreadoptions));
}
std::string value;
bool isExist = db->KeyMayExist(rop, std::move(std::string(key, key_len)), &value);
RETVAL_BOOL(isExist);
}
static PHP_METHOD(rocksdb, keyExist)
{
char *key;
size_t key_len;
zval *zreadoptions = nullptr;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(key, key_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(zreadoptions)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
DB *db = php_rocksdb_db_get_ptr(ZEND_THIS);
ReadOptions rop;
if (zreadoptions)
{
check_rocksdb_db_read_options(rop, Z_ARRVAL_P(zreadoptions));
}
std::string value;
Status s = db->Get(rop, std::string(key, key_len), &value);
RETVAL_BOOL(!s.IsNotFound());
}
static const zend_function_entry rocksdb_methods[] =
{
PHP_ME(rocksdb, __construct, arginfo_rocksdb_db_void, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, open, arginfo_rocksdb_db_open, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, openForReadOnly, arginfo_rocksdb_db_openForReadOnly, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, openAsSecondary, arginfo_rocksdb_db_openAsSecondary, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, put, arginfo_rocksdb_db_put, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, write, arginfo_rocksdb_db_write, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, get, arginfo_rocksdb_db_get, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, del, arginfo_rocksdb_db_del, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, deleteRange, arginfo_rocksdb_db_deleteRange, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, newIterator, arginfo_rocksdb_db_newIterator, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, close, arginfo_rocksdb_db_void, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, destroyDB, arginfo_rocksdb_db_destroyDB, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(rocksdb, keyMayExist, arginfo_rocksdb_db_keyMayExist, ZEND_ACC_PUBLIC)
PHP_ME(rocksdb, keyExist, arginfo_rocksdb_db_keyExist, ZEND_ACC_PUBLIC)
PHP_FE_END
};
PHP_MINIT_FUNCTION(rocksdb)
{
ROCKSDB_INIT_CLASS_ENTRY(rocksdb_db, "RocksDB\\DB", NULL, NULL, rocksdb_methods);
ROCKSDB_SET_CLASS_CUSTOM_OBJECT(rocksdb_db, php_rocksdb_db_create_object, php_rocksdb_db_free_object, rocksdb_db_t, std);
php_rocksdb_exception_minit(module_number);
php_rocksdb_iterator_minit(module_number);
php_rocksdb_write_batch_minit(module_number);
php_rocksdb_transaction_db_minit(module_number);
php_rocksdb_transaction_minit(module_number);
php_rocksdb_snapshot_minit(module_number);
return SUCCESS;
}
PHP_MINFO_FUNCTION(rocksdb)
{
char buf[64];
php_info_print_table_start();
php_info_print_table_header(2, "RocksDB support", "enabled");
php_info_print_table_row(2, "Author", "Codinghuang <https://github.com/huanghantao>");
php_info_print_table_row(2, "Version", PHP_ROCKSDB_VERSION);
snprintf(buf, sizeof(buf), "%s %s", __DATE__, __TIME__);
php_info_print_table_row(2, "Built", buf);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
static const zend_function_entry rocksdb_functions[] = {
PHP_FE_END
};
zend_module_entry rocksdb_module_entry = {
STANDARD_MODULE_HEADER,
"rocksdb", /* Extension name */
rocksdb_functions, /* zend_function_entry */
PHP_MINIT(rocksdb), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(rocksdb), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(rocksdb), /* PHP_MINFO - Module info */
PHP_ROCKSDB_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_ROCKSDB
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(rocksdb)
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codinghuang/php-rocksdb.git
git@gitee.com:codinghuang/php-rocksdb.git
codinghuang
php-rocksdb
php-rocksdb
master

搜索帮助