-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
executable file
·83 lines (73 loc) · 1.81 KB
/
db.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
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
import sys, json
class SimpleDB:
def __init__(self, fname):
self.index_table = dict()
self.index_table.setdefault(-1)
self.fname = fname
self.fp = None
def build_index(self):
fp = open(self.fname)
if not fp:
sys.stderr.write('\nERROR: FAIL TO OPEN FILE [%s].\n' % (self.fname))
return -1
fp.seek(0)
while 1:
offset = fp.tell()
line = fp.readline().strip()
if not line: break
[id, dummy] = line.split('\t', 1)
self.index_table[id] = offset
fp.close()
return 0
def connect(self):
if self.is_connect(): self.close()
try:
self.fp = open(self.fname)
except:
sys.stderr.write('\nERROR: FAIL TO OPEN FILE [%s].\n' % (self.fname))
return -1
return 0
def close(self):
try:
self.fp.close()
except:
sys.stderr.write('\nERROR: Fail to close file [%s].\n' % (self.fname))
self.fp = None
return -1
self.fp = None
return 0
def is_connect(self):
return self.fp != None
def exist_key(self, id):
if id in self.index_table: return True
else: return False
def read_line(self, id):
if not self.is_connect():
sys.stderr.write('\nERROR: CONNECTION NOT ESTABLISH.\n')
return None
if not id in self.index_table:
sys.stderr.write('\nERROR: ID [%s] NOT EXIST.\n' % (id))
return None
offset = self.index_table[id]
self.fp.seek(offset)
line = self.fp.readline().strip()
return line
def read_json(self, id):
line = self.read_line(id)
if line == None: return None
[id, jsonStr] = line.split('\t', 1)
try:
jsonObj = json.loads(jsonStr, encoding = 'gbk')
except:
sys.stderr.write('\nERROR: FAIL TO PRASE JSON STR [%s].\n' % (jsonStr))
return None
return jsonObj
'''
from db import SimpleDB
db = SimpleDB('novel_index.test')
db.build_index()
db.connect()
line = db.read_line('296117440')
json = db.read_json('296117440')
db.close()
'''