Skip to content

Commit

Permalink
#258 Removed DbNode from sqlalchemy models, Django model and dummy model
Browse files Browse the repository at this point in the history
  • Loading branch information
lekah committed Jun 1, 2017
1 parent 08db0bf commit f230162
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 181 deletions.
53 changes: 0 additions & 53 deletions aiida/backends/djsite/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ class DbNode(m.Model):
* A is 'input' of C.
* C is 'output' of A.
* A is 'parent' of B,C
* C,B are 'children' of A.
:note: parents and children are stored in the DbPath table, the transitive
closure table, automatically updated via DB triggers whenever a link is
added to or removed from the DbLink table.
Internal attributes, that define the node itself,
are stored in the DbAttribute table; further user-defined attributes,
Expand Down Expand Up @@ -170,9 +164,6 @@ class DbNode(m.Model):
# Direct links
outputs = m.ManyToManyField('self', symmetrical=False,
related_name='inputs', through='DbLink')
# Transitive closure
children = m.ManyToManyField('self', symmetrical=False,
related_name='parents', through='DbPath')

# Used only if dbnode is a calculation, or remotedata
# Avoid that computers can be deleted if at least a node exists pointing
Expand Down Expand Up @@ -305,50 +296,6 @@ def __str__(self):
self.output.pk, )


@python_2_unicode_compatible
class DbPath(m.Model):
"""
Transitive closure table for all dbnode paths.
"""
parent = m.ForeignKey('DbNode', related_name='child_paths', editable=False)
child = m.ForeignKey('DbNode', related_name='parent_paths', editable=False)
depth = m.IntegerField(editable=False)

# Used to delete or to expand the path
entry_edge_id = m.IntegerField(null=True, editable=False)
direct_edge_id = m.IntegerField(null=True, editable=False)
exit_edge_id = m.IntegerField(null=True, editable=False)

def __str__(self):
return "{} ({}) ==[{}]==>> {} ({})".format(
self.parent.get_simple_name(invalid_result="Unknown node"),
self.parent.pk,
self.depth,
self.child.get_simple_name(invalid_result="Unknown node"),
self.child.pk, )

def expand(self):
"""
Method to expand a DbPath (recursive function), i.e., to get a list
of all dbnodes that are traversed in the given path.
:return: list of DbNode objects representing the expanded DbPath
"""

if self.depth == 0:
return [self.parent, self.child]
else:
path_entry = []
path_direct = DbPath.objects.get(id=self.direct_edge_id).expand()
path_exit = []
# we prevent DbNode repetitions
if self.entry_edge_id != self.direct_edge_id:
path_entry = DbPath.objects.get(id=self.entry_edge_id).expand()[:-1]
if self.exit_edge_id != self.direct_edge_id:
path_exit = DbPath.objects.get(id=self.exit_edge_id).expand()[1:]

return path_entry + path_direct + path_exit


attrdatatype_choice = (
('float', 'float'),
Expand Down
34 changes: 0 additions & 34 deletions aiida/backends/djsite/querybuilder_django/dummy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,6 @@ class DbLink(Base):
output = relationship("DbNode", primaryjoin="DbLink.output_id == DbNode.id")
label = Column(String(255), index=True, nullable=False)


class DbPath(Base):
__tablename__ = "db_dbpath"
id = Column(Integer, primary_key=True)
parent_id = Column(
Integer,
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED")
)
child_id = Column(
Integer,
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED")
)
parent = relationship(
"DbNode",
primaryjoin="DbPath.parent_id == DbNode.id",
backref="child_paths"
)
child = relationship(
"DbNode",
primaryjoin="DbPath.child_id == DbNode.id",
backref="parent_paths"
)
depth = Column(Integer)
entry_edge_id = Column(Integer)
direct_edge_id = Column(Integer)
exit_edge_id = Column(Integer)

class DbCalcState(Base):
__tablename__ = "db_dbcalcstate"
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -320,13 +293,6 @@ class DbNode(Base):
passive_deletes = True
)

children = relationship(
"DbNode",
secondary = "db_dbpath",
primaryjoin = "DbNode.id == DbPath.parent_id",
secondaryjoin = "DbNode.id == DbPath.child_id",
backref = "parents"
)
def get_aiida_class(self):
"""
Return the corresponding instance of
Expand Down
12 changes: 2 additions & 10 deletions aiida/backends/djsite/querybuilder_django/querybuilder_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sqlalchemy.orm import aliased
from sqlalchemy.orm.attributes import InstrumentedAttribute

from sqlalchemy.sql.expression import cast
from sqlalchemy.sql.expression import cast, ColumnClause
from sqlalchemy.sql.elements import Cast, Label
from aiida.common.exceptions import InputValidationError
from aiida.backends.general.querybuilder_interface import QueryBuilderInterface
Expand Down Expand Up @@ -104,7 +104,7 @@ def get_filter_expr_from_column(self, operator, value, column):

# Label is used because it is what is returned for the
# 'state' column by the hybrid_column construct
if not isinstance(column, (Cast, InstrumentedAttribute, Label)):
if not isinstance(column, (Cast, InstrumentedAttribute, Label, ColumnClause)):
raise TypeError(
'column ({}) {} is not a valid column'.format(
type(column), column
Expand Down Expand Up @@ -282,14 +282,6 @@ def get_filter_expr(
return expr




def prepare_with_dbpath(self):
#~ from aiida.backends.querybuild.dummy_model import DbPath as DummyPath
self.Path = dummy_model.DbPath



def get_session(self):
return dummy_model.get_aldjemy_session()
# return dummy_model.session
Expand Down
12 changes: 0 additions & 12 deletions aiida/backends/general/querybuilder_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ def AiidaComputer(self):
pass

@abstractmethod
def prepare_with_dbpath(self):
"""
A method to use the DbPath, if this is supported, or throw an
exception if not.
The overrider must fill add the DbPath-ORM as an attribute to self::
from aiida.backends.implementation.model import DbPath
self.path = DbPath
"""
pass
@abstractmethod
def get_session(self):
"""
:returns: a valid session, an instance of sqlalchemy.orm.session.Session
Expand Down
72 changes: 0 additions & 72 deletions aiida/backends/sqlalchemy/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,6 @@ def outputs(self):
def inputs(self):
return self.inputs_q.all()

# children via db_dbpath
# suggest change name to descendants
children_q = relationship(
"DbNode", secondary="db_dbpath",
primaryjoin="DbNode.id == DbPath.parent_id",
secondaryjoin="DbNode.id == DbPath.child_id",
backref=backref("parents_q", passive_deletes=True, lazy='dynamic'),
lazy='dynamic',
passive_deletes=True
)

@property
def children(self):
return self.children_q.all()

@property
def parents(self):
return self.parents_q.all()

def __init__(self, *args, **kwargs):
super(DbNode, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -376,56 +357,3 @@ def __str__(self):
self.output.pk
)


class DbPath(Base):
__tablename__ = "db_dbpath"

id = Column(Integer, primary_key=True)
parent_id = Column(
Integer,
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED")
)
child_id = Column(
Integer,
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED")
)

parent = relationship(
"DbNode",
primaryjoin="DbPath.parent_id == DbNode.id",
backref="child_paths"
)
child = relationship(
"DbNode",
primaryjoin="DbPath.child_id == DbNode.id",
backref="parent_paths"
)

depth = Column(Integer)

entry_edge_id = Column(Integer)
direct_edge_id = Column(Integer)
exit_edge_id = Column(Integer)

def expand(self):
"""
Method to expand a DbPath (recursive function), i.e., to get a list
of all dbnodes that are traversed in the given path.
:return: list of DbNode objects representing the expanded DbPath
"""

if self.depth == 0:
return [self.parent_id, self.child_id]
else:
path_entry = []
path_direct = DbPath.query.filter_by(id=self.direct_edge_id).first().expand()
path_exit = []
# we prevent DbNode repetitions
if self.entry_edge_id != self.direct_edge_id:
path_entry = DbPath.query.filter_by(id=self.entry_edge_id).first().expand()[:-1]
if self.exit_edge_id != self.direct_edge_id:
path_exit = DbPath.query.filter_by(id=self.exit_edge_id).first().expand()[1:]

return path_entry + path_direct + path_exit

0 comments on commit f230162

Please sign in to comment.