Simplify callers of diskfile.[read|write]_metadata()

As it happens, diskfile.read_metadata() and diskfile.write_metadata()
can take either an open file or a filename as their first arguments
(since xattr.[get|set]xattr() can), so we can clean up a couple places
where we were opening a file just to call read_metadata() or
write_metadata() on it. This results in 2 fewer system calls.

Example strace output:

/* read_metadata(filename) */
getxattr("/mnt/sdb1/1/node/sdb1/afile", "user.some.key", 0x0, 0) = 10
getxattr("/mnt/sdb1/1/node/sdb1/afile", "user.some.key", "some-value", 10) = 10

/* fp = open(filename); read_metadata(fp) */
open("/mnt/sdb1/1/node/sdb1/afile", O_RDONLY) = 4
fstat(4, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
fgetxattr(4, "user.some.key", 0x0, 0)   = 10
fgetxattr(4, "user.some.key", "some-value", 10) = 10

Change-Id: I321d8663b9e9e47b8f3ee6c21a1b65b408bb80e6
This commit is contained in:
Samuel Merritt 2013-10-21 13:26:11 -07:00
parent 7ccde73974
commit 0a26bb20b1
2 changed files with 12 additions and 20 deletions

View File

@ -74,7 +74,7 @@ def read_metadata(fd):
"""
Helper function to read the pickled metadata from an object file.
:param fd: file descriptor to load the metadata from
:param fd: file descriptor or filename to load the metadata from
:returns: dictionary of metadata
"""
@ -93,7 +93,7 @@ def write_metadata(fd, metadata):
"""
Helper function to write pickled metadata for an object file.
:param fd: file descriptor to write the metadata
:param fd: file descriptor or filename to write the metadata
:param metadata: metadata to write
"""
metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
@ -849,8 +849,7 @@ class DiskFile(object):
if not ts_file:
exc = DiskFileNotExist()
else:
with open(ts_file) as fp:
metadata = read_metadata(fp)
metadata = read_metadata(ts_file)
# All well and good that we have found a tombstone file, but
# we don't have a data file so we are just going to raise an
# exception that we could not find the object, providing the
@ -941,8 +940,7 @@ class DiskFile(object):
fp = open(data_file, 'rb')
datafile_metadata = read_metadata(fp)
if meta_file:
with open(meta_file) as mfp:
self._metadata = read_metadata(mfp)
self._metadata = read_metadata(meta_file)
sys_metadata = dict(
[(key, val) for key, val in datafile_metadata.iteritems()
if key.lower() in DATAFILE_SYSTEM_META])

View File

@ -67,11 +67,9 @@ class TestObjectFailures(TestCase):
obj = 'object-%s' % uuid4()
onode, opart, data_file = self._setup_data_file(container, obj,
'VERIFY')
with open(data_file) as fpointer:
metadata = read_metadata(fpointer)
metadata = read_metadata(data_file)
metadata['ETag'] = 'badetag'
with open(data_file) as fpointer:
write_metadata(fpointer, metadata)
write_metadata(data_file, metadata)
odata = direct_client.direct_get_object(
onode, opart, self.account, container, obj)[-1]
@ -88,11 +86,10 @@ class TestObjectFailures(TestCase):
obj = 'object-range-%s' % uuid4()
onode, opart, data_file = self._setup_data_file(container, obj,
'RANGE')
with open(data_file) as fpointer:
metadata = read_metadata(fpointer)
metadata = read_metadata(data_file)
metadata['ETag'] = 'badetag'
with open(data_file) as fpointer:
write_metadata(fpointer, metadata)
write_metadata(data_file, metadata)
for header, result in [({'Range': 'bytes=0-2'}, 'RAN'),
({'Range': 'bytes=1-11'}, 'ANGE'),
({'Range': 'bytes=0-11'}, 'RANGE')]:
@ -111,8 +108,7 @@ class TestObjectFailures(TestCase):
container = 'container-zbyte-%s' % uuid4()
obj = 'object-zbyte-%s' % uuid4()
onode, opart, data_file = self._setup_data_file(container, obj, 'DATA')
with open(data_file) as fpointer:
metadata = read_metadata(fpointer)
metadata = read_metadata(data_file)
unlink(data_file)
with open(data_file, 'w') as fpointer:
@ -129,8 +125,7 @@ class TestObjectFailures(TestCase):
container = 'container-zbyte-%s' % uuid4()
obj = 'object-zbyte-%s' % uuid4()
onode, opart, data_file = self._setup_data_file(container, obj, 'DATA')
with open(data_file) as fpointer:
metadata = read_metadata(fpointer)
metadata = read_metadata(data_file)
unlink(data_file)
with open(data_file, 'w') as fpointer:
@ -147,8 +142,7 @@ class TestObjectFailures(TestCase):
container = 'container-zbyte-%s' % uuid4()
obj = 'object-zbyte-%s' % uuid4()
onode, opart, data_file = self._setup_data_file(container, obj, 'DATA')
with open(data_file) as fpointer:
metadata = read_metadata(fpointer)
metadata = read_metadata(data_file)
unlink(data_file)
with open(data_file, 'w') as fpointer: