Skip to content

Commit e0068d8

Browse files
author
Sailesh Mukil Gangatharan
committed
Introduce exec_env.py to maintain global state + minor bug fix
1. The global database_instances is moved to exec_env.py to avoid circular imports. 2. SpannerFiledInfo.typename populated with the correct name now
1 parent 402b9d3 commit e0068d8

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

spanner_graphs/database.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,9 @@ class SpannerFieldInfo:
7272

7373
def get_as_field_info_list(fields: List[StructType.Field]) -> List[SpannerFieldInfo]:
7474
"""Converts a list of StructType.Field to a list of SpannerFieldInfo."""
75-
return [SpannerFieldInfo(name=field.name, typename=field.type_.code.name) for field in fields]
75+
return [SpannerFieldInfo(name=field.name, typename=TypeCode(field.type_.code).name) for field in fields]
7676

7777

78-
# Global dict of database instances created in a single session
79-
database_instances: dict[str, SpannerDatabase | MockSpannerDatabase] = {}
80-
81-
def get_database_instance(project: str, instance: str, database: str, mock = False) -> SpannerDatabase:
82-
if mock:
83-
return MockSpannerDatabase()
84-
85-
key = f"{project}_{instance}_{database}"
86-
db = database_instances.get(key)
87-
88-
# Currently, we only create and return CloudSpannerDatabase instances. In the future, different
89-
# implementations could be introduced.
90-
if not db:
91-
db = CloudSpannerDatabase(project, instance, database)
92-
database_instances[key] = db
93-
94-
return db
95-
9678
class MockSpannerResult:
9779

9880
def __init__(self, file_path: str):

spanner_graphs/exec_env.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
# Copyright 2024 Google LLC
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""
17+
This module maintains state for the execution environment of a session
18+
"""
19+
from typing import Dict, Union
20+
21+
from spanner_graphs.database import SpannerDatabase, MockSpannerDatabase
22+
from spanner_graphs.cloud_database import CloudSpannerDatabase
23+
24+
# Global dict of database instances created in a single session
25+
database_instances: Dict[str, Union[SpannerDatabase, MockSpannerDatabase]] = {}
26+
27+
def get_database_instance(project: str, instance: str, database: str, mock = False):
28+
if mock:
29+
return MockSpannerDatabase()
30+
31+
key = f"{project}_{instance}_{database}"
32+
db = database_instances.get(key)
33+
34+
# Currently, we only create and return CloudSpannerDatabase instances. In the future, different
35+
# implementations could be introduced.
36+
if not db:
37+
db = CloudSpannerDatabase(project, instance, database)
38+
database_instances[key] = db
39+
40+
return db
41+

spanner_graphs/graph_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from google.cloud.spanner_v1 import TypeCode
2626

2727
from spanner_graphs.conversion import get_nodes_edges
28-
from spanner_graphs.database import get_database_instance
28+
from spanner_graphs.exec_env import get_database_instance
2929

3030

3131
# Mapping of string types from frontend to Spanner TypeCode enum values

spanner_graphs/magics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from ipywidgets import interact
3434
from jinja2 import Template
3535

36-
from spanner_graphs.database import get_database_instance
36+
from spanner_graphs.exec_env import get_database_instance
3737
from spanner_graphs.graph_server import (
3838
GraphServer, execute_query, execute_node_expansion,
3939
validate_node_expansion_request

0 commit comments

Comments
 (0)