Migrate file storage metadata to json
As suggested by @CapnBry, that should give us a significant speed boost considering that yaml serialization and deserialization is some orders of magnitude slower than json. We only do that for the file metadata for now - it has the biggest gain to expect from a speed boost, and having the configuration as yaml actually has its readability advantages.
This commit is contained in:
1 parent
be4b98b9da
commit
135cf6a9cc
2 files changed
+104
-38
No files matched your search
@@ -177,8 +177,8 @@ class StorageInterface(object):
|
||||
Removes the folder at ``path``
|
||||
|
||||
:param string path: the path of the folder to remove
|
||||
:param bool recursive: if set to True, contained folders and files will also be removed, otherwise and error will
|
||||
be raised if the folder is not empty (apart from ``.metadata.yaml``) when it's to be removed
|
||||
:param bool recursive: if set to True, contained folders and files will also be removed, otherwise an error will
|
||||
be raised if the folder is not empty (apart from any metadata files) when it's to be removed
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@@ -444,7 +444,7 @@ class LocalFileStorage(StorageInterface):
|
||||
"""
|
||||
The ``LocalFileStorage`` is a storage implementation which holds all files, folders and metadata on disk.
|
||||
|
||||
Metadata is managed inside ``.metadata.yaml`` files in the respective folders, indexed by the sanitized filenames
|
||||
Metadata is managed inside ``.metadata.json`` files in the respective folders, indexed by the sanitized filenames
|
||||
stored within the folder. Metadata access is managed through an LRU cache to minimize access overhead.
|
||||
|
||||
This storage type implements :func:`path_on_disk`.
|
||||
@@ -565,7 +565,7 @@ class LocalFileStorage(StorageInterface):
|
||||
path = os.path.join(self.basefolder, path)
|
||||
|
||||
def last_modified_for_path(p):
|
||||
metadata = os.path.join(p, ".metadata.yaml")
|
||||
metadata = os.path.join(p, ".metadata.json")
|
||||
if os.path.exists(metadata):
|
||||
return max(os.stat(p).st_mtime, os.stat(metadata).st_mtime)
|
||||
else:
|
||||
@@ -634,7 +634,7 @@ class LocalFileStorage(StorageInterface):
|
||||
|
||||
empty = True
|
||||
for entry in scandir(folder_path):
|
||||
if entry.name == ".metadata.yaml":
|
||||
if entry.name == ".metadata.json" or entry.name == ".metadata.yaml":
|
||||
continue
|
||||
empty = False
|
||||
break
|
||||
@@ -1115,7 +1115,7 @@ class LocalFileStorage(StorageInterface):
|
||||
try:
|
||||
print_time = float(print_time)
|
||||
except:
|
||||
self._logger.warn("Invalid print time value found in print history for {} in {}/.metadata.yaml: {!r}".format(name, path, print_time))
|
||||
self._logger.warn("Invalid print time value found in print history for {} in {}/.metadata.json: {!r}".format(name, path, print_time))
|
||||
continue
|
||||
|
||||
if not printer_profile in former_print_times:
|
||||
@@ -1518,14 +1518,16 @@ class LocalFileStorage(StorageInterface):
|
||||
if path in self._metadata_cache:
|
||||
return deepcopy(self._metadata_cache[path])
|
||||
|
||||
metadata_path = os.path.join(path, ".metadata.yaml")
|
||||
self._migrate_metadata(path)
|
||||
|
||||
metadata_path = os.path.join(path, ".metadata.json")
|
||||
if os.path.exists(metadata_path):
|
||||
with open(metadata_path) as f:
|
||||
try:
|
||||
import yaml
|
||||
metadata = yaml.safe_load(f)
|
||||
import json
|
||||
metadata = json.load(f)
|
||||
except:
|
||||
self._logger.exception("Error while reading .metadata.yaml from {path}".format(**locals()))
|
||||
self._logger.exception("Error while reading .metadata.json from {path}".format(**locals()))
|
||||
else:
|
||||
if isinstance(metadata, dict):
|
||||
self._metadata_cache[path] = deepcopy(metadata)
|
||||
@@ -1534,27 +1536,63 @@ class LocalFileStorage(StorageInterface):
|
||||
|
||||
def _save_metadata(self, path, metadata):
|
||||
with self._get_metadata_lock(path):
|
||||
metadata_path = os.path.join(path, ".metadata.yaml")
|
||||
metadata_path = os.path.join(path, ".metadata.json")
|
||||
try:
|
||||
import yaml
|
||||
import json
|
||||
with atomic_write(metadata_path) as f:
|
||||
yaml.safe_dump(metadata, stream=f, default_flow_style=False, indent=" ", allow_unicode=True)
|
||||
json.dump(metadata, f, indent=4, separators=(",", ": "))
|
||||
except:
|
||||
self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals()))
|
||||
self._logger.exception("Error while writing .metadata.json to {path}".format(**locals()))
|
||||
else:
|
||||
self._metadata_cache[path] = deepcopy(metadata)
|
||||
|
||||
def _delete_metadata(self, path):
|
||||
with self._get_metadata_lock(path):
|
||||
metadata_path = os.path.join(path, ".metadata.yaml")
|
||||
if os.path.exists(metadata_path):
|
||||
try:
|
||||
os.remove(metadata_path)
|
||||
except:
|
||||
self._logger.exception("Error while deleting .metadata.yaml from {path}".format(**locals()))
|
||||
metadata_files = (".metadata.json", ".metadata.yaml")
|
||||
for metadata_file in metadata_files:
|
||||
metadata_path = os.path.join(path, metadata_file)
|
||||
if os.path.exists(metadata_path):
|
||||
try:
|
||||
os.remove(metadata_path)
|
||||
except:
|
||||
self._logger.exception("Error while deleting {metadata_file} from {path}".format(**locals()))
|
||||
if path in self._metadata_cache:
|
||||
del self._metadata_cache[path]
|
||||
|
||||
def _migrate_metadata(self, path):
|
||||
# we switched to json in 1.3.9 - if we still have yaml here, migrate it now
|
||||
import yaml
|
||||
import json
|
||||
|
||||
with self._get_metadata_lock(path):
|
||||
metadata_path_yaml = os.path.join(path, ".metadata.yaml")
|
||||
metadata_path_json = os.path.join(path, ".metadata.json")
|
||||
|
||||
if not os.path.exists(metadata_path_yaml):
|
||||
# nothing to migrate
|
||||
return
|
||||
|
||||
if os.path.exists(metadata_path_json):
|
||||
# already migrated
|
||||
# TODO 1.3.10 Remove ".metadata.yaml" files
|
||||
return
|
||||
|
||||
with open(metadata_path_yaml) as f:
|
||||
try:
|
||||
metadata = yaml.safe_load(f)
|
||||
except:
|
||||
self._logger.exception("Error while reading .metadata.yaml from {path}".format(**locals()))
|
||||
return
|
||||
|
||||
if not isinstance(metadata, dict):
|
||||
# looks invalid, ignore it
|
||||
return
|
||||
|
||||
with atomic_write(metadata_path_json) as f:
|
||||
json.dump(metadata, f, indent=4, separators=(",", ": "))
|
||||
|
||||
# TODO 1.3.10 Remove ".metadata.yaml" files
|
||||
|
||||
@contextmanager
|
||||
def _get_metadata_lock(self, path):
|
||||
with self._metadata_lock_mutex:
|
||||
|
||||
@@ -189,7 +189,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.assertIsNone(after_stl_metadata)
|
||||
self.assertIsNotNone(copied_metadata)
|
||||
self.assertDictEqual(before_stl_metadata, copied_metadata)
|
||||
|
||||
|
||||
def test_copy_file_new_display(self):
|
||||
self._add_file("bp_case.stl", FILE_BP_CASE_STL)
|
||||
try:
|
||||
@@ -197,10 +197,10 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.fail("Expected an exception")
|
||||
except StorageError as e:
|
||||
self.assertEqual(e.code, StorageError.SOURCE_EQUALS_DESTINATION)
|
||||
|
||||
|
||||
def test_move_file_new_display(self):
|
||||
self._add_file("bp_case.stl", FILE_BP_CASE_STL)
|
||||
|
||||
|
||||
before_metadata = self.storage.get_metadata("bp_case.stl")
|
||||
self.storage.move_file("bp_case.stl", u"bp_cäse.stl")
|
||||
after_metadata = self.storage.get_metadata("bp_case.stl")
|
||||
@@ -210,19 +210,19 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.assertIsNotNone(before_metadata)
|
||||
self.assertIsNotNone(after_metadata)
|
||||
self.assertDictContainsSubset(dict(display=u"bp_cäse.stl"), after_metadata)
|
||||
|
||||
|
||||
@data("copy_file", "move_file")
|
||||
def test_copy_move_file_different_display(self, operation):
|
||||
self._add_file("bp_case.stl", FILE_BP_CASE_STL, display=u"bp_cäse.stl")
|
||||
|
||||
|
||||
before_metadata = self.storage.get_metadata("bp_case.stl")
|
||||
getattr(self.storage, operation)("bp_case.stl", "test.stl")
|
||||
after_metadata = self.storage.get_metadata("test.stl")
|
||||
|
||||
|
||||
self.assertIsNotNone(before_metadata)
|
||||
self.assertIsNotNone(after_metadata)
|
||||
self.assertNotIn("display", after_metadata)
|
||||
|
||||
|
||||
@data("copy_file", "move_file")
|
||||
def test_copy_move_file_same(self, operation):
|
||||
self._add_file("bp_case.stl", FILE_BP_CASE_STL)
|
||||
@@ -264,7 +264,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
|
||||
def test_add_folder(self):
|
||||
self._add_and_verify_folder("test", "test")
|
||||
|
||||
|
||||
def test_add_folder_with_display(self):
|
||||
self._add_and_verify_folder("test", "test", display=u"täst")
|
||||
metadata = self.storage.get_metadata("test")
|
||||
@@ -304,11 +304,11 @@ class LocalStorageTest(unittest.TestCase):
|
||||
|
||||
def test_remove_folder_with_display(self):
|
||||
self._add_folder("folder", display=u"földer")
|
||||
|
||||
|
||||
before_metadata = self.storage.get_metadata("folder")
|
||||
self.storage.remove_folder("folder")
|
||||
after_metadata = self.storage.get_metadata("folder")
|
||||
|
||||
|
||||
self.assertIsNotNone(before_metadata)
|
||||
self.assertDictEqual(before_metadata, dict(display=u"földer"))
|
||||
self.assertIsNone(after_metadata)
|
||||
@@ -326,7 +326,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "source", "crazyradio.stl")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(self.basefolder, "destination")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(self.basefolder, "destination", "copied")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", ".metadata.yaml")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", ".metadata.json")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", "crazyradio.stl")))
|
||||
|
||||
self.assertIsNotNone(source_metadata)
|
||||
@@ -347,7 +347,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.assertFalse(os.path.isfile(os.path.join(self.basefolder, "source", "crazyradio.stl")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(self.basefolder, "destination")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(self.basefolder, "destination", "copied")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", ".metadata.yaml")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", ".metadata.json")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.basefolder, "destination", "copied", "crazyradio.stl")))
|
||||
|
||||
self.assertIsNotNone(before_source_metadata)
|
||||
@@ -365,19 +365,19 @@ class LocalStorageTest(unittest.TestCase):
|
||||
|
||||
def test_move_folder_new_display(self):
|
||||
self._add_folder("folder")
|
||||
|
||||
|
||||
before_metadata = self.storage.get_metadata("folder")
|
||||
self.storage.move_folder("folder", u"földer")
|
||||
after_metadata = self.storage.get_metadata("folder")
|
||||
|
||||
|
||||
self.assertIsNone(before_metadata)
|
||||
self.assertIsNotNone(after_metadata)
|
||||
self.assertDictEqual(after_metadata, dict(display=u"földer"))
|
||||
|
||||
|
||||
@data("copy_folder", "move_folder")
|
||||
def test_copy_move_folder_different_display(self, operation):
|
||||
self._add_folder("folder", display=u"földer")
|
||||
|
||||
|
||||
before_metadata = self.storage.get_metadata("folder")
|
||||
getattr(self.storage, operation)("folder", "test")
|
||||
after_metadata = self.storage.get_metadata("test")
|
||||
@@ -394,7 +394,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.fail("Expected an exception")
|
||||
except StorageError as e:
|
||||
self.assertEqual(e.code, StorageError.SOURCE_EQUALS_DESTINATION)
|
||||
|
||||
|
||||
@data("copy_folder", "move_folder")
|
||||
def test_copy_move_folder_missing_source(self, operation):
|
||||
try:
|
||||
@@ -639,6 +639,34 @@ class LocalStorageTest(unittest.TestCase):
|
||||
self.assertEqual(expected_path, sanitized_path)
|
||||
return sanitized_path
|
||||
|
||||
def test_migrate_metadata_to_json(self):
|
||||
metadata = {
|
||||
"test.gco": {
|
||||
"hash": "aabbccddeeff",
|
||||
"links": [],
|
||||
"notes": []
|
||||
}
|
||||
}
|
||||
yaml_path = os.path.join(self.basefolder, ".metadata.yaml")
|
||||
json_path = os.path.join(self.basefolder, ".metadata.json")
|
||||
|
||||
# prepare
|
||||
import yaml
|
||||
with open(yaml_path, "wb") as f:
|
||||
yaml.safe_dump(metadata, f)
|
||||
|
||||
# migrate
|
||||
self.storage._migrate_metadata(self.basefolder)
|
||||
|
||||
# verify
|
||||
self.assertTrue(os.path.exists(json_path))
|
||||
self.assertTrue(os.path.exists(yaml_path)) # TODO 1.3.10 change to assertFalse
|
||||
|
||||
import json
|
||||
with open(json_path) as f:
|
||||
json_metadata = json.load(f)
|
||||
self.assertDictEqual(metadata, json_metadata)
|
||||
|
||||
def _add_file(self, path, file_object, links=None, overwrite=False, display=None):
|
||||
"""
|
||||
Adds a file to the storage.
|
||||
@@ -659,7 +687,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
folder_path = os.path.join(self.basefolder, os.path.join(*split_path[:-1]))
|
||||
|
||||
self.assertTrue(os.path.isfile(file_path))
|
||||
self.assertTrue(os.path.isfile(os.path.join(folder_path, ".metadata.yaml")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(folder_path, ".metadata.json")))
|
||||
|
||||
metadata = self.storage.get_metadata(sanitized_path)
|
||||
self.assertIsNotNone(metadata)
|
||||
|
||||
Reference in new issue
Block a user