[docs]definsert(self,objs:Union[OBJECT,List[OBJECT]],**kwargs):ifnotisinstance(objs,list):objs=[objs]forobjinobjs:if"id"notinobj:raiseValueError("Each object must have an 'id' field.")obj_id=str(obj["id"])forkey,valueinobj.items():ifkey=="id":continueifisinstance(value,(dict,list)):value=json.dumps(value)self.hdf5_group.create_dataset(f"{obj_id}/{key}",data=value)
[docs]defdelete(self,objs:Union[OBJECT,List[OBJECT]],**kwargs)->int:ifnotisinstance(objs,list):objs=[objs]count=0forobjinobjs:if"id"notinobj:raiseValueError("Each object must have an 'id' field.")obj_id=str(obj["id"])ifobj_idinself.hdf5_group:delself.hdf5_group[obj_id]count+=1returncount
[docs]defdelete_where(self,where:Optional[Dict[str,Any]]=None,missing_ok=True,**kwargs)->int:logger.info(f"Deleting from {self.target_class_name} where: {where}")ifwhereisNone:where={}results=self.query(Query(where_clause=where)).rowscount=self.delete(results)returncount
[docs]defquery_facets(self,where:Dict=None,facet_columns:List[str]=None,facet_limit=DEFAULT_FACET_LIMIT,**kwargs)->Dict[str,List[Tuple[Any,int]]]:results={}ifnotfacet_columns:facet_columns=list(self.class_definition().attributes.keys())forcolinfacet_columns:logger.debug(f"Faceting on {col}")facet_counts={}forobjinself.query(Query(where_clause=where)).rows:ifcolinobj:value=obj[col]ifisinstance(value,list):forvinvalue:facet_counts[v]=facet_counts.get(v,0)+1else:facet_counts[value]=facet_counts.get(value,0)+1facet_counts=sorted(facet_counts.items(),key=lambdax:x[1],reverse=True)[:facet_limit]results[col]=facet_countsreturnresults