-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldb.php
More file actions
executable file
·98 lines (79 loc) · 1.92 KB
/
Copy pathldb.php
File metadata and controls
executable file
·98 lines (79 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env php
<?php
/**
* LevelDB Shell
* Copyright(c) 2012 Shaun Li <shonhen@gmail.com>
* MIT Licensed
*/
class UsageException extends Exception {
}
class LevelDBShell {
private $_db;
public function __construct($dbfolder) {
$this->_db = new LevelDB($dbfolder);
}
protected function db() {
return $this->_db;
}
public function actionKeys($pattern = '') {
$iter = new LevelDBIterator($this->db());
foreach ($iter as $key => $value) {
if (!$pattern)
echo $key . PHP_EOL;
if (preg_match('/^' . $pattern . '$/', $key))
echo $key . PHP_EOL;
}
}
public function actionGet($key) {
echo $this->db()->get($key) . PHP_EOL;
}
public function actionSet($key, $value) {
$this->db()->set($key, $value);
}
public function actionDelete($key) {
$this->db()->delete($key);
}
public static function run($args) {
if (empty($args))
throw new UsageException;
$dbfolder = null;
$command = null;
$arguments = array();
while (!empty($args)) {
$arg = array_shift($args);
switch ($arg) {
case '-d':
$dbfolder = array_shift($args);
break;
default:
if (!$command)
$command = $arg;
else
$arguments[] = $arg;
break;
}
}
if (!$dbfolder)
throw new UsageException('Database folder is missing');
$shell = new LevelDBShell($dbfolder);
$method = 'action' . ucfirst($command);
if (!method_exists($shell, $method))
throw new UsageException('Unknown command - ' . $command);
call_user_method_array($method, $shell, $arguments);
}
}
try {
LevelDBShell::run(array_slice($argv, 1));
} catch (UsageException $e) {
if ($msg = $e->getMessage())
echo $msg . PHP_EOL;
echo <<< END
Usage:
ldb -d <folder> <command> [<args>]
The most commonly used commands are:
keys [pattern]
set <key> <value>
get <key>
delete <key>
END;
}