-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdatabase.service.ts
336 lines (304 loc) · 13.6 KB
/
database.service.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { Injectable } from "@angular/core";
import { debounceTime } from "rxjs/operators";
import { decode } from "base64-arraybuffer";
import { SQLite, SQLiteDatabaseConfig, SQLiteObject } from "@ionic-native/sqlite/ngx";
import Dexie from "dexie";
import deepmerge from "deepmerge";
import * as maplibregl from "maplibre-gl";
import * as pako from "pako";
import { LoggingService } from "./logging.service";
import { RunningContextService } from "./running-context.service";
import { ToastService } from "./toast.service";
import { ResourcesService } from "./resources.service";
import { initialState } from "../reducers/initial-state";
import { NgRedux, classToActionMiddleware } from "../reducers/infra/ng-redux.module";
import { rootReducer } from "../reducers/root.reducer";
import type { ApplicationState, ShareUrl, Trace } from "../models/models";
export type ImageUrlAndData = {
imageUrl: string;
data: string;
}
@Injectable()
export class DatabaseService {
private static readonly STATE_DB_NAME = "State";
private static readonly STATE_TABLE_NAME = "state";
private static readonly STATE_DOC_ID = "state";
private static readonly POIS_DB_NAME = "PointsOfInterest";
private static readonly POIS_TABLE_NAME = "pois";
private static readonly POIS_UPLOAD_QUEUE_TABLE_NAME = "uploadQueue";
private static readonly POIS_ID_COLUMN = "properties.poiId";
private static readonly POIS_LOCATION_COLUMN = "[properties.poiGeolocation.lat+properties.poiGeolocation.lon]";
private static readonly IMAGES_DB_NAME = "Images";
private static readonly IMAGES_TABLE_NAME = "images";
private static readonly SHARE_URLS_DB_NAME = "ShareUrls";
private static readonly SHARE_URLS_TABLE_NAME = "shareUrls";
private static readonly TRACES_DB_NAME = "Traces";
private static readonly TRACES_TABLE_NAME = "traces";
private stateDatabase: Dexie;
// HM TODO: only for cordova?
private poisDatabase: Dexie;
private imagesDatabase: Dexie;
private shareUrlsDatabase: Dexie;
private tracesDatabase: Dexie;
private sourceDatabases: Map<string, SQLiteObject>;
private updating: boolean;
constructor(private readonly loggingService: LoggingService,
private readonly runningContext: RunningContextService,
private readonly sqlite: SQLite,
private readonly toastService: ToastService,
private readonly resources: ResourcesService,
private readonly ngRedux: NgRedux<ApplicationState>) {
this.updating = false;
this.sourceDatabases = new Map<string, SQLiteObject>();
}
public async initialize() {
this.stateDatabase = new Dexie(DatabaseService.STATE_DB_NAME);
this.stateDatabase.version(1).stores({
state: "id"
});
this.poisDatabase = new Dexie(DatabaseService.POIS_DB_NAME);
this.poisDatabase.version(1).stores({
pois: DatabaseService.POIS_ID_COLUMN + "," + DatabaseService.POIS_LOCATION_COLUMN,
});
this.poisDatabase.version(2).stores({
uploadQueue: DatabaseService.POIS_ID_COLUMN
});
this.imagesDatabase = new Dexie(DatabaseService.IMAGES_DB_NAME);
this.imagesDatabase.version(1).stores({
images: "imageUrl"
});
this.shareUrlsDatabase = new Dexie(DatabaseService.SHARE_URLS_DB_NAME);
this.shareUrlsDatabase.version(1).stores({
shareUrls: "id"
});
this.tracesDatabase = new Dexie(DatabaseService.TRACES_DB_NAME);
this.tracesDatabase.version(1).stores({
traces: "id",
});
this.initCustomTileLoadFunction();
if (this.runningContext.isIFrame) {
this.ngRedux.configureStore(rootReducer, initialState, [classToActionMiddleware]);
return;
}
let storedState = initialState;
let dbState = await this.stateDatabase.table(DatabaseService.STATE_TABLE_NAME).get(DatabaseService.STATE_DOC_ID);
if (dbState != null) {
storedState = this.initialStateUpgrade(dbState.state);
} else {
// initial load ever:
if (this.runningContext.isCordova) {
initialState.gpsState.tracking = "tracking";
}
this.updateState(initialState);
}
if (storedState.offlineState.lastModifiedDate !== null) {
if (await Dexie.exists("IHM")) {
await Dexie.delete("IHM");
await Dexie.delete("Contour");
await Dexie.delete("TerrainRGB");
storedState.offlineState.lastModifiedDate = null;
this.toastService.confirm({ type: "Ok", message: this.resources.databaseUpgrade });
}
}
this.ngRedux.configureStore(rootReducer, storedState, [classToActionMiddleware]);
this.ngRedux.select().pipe(debounceTime(2000)).subscribe((state: any) => {
this.updateState(state);
});
}
private initCustomTileLoadFunction() {
maplibregl.addProtocol("custom", (params, callback) => {
this.getTile(params.url).then((tileBuffer) => {
if (tileBuffer) {
callback(null, tileBuffer, null, null);
} else {
let message = `Tile is not in DB: ${params.url}`;
this.loggingService.debug(message);
callback(new Error(message));
}
});
return { cancel: () => { } };
});
}
public async uninitialize() {
let finalState = this.ngRedux.getState();
// reduce database size and memory footprint
finalState.routes.past = [];
finalState.routes.future = [];
finalState.poiState.selectedPointOfInterest = null;
finalState.poiState.isSidebarOpen = false;
await this.updateState(finalState);
for (let dbKey of this.sourceDatabases.keys()) {
await this.closeDatabase(dbKey);
}
}
public async closeDatabase(dbKey: string) {
this.loggingService.info("[Database] Closing database: " + dbKey);
let db = this.sourceDatabases.get(dbKey);
if (db != null) {
await db.close();
this.sourceDatabases.delete(dbKey);
} else if (this.sourceDatabases.keys.length > 0) {
this.loggingService.warning("Unable to close database: " + dbKey);
}
}
private async updateState(state: ApplicationState) {
if (this.updating) {
return;
}
this.updating = true;
try {
await this.stateDatabase.table(DatabaseService.STATE_TABLE_NAME).put({
id: DatabaseService.STATE_DOC_ID,
state
});
} catch (ex) {
this.loggingService.warning("[Database] Unable to store the state: " + ex.message);
} finally {
this.updating = false;
}
}
private getSourceNameFromUrl(url: string) {
return url.replace("custom://", "").split("/")[0];
}
public async getTile(url: string): Promise<ArrayBuffer> {
let splitUrl = url.split("/");
let dbName = this.getSourceNameFromUrl(url);
let z = +splitUrl[splitUrl.length - 3];
let x = +splitUrl[splitUrl.length - 2];
let y = +(splitUrl[splitUrl.length - 1].split(".")[0]);
return this.getTileFromDatabase(dbName, z, x, y);
}
private async getTileFromDatabase(dbName: string, z: number, x: number, y: number): Promise<ArrayBuffer> {
let db = await this.getDatabase(dbName);
let params = [
z,
x,
Math.pow(2, z) - y - 1
];
return new Promise<ArrayBuffer>((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql("SELECT HEX(tile_data) as tile_data_hex FROM tiles " +
"WHERE zoom_level = ? AND tile_column = ? AND tile_row = ? limit 1",
params,
(_: any, res: any) => {
if (res.rows.length !== 1) {
reject(new Error("No tile..."));
return;
}
const hexData = res.rows.item(0).tile_data_hex;
let binData = new Uint8Array(hexData.match(/.{1,2}/g).map((byte: string) => parseInt(byte, 16)));
let isGzipped = binData[0] === 0x1f && binData[1] === 0x8b;
if (isGzipped) {
binData = pako.inflate(binData);
}
resolve(binData.buffer);
},
(error: Error) => {
reject(error);
}
);
});
});
}
private async getDatabase(dbName: string): Promise<SQLiteObject> {
if (!this.sourceDatabases.has(dbName)) {
let config: SQLiteDatabaseConfig = {
createFromLocation: 1,
name: dbName + ".mbtiles"
};
if (this.runningContext.isIos) {
config.iosDatabaseLocation = "Documents";
} else {
config.location = "default";
(config as any).androidDatabaseProvider = "system";
}
let db = await this.sqlite.create(config);
this.sourceDatabases.set(dbName, db);
}
return this.sourceDatabases.get(dbName);
}
public storePois(pois: GeoJSON.Feature[]): Promise<any> {
return this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).bulkPut(pois);
}
public deletePois(poiIds: string[]): Promise<void> {
return this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).bulkDelete(poiIds);
}
public async getPoisForClustering(): Promise<GeoJSON.Feature<GeoJSON.Point>[]> {
this.loggingService.debug("[Database] Startting getting pois for clustering in chunks");
let features = [] as GeoJSON.Feature<GeoJSON.Point>[];
let index = 0;
let size = 2000;
let currentFeatures = [];
do {
currentFeatures = await this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).offset(index * size).limit(size).toArray();
features = features.concat(currentFeatures);
index++;
} while (currentFeatures.length !== 0);
this.loggingService.debug("[Database] Finished getting pois for clustering in chunks: " + features.length);
let pointFeatures = features.map((feature: GeoJSON.Feature) => {
let geoLocation = feature.properties.poiGeolocation;
let pointFeature = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [parseFloat(geoLocation.lon), parseFloat(geoLocation.lat)]
},
properties: feature.properties
} as GeoJSON.Feature<GeoJSON.Point>;
return pointFeature;
});
return pointFeatures;
}
public getPoiById(id: string): Promise<GeoJSON.Feature> {
return this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).get(id);
}
public addPoiToUploadQueue(feature: GeoJSON.Feature): Promise<any> {
return this.poisDatabase.table(DatabaseService.POIS_UPLOAD_QUEUE_TABLE_NAME).put(feature);
}
public getPoiFromUploadQueue(featureId: string): Promise<GeoJSON.Feature> {
return this.poisDatabase.table(DatabaseService.POIS_UPLOAD_QUEUE_TABLE_NAME).get(featureId);
}
public removePoiFromUploadQueue(featureId: string): Promise<void> {
return this.poisDatabase.table(DatabaseService.POIS_UPLOAD_QUEUE_TABLE_NAME).delete(featureId);
}
public storeImages(images: ImageUrlAndData[]): Promise<any> {
return this.imagesDatabase.table(DatabaseService.IMAGES_TABLE_NAME).bulkPut(images);
}
public async getImageByUrl(imageUrl: string): Promise<string> {
let imageAndData = await this.imagesDatabase.table(DatabaseService.IMAGES_TABLE_NAME).get(imageUrl) as ImageUrlAndData;
if (imageAndData != null) {
return imageAndData.data;
}
return null;
}
public storeShareUrl(shareUrl: ShareUrl): Promise<any> {
return this.shareUrlsDatabase.table(DatabaseService.SHARE_URLS_TABLE_NAME).put(shareUrl);
}
public getShareUrlById(id: string): Promise<ShareUrl> {
return this.shareUrlsDatabase.table(DatabaseService.SHARE_URLS_TABLE_NAME).get(id);
}
public deleteShareUrlById(id: string): Promise<void> {
return this.shareUrlsDatabase.table(DatabaseService.SHARE_URLS_TABLE_NAME).delete(id);
}
public storeTrace(trace: Trace): Promise<any> {
return this.tracesDatabase.table(DatabaseService.TRACES_TABLE_NAME).put(trace);
}
public getTraceById(id: string): Promise<Trace> {
return this.tracesDatabase.table(DatabaseService.TRACES_TABLE_NAME).get(id);
}
public deleteTraceById(id: string): Promise<void> {
return this.tracesDatabase.table(DatabaseService.TRACES_TABLE_NAME).delete(id);
}
private initialStateUpgrade(dbState: ApplicationState): ApplicationState {
let storedState = deepmerge(initialState, dbState, {
arrayMerge: (destinationArray, sourceArray) => sourceArray == null ? destinationArray : sourceArray
});
storedState.inMemoryState = initialState.inMemoryState;
if (!this.runningContext.isCordova) {
storedState.routes = initialState.routes;
storedState.poiState = initialState.poiState;
storedState.gpsState = initialState.gpsState;
}
return storedState;
}
}