Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(add) refresh argument on data saver #4

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions matorage/data/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class DataSaver(object):
Keep in mind that using memory is fast because it doesn't use disk IO, but it's not always good.
If default option(False), then `HDF5_SEC2` driver will be used on posix OS(or `HDF5_WINDOWS` in Windows).

refresh (:obj:`boolean`, optional, defaults to `False`):
All existing data is erased and overwritten.


Single Process example

Expand Down Expand Up @@ -118,6 +121,7 @@ def __init__(
multipart_upload_size=5 * _MB,
num_worker_threads=4,
inmemory=False,
refresh=False,
):

self.config = config
Expand Down Expand Up @@ -147,7 +151,7 @@ def __init__(
if not check_nas(self.config.endpoint)
else NAS(self.config.endpoint)
)
self._check_and_create_bucket()
self._check_and_create_bucket(refresh=refresh)

self._uploader = Uploader(
client=self._client,
Expand Down Expand Up @@ -191,11 +195,15 @@ def _append_all(self):
for name, array in self._datas.items():
self._earray[name].append(array[batch_idx, None])

def _check_and_create_bucket(self):
def _check_and_create_bucket(self, refresh):
if not self._client.bucket_exists(self.config.bucket_name):
self._client.make_bucket(
self.config.bucket_name, location=self.config.region
)
elif refresh:
objects = self._client.list_objects(self.config.bucket_name, recursive=True)
for obj in objects:
self._client.remove_object(self.config.bucket_name, obj.object_name)

def _check_attr_name(self, name):
"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_datasaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ def test_datasaver_nas(self):
self.data_saver({"x": x})
self.data_saver.disconnect()

def test_datasaver_refresh(self):

self.data_config = DataConfig(
**self.storage_config,
dataset_name="test_datasaver_refresh",
attributes=[DataAttribute("x", "float64", (2), itemsize=32)],
)
for refresh in [False, True]:
self.data_saver = DataSaver(config=self.data_config, refresh=refresh)
x = np.asarray([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
self.assertEqual(x.shape, (3, 2))
self.data_saver({"x": x})
self.data_saver.disconnect()

@unittest.skipIf(
'access_key' not in os.environ or 'secret_key' not in os.environ, 'S3 Skip'
Expand Down