Skip to content

Commit 443e6b5

Browse files
committed
(improvement) metadata: replace dict_factory with lightweight _RowView
Replace dict_factory in _SchemaParser._handle_results and get_column_from_system_local with _row_factory, eliminating per-row dict allocation during schema parsing. Also refactor SchemaParserV4._build_keyspace_metadata_internal to read from the row without mutating it, since _RowView is read-only. Note: V22-only dict_factory call sites are left unchanged as they do not affect the V3/V4 code path (V3 and V4 fully override _query_all).
1 parent 2a92d1f commit 443e6b5

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

cassandra/metadata.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ def get_next_pages():
20682068
yield next_result.parsed_rows
20692069

20702070
result.parsed_rows += itertools.chain(*get_next_pages())
2071-
return dict_factory(result.column_names, result.parsed_rows) if result else []
2071+
return _row_factory(result.column_names, result.parsed_rows) if result else []
20722072
else:
20732073
raise result
20742074

@@ -3120,11 +3120,12 @@ def get_all_keyspaces(self):
31203120

31213121
@staticmethod
31223122
def _build_keyspace_metadata_internal(row):
3123-
# necessary fields that aren't int virtual ks
3124-
row["durable_writes"] = row.get("durable_writes", None)
3125-
row["replication"] = row.get("replication", {})
3126-
row["replication"]["class"] = row["replication"].get("class", None)
3127-
return super(SchemaParserV4, SchemaParserV4)._build_keyspace_metadata_internal(row)
3123+
# Read without mutating the row, since _RowView is read-only
3124+
name = row["keyspace_name"]
3125+
durable_writes = row.get("durable_writes", None)
3126+
replication = dict(row.get("replication")) if row.get("replication") else {}
3127+
replication_class = replication.pop("class") if 'class' in replication else None
3128+
return KeyspaceMetadata(name, durable_writes, replication_class, replication)
31283129

31293130

31303131
class SchemaParserDSE67(SchemaParserV4):
@@ -3190,7 +3191,7 @@ def get_table(self, keyspaces, keyspace, table):
31903191
def _build_keyspace_metadata_internal(row):
31913192
name = row["keyspace_name"]
31923193
durable_writes = row.get("durable_writes", None)
3193-
replication = dict(row.get("replication")) if 'replication' in row else {}
3194+
replication = dict(row.get("replication")) if row.get("replication") else {}
31943195
replication_class = replication.pop("class") if 'class' in replication else None
31953196
graph_engine = row.get("graph_engine", None)
31963197
return KeyspaceMetadata(name, durable_writes, replication_class, replication, graph_engine)
@@ -3528,7 +3529,7 @@ def get_column_from_system_local(connection, column_name: str, timeout, metadata
35283529
, timeout=timeout, fail_on_error=False)
35293530
if not success or not local_result.parsed_rows:
35303531
return ""
3531-
local_rows = dict_factory(local_result.column_names, local_result.parsed_rows)
3532+
local_rows = _row_factory(local_result.column_names, local_result.parsed_rows)
35323533
local_row = local_rows[0]
35333534
return local_row.get(column_name)
35343535

0 commit comments

Comments
 (0)