We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Caching fails on windows with s3://
### Describe the issue

Caching path fails on windows

```python3
import hub
ds = hub.load("eurosat/eurosat-rgb") # is not loaded
```

Error shown
```
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Folder/.activeloop/cache/s3:'
```

Likely caused by converting Linux style paths to windows.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `hub/store/store.py`
Content:
```
1 import posixpath
2 import shutil
3 from hub.store.cache import Cache
4 from hub.store.lru_cache import LRUCache
5 
6 from hub.client.hub_control import HubControlClient
7 import configparser
8 from typing import MutableMapping, Tuple
9 
10 import fsspec
11 import gcsfs
12 import zarr
13 from hub.store.azure_fs import AzureBlobFileSystem
14 import os
15 import re
16 
17 
18 def _connect(tag):
19     """Connects to the backend and receives credentials"""
20 
21     creds = HubControlClient().get_config()
22     dataset = HubControlClient().get_dataset_path(tag)
23 
24     # If dataset is in DB then return the path
25     # Otherwise construct the path from the tag
26     if dataset and "path" in dataset:
27         path = dataset["path"]
28     else:
29         sub_tags = tag.split("/")
30         # Get repository path from the cred location
31         path = "/".join(creds["bucket"].split("/")[:-1])
32         path = f"{path}/{sub_tags[0]}/{sub_tags[-1]}"
33     return path, creds
34 
35 
36 def get_fs_and_path(url: str, token=None) -> Tuple[fsspec.AbstractFileSystem, str]:
37     if url.startswith("s3://"):
38         token = token or dict()
39         token = read_aws_creds(token) if isinstance(token, str) else token
40         return (
41             fsspec.filesystem(
42                 "s3",
43                 key=token.get("aws_access_key_id"),
44                 secret=token.get("aws_secret_access_key"),
45                 token=token.get("aws_session_token"),
46             ),
47             url[5:],
48         )
49     elif url.startswith("gcs://"):
50         return gcsfs.GCSFileSystem(token=token), url[6:]
51     elif url.find("blob.core.windows.net/") != -1:
52         account_name = url.split(".")[0]
53         account_name = account_name[8:] if url.startswith("https://") else account_name
54         return (
55             AzureBlobFileSystem(
56                 account_name=account_name,
57                 account_key=token.get("account_key"),
58             ),
59             url[url.find("blob.core.windows.net/") + 22 :],
60         )
61     elif (
62         url.startswith("../")
63         or url.startswith("./")
64         or url.startswith("/")
65         or url.startswith("~/")
66     ):
67         return fsspec.filesystem("file"), url
68     elif (
69         # windows local file system
70         re.search("^[A-Za-z]:", url)
71     ):
72         return fsspec.filesystem("file"), url
73     else:
74         # TOOD check if url is username/dataset:version
75         url, creds = _connect(url)
76         fs = fsspec.filesystem(
77             "s3",
78             key=creds["access_key"],
79             secret=creds["secret_key"],
80             token=creds["session_token"],
81             client_kwargs={
82                 "endpoint_url": creds["endpoint"],
83                 "region_name": creds["region"],
84             },
85         )
86         return (fs, url)
87 
88 
89 def read_aws_creds(filepath: str):
90     parser = configparser.ConfigParser()
91     parser.read(filepath)
92     return {section: dict(parser.items(section)) for section in parser.sections()}
93 
94 
95 def _get_storage_map(fs, path):
96     return StorageMapWrapperWithCommit(fs.get_mapper(path, check=False, create=False))
97 
98 
99 def get_storage_map(fs, path, memcache=2 ** 26, lock=True, storage_cache=2 ** 28):
100     store = _get_storage_map(fs, path)
101     cache_path = posixpath.expanduser(posixpath.join("~/.activeloop/cache/", path))
102     if storage_cache and storage_cache > 0:
103         os.makedirs(cache_path, exist_ok=True)
104         store = LRUCache(
105             zarr.LMDBStore(cache_path, buffers=True, lock=lock), store, storage_cache
106         )
107     if memcache and memcache > 0:
108         store = LRUCache(zarr.MemoryStore(), store, memcache)
109     return store
110 
111 
112 class StorageMapWrapperWithCommit(MutableMapping):
113     def __init__(self, map):
114         self._map = map
115         self.root = self._map.root
116 
117     def __getitem__(self, slice_):
118         return self._map[slice_]
119 
120     def __setitem__(self, slice_, value):
121         self._map[slice_] = value
122 
123     def __delitem__(self, slice_):
124         del self._map[slice_]
125 
126     def __len__(self):
127         return len(self._map)
128 
129     def __iter__(self):
130         yield from self._map
131 
132     def flush(self):
133         pass
134 
135     def commit(self):
136         """ Deprecated alias to flush()"""
137         self.flush()
138 
139     def close(self):
140         pass
141 
142     def __enter__(self):
143         return self
144 
145     def __exit__(self, exc_type, exc_value, exc_traceback):
146         self.close()
147
```

Path: `hub/store/lru_cache.py`
Content:
```
1 from collections import OrderedDict
2 from collections.abc import MutableMapping
3 
4 # from multiprocessing import Lock
5 
6 import fcntl
7 import hashlib
8 import uuid
9 
10 
11 class SystemMutex:
12     def __init__(self, name=str(uuid.uuid4())):
13         self.name = name
14         self._lockid = hashlib.sha1(self.name.encode("utf8")).hexdigest()
15 
16     def __enter__(self):
17         self.fp = open(f"/tmp/.lock-{self._lockid}.lck", "wb")
18         fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX)
19 
20     def __exit__(self, _type, value, tb):
21         fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN)
22         self.fp.close()
23 
24 
25 class DummyLock:
26     def __init__(self):
27         pass
28 
29     def __enter__(self):
30         return self
31 
32     def __exit__(self, *args):
33         pass
34 
35 
36 Lock = DummyLock
37 
38 
39 class LRUCache(MutableMapping):
40     def __init__(
41         self,
42         cache_storage: MutableMapping,
43         actual_storage: MutableMapping,
44         max_size,
45     ):
46         """Creates LRU cache using cache_storage and actual_storage containers
47         max_size -> maximum cache size that is allowed
48         """
49         self._dirty = set()
50         self._mutex = Lock()
51         self._max_size = max_size
52         self._cache_storage = cache_storage
53         self._actual_storage = actual_storage
54         self._total_cached = 0
55         self._cached_items = OrderedDict()
56         # assert len(self._cache_storage) == 0, "Initially cache storage should be empty"
57 
58     @property
59     def cache_storage(self):
60         """Storage which is used for caching
61         Returns MutableMapping
62         """
63         return self._cache_storage
64 
65     @property
66     def actual_storage(self):
67         """Storage which is used for actual storing (not caching)
68         Returns MutableMapping
69         """
70         return self._actual_storage
71 
72     def __enter__(self):
73         return self
74 
75     def __exit__(self, *args):
76         self.close()
77 
78     def _flush_dirty(self):
79         for item in self._dirty:
80             self._actual_storage[item] = self._cache_storage[item]
81         self._dirty.clear()
82 
83     def flush(self):
84         self._flush_dirty()
85         if hasattr(self._cache_storage, "flush"):
86             self._cache_storage.flush()
87         if hasattr(self._actual_storage, "flush"):
88             self._actual_storage.flush()
89 
90     def close(self):
91         self._flush_dirty()
92         if hasattr(self._cache_storage, "close"):
93             self._cache_storage.close()
94         if hasattr(self._actual_storage, "close"):
95             self._actual_storage.close()
96 
97     def commit(self):
98         self.close()
99 
100     def __getitem__(self, key):
101         """ Gets item and puts it in the cache if not there """
102         with self._mutex:
103             if key in self._cached_items:
104                 self._cached_items.move_to_end(key)
105                 return self._cache_storage[key]
106             else:
107                 result = self._actual_storage[key]
108                 self._free_memory(len(result))
109                 self._append_cache(key, result)
110                 return result
111 
112     def __setitem__(self, key, value):
113         """ Sets item and puts it in the cache if not there"""
114         with self._mutex:
115             if key in self._cached_items:
116                 self._total_cached -= self._cached_items.pop(key)
117             self._free_memory(len(value))
118             self._append_cache(key, value)
119             if key not in self._dirty:
120                 self._dirty.add(key)
121 
122     def __delitem__(self, key):
123         deleted_from_cache = False
124         with self._mutex:
125             if key in self._cached_items:
126                 self._total_cached -= self._cached_items.pop(key)
127                 del self._cache_storage[key]
128                 self._dirty.discard(key)
129                 deleted_from_cache = True
130             try:
131                 del self._actual_storage[key]
132             except KeyError:
133                 if not deleted_from_cache:
134                     raise
135 
136     def __len__(self):
137         return len(
138             self.actual_storage
139         )  # TODO: In future might need to fix this to return proper len
140 
141     def __iter__(self):
142         cached_keys = set(self._dirty)
143         for i in self.actual_storage:
144             cached_keys.discard(i)
145             yield i
146         yield from sorted(cached_keys)
147 
148     def _free_memory(self, extra_size):
149         while (
150             self._total_cached > 0 and extra_size + self._total_cached > self._max_size
151         ):
152             item, itemsize = self._cached_items.popitem(last=False)
153             if item in self._dirty:
154                 self._actual_storage[item] = self._cache_storage[item]
155                 self._dirty.discard(item)
156             del self._cache_storage[item]
157             self._total_cached -= itemsize
158 
159     def _append_cache(self, key, value):
160         self._total_cached += len(value)
161         self._cached_items[key] = len(value)
162         self._cache_storage[key] = value
163
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/hub/store/lru_cache.py b/hub/store/lru_cache.py
--- a/hub/store/lru_cache.py
+++ b/hub/store/lru_cache.py
@@ -3,25 +3,10 @@
 
 # from multiprocessing import Lock
 
-import fcntl
 import hashlib
 import uuid
 
 
-class SystemMutex:
-    def __init__(self, name=str(uuid.uuid4())):
-        self.name = name
-        self._lockid = hashlib.sha1(self.name.encode("utf8")).hexdigest()
-
-    def __enter__(self):
-        self.fp = open(f"/tmp/.lock-{self._lockid}.lck", "wb")
-        fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX)
-
-    def __exit__(self, _type, value, tb):
-        fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN)
-        self.fp.close()
-
-
 class DummyLock:
     def __init__(self):
         pass
diff --git a/hub/store/store.py b/hub/store/store.py
--- a/hub/store/store.py
+++ b/hub/store/store.py
@@ -96,9 +96,32 @@
     return StorageMapWrapperWithCommit(fs.get_mapper(path, check=False, create=False))
 
 
+def get_cache_path(path, cache_folder="~/.activeloop/cache/"):
+    if (
+        path.startswith("s3://")
+        or path.startswith("gcs://")
+    ):
+        path = '//'.join(path.split("//")[1:])
+    elif (
+        path.startswith("../")
+        or path.startswith("./")
+        or path.startswith("/")
+        or path.startswith("~/")
+    ):
+        path = "/".join(path.split("/")[1:])
+    elif path.find("://") != -1:
+        path = path.split("://")[-1]
+    elif path.find(":\\") != -1:
+        path = path.split(":\\")[-1]
+    else:
+        # path is username/dataset or username/dataset:version
+        path = path.replace(':', '/')
+    return os.path.expanduser(posixpath.join(cache_folder, path))
+
+
 def get_storage_map(fs, path, memcache=2 ** 26, lock=True, storage_cache=2 ** 28):
     store = _get_storage_map(fs, path)
-    cache_path = posixpath.expanduser(posixpath.join("~/.activeloop/cache/", path))
+    cache_path = get_cache_path(path)
     if storage_cache and storage_cache > 0:
         os.makedirs(cache_path, exist_ok=True)
         store = LRUCache(

{"golden_diff": "diff --git a/hub/store/lru_cache.py b/hub/store/lru_cache.py\n--- a/hub/store/lru_cache.py\n+++ b/hub/store/lru_cache.py\n@@ -3,25 +3,10 @@\n \n # from multiprocessing import Lock\n \n-import fcntl\n import hashlib\n import uuid\n \n \n-class SystemMutex:\n-    def __init__(self, name=str(uuid.uuid4())):\n-        self.name = name\n-        self._lockid = hashlib.sha1(self.name.encode(\"utf8\")).hexdigest()\n-\n-    def __enter__(self):\n-        self.fp = open(f\"/tmp/.lock-{self._lockid}.lck\", \"wb\")\n-        fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX)\n-\n-    def __exit__(self, _type, value, tb):\n-        fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN)\n-        self.fp.close()\n-\n-\n class DummyLock:\n     def __init__(self):\n         pass\ndiff --git a/hub/store/store.py b/hub/store/store.py\n--- a/hub/store/store.py\n+++ b/hub/store/store.py\n@@ -96,9 +96,32 @@\n     return StorageMapWrapperWithCommit(fs.get_mapper(path, check=False, create=False))\n \n \n+def get_cache_path(path, cache_folder=\"~/.activeloop/cache/\"):\n+    if (\n+        path.startswith(\"s3://\")\n+        or path.startswith(\"gcs://\")\n+    ):\n+        path = '//'.join(path.split(\"//\")[1:])\n+    elif (\n+        path.startswith(\"../\")\n+        or path.startswith(\"./\")\n+        or path.startswith(\"/\")\n+        or path.startswith(\"~/\")\n+    ):\n+        path = \"/\".join(path.split(\"/\")[1:])\n+    elif path.find(\"://\") != -1:\n+        path = path.split(\"://\")[-1]\n+    elif path.find(\":\\\\\") != -1:\n+        path = path.split(\":\\\\\")[-1]\n+    else:\n+        # path is username/dataset or username/dataset:version\n+        path = path.replace(':', '/')\n+    return os.path.expanduser(posixpath.join(cache_folder, path))\n+\n+\n def get_storage_map(fs, path, memcache=2 ** 26, lock=True, storage_cache=2 ** 28):\n     store = _get_storage_map(fs, path)\n-    cache_path = posixpath.expanduser(posixpath.join(\"~/.activeloop/cache/\", path))\n+    cache_path = get_cache_path(path)\n     if storage_cache and storage_cache > 0:\n         os.makedirs(cache_path, exist_ok=True)\n         store = LRUCache(\n", "issue": "Caching fails on windows with s3://\n### Describe the issue\r\n\r\nCaching path fails on windows\r\n\r\n```python3\r\nimport hub\r\nds = hub.load(\"eurosat/eurosat-rgb\") # is not loaded\r\n```\r\n\r\nError shown\r\n```\r\nOSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\\\Users\\\\Folder/.activeloop/cache/s3:'\r\n```\r\n\r\nLikely caused by converting Linux style paths to windows.\n", "before_files": [{"content": "import posixpath\nimport shutil\nfrom hub.store.cache import Cache\nfrom hub.store.lru_cache import LRUCache\n\nfrom hub.client.hub_control import HubControlClient\nimport configparser\nfrom typing import MutableMapping, Tuple\n\nimport fsspec\nimport gcsfs\nimport zarr\nfrom hub.store.azure_fs import AzureBlobFileSystem\nimport os\nimport re\n\n\ndef _connect(tag):\n    \"\"\"Connects to the backend and receives credentials\"\"\"\n\n    creds = HubControlClient().get_config()\n    dataset = HubControlClient().get_dataset_path(tag)\n\n    # If dataset is in DB then return the path\n    # Otherwise construct the path from the tag\n    if dataset and \"path\" in dataset:\n        path = dataset[\"path\"]\n    else:\n        sub_tags = tag.split(\"/\")\n        # Get repository path from the cred location\n        path = \"/\".join(creds[\"bucket\"].split(\"/\")[:-1])\n        path = f\"{path}/{sub_tags[0]}/{sub_tags[-1]}\"\n    return path, creds\n\n\ndef get_fs_and_path(url: str, token=None) -> Tuple[fsspec.AbstractFileSystem, str]:\n    if url.startswith(\"s3://\"):\n        token = token or dict()\n        token = read_aws_creds(token) if isinstance(token, str) else token\n        return (\n            fsspec.filesystem(\n                \"s3\",\n                key=token.get(\"aws_access_key_id\"),\n                secret=token.get(\"aws_secret_access_key\"),\n                token=token.get(\"aws_session_token\"),\n            ),\n            url[5:],\n        )\n    elif url.startswith(\"gcs://\"):\n        return gcsfs.GCSFileSystem(token=token), url[6:]\n    elif url.find(\"blob.core.windows.net/\") != -1:\n        account_name = url.split(\".\")[0]\n        account_name = account_name[8:] if url.startswith(\"https://\") else account_name\n        return (\n            AzureBlobFileSystem(\n                account_name=account_name,\n                account_key=token.get(\"account_key\"),\n            ),\n            url[url.find(\"blob.core.windows.net/\") + 22 :],\n        )\n    elif (\n        url.startswith(\"../\")\n        or url.startswith(\"./\")\n        or url.startswith(\"/\")\n        or url.startswith(\"~/\")\n    ):\n        return fsspec.filesystem(\"file\"), url\n    elif (\n        # windows local file system\n        re.search(\"^[A-Za-z]:\", url)\n    ):\n        return fsspec.filesystem(\"file\"), url\n    else:\n        # TOOD check if url is username/dataset:version\n        url, creds = _connect(url)\n        fs = fsspec.filesystem(\n            \"s3\",\n            key=creds[\"access_key\"],\n            secret=creds[\"secret_key\"],\n            token=creds[\"session_token\"],\n            client_kwargs={\n                \"endpoint_url\": creds[\"endpoint\"],\n                \"region_name\": creds[\"region\"],\n            },\n        )\n        return (fs, url)\n\n\ndef read_aws_creds(filepath: str):\n    parser = configparser.ConfigParser()\n    parser.read(filepath)\n    return {section: dict(parser.items(section)) for section in parser.sections()}\n\n\ndef _get_storage_map(fs, path):\n    return StorageMapWrapperWithCommit(fs.get_mapper(path, check=False, create=False))\n\n\ndef get_storage_map(fs, path, memcache=2 ** 26, lock=True, storage_cache=2 ** 28):\n    store = _get_storage_map(fs, path)\n    cache_path = posixpath.expanduser(posixpath.join(\"~/.activeloop/cache/\", path))\n    if storage_cache and storage_cache > 0:\n        os.makedirs(cache_path, exist_ok=True)\n        store = LRUCache(\n            zarr.LMDBStore(cache_path, buffers=True, lock=lock), store, storage_cache\n        )\n    if memcache and memcache > 0:\n        store = LRUCache(zarr.MemoryStore(), store, memcache)\n    return store\n\n\nclass StorageMapWrapperWithCommit(MutableMapping):\n    def __init__(self, map):\n        self._map = map\n        self.root = self._map.root\n\n    def __getitem__(self, slice_):\n        return self._map[slice_]\n\n    def __setitem__(self, slice_, value):\n        self._map[slice_] = value\n\n    def __delitem__(self, slice_):\n        del self._map[slice_]\n\n    def __len__(self):\n        return len(self._map)\n\n    def __iter__(self):\n        yield from self._map\n\n    def flush(self):\n        pass\n\n    def commit(self):\n        \"\"\" Deprecated alias to flush()\"\"\"\n        self.flush()\n\n    def close(self):\n        pass\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, exc_type, exc_value, exc_traceback):\n        self.close()\n", "path": "hub/store/store.py"}, {"content": "from collections import OrderedDict\nfrom collections.abc import MutableMapping\n\n# from multiprocessing import Lock\n\nimport fcntl\nimport hashlib\nimport uuid\n\n\nclass SystemMutex:\n    def __init__(self, name=str(uuid.uuid4())):\n        self.name = name\n        self._lockid = hashlib.sha1(self.name.encode(\"utf8\")).hexdigest()\n\n    def __enter__(self):\n        self.fp = open(f\"/tmp/.lock-{self._lockid}.lck\", \"wb\")\n        fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX)\n\n    def __exit__(self, _type, value, tb):\n        fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN)\n        self.fp.close()\n\n\nclass DummyLock:\n    def __init__(self):\n        pass\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, *args):\n        pass\n\n\nLock = DummyLock\n\n\nclass LRUCache(MutableMapping):\n    def __init__(\n        self,\n        cache_storage: MutableMapping,\n        actual_storage: MutableMapping,\n        max_size,\n    ):\n        \"\"\"Creates LRU cache using cache_storage and actual_storage containers\n        max_size -> maximum cache size that is allowed\n        \"\"\"\n        self._dirty = set()\n        self._mutex = Lock()\n        self._max_size = max_size\n        self._cache_storage = cache_storage\n        self._actual_storage = actual_storage\n        self._total_cached = 0\n        self._cached_items = OrderedDict()\n        # assert len(self._cache_storage) == 0, \"Initially cache storage should be empty\"\n\n    @property\n    def cache_storage(self):\n        \"\"\"Storage which is used for caching\n        Returns MutableMapping\n        \"\"\"\n        return self._cache_storage\n\n    @property\n    def actual_storage(self):\n        \"\"\"Storage which is used for actual storing (not caching)\n        Returns MutableMapping\n        \"\"\"\n        return self._actual_storage\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, *args):\n        self.close()\n\n    def _flush_dirty(self):\n        for item in self._dirty:\n            self._actual_storage[item] = self._cache_storage[item]\n        self._dirty.clear()\n\n    def flush(self):\n        self._flush_dirty()\n        if hasattr(self._cache_storage, \"flush\"):\n            self._cache_storage.flush()\n        if hasattr(self._actual_storage, \"flush\"):\n            self._actual_storage.flush()\n\n    def close(self):\n        self._flush_dirty()\n        if hasattr(self._cache_storage, \"close\"):\n            self._cache_storage.close()\n        if hasattr(self._actual_storage, \"close\"):\n            self._actual_storage.close()\n\n    def commit(self):\n        self.close()\n\n    def __getitem__(self, key):\n        \"\"\" Gets item and puts it in the cache if not there \"\"\"\n        with self._mutex:\n            if key in self._cached_items:\n                self._cached_items.move_to_end(key)\n                return self._cache_storage[key]\n            else:\n                result = self._actual_storage[key]\n                self._free_memory(len(result))\n                self._append_cache(key, result)\n                return result\n\n    def __setitem__(self, key, value):\n        \"\"\" Sets item and puts it in the cache if not there\"\"\"\n        with self._mutex:\n            if key in self._cached_items:\n                self._total_cached -= self._cached_items.pop(key)\n            self._free_memory(len(value))\n            self._append_cache(key, value)\n            if key not in self._dirty:\n                self._dirty.add(key)\n\n    def __delitem__(self, key):\n        deleted_from_cache = False\n        with self._mutex:\n            if key in self._cached_items:\n                self._total_cached -= self._cached_items.pop(key)\n                del self._cache_storage[key]\n                self._dirty.discard(key)\n                deleted_from_cache = True\n            try:\n                del self._actual_storage[key]\n            except KeyError:\n                if not deleted_from_cache:\n                    raise\n\n    def __len__(self):\n        return len(\n            self.actual_storage\n        )  # TODO: In future might need to fix this to return proper len\n\n    def __iter__(self):\n        cached_keys = set(self._dirty)\n        for i in self.actual_storage:\n            cached_keys.discard(i)\n            yield i\n        yield from sorted(cached_keys)\n\n    def _free_memory(self, extra_size):\n        while (\n            self._total_cached > 0 and extra_size + self._total_cached > self._max_size\n        ):\n            item, itemsize = self._cached_items.popitem(last=False)\n            if item in self._dirty:\n                self._actual_storage[item] = self._cache_storage[item]\n                self._dirty.discard(item)\n            del self._cache_storage[item]\n            self._total_cached -= itemsize\n\n    def _append_cache(self, key, value):\n        self._total_cached += len(value)\n        self._cached_items[key] = len(value)\n        self._cache_storage[key] = value\n", "path": "hub/store/lru_cache.py"}], "after_files": [{"content": "import posixpath\nimport shutil\nfrom hub.store.cache import Cache\nfrom hub.store.lru_cache import LRUCache\n\nfrom hub.client.hub_control import HubControlClient\nimport configparser\nfrom typing import MutableMapping, Tuple\n\nimport fsspec\nimport gcsfs\nimport zarr\nfrom hub.store.azure_fs import AzureBlobFileSystem\nimport os\nimport re\n\n\ndef _connect(tag):\n    \"\"\"Connects to the backend and receives credentials\"\"\"\n\n    creds = HubControlClient().get_config()\n    dataset = HubControlClient().get_dataset_path(tag)\n\n    # If dataset is in DB then return the path\n    # Otherwise construct the path from the tag\n    if dataset and \"path\" in dataset:\n        path = dataset[\"path\"]\n    else:\n        sub_tags = tag.split(\"/\")\n        # Get repository path from the cred location\n        path = \"/\".join(creds[\"bucket\"].split(\"/\")[:-1])\n        path = f\"{path}/{sub_tags[0]}/{sub_tags[-1]}\"\n    return path, creds\n\n\ndef get_fs_and_path(url: str, token=None) -> Tuple[fsspec.AbstractFileSystem, str]:\n    if url.startswith(\"s3://\"):\n        token = token or dict()\n        token = read_aws_creds(token) if isinstance(token, str) else token\n        return (\n            fsspec.filesystem(\n                \"s3\",\n                key=token.get(\"aws_access_key_id\"),\n                secret=token.get(\"aws_secret_access_key\"),\n                token=token.get(\"aws_session_token\"),\n            ),\n            url[5:],\n        )\n    elif url.startswith(\"gcs://\"):\n        return gcsfs.GCSFileSystem(token=token), url[6:]\n    elif url.find(\"blob.core.windows.net/\") != -1:\n        account_name = url.split(\".\")[0]\n        account_name = account_name[8:] if url.startswith(\"https://\") else account_name\n        return (\n            AzureBlobFileSystem(\n                account_name=account_name,\n                account_key=token.get(\"account_key\"),\n            ),\n            url[url.find(\"blob.core.windows.net/\") + 22 :],\n        )\n    elif (\n        url.startswith(\"../\")\n        or url.startswith(\"./\")\n        or url.startswith(\"/\")\n        or url.startswith(\"~/\")\n    ):\n        return fsspec.filesystem(\"file\"), url\n    elif (\n        # windows local file system\n        re.search(\"^[A-Za-z]:\", url)\n    ):\n        return fsspec.filesystem(\"file\"), url\n    else:\n        # TOOD check if url is username/dataset:version\n        url, creds = _connect(url)\n        fs = fsspec.filesystem(\n            \"s3\",\n            key=creds[\"access_key\"],\n            secret=creds[\"secret_key\"],\n            token=creds[\"session_token\"],\n            client_kwargs={\n                \"endpoint_url\": creds[\"endpoint\"],\n                \"region_name\": creds[\"region\"],\n            },\n        )\n        return (fs, url)\n\n\ndef read_aws_creds(filepath: str):\n    parser = configparser.ConfigParser()\n    parser.read(filepath)\n    return {section: dict(parser.items(section)) for section in parser.sections()}\n\n\ndef _get_storage_map(fs, path):\n    return StorageMapWrapperWithCommit(fs.get_mapper(path, check=False, create=False))\n\n\ndef get_cache_path(path, cache_folder=\"~/.activeloop/cache/\"):\n    if (\n        path.startswith(\"s3://\")\n        or path.startswith(\"gcs://\")\n    ):\n        path = '//'.join(path.split(\"//\")[1:])\n    elif (\n        path.startswith(\"../\")\n        or path.startswith(\"./\")\n        or path.startswith(\"/\")\n        or path.startswith(\"~/\")\n    ):\n        path = \"/\".join(path.split(\"/\")[1:])\n    elif path.find(\"://\") != -1:\n        path = path.split(\"://\")[-1]\n    elif path.find(\":\\\\\") != -1:\n        path = path.split(\":\\\\\")[-1]\n    else:\n        # path is username/dataset or username/dataset:version\n        path = path.replace(':', '/')\n    return os.path.expanduser(posixpath.join(cache_folder, path))\n\n\ndef get_storage_map(fs, path, memcache=2 ** 26, lock=True, storage_cache=2 ** 28):\n    store = _get_storage_map(fs, path)\n    cache_path = get_cache_path(path)\n    if storage_cache and storage_cache > 0:\n        os.makedirs(cache_path, exist_ok=True)\n        store = LRUCache(\n            zarr.LMDBStore(cache_path, buffers=True, lock=lock), store, storage_cache\n        )\n    if memcache and memcache > 0:\n        store = LRUCache(zarr.MemoryStore(), store, memcache)\n    return store\n\n\nclass StorageMapWrapperWithCommit(MutableMapping):\n    def __init__(self, map):\n        self._map = map\n        self.root = self._map.root\n\n    def __getitem__(self, slice_):\n        return self._map[slice_]\n\n    def __setitem__(self, slice_, value):\n        self._map[slice_] = value\n\n    def __delitem__(self, slice_):\n        del self._map[slice_]\n\n    def __len__(self):\n        return len(self._map)\n\n    def __iter__(self):\n        yield from self._map\n\n    def flush(self):\n        pass\n\n    def commit(self):\n        \"\"\" Deprecated alias to flush()\"\"\"\n        self.flush()\n\n    def close(self):\n        pass\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, exc_type, exc_value, exc_traceback):\n        self.close()\n", "path": "hub/store/store.py"}, {"content": "from collections import OrderedDict\nfrom collections.abc import MutableMapping\n\n# from multiprocessing import Lock\n\nimport hashlib\nimport uuid\n\n\nclass DummyLock:\n    def __init__(self):\n        pass\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, *args):\n        pass\n\n\nLock = DummyLock\n\n\nclass LRUCache(MutableMapping):\n    def __init__(\n        self,\n        cache_storage: MutableMapping,\n        actual_storage: MutableMapping,\n        max_size,\n    ):\n        \"\"\"Creates LRU cache using cache_storage and actual_storage containers\n        max_size -> maximum cache size that is allowed\n        \"\"\"\n        self._dirty = set()\n        self._mutex = Lock()\n        self._max_size = max_size\n        self._cache_storage = cache_storage\n        self._actual_storage = actual_storage\n        self._total_cached = 0\n        self._cached_items = OrderedDict()\n        # assert len(self._cache_storage) == 0, \"Initially cache storage should be empty\"\n\n    @property\n    def cache_storage(self):\n        \"\"\"Storage which is used for caching\n        Returns MutableMapping\n        \"\"\"\n        return self._cache_storage\n\n    @property\n    def actual_storage(self):\n        \"\"\"Storage which is used for actual storing (not caching)\n        Returns MutableMapping\n        \"\"\"\n        return self._actual_storage\n\n    def __enter__(self):\n        return self\n\n    def __exit__(self, *args):\n        self.close()\n\n    def _flush_dirty(self):\n        for item in self._dirty:\n            self._actual_storage[item] = self._cache_storage[item]\n        self._dirty.clear()\n\n    def flush(self):\n        self._flush_dirty()\n        if hasattr(self._cache_storage, \"flush\"):\n            self._cache_storage.flush()\n        if hasattr(self._actual_storage, \"flush\"):\n            self._actual_storage.flush()\n\n    def close(self):\n        self._flush_dirty()\n        if hasattr(self._cache_storage, \"close\"):\n            self._cache_storage.close()\n        if hasattr(self._actual_storage, \"close\"):\n            self._actual_storage.close()\n\n    def commit(self):\n        self.close()\n\n    def __getitem__(self, key):\n        \"\"\" Gets item and puts it in the cache if not there \"\"\"\n        with self._mutex:\n            if key in self._cached_items:\n                self._cached_items.move_to_end(key)\n                return self._cache_storage[key]\n            else:\n                result = self._actual_storage[key]\n                self._free_memory(len(result))\n                self._append_cache(key, result)\n                return result\n\n    def __setitem__(self, key, value):\n        \"\"\" Sets item and puts it in the cache if not there\"\"\"\n        with self._mutex:\n            if key in self._cached_items:\n                self._total_cached -= self._cached_items.pop(key)\n            self._free_memory(len(value))\n            self._append_cache(key, value)\n            if key not in self._dirty:\n                self._dirty.add(key)\n\n    def __delitem__(self, key):\n        deleted_from_cache = False\n        with self._mutex:\n            if key in self._cached_items:\n                self._total_cached -= self._cached_items.pop(key)\n                del self._cache_storage[key]\n                self._dirty.discard(key)\n                deleted_from_cache = True\n            try:\n                del self._actual_storage[key]\n            except KeyError:\n                if not deleted_from_cache:\n                    raise\n\n    def __len__(self):\n        return len(\n            self.actual_storage\n        )  # TODO: In future might need to fix this to return proper len\n\n    def __iter__(self):\n        cached_keys = set(self._dirty)\n        for i in self.actual_storage:\n            cached_keys.discard(i)\n            yield i\n        yield from sorted(cached_keys)\n\n    def _free_memory(self, extra_size):\n        while (\n            self._total_cached > 0 and extra_size + self._total_cached > self._max_size\n        ):\n            item, itemsize = self._cached_items.popitem(last=False)\n            if item in self._dirty:\n                self._actual_storage[item] = self._cache_storage[item]\n                self._dirty.discard(item)\n            del self._cache_storage[item]\n            self._total_cached -= itemsize\n\n    def _append_cache(self, key, value):\n        self._total_cached += len(value)\n        self._cached_items[key] = len(value)\n        self._cache_storage[key] = value\n", "path": "hub/store/lru_cache.py"}]}