Commit 0c3b1e65 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Implement a bit more of MySQLDatabaseManager. Some are still missing.

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@49 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 062626e8
......@@ -80,6 +80,16 @@ class DatabaseManager(object):
search from unfinished transactions as well."""
raise NotImplementedError('this method must be overridden')
def getObject(self, oid, tid = None, before_tid = None):
"""Return a tuple of an object ID, a serial, a compression
specification, a checksum, and object data, if a given object
ID is present. Otherwise, return None. If tid is None and
before_tid is None, the latest revision is taken. If tid is
specified, the given revision is taken. If tid is not specified,
but before_tid is specified, the latest revision before the
given revision is taken."""
raise NotImplementedError('this method must be overridden')
def changePartitionTable(self, ptid, cell_list):
"""Change a part of a partition table. The list of cells is
a tuple of tuples, each of which consists of an offset (row ID),
......@@ -93,3 +103,16 @@ class DatabaseManager(object):
thrown away."""
raise NotImplementedError('this method must be overridden')
def storeTransaction(self, tid, object_list, transaction):
"""Store a transaction temporarily. Note that this transaction
is not finished yet. The list of objects contains tuples,
each of which consists of an object ID, a compression specification,
a checksum and object data. The transaction is either None or
a tuple of the list of oids, user information, a description and
extension information."""
raise NotImplementedError('this method must be overridden')
def finishTransaction(self, tid):
"""Finish a transaction specified by a given ID, by moving
temporarily data to a finished area."""
raise NotImplementedError('this method must be overridden')
......@@ -92,7 +92,7 @@ class MySQLDatabaseManager(DatabaseManager):
q("""CREATE TABLE IF NOT EXISTS obj (
oid BINARY(8) NOT NULL,
serial BINARY(8) NOT NULL,
checksum BINARY(4) NOT NULL,
checksum INT UNSIGNED NOT NULL,
compression TINYINT UNSIGNED NOT NULL,
value MEDIUMBLOB NOT NULL,
PRIMARY KEY (oid, serial)
......@@ -111,7 +111,7 @@ class MySQLDatabaseManager(DatabaseManager):
q("""CREATE TABLE IF NOT EXISTS tobj (
oid BINARY(8) NOT NULL,
serial BINARY(8) NOT NULL,
checksum BINARY(4) NOT NULL,
checksum INT UNSIGNED NOT NULL,
compression TINYINT UNSIGNED NOT NULL,
value MEDIUMBLOB NOT NULL
) ENGINE = InnoDB""")
......@@ -267,6 +267,28 @@ class MySQLDatabaseManager(DatabaseManager):
return True
return False
def getObject(self, oid, tid = None, before_tid = None):
q = self.query
e = self.escape
oid = e(oid)
if tid is not None:
tid = e(tid)
r = q("""SELECT * FROM obj WHERE oid = '%s' AND serial = '%s'""" \
% (oid, tid))
elif before_tid is not None:
before_tid = e(before_tid)
r = q("""SELECT * FROM obj WHERE oid = '%s' AND serial < '%s' ORDER BY serial DESC LIMIT 1""" \
% (oid, before_tid))
else:
# XXX I want to express "HAVING serial = MAX(serial)", but
# MySQL does not use an index for a HAVING clause!
r = q("""SELECT * FROM obj WHERE oid = '%s' ORDER BY serial DESC LIMIT 1""" \
% oid)
try:
return r[0]
except IndexError:
return None
def doSetPartitionTable(self, ptid, cell_list, reset):
q = self.query
e = self.escape
......@@ -295,3 +317,51 @@ class MySQLDatabaseManager(DatabaseManager):
def setPartitionTable(self, ptid, cell_list):
self.doSetPartitionTable(ptid, cell_list, False)
def storeTransaction(self, tid, object_list, transaction):
q = self.query
e = self.escape
tid = e(tid)
self.begin()
try:
# XXX it might be more efficient to insert multiple objects
# at a time, but it is potentially dangerous, because
# a packet to MySQL can exceed the maximum packet size.
# However, I do not think this would be a big problem, because
# tobj has no index, so inserting one by one should not be
# significantly different from inserting many at a time.
for oid, compression, checksum, data in object_list:
oid = e(oid)
data = e(data)
q("""INSERT INTO tobj VALUES ('%s', '%s', %d, %d, '%s')""" \
% (oid, tid, compression, checksum, data))
if transaction is not None:
oid_list, user, desc, ext = transaction
oids = e(''.join(oid_list))
user = e(user)
desc = e(desc)
ext = e(ext)
q("""INSERT INTO ttrans VALUES ('%s', '%s', '%s', '%s', '%s')""" \
% (tid, oids, user, desc, ext))
except:
self.rollback()
raise
self.commit()
def finishTransaction(self, tid):
q = self.query
e = self.escape
tid = e(tid)
self.begin()
try:
q("""INSERT INTO obj SELECT * FROM tobj WHERE tobj.serial = '%s'""" \
% tid)
q("""DELETE FROM tobj WHERE serial = '%s'""" % tid)
q("""INSERT INTO trans SELECT * FROM ttrans WHERE ttrans.tid = '%s'""" \
% tid)
q("""DELETE FROM ttrans WHERE tid = '%s'""" % tid)
except:
self.rollback()
raise
self.commit()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment