-
Notifications
You must be signed in to change notification settings - Fork 31
/
DBTempLog.py
193 lines (157 loc) · 5.68 KB
/
DBTempLog.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright (C) 2008 LibreSoft
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors :
# Carlos Garcia Campos <[email protected]>
from ContentHandler import ContentHandler
from Database import SqliteDatabase, MysqlDatabase, TableAlreadyExists, statement, ICursor
from Repository import Commit
from AsyncQueue import AsyncQueue
import threading
from cStringIO import StringIO
from cPickle import dump, load
class DBTempLog:
INTERVAL_SIZE = 100
def __init__(self, db):
self.db = db
self._need_clear = False
try:
self.__create_table()
except TableAlreadyExists:
# FIXME: we can use this to recover from a crash
self._need_clear = True
self.__drop_table()
self.__create_table()
self.queue = AsyncQueue(50)
self.writer_thread = threading.Thread(target=self.__writer,
args=(self.queue,))
self.writer_thread.setDaemon(True)
self.writer_thread.start()
def __create_table(self):
cnn = self.db.connect()
cursor = cnn.cursor()
if isinstance(self.db, SqliteDatabase):
import sqlite3
try:
cursor.execute("CREATE TABLE _temp_log (" +
"id integer primary key autoincrement," +
"rev varchar," +
"date datetime," +
"object blob" +
")")
except sqlite3.OperationalError:
cursor.close()
raise TableAlreadyExists
except:
raise
elif isinstance(self.db, MysqlDatabase):
import _mysql_exceptions
try:
cursor.execute("CREATE TABLE _temp_log (" +
"id INT AUTO_INCREMENT PRIMARY KEY," +
"rev mediumtext," +
"date datetime," +
"object LONGBLOB" +
") CHARACTER SET=utf8")
except _mysql_exceptions.OperationalError, e:
if e.args[0] == 1050:
cursor.close()
raise TableAlreadyExists
raise
except:
raise
cnn.commit()
cursor.close()
cnn.close()
self._need_clear = True
def __drop_table(self):
if not self._need_clear:
return
cnn = self.db.connect()
cursor = cnn.cursor()
cursor.execute("DROP TABLE _temp_log")
cnn.commit()
cursor.close()
cnn.close()
self._need_clear = False
def __writer(self, queue):
cnn = self.db.connect()
cursor = cnn.cursor()
commits = []
n_commits = 0
while True:
commit = queue.get()
if not isinstance(commit, Commit):
queue.done()
break
io = StringIO()
dump(commit, io, -1)
obj = io.getvalue()
io.close()
commits.append((commit.revision, commit.date, self.db.to_binary(obj)))
n_commits += 1
del commit
if n_commits == 50:
cursor.executemany(
statement("INSERT into _temp_log (rev, date, object) values (?, ?, ?)", self.db.place_holder),
commits)
cnn.commit()
del commits
commits = []
n_commits = 0
queue.done()
if commits:
cursor.executemany(
statement("INSERT into _temp_log (rev, date, object) values (?, ?, ?)", self.db.place_holder), commits)
cnn.commit()
del commits
cursor.close()
cnn.close()
def insert(self, commit):
self.queue.put(commit)
def foreach(self, cb, order=None):
self.flush()
cnn = self.db.connect()
if order is None or order == ContentHandler.ORDER_REVISION:
query = "SELECT object from _temp_log order by id desc"
else:
query = "SELECT object from _temp_log order by date asc"
# We need to split the query to save memory
icursor = ICursor(cnn.cursor(), self.INTERVAL_SIZE)
icursor.execute(statement(query, self.db.place_holder))
rs = icursor.fetchmany()
while rs:
for t in rs:
obj = t[0]
io = StringIO(obj)
commit = load(io)
io.close()
cb(commit)
rs = icursor.fetchmany()
icursor.close()
cnn.close()
def flush(self):
self.queue.join()
if self.writer_thread.isAlive():
# Tell the thread to exit
# The value doesn't really matter
self.queue.put("END")
self.writer_thread.join()
def clear(self):
self.__drop_table()
def __del__(self):
self.flush()
self.clear()