Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove_node method to SQLBackend #58

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions grand/backends/_sqlbackend.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Hashable, Generator, Optional, Iterable
from typing import Hashable, Generator
import time

import pandas as pd
import sqlalchemy
from sqlalchemy.pool import NullPool
from sqlalchemy.sql import select
from sqlalchemy import and_, or_, func, Index
from sqlalchemy.sql import delete, select
from sqlalchemy import or_, func, Index

from .backend import Backend

Expand Down Expand Up @@ -192,6 +191,29 @@ def _upsert_node(self, node_name: Hashable, metadata: dict) -> Hashable:
parameters={self._primary_key: node_name, "_metadata": metadata},
)

def remove_node(self, u: Hashable) -> None:
"""
Removes nodes and related edges for name.

Args:
u (Hashable): id of the node
"""

# Remove nodes
statement = delete(self._node_table).where(
self._node_table.c[self._primary_key] == str(u)
)
self._connection.execute(statement)

# Remove edges for node
statement = delete(self._edge_table).where(
or_(
self._edge_table.c[self._edge_source_key] == str(u),
self._edge_table.c[self._edge_target_key] == str(u)
)
)
self._connection.execute(statement)

def all_nodes_as_iterable(self, include_metadata: bool = False) -> Generator:
"""
Get a generator of all of the nodes in this graph.
Expand Down Expand Up @@ -233,7 +255,7 @@ def has_node(self, u: Hashable) -> bool:
self._node_table.c[self._primary_key] == str(u)
)
).fetchall()
)
) > 0

def add_edge(self, u: Hashable, v: Hashable, metadata: dict):
"""
Expand Down
11 changes: 11 additions & 0 deletions grand/backends/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ def test_sqlite_persistence(self):
nodes = list(backend.all_nodes_as_iterable())
# assert
assert node0 in nodes

# test remove_node
backend = SQLBackend(db_url=url, directed=True)
node1, node2 = backend.add_node("A", {}), backend.add_node("B", {})
backend.add_edge(node1, node2, {})
assert backend.has_node(node1)
assert backend.has_edge(node1, node2)
backend.remove_node(node1)
assert not backend.has_node(node1)
assert not backend.has_edge(node1, node2)

# cleanup
os.remove(dbpath)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"sql": ["SQLAlchemy>=1.3"],
"dynamodb": ["boto3"],
"igraph": ["igraph"],
"networkit": ["cmake", "cython", "networkit"],
"networkit": ["cmake", "cython", "networkit", "numpy<2.0.0"],
},
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
Loading