-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtdb-grb.cpp
More file actions
244 lines (199 loc) · 7.5 KB
/
tdb-grb.cpp
File metadata and controls
244 lines (199 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "tdb-grb.h"
#include "GraphBLAS.h"
#include <cstdio>
#include <sqlite3.h>
#include <vector>
#define SYSTEM_VECTOR_BLOCK_SIZE 8192
#define STATION_VECTOR_BLOCK_SIZE SYSTEM_VECTOR_BLOCK_SIZE * 16
#define LISTING_VECTOR_BLOCK_SIZE STATION_VECTOR_BLOCK_SIZE * 64
TDB::TDB(const char *path) {
int err = sqlite3_open_v2(
path, &conn, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, nullptr);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to open %s: %s\n", path, sqlite3_errmsg(conn));
}
}
std::vector<System> TDB::loadSystems() {
const char *sqlStmt = "SELECT system_id,pos_x,pos_y,pos_z FROM System;";
sqlite3_stmt *stmt;
int err = sqlite3_prepare_v2(conn, sqlStmt, -1, &stmt, nullptr);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to compile loadSystems sql: %s\n",
sqlite3_errmsg(conn));
return std::vector<System>();
}
std::vector<System> systems;
systems.reserve(SYSTEM_VECTOR_BLOCK_SIZE);
while (true) {
int ret = sqlite3_step(stmt);
if (ret == SQLITE_DONE) {
break;
} else if (ret != SQLITE_ROW) {
fprintf(stderr, "sqlite step error: %s\n", sqlite3_errmsg(conn));
return std::vector<System>();
}
System system;
system.id = sqlite3_column_int(stmt, 1);
system.x = sqlite3_column_double(stmt, 2);
system.y = sqlite3_column_double(stmt, 3);
system.z = sqlite3_column_double(stmt, 4);
systems.push_back(system);
if (systems.capacity() <= systems.size()) {
systems.reserve(systems.size() + SYSTEM_VECTOR_BLOCK_SIZE);
}
}
err = sqlite3_finalize(stmt);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to finalize loadsystems sql: %s\n",
sqlite3_errmsg(conn));
}
return systems;
}
int TDB::loadMarketInfo(MarketInfo *info) {
info->systems.clear();
info->stations.clear();
info->listings.clear();
const char *systemLoadSQL =
"SELECT system_id,pos_x,pos_y,pos_z FROM System;";
sqlite3_stmt *stmt;
int err = sqlite3_prepare_v2(conn, systemLoadSQL, -1, &stmt, nullptr);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to compile loadSystems sql: %s\n",
sqlite3_errmsg(conn));
return 1;
}
info->systems.reserve(SYSTEM_VECTOR_BLOCK_SIZE);
info->stations.reserve(STATION_VECTOR_BLOCK_SIZE);
info->listings.reserve(LISTING_VECTOR_BLOCK_SIZE);
while (true) {
int ret = sqlite3_step(stmt);
if (ret == SQLITE_DONE) {
break;
} else if (ret != SQLITE_ROW) {
fprintf(stderr, "sqlite step error: %s\n", sqlite3_errmsg(conn));
return 1;
}
System system;
system.id = sqlite3_column_int64(stmt, 0);
system.index = info->systems.size();
system.x = sqlite3_column_double(stmt, 1);
system.y = sqlite3_column_double(stmt, 2);
system.z = sqlite3_column_double(stmt, 3);
info->systems.push_back(system);
if (info->systems.capacity() <= info->systems.size()) {
info->systems.reserve(info->systems.size() +
SYSTEM_VECTOR_BLOCK_SIZE);
}
}
err = sqlite3_finalize(stmt);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to finalize loadsystems sql: %s\n",
sqlite3_errmsg(conn));
return 1;
}
const char *stationloadSQL =
"SELECT station_id FROM station WHERE type_id != 24 AND type_id != 0 "
"AND system_id = ?;";
err = sqlite3_prepare_v2(conn, stationloadSQL, -1, &stmt, nullptr);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to compile stationLoad sql: %s\n",
sqlite3_errmsg(conn));
return 1;
}
for (auto &system : info->systems) {
sqlite3_reset(stmt);
sqlite3_bind_int64(stmt, 1, system.id);
// printf("systemID: %ld\n", system.id);
system.stationStartIndex = info->stations.size();
while (true) {
int ret = sqlite3_step(stmt);
if (ret == SQLITE_DONE) {
break;
} else if (ret != SQLITE_ROW) {
fprintf(stderr, "stationload sqlite step error: %s\n",
sqlite3_errmsg(conn));
return 1;
}
Station station;
station.systemID = system.id;
station.id = sqlite3_column_int64(stmt, 0);
info->stations.push_back(station);
if (info->stations.capacity() <= info->stations.size()) {
info->stations.reserve(info->stations.size() +
STATION_VECTOR_BLOCK_SIZE);
}
}
system.nStations = info->stations.size() - system.stationStartIndex;
}
sqlite3_finalize(stmt);
const char *listingLoadSQL =
"SELECT item_id,demand_price,demand_units,supply_price,supply_units "
"FROM "
"StationItem WHERE station_id=? ORDER BY item_id ASC;";
err = sqlite3_prepare_v2(conn, listingLoadSQL, -1, &stmt, nullptr);
if (err != SQLITE_OK) {
fprintf(stderr, "failed to compile listingLoadSQL sql: %s\n",
sqlite3_errmsg(conn));
return 1;
}
for (auto &station : info->stations) {
sqlite3_reset(stmt);
sqlite3_bind_int64(stmt, 1, station.id);
station.listingStartIndex = info->listings.size();
while (true) {
int ret = sqlite3_step(stmt);
if (ret == SQLITE_DONE) {
break;
} else if (ret != SQLITE_ROW) {
fprintf(stderr, "listingload sqlite step error: %s\n",
sqlite3_errmsg(conn));
return 1;
}
ItemPricing listing;
listing.itemID = sqlite3_column_int64(stmt, 0);
listing.demandPrice = sqlite3_column_int64(stmt, 1);
listing.demandQuantity = sqlite3_column_int64(stmt, 2);
listing.supplyPrice = sqlite3_column_int64(stmt, 3);
listing.supplyQuantity = sqlite3_column_int64(stmt, 4);
info->listings.push_back(listing);
if (info->listings.capacity() <= info->listings.size()) {
info->listings.reserve(info->listings.size() +
LISTING_VECTOR_BLOCK_SIZE);
}
}
station.nListings = info->listings.size() - station.listingStartIndex;
}
sqlite3_finalize(stmt);
return 0;
}
GrB_Vector TDB::loadItemTypes() {
std::vector<GrB_Index> items;
std::vector<int32_t> indices;
const char *querySQL = "select item_id from Item;";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(conn, querySQL, -1, &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "failed to compile loadItemTypes() query: %s\n",
sqlite3_errmsg(conn));
return GrB_NULL;
}
GrB_Vector vec;
int err;
int index = 0;
do {
err = sqlite3_step(stmt);
if (err == SQLITE_ROW) {
items.push_back(sqlite3_column_int64(stmt, 1));
indices.push_back(index++);
}
} while (err == SQLITE_ROW);
if (err != SQLITE_DONE) {
fprintf(stderr, "TDB::loadItemTypes sqlite3 error: %s\n",
sqlite3_errmsg(conn));
sqlite3_finalize(stmt);
return GrB_NULL;
}
GrB_Vector_new(&vec, GrB_INT32, items.back());
GrB_Vector_build_INT32(vec, items.data(), indices.data(), index, GrB_NULL);
sqlite3_finalize(stmt);
return vec;
}