Skip to content

Commit

Permalink
Fixes for StationLink
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Nov 16, 2014
1 parent ad41e04 commit 96040f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data/TradeDangerous.db-journal
data/TradeDangerous.prices
market/
*.orig
*.prev

wip/
pycallgraph.png
35 changes: 21 additions & 14 deletions buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,21 +711,24 @@ def generateStationLink(tdenv, db):
tdenv.DEBUG0("Generating StationLink table")
db.create_function("sqrt", 1, math.sqrt)
db.execute("""
INSERT INTO StationLink
INSERT INTO StationLink
SELECT lhs.system_id AS lhs_system_id,
lhs.station_id AS lhs_station_id,
rhs.system_id AS rhs_system_id,
rhs.station_id AS rhs_station_id,
((lSys.pos_x - rSys.pos_x) * (lSys.pos_x - rSys.pos_x)) +
((lSys.pos_y - rSys.pos_y) * (lSys.pos_y - rSys.pos_y)) +
((lSys.pos_z - rSys.pos_z) * (lSys.pos_z - rSys.pos_z)) dist
FROM System AS lSys
INNER JOIN Station lhs
ON (lSys.system_id = lhs.system_id),
System AS rSys
INNER JOIN Station rhs
ON (rSys.system_id = rhs.system_id)
WHERE lhs.system_id != rhs.system_id
sqrt(
((lSys.pos_x - rSys.pos_x) * (lSys.pos_x - rSys.pos_x)) +
((lSys.pos_y - rSys.pos_y) * (lSys.pos_y - rSys.pos_y)) +
((lSys.pos_z - rSys.pos_z) * (lSys.pos_z - rSys.pos_z))
)
FROM Station AS lhs
INNER JOIN System AS lSys
ON (lhs.system_id = lSys.system_id),
Station AS rhs
INNER JOIN System AS rSys
ON (rhs.system_id = rSys.system_id)
WHERE
lhs.system_id != rhs.system_id
""")
db.commit()

Expand Down Expand Up @@ -754,6 +757,9 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal
backupDBName = dbFilename + ".prev"
tempPath, backupPath = Path(tempDBName), Path(backupDBName)

if tempPath.exists():
tempPath.unlink()

tempDB = sqlite3.connect(tempDBName)
# Read the SQL script so we are ready to populate structure, etc.
tdenv.DEBUG0("Executing SQL Script '{}' from '{}'", sqlPath, os.getcwd())
Expand All @@ -777,9 +783,10 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal

tdenv.DEBUG0("Swapping out db files")

if backupPath.exists():
backupPath.unlink()
dbPath.rename(backupPath)
if dbPath.exists():
if backupPath.exists():
backupPath.unlink()
dbPath.rename(backupPath)
tempPath.rename(dbPath)

tdenv.DEBUG0("Finished")
Expand Down
18 changes: 9 additions & 9 deletions data/TradeDangerous.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ CREATE INDEX idx_buying_price ON StationBuying (item_id, price);

CREATE TABLE StationLink
(
lhs_system_id INTEGER NOT NULL,
lhs_station_id INTEGER NOT NULL,
rhs_system_id INTEGER NOT NULL CHECK (lhs_system_id != rhs_system_id),
rhs_station_id INTEGER NOT NULL CHECK (lhs_station_id != rhs_station_id),
dist DOUBLE NOT NULL CHECK (dist > 0.0),
PRIMARY KEY (lhs_station_id, rhs_station_id)
)
lhs_system_id INTEGER NOT NULL,
lhs_station_id INTEGER NOT NULL,
rhs_system_id INTEGER NOT NULL,
rhs_station_id INTEGER NOT NULL,
dist DOUBLE NOT NULL CHECK (dist > 0.0),
PRIMARY KEY (lhs_station_id, rhs_station_id)
) WITHOUT ROWID
;
CREATE INDEX idx_stn_dist ON StationLink (lhs_station_id, rhs_station_id);
CREATE INDEX idx_stn_sys ON StationLink (lhs_system_id, rhs_system_id);
CREATE INDEX idx_stn_dist ON StationLink (lhs_station_id, dist, rhs_station_id);
CREATE INDEX idx_stn_sys ON StationLink (lhs_system_id, rhs_system_id, dist);

CREATE VIEW vPrice AS
SELECT si.station_id AS station_id,
Expand Down

0 comments on commit 96040f7

Please sign in to comment.