Commit d955f79a authored by Kirill Smelkov's avatar Kirill Smelkov

Factor out utility routines to zodbtool.util

parent 66946b8d
# zodbtool - various utility routines
# Copyright (C) 2016 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
def ashex(s):
return s.encode('hex')
# something that is greater than everything else
class Inf:
def __cmp__(self, other):
return +1
inf = Inf()
# get next item from iter -> (item, !stop)
def nextitem(it):
try:
item = it.next()
except StopIteration:
return None, False
else:
return item, True
# objects of a IStorageTransactionInformation
def txnobjv(txn):
objv = []
for obj in txn:
assert obj.tid == txn.tid
assert obj.version == ''
objv.append(obj)
objv.sort(key = lambda obj: obj.oid) # in canonical order
return objv
# "tidmin..tidmax" -> (tidmin, tidmax)
class TidRangeInvalid(Exception):
pass
def parse_tidrange(tidrange):
try:
tidmin, tidmax = tidrange.split("..")
except ValueError: # not exactly 2 parts in between ".."
raise TidRangeInvalid(tidrange)
try:
tidmin = tidmin.decode("hex")
tidmax = tidmax.decode("hex")
except TypeError: # hex decoding error
raise TidRangeInvalid(tidrange)
# empty tid means -inf / +inf respectively
# ( which is None in IStorage.iterator() )
return (tidmin or None, tidmax or None)
...@@ -16,39 +16,9 @@ Exit status is 0 if inputs are the same, 1 if different, 2 if error. ...@@ -16,39 +16,9 @@ Exit status is 0 if inputs are the same, 1 if different, 2 if error.
""" """
from __future__ import print_function from __future__ import print_function
from zodbtool.util import ashex, inf, nextitem, txnobjv, parse_tidrange, TidRangeInvalid
from time import time from time import time
def ashex(s):
return s.encode('hex')
# something that is greater than everything else
class Inf:
def __cmp__(self, other):
return +1
inf = Inf()
# get next item from iter -> (item, !stop)
def nextitem(it):
try:
item = it.next()
except StopIteration:
return None, False
else:
return item, True
# objects of a IStorageTransactionInformation
def txnobjv(txn):
objv = []
for obj in txn:
assert obj.tid == txn.tid
assert obj.version == ''
objv.append(obj)
objv.sort(key = lambda obj: obj.oid) # in canonical order
return objv
# compare two storage transactions # compare two storage transactions
# 0 - equal, 1 - non-equal # 0 - equal, 1 - non-equal
def txncmp(txn1, txn2): def txncmp(txn1, txn2):
...@@ -146,26 +116,6 @@ Options: ...@@ -146,26 +116,6 @@ Options:
-h --help show this help -h --help show this help
""", file=out) """, file=out)
# tidmin..tidmax -> (tidmin, tidmax)
class TidRangeInvalid(Exception):
pass
def parse_tidrange(tidrange):
try:
tidmin, tidmax = tidrange.split("..")
except ValueError: # not exactly 2 parts in between ".."
raise TidRangeInvalid(tidrange)
try:
tidmin = tidmin.decode("hex")
tidmax = tidmax.decode("hex")
except TypeError: # hex decoding error
raise TidRangeInvalid(tidrange)
# empty tid means -inf / +inf respectively
# ( which is None in IStorage.iterator() )
return (tidmin or None, tidmax or None)
def main2(): def main2():
verbose = False verbose = False
......
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