Skip to content

Commit dcd88bd

Browse files
committed
simplify
Signed-off-by: Achal Shah <achals@gmail.com>
1 parent 64c3971 commit dcd88bd

1 file changed

Lines changed: 41 additions & 75 deletions

File tree

  • sdk/python/feast/infra/registry_stores

sdk/python/feast/infra/registry_stores/sql.py

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,7 @@ def get_entity(self, name: str, project: str, allow_cache: bool = False) -> Enti
162162
raise EntityNotFoundException(name, project=project)
163163

164164
def list_entities(self, project: str, allow_cache: bool = False) -> List[Entity]:
165-
with self.engine.connect() as conn:
166-
stmt = select(entities)
167-
rows = conn.execute(stmt).all()
168-
if rows:
169-
return [
170-
Entity.from_proto(EntityProto.FromString(row["entity_proto"]))
171-
for row in rows
172-
]
173-
return []
165+
return self._list_objects(entities, EntityProto, Entity, "entity_proto")
174166

175167
def delete_entity(self, name: str, project: str, commit: bool = True):
176168
with self.engine.connect() as conn:
@@ -193,17 +185,9 @@ def get_data_source(
193185
def list_data_sources(
194186
self, project: str, allow_cache: bool = False
195187
) -> List[DataSource]:
196-
with self.engine.connect() as conn:
197-
stmt = select(data_sources)
198-
rows = conn.execute(stmt).all()
199-
if rows:
200-
return [
201-
DataSource.from_proto(
202-
DataSourceProto.FromString(row["data_source_proto"])
203-
)
204-
for row in rows
205-
]
206-
return []
188+
return self._list_objects(
189+
data_sources, DataSourceProto, DataSource, "data_source_proto"
190+
)
207191

208192
def apply_data_source(
209193
self, data_source: DataSource, project: str, commit: bool = True
@@ -248,77 +232,46 @@ def delete_data_source(self, name: str, project: str, commit: bool = True):
248232
def list_feature_services(
249233
self, project: str, allow_cache: bool = False
250234
) -> List[FeatureService]:
251-
with self.engine.connect() as conn:
252-
stmt = select(feature_services)
253-
rows = conn.execute(stmt).all()
254-
if rows:
255-
return [
256-
FeatureService.from_proto(
257-
FeatureServiceProto.FromString(row["feature_service_proto"])
258-
)
259-
for row in rows
260-
]
261-
return []
235+
return self._list_objects(
236+
feature_services,
237+
FeatureServiceProto,
238+
FeatureService,
239+
"feature_service_proto",
240+
)
262241

263242
def list_feature_views(
264243
self, project: str, allow_cache: bool = False
265244
) -> List[FeatureView]:
266-
with self.engine.connect() as conn:
267-
stmt = select(feature_views)
268-
rows = conn.execute(stmt).all()
269-
if rows:
270-
return [
271-
FeatureView.from_proto(
272-
FeatureViewProto.FromString(row["feature_view_proto"])
273-
)
274-
for row in rows
275-
]
276-
return []
245+
return self._list_objects(
246+
feature_views, FeatureViewProto, FeatureView, "feature_view_proto"
247+
)
277248

278249
def list_saved_datasets(
279250
self, project: str, allow_cache: bool = False
280251
) -> List[SavedDataset]:
281-
with self.engine.connect() as conn:
282-
stmt = select(saved_datasets)
283-
rows = conn.execute(stmt).all()
284-
if rows:
285-
return [
286-
SavedDataset.from_proto(
287-
SavedDatasetProto.FromString(row["saved_dataset_proto"])
288-
)
289-
for row in rows
290-
]
291-
return []
252+
return self._list_objects(
253+
saved_datasets, SavedDatasetProto, SavedDataset, "saved_dataset_proto"
254+
)
292255

293256
def list_request_feature_views(
294257
self, project: str, allow_cache: bool = False
295258
) -> List[RequestFeatureView]:
296-
with self.engine.connect() as conn:
297-
stmt = select(request_feature_views)
298-
rows = conn.execute(stmt).all()
299-
if rows:
300-
return [
301-
RequestFeatureView.from_proto(
302-
RequestFeatureViewProto.FromString(row["feature_view_proto"])
303-
)
304-
for row in rows
305-
]
306-
return []
259+
return self._list_objects(
260+
request_feature_views,
261+
RequestFeatureViewProto,
262+
RequestFeatureView,
263+
"feature_view_proto",
264+
)
307265

308266
def list_on_demand_feature_views(
309267
self, project: str, allow_cache: bool = False
310268
) -> List[OnDemandFeatureView]:
311-
with self.engine.connect() as conn:
312-
stmt = select(on_demand_feature_views)
313-
rows = conn.execute(stmt).all()
314-
if rows:
315-
return [
316-
OnDemandFeatureView.from_proto(
317-
OnDemandFeatureViewProto.FromString(row["feature_view_proto"])
318-
)
319-
for row in rows
320-
]
321-
return []
269+
return self._list_objects(
270+
on_demand_feature_views,
271+
OnDemandFeatureViewProto,
272+
OnDemandFeatureView,
273+
"feature_view_proto",
274+
)
322275

323276
def apply_saved_dataset(
324277
self, saved_dataset: SavedDataset, project: str, commit: bool = True,
@@ -372,3 +325,16 @@ def _apply_object(
372325
},
373326
)
374327
conn.execute(insert_stmt)
328+
329+
def _list_objects(self, table, proto_class, python_class, proto_field_name):
330+
with self.engine.connect() as conn:
331+
stmt = select(table)
332+
rows = conn.execute(stmt).all()
333+
if rows:
334+
return [
335+
python_class.from_proto(
336+
proto_class.FromString(row[proto_field_name])
337+
)
338+
for row in rows
339+
]
340+
return []

0 commit comments

Comments
 (0)