2424_TEMP_TABLE_ID_FORMAT = "bqdf{date}_{session_id}_{random_id}"
2525
2626
27- class TemporaryGbqStorageManager :
27+ class AnonymousDatasetManager :
2828 """
2929 Responsible for allocating and cleaning up temporary gbq tables used by a BigFrames session.
3030 """
@@ -46,32 +46,42 @@ def __init__(
4646 )
4747
4848 self .session_id = session_id
49- self ._table_ids : List [str ] = []
49+ self ._table_ids : List [bigquery . TableReference ] = []
5050 self ._kms_key = kms_key
5151
52- def create_temp_table (
52+ def allocate_and_create_temp_table (
5353 self , schema : Sequence [bigquery .SchemaField ], cluster_cols : Sequence [str ]
5454 ) -> bigquery .TableReference :
55- # Can't set a table in _SESSION as destination via query job API, so we
56- # run DDL, instead.
55+ """
56+ Allocates and and creates a table in the anonymous dataset.
57+ The table will be cleaned up by clean_up_tables.
58+ """
5759 expiration = (
5860 datetime .datetime .now (datetime .timezone .utc ) + constants .DEFAULT_EXPIRATION
5961 )
6062 table = bf_io_bigquery .create_temp_table (
6163 self .bqclient ,
62- self ._random_table (),
64+ self .allocate_temp_table (),
6365 expiration ,
6466 schema = schema ,
6567 cluster_columns = list (cluster_cols ),
6668 kms_key = self ._kms_key ,
6769 )
6870 return bigquery .TableReference .from_string (table )
6971
70- def _random_table (self , skip_cleanup : bool = False ) -> bigquery .TableReference :
72+ def allocate_temp_table (self ) -> bigquery .TableReference :
73+ """
74+ Allocates a unique table id, but does not create the table.
75+ The table will be cleaned up by clean_up_tables.
76+ """
77+ table_id = self .generate_unique_resource_id ()
78+ self ._table_ids .append (table_id )
79+ return table_id
80+
81+ def generate_unique_resource_id (self ) -> bigquery .TableReference :
7182 """Generate a random table ID with BigQuery DataFrames prefix.
7283
73- The generated ID will be stored and checked for deletion when the
74- session is closed, unless skip_cleanup is True.
84+ This resource will not be cleaned up by this manager.
7585
7686 Args:
7787 skip_cleanup (bool, default False):
@@ -87,16 +97,9 @@ def _random_table(self, skip_cleanup: bool = False) -> bigquery.TableReference:
8797 table_id = _TEMP_TABLE_ID_FORMAT .format (
8898 date = now .strftime ("%Y%m%d" ), session_id = self .session_id , random_id = random_id
8999 )
90- if not skip_cleanup :
91- self ._table_ids .append (table_id )
92100 return self .dataset .table (table_id )
93101
94102 def clean_up_tables (self ):
95103 """Delete tables that were created with this session's session_id."""
96- client = self .bqclient
97- project_id = self .dataset .project
98- dataset_id = self .dataset .dataset_id
99-
100- for table_id in self ._table_ids :
101- full_id = "." .join ([project_id , dataset_id , table_id ])
102- client .delete_table (full_id , not_found_ok = True )
104+ for table_ref in self ._table_ids :
105+ self .bqclient .delete_table (table_ref , not_found_ok = True )
0 commit comments