9
9
from __future__ import annotations
10
10
11
11
from typing import Protocol
12
- import random
12
+ import hashlib
13
13
import shutil
14
14
15
15
import fsspec
16
16
import fsspec .implementations .local
17
17
import zarr .storage
18
18
19
+ from zcollection import fs_utils
20
+
19
21
from .. import dataset , storage , sync
20
22
from .time_series import merge_time_series
21
23
22
24
__all__ = ('MergeCallable' , 'perform' , 'merge_time_series' )
23
25
24
- #: Character set used to create a temporary directory.
25
- CHARACTERS = 'abcdefghijklmnopqrstuvwxyz0123456789_'
26
-
27
26
28
27
#: pylint: disable=too-few-public-methods,duplicate-code
29
28
class MergeCallable (Protocol ):
@@ -88,6 +87,12 @@ def _rename(
88
87
fs .mv (source , dest , recursive = True )
89
88
90
89
90
+ def _extract_root_dirname (dirname : str , sep : str ) -> str :
91
+ """Extracts the root directory name from a partition name."""
92
+ parts = filter (lambda x : '=' not in x , dirname .split (sep ))
93
+ return sep .join (parts )
94
+
95
+
91
96
def _update_fs (
92
97
dirname : str ,
93
98
zds : dataset .Dataset ,
@@ -103,9 +108,13 @@ def _update_fs(
103
108
fs: The file system that the partition is stored on.
104
109
synchronizer: The instance handling access to critical resources.
105
110
"""
106
- # Name of the temporary directory.
107
- temp : str = dirname + '.' + '' .join (
108
- random .choice (CHARACTERS ) for _ in range (10 ))
111
+ # Building a temporary directory to store the new data. The name of the
112
+ # temporary directory is the hash of the partition name.
113
+ temp : str = fs_utils .join_path (
114
+ _extract_root_dirname (dirname , fs .sep ),
115
+ hashlib .sha256 (dirname .encode ()).hexdigest ())
116
+ if fs .exists (temp ):
117
+ fs .rm (temp , recursive = True )
109
118
110
119
# Initializing Zarr group
111
120
zarr .storage .init_group (store = fs .get_mapper (temp ))
0 commit comments