Commit 9dbe70f3 authored by Kirill Smelkov's avatar Kirill Smelkov

*: Don't forget to close opened ZODB storage

Storages need to be closed to indicate a clean access end. If a storage is
not closed cleanly it might require to spend time and resources on next
open. For example FileStorage might need to recompute the index.

/reviewed-on !6
parent 220d1121
...@@ -12,6 +12,7 @@ import shutil ...@@ -12,6 +12,7 @@ import shutil
from ZODB.FileStorage import FileIterator, FileStorage, packed_version from ZODB.FileStorage import FileIterator, FileStorage, packed_version
from ZODB.FileStorage.format import FileStorageFormatter from ZODB.FileStorage.format import FileStorageFormatter
from ZODB.utils import get_pickle_metadata from ZODB.utils import get_pickle_metadata
from golang import func, defer
class DeltaFileStorage( class DeltaFileStorage(
FileStorageFormatter, FileStorageFormatter,
...@@ -22,6 +23,9 @@ class DeltaFileStorage( ...@@ -22,6 +23,9 @@ class DeltaFileStorage(
def iterator(self, start=None, stop=None): def iterator(self, start=None, stop=None):
return DeltaFileIterator(self._file_name, start, stop) return DeltaFileIterator(self._file_name, start, stop)
def close(self):
pass
class DeltaFileIterator(FileIterator): class DeltaFileIterator(FileIterator):
def __init__(self, filename, start=None, stop=None, pos=0L): def __init__(self, filename, start=None, stop=None, pos=0L):
assert isinstance(filename, str) assert isinstance(filename, str)
...@@ -157,11 +161,13 @@ def report(rep, csv=False): ...@@ -157,11 +161,13 @@ def report(rep, csv=False):
rep.FBYTES * 100.0 / rep.DBYTES, rep.FBYTES * 100.0 / rep.DBYTES,
rep.FBYTES * 1.0 / rep.FOIDS) rep.FBYTES * 1.0 / rep.FOIDS)
@func
def analyze(path, use_dbm, delta_fs): def analyze(path, use_dbm, delta_fs):
if delta_fs: if delta_fs:
fs = DeltaFileStorage(path, read_only=1) fs = DeltaFileStorage(path, read_only=1)
else: else:
fs = FileStorage(path, read_only=1) fs = FileStorage(path, read_only=1)
defer(fs.close)
fsi = fs.iterator() fsi = fs.iterator()
report = Report(use_dbm, delta_fs) report = Report(use_dbm, delta_fs)
for txn in fsi: for txn in fsi:
......
...@@ -34,6 +34,7 @@ from __future__ import print_function ...@@ -34,6 +34,7 @@ from __future__ import print_function
from zodbtools.util import ashex, inf, nextitem, txnobjv, parse_tidrange, TidRangeInvalid, \ from zodbtools.util import ashex, inf, nextitem, txnobjv, parse_tidrange, TidRangeInvalid, \
storageFromURL storageFromURL
from time import time from time import time
from golang import func, defer
# compare two storage transactions # compare two storage transactions
# 0 - equal, 1 - non-equal # 0 - equal, 1 - non-equal
...@@ -127,6 +128,7 @@ Options: ...@@ -127,6 +128,7 @@ Options:
-h --help show this help -h --help show this help
""", file=out) """, file=out)
@func
def main2(argv): def main2(argv):
verbose = False verbose = False
...@@ -159,8 +161,8 @@ def main2(argv): ...@@ -159,8 +161,8 @@ def main2(argv):
print("E: invalid tidrange: %s" % e, file=sys.stderr) print("E: invalid tidrange: %s" % e, file=sys.stderr)
sys.exit(2) sys.exit(2)
stor1 = storageFromURL(storurl1, read_only=True) stor1 = storageFromURL(storurl1, read_only=True); defer(stor1.close)
stor2 = storageFromURL(storurl2, read_only=True) stor2 = storageFromURL(storurl2, read_only=True); defer(stor2.close)
zcmp = storcmp(stor1, stor2, tidmin, tidmax, verbose) zcmp = storcmp(stor1, stor2, tidmin, tidmax, verbose)
sys.exit(1 if zcmp else 0) sys.exit(1 if zcmp else 0)
......
...@@ -44,7 +44,7 @@ from zodbtools.util import ashex, storageFromURL ...@@ -44,7 +44,7 @@ from zodbtools.util import ashex, storageFromURL
from ZODB.utils import p64, u64, z64 from ZODB.utils import p64, u64, z64
from ZODB.POSException import POSKeyError from ZODB.POSException import POSKeyError
from ZODB._compat import BytesIO from ZODB._compat import BytesIO
from golang import panic from golang import func, defer, panic
# zodbcommit commits new transaction into ZODB storage with data specified by # zodbcommit commits new transaction into ZODB storage with data specified by
...@@ -135,6 +135,7 @@ Options: ...@@ -135,6 +135,7 @@ Options:
-h --help show this help -h --help show this help
""", file=out) """, file=out)
@func
def main(argv): def main(argv):
try: try:
optv, argv = getopt.getopt(argv[1:], "h", ["help"]) optv, argv = getopt.getopt(argv[1:], "h", ["help"])
...@@ -156,6 +157,7 @@ def main(argv): ...@@ -156,6 +157,7 @@ def main(argv):
at = argv[1].decode('hex') at = argv[1].decode('hex')
stor = storageFromURL(storurl) stor = storageFromURL(storurl)
defer(stor.close)
zin = 'txn 0000000000000000 " "\n' # artificial transaction header zin = 'txn 0000000000000000 " "\n' # artificial transaction header
zin += sys.stdin.read() zin += sys.stdin.read()
......
...@@ -65,7 +65,7 @@ import sys ...@@ -65,7 +65,7 @@ import sys
import logging import logging
import re import re
from golang.gcompat import qq from golang.gcompat import qq
from golang import strconv from golang import func, defer, strconv
# txn_raw_extension returns raw extension from txn metadata # txn_raw_extension returns raw extension from txn metadata
def txn_raw_extension(stor, txn): def txn_raw_extension(stor, txn):
...@@ -240,6 +240,7 @@ Options: ...@@ -240,6 +240,7 @@ Options:
-h --help show this help -h --help show this help
""", file=out) """, file=out)
@func
def main(argv): def main(argv):
hashonly = False hashonly = False
...@@ -273,6 +274,7 @@ def main(argv): ...@@ -273,6 +274,7 @@ def main(argv):
sys.exit(2) sys.exit(2)
stor = storageFromURL(storurl, read_only=True) stor = storageFromURL(storurl, read_only=True)
defer(stor.close)
zodbdump(stor, tidmin, tidmax, hashonly) zodbdump(stor, tidmin, tidmax, hashonly)
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
from __future__ import print_function from __future__ import print_function
from zodbtools.util import ashex, storageFromURL from zodbtools.util import ashex, storageFromURL
from collections import OrderedDict from collections import OrderedDict
from golang import func, defer
import sys import sys
# {} parameter_name -> get_parameter(stor) # {} parameter_name -> get_parameter(stor)
...@@ -71,6 +72,7 @@ Options: ...@@ -71,6 +72,7 @@ Options:
-h --help show this help -h --help show this help
""", file=out) """, file=out)
@func
def main(argv): def main(argv):
try: try:
optv, argv = getopt.getopt(argv[1:], "h", ["help"]) optv, argv = getopt.getopt(argv[1:], "h", ["help"])
...@@ -91,5 +93,6 @@ def main(argv): ...@@ -91,5 +93,6 @@ def main(argv):
sys.exit(2) sys.exit(2)
stor = storageFromURL(storurl, read_only=True) stor = storageFromURL(storurl, read_only=True)
defer(stor.close)
zodbinfo(stor, argv[1:]) zodbinfo(stor, argv[1:])
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