-
Notifications
You must be signed in to change notification settings - Fork 19
/
search_leveldb.py
54 lines (43 loc) · 1.43 KB
/
search_leveldb.py
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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import os
import sys
import re
from optparse import OptionParser
import time
import leveldb
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("--verbose", action="store_const", const=1, dest="verbose", help="verbose mode")
parser.add_option("-d", "--db", dest="dbdir",help="db dir path", metavar="DB")
(options, args) = parser.parse_args()
if options.verbose == 1 : VERBOSE = 1
db_dir = options.dbdir
if db_dir == None :
parser.print_help()
sys.exit(1)
db = leveldb.LevelDB(db_dir)
lock_file = db_dir '/LOCK'
if os.path.exists(lock_file) :
try : os.remove(lock_file)
except OSError :
sys.stderr.write("remove lock file(%s) fail\n" % (lock_file))
sys.exit(1)
startTime = time.time()
linecount = 0
while 1 :
try : line = sys.stdin.readline()
except KeyboardInterrupt : break
if not line : break
try : line = line.strip()
except : continue
if not line : continue
linecount = 1
if linecount % 1000 == 0 :
sys.stderr.write("[linecount]" "\t" str(linecount) "\n")
key,value = line.split('\t',1)
if not key or not value : continue
ret = db.Get(key)
if ret : print ret
durationTime = time.time() - startTime
sys.stderr.write("duration time = %f\n" % durationTime)