-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed FilesystemCache class with corresponding unit tests
- Loading branch information
1 parent
559bab5
commit b686f94
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
jax/experimental/compilation_cache/compilation_cache_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from absl.testing import absltest | ||
import jax.test_util as jtu | ||
import file_system_cache as fsc | ||
|
||
class CompilationClassTest(jtu.JaxTestCase): | ||
|
||
def test_new_directory_and_path(self): | ||
fileSystemCacheInstance = fsc.FileSystemCache("/tmp/new_cache_directory") | ||
self.assertEqual(fileSystemCacheInstance.get("nonExistantFile"), None) | ||
|
||
def test_new_directory_existing_path(self): | ||
fileSystemCacheInstance = fsc.FileSystemCache("/tmp/new_cache_directory") | ||
fileSystemCacheInstance.put("foo", b"bar") | ||
self.assertEqual(fileSystemCacheInstance.get("foo"), b"bar") | ||
|
||
if __name__ == "__main__": | ||
absltest.main(testLoader=jtu.JaxTestLoader()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
## Core functionality: | ||
##create my own directory in experimental folder in jax | ||
##file for this class then seperate file for tests | ||
|
||
|
||
from typing import Optional | ||
import os | ||
import tempfile | ||
|
||
class FileSystemCache: | ||
|
||
def __init__(self, path: str): | ||
"""Sets up a cache at 'path'. Cached values may already be present.""" | ||
if not os.path.isdir(path): | ||
print("HERE\n") | ||
os.mkdir(path) | ||
self.path = path | ||
|
||
def get(self, key: str) -> Optional[bytes]: | ||
"""Returns None if 'key' isn't present.""" | ||
path_to_key = os.path.join(self.path, key) | ||
if(os.path.exists(path_to_key)): | ||
with open(path_to_key, "rb") as file: | ||
file_contents = file.read() | ||
return file_contents | ||
else: | ||
return None | ||
|
||
def put(self, key: str, value: bytes): | ||
"""Adds new cache entry, possibly evicting older entries.""" | ||
with open(os.path.join(self.path, key), "wb") as file: | ||
file.write(value) |