Skip to content

Commit e42acdb

Browse files
committed
add rocksdb
1 parent 40d1527 commit e42acdb

File tree

9 files changed

+476
-1
lines changed

9 files changed

+476
-1
lines changed

examples/rocksdb

-1
This file was deleted.

examples/rocksdb/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.idea
2+
/cmake-build-debug/
3+
/db
4+
.cproject
5+
*.so
6+
.project
7+
.settings

examples/rocksdb/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PHP_INCLUDE = `php-config --includes`
2+
PHP_LIBS = `php-config --libs`
3+
PHP_LDFLAGS = `php-config --ldflags`
4+
PHP_INCLUDE_DIR = `php-config --include-dir`
5+
PHP_EXTENSION_DIR = `php-config --extension-dir`
6+
7+
php-rocksdb.so: rocksdb.cpp
8+
c++ -DHAVE_CONFIG_H -g -o php-rocksdb.so -O0 -fPIC -shared rocksdb.cpp -std=c++11 -lphpx -lrocksdb ${PHP_INCLUDE} -I${PHP_INCLUDE_DIR}
9+
install: php-rocksdb.so
10+
cp php-rocksdb.so ${PHP_EXTENSION_DIR}/
11+
clean:
12+
rm *.so

examples/rocksdb/examples/del.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
$config = array(
3+
'options' => array(
4+
'create_if_missing' => true, // if the specified database didn't exist will create a new one
5+
'error_if_exists' => false, // if the opened database exsits will throw exception
6+
'paranoid_checks' => false,
7+
'paranoid_checks' => false,
8+
'merge_operator' => ',',
9+
),
10+
/* default readoptions */
11+
'readoptions' => array(
12+
'verify_check_sum' => false,
13+
'fill_cache' => true,
14+
),
15+
/* default write options */
16+
'writeoptions' => array(
17+
'sync' => false
18+
),
19+
);
20+
$db_name = dirname(__DIR__)."/db/test6";
21+
$r = new RocksDB($db_name, $config['options'], $config['readoptions'], $config['writeoptions']);
22+
23+
$key = "key_test";
24+
$r->put($key, "hello world");
25+
26+
$key = "key_test";
27+
$res = $r->get($key);
28+
echo "get {$key} res:".var_export($res,1)."\n";
29+
30+
$r->delete($key);
31+
32+
$res = $r->get($key);
33+
echo "get {$key} res:".var_export($res,1)."\n";

examples/rocksdb/examples/get.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
$config = array(
3+
'options' => array(
4+
'create_if_missing' => true, // if the specified database didn't exist will create a new one
5+
'error_if_exists' => false, // if the opened database exsits will throw exception
6+
'paranoid_checks' => false,
7+
'paranoid_checks' => false,
8+
'merge_operator' => ',',
9+
),
10+
/* default readoptions */
11+
'readoptions' => array(
12+
'verify_check_sum' => false,
13+
'fill_cache' => true,
14+
),
15+
/* default write options */
16+
'writeoptions' => array(
17+
'sync' => false
18+
),
19+
);
20+
$db_name = dirname(__DIR__)."/db/test6";
21+
$r = new RocksDB($db_name, $config['options'], $config['readoptions'], $config['writeoptions']);
22+
23+
$key = "key_test";
24+
$r->put($key, "hello world");
25+
$res = $r->get($key);
26+
echo "get {$key} res:".var_export($res,1)."\n";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* A MergeOperator for rocksdb that implements string append.
3+
* @author Deon Nicholas ([email protected])
4+
* Copyright 2013 Facebook
5+
*/
6+
7+
#include "stringappend.h"
8+
9+
#include <memory>
10+
#include <assert.h>
11+
12+
#include "rocksdb/slice.h"
13+
#include "rocksdb/merge_operator.h"
14+
#include "utilities/merge_operators.h"
15+
16+
namespace rocksdb {
17+
18+
// Constructor: also specify the delimiter character.
19+
StringAppendOperator::StringAppendOperator(char delim_char)
20+
: delim_(delim_char) {
21+
}
22+
23+
// Implementation for the merge operation (concatenates two strings)
24+
bool StringAppendOperator::Merge(const Slice& key,
25+
const Slice* existing_value,
26+
const Slice& value,
27+
std::string* new_value,
28+
Logger* logger) const {
29+
30+
// Clear the *new_value for writing.
31+
assert(new_value);
32+
new_value->clear();
33+
34+
if (!existing_value) {
35+
// No existing_value. Set *new_value = value
36+
new_value->assign(value.data(),value.size());
37+
} else {
38+
// Generic append (existing_value != null).
39+
// Reserve *new_value to correct size, and apply concatenation.
40+
new_value->reserve(existing_value->size() + 1 + value.size());
41+
new_value->assign(existing_value->data(),existing_value->size());
42+
new_value->append(1,delim_);
43+
new_value->append(value.data(), value.size());
44+
}
45+
46+
return true;
47+
}
48+
49+
const char* StringAppendOperator::Name() const {
50+
return "StringAppendOperator";
51+
}
52+
53+
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator() {
54+
return std::make_shared<StringAppendOperator>(',');
55+
}
56+
57+
} // namespace rocksdb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* A MergeOperator for rocksdb that implements string append.
3+
* @author Deon Nicholas ([email protected])
4+
* Copyright 2013 Facebook
5+
*/
6+
7+
#pragma once
8+
#include "rocksdb/merge_operator.h"
9+
#include "rocksdb/slice.h"
10+
11+
namespace rocksdb {
12+
13+
class StringAppendOperator : public AssociativeMergeOperator {
14+
public:
15+
// Constructor: specify delimiter
16+
explicit StringAppendOperator(char delim_char);
17+
18+
virtual bool Merge(const Slice& key,
19+
const Slice* existing_value,
20+
const Slice& value,
21+
std::string* new_value,
22+
Logger* logger) const override;
23+
24+
virtual const char* Name() const override;
25+
26+
private:
27+
char delim_; // The delimiter is inserted between elements
28+
29+
};
30+
31+
} // namespace rocksdb

0 commit comments

Comments
 (0)