-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: contexts for temporary DBLayers
- Loading branch information
1 parent
a6bcbae
commit 4b92875
Showing
11 changed files
with
69 additions
and
34 deletions.
There are no files selected for viewing
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
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
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,25 @@ | ||
import uuid | ||
from typing import Optional | ||
|
||
from coolname import generate_slug | ||
|
||
|
||
def get_unique_id( | ||
slug_len: int = 3, | ||
add_uuid: bool = True, | ||
prefix: Optional[str] = None, | ||
max_len: int = 320, | ||
) -> str: # pragma: no cover | ||
slug = generate_slug(slug_len) | ||
if prefix is not None: | ||
result = f"{prefix}-{slug}" | ||
else: | ||
result = f"{slug}" | ||
|
||
if add_uuid: | ||
unique_id = str(uuid.uuid1()) | ||
result += unique_id | ||
result = result[:max_len] | ||
while not result[-1].isalpha(): | ||
result = result[:-1] | ||
return result |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Common project-wide constants.""" | ||
|
||
DEFAULT_PROJECT = "zetta-research" | ||
DEFAULT_FIRESTORE_DB = "zetta-utils" | ||
RUN_DATABASE: str | None = "run-db" |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .backend import DatastoreBackend | ||
from .build import build_datastore_layer | ||
from .temp_layer_ctx import temp_datastore_layer_ctx |
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,18 @@ | ||
from contextlib import contextmanager | ||
|
||
from zetta_utils.common import get_unique_id | ||
from zetta_utils.constants import DEFAULT_PROJECT | ||
|
||
from .build import build_datastore_layer | ||
|
||
|
||
@contextmanager | ||
def temp_datastore_layer_ctx( | ||
prefix: str | None = None, project: str = DEFAULT_PROJECT | ||
): # pragma: no cover # pure delegation | ||
db_layer = build_datastore_layer( | ||
namespace=get_unique_id(prefix=prefix, add_uuid=False), project=project | ||
) | ||
yield db_layer | ||
# TODO: Clear in batches to not go over the datastore limit | ||
db_layer.backend.clear() |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .backend import FirestoreBackend | ||
from .build import build_firestore_layer | ||
from .temp_layer_ctx import temp_firestore_layer_ctx |
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,17 @@ | ||
from contextlib import contextmanager | ||
|
||
from zetta_utils.common import get_unique_id | ||
from zetta_utils.constants import DEFAULT_FIRESTORE_DB, DEFAULT_PROJECT | ||
|
||
from .build import build_firestore_layer | ||
|
||
|
||
@contextmanager | ||
def temp_firestore_layer_ctx( | ||
prefix: str | None = None, database=DEFAULT_FIRESTORE_DB, project: str = DEFAULT_PROJECT | ||
): # pragma: no cover # pure delegation | ||
db_layer = build_firestore_layer( | ||
collection=get_unique_id(prefix=prefix, add_uuid=False), database=database, project=project | ||
) | ||
yield db_layer | ||
db_layer.backend.clear() |
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
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
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