Some performance improvements.
This commit is contained in:
parent
f82efbbaa2
commit
b499a6cb0b
15
app/db.py
15
app/db.py
|
@ -50,9 +50,16 @@ class Graph:
|
||||||
|
|
||||||
def node(self, uri):
|
def node(self, uri):
|
||||||
# looks up a node by uri (essentially [[wikilink]]).
|
# looks up a node by uri (essentially [[wikilink]]).
|
||||||
# horrible
|
# this used to be even worse :)
|
||||||
return wikilink_to_node(uri)
|
try:
|
||||||
|
nodes = [node for node in G.nodes() if node.wikilink == uri]
|
||||||
|
return nodes[0]
|
||||||
|
except (KeyError, IndexError):
|
||||||
|
# We'll handle 404 in the template, as we want to show backlinks to non-existent nodes.
|
||||||
|
# Return an empty.
|
||||||
|
return Node(uri)
|
||||||
|
|
||||||
|
@cachetools.func.ttl_cache(maxsize=2, ttl=20)
|
||||||
def nodes(self, include_journals=True):
|
def nodes(self, include_journals=True):
|
||||||
# returns a list of all nodes
|
# returns a list of all nodes
|
||||||
|
|
||||||
|
@ -150,7 +157,7 @@ class Node:
|
||||||
nodes = []
|
nodes = []
|
||||||
for wikilink in self.back_links():
|
for wikilink in self.back_links():
|
||||||
n = G.node(wikilink)
|
n = G.node(wikilink)
|
||||||
if self.wikilink in [n.wikilink for n in n.pull_nodes()]:
|
if wikilink in [n.wikilink for n in n.pull_nodes()]:
|
||||||
nodes.append(n)
|
nodes.append(n)
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
@ -319,10 +326,12 @@ def all_journals():
|
||||||
nodes = [node for node in nodes if util.is_journal(node.wikilink)]
|
nodes = [node for node in nodes if util.is_journal(node.wikilink)]
|
||||||
return sorted(nodes, key=attrgetter('wikilink'), reverse=True)
|
return sorted(nodes, key=attrgetter('wikilink'), reverse=True)
|
||||||
|
|
||||||
|
# Deprecated.
|
||||||
def nodes_by_wikilink(wikilink):
|
def nodes_by_wikilink(wikilink):
|
||||||
nodes = [node for node in G.nodes() if node.wikilink == wikilink]
|
nodes = [node for node in G.nodes() if node.wikilink == wikilink]
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
# Deprecated.
|
||||||
def wikilink_to_node(node):
|
def wikilink_to_node(node):
|
||||||
try:
|
try:
|
||||||
return nodes_by_wikilink(node)[0]
|
return nodes_by_wikilink(node)[0]
|
||||||
|
|
Loading…
Reference in New Issue