Skip to content

Commit

Permalink
Have buildCache generate a SystemLinks table to reduce runtime calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Nov 10, 2014
1 parent 8f088e1 commit 58a27e8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sys
import os
import csv
import math
from pathlib import Path
from collections import namedtuple
from tradeexcept import TradeException
Expand Down Expand Up @@ -697,6 +698,35 @@ def processImportFile(tdenv, db, importPath, tableName):
table=tableName)


def generateSystemLinks(tdenv, db, maxLinkDist):
tdenv.DEBUG(0, "Generating SystemLinks table")
db.create_function("sqrt", 1, math.sqrt)
db.execute("""
CREATE TABLE SystemLinks
AS SELECT lhs.system_id AS lhs_id,
rhs.system_id AS rhs_id,
((lhs.pos_x - rhs.pos_x) * (lhs.pos_x - rhs.pos_x)) +
((lhs.pos_y - rhs.pos_y) * (lhs.pos_y - rhs.pos_y)) +
((lhs.pos_z - rhs.pos_z) * (lhs.pos_z - rhs.pos_z)) dist
FROM System AS lhs, System AS rhs
WHERE lhs.system_id != rhs.system_id
AND rhs.pos_x BETWEEN lhs.pos_x - {mld} and lhs.pos_x + {mld}
AND rhs.pos_y BETWEEN lhs.pos_y - {mld} and lhs.pos_y + {mld}
AND rhs.pos_z BETWEEN lhs.pos_z - {mld} and lhs.pos_z + {mld}
AND dist <= {mldsq}
""".format(
mld=maxLinkDist,
mldsq=maxLinkDist**2,
))
db.execute("UPDATE SystemLinks SET dist = sqrt(dist)")
db.execute("""
CREATE UNIQUE INDEX
idx_system_links_systems
ON SystemLinks(lhs_id, rhs_id)
""")
db.commit()


######################################################################

def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=False):
Expand Down Expand Up @@ -746,6 +776,8 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal
newDB.executescript(importScript)
newDB.commit()

generateSystemLinks(tdenv, newDB, maxLinkDist=80)

tdenv.DEBUG(0, "Finished")


0 comments on commit 58a27e8

Please sign in to comment.