-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNakshaHub.java
296 lines (274 loc) · 12.8 KB
/
NakshaHub.java
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
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub;
import static com.here.naksha.lib.core.exceptions.UncheckedException.unchecked;
import static com.here.naksha.lib.core.models.PluginCache.getStorageConstructor;
import static com.here.naksha.lib.core.util.storage.RequestHelper.createFeatureRequest;
import static com.here.naksha.lib.core.util.storage.RequestHelper.createWriteCollectionsRequest;
import static com.here.naksha.lib.core.util.storage.RequestHelper.readFeaturesByIdRequest;
import static com.here.naksha.lib.core.util.storage.RequestHelper.readFeaturesByIdsRequest;
import static com.here.naksha.lib.core.util.storage.ResultHelper.readFeatureFromResult;
import com.here.naksha.lib.core.INaksha;
import com.here.naksha.lib.core.NakshaAdminCollection;
import com.here.naksha.lib.core.NakshaContext;
import com.here.naksha.lib.core.NakshaVersion;
import com.here.naksha.lib.core.exceptions.NoCursor;
import com.here.naksha.lib.core.lambdas.Fe1;
import com.here.naksha.lib.core.models.geojson.implementation.XyzFeature;
import com.here.naksha.lib.core.models.naksha.Storage;
import com.here.naksha.lib.core.models.storage.ErrorResult;
import com.here.naksha.lib.core.models.storage.IfConflict;
import com.here.naksha.lib.core.models.storage.IfExists;
import com.here.naksha.lib.core.models.storage.Result;
import com.here.naksha.lib.core.storage.IReadSession;
import com.here.naksha.lib.core.storage.IStorage;
import com.here.naksha.lib.core.storage.IWriteSession;
import com.here.naksha.lib.core.util.IoHelp;
import com.here.naksha.lib.core.util.json.Json;
import com.here.naksha.lib.core.util.storage.ResultHelper;
import com.here.naksha.lib.core.view.ViewDeserialize;
import com.here.naksha.lib.hub.storages.NHAdminStorage;
import com.here.naksha.lib.hub.storages.NHSpaceStorage;
import com.here.naksha.lib.psql.PsqlStorage;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NakshaHub implements INaksha {
/**
* The id of default NakshaHub Config feature object
*/
public static final @NotNull String DEF_CFG_ID = "default-config";
private static final @NotNull Logger logger = LoggerFactory.getLogger(NakshaHub.class);
/**
* The NakshaHub config.
*/
protected final @NotNull NakshaHubConfig nakshaHubConfig;
/**
* Singleton instance of physical admin storage implementation
*/
protected final @NotNull IStorage psqlStorage;
/**
* Singleton instance of AdminStorage, which internally uses physical admin storage (i.e. PsqlStorage)
*/
protected final @NotNull IStorage adminStorageInstance;
/**
* Singleton instance of Space Storage, which is responsible to manage admin collections as spaces and support respective read/write
* operations on spaces
*/
protected final @NotNull IStorage spaceStorageInstance;
@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public NakshaHub(
final @NotNull String appName,
final @NotNull String storageUrl,
final @Nullable NakshaHubConfig customCfg,
final @Nullable String configId) {
// create storage instance upfront
this.psqlStorage = new PsqlStorage("naksha-admin-db", appName, storageUrl);
this.adminStorageInstance = new NHAdminStorage(this.psqlStorage);
this.spaceStorageInstance = new NHSpaceStorage(this, new NakshaEventPipelineFactory(this));
// setup backend storage DB and Hub config
final NakshaHubConfig finalCfg = this.storageSetup(customCfg, configId);
if (finalCfg == null) {
throw new RuntimeException("Server configuration not found! Neither in Admin storage nor a default file.");
}
this.nakshaHubConfig = finalCfg;
}
private @Nullable NakshaHubConfig storageSetup(
final @Nullable NakshaHubConfig customCfg, final @Nullable String configId) {
/**
* 1. Init Admin Storage
* 2. Create all Admin collections
* 3. run maintenance during startup to ensure history partitions are available
* 4. fetch / add latest config (ordered preference DB,Custom,Default)
*/
// 1. Init Admin Storage
if (customCfg != null && customCfg.storageParams != null) {
getAdminStorage().initStorage(customCfg.storageParams);
} else {
getAdminStorage().initStorage();
}
// 2. Create all Admin collections in Admin DB
final NakshaContext nakshaContext = new NakshaContext().withAppId(NakshaHubConfig.defaultAppName());
nakshaContext.attachToCurrentThread();
try (final IWriteSession admin = getAdminStorage().newWriteSession(nakshaContext, true)) {
final Result wrResult = admin.execute(createWriteCollectionsRequest(NakshaAdminCollection.ALL));
if (wrResult == null) {
admin.rollback(true);
throw unchecked(new Exception("Unable to create Admin collections in Admin DB. Null result!"));
} else if (wrResult instanceof ErrorResult er) {
admin.rollback(true);
throw unchecked(new Exception(
"Unable to create Admin collections in Admin DB. " + er.toString(), er.exception));
}
admin.commit(true);
} // close Admin DB connection
// 3. run one-time maintenance on Admin DB to ensure history partitions are available
getAdminStorage().maintainNow();
// TODO HP : This step to be removed later (once we have ability to add custom storages)
// fetch / add default storage implementation
try (final IWriteSession admin = getAdminStorage().newWriteSession(nakshaContext, true)) {
Storage defStorage = null;
try (final Json json = Json.get()) {
final String storageJson = IoHelp.readResource("config/default-storage.json");
defStorage = json.reader(ViewDeserialize.Storage.class)
.forType(Storage.class)
.readValue(storageJson);
} catch (Exception e) {
throw unchecked(new Exception("Unable to read default Storage file. " + e.getMessage(), e));
}
// persist in Admin DB (if not already exists)
final Result wrResult =
admin.execute(createFeatureRequest(NakshaAdminCollection.STORAGES, defStorage, true));
if (wrResult == null) {
admin.rollback(true);
throw unchecked(new Exception("Unable to add default storage in Admin DB. Null result!"));
} else if (wrResult instanceof ErrorResult er) {
admin.rollback(true);
throw unchecked(
new Exception("Unable to add default storage in Admin DB. " + er.toString(), er.exception));
}
admin.commit(true);
} // close Admin DB connection
// 4. fetch / add latest config
return configSetup(nakshaContext, customCfg, configId);
}
private @Nullable NakshaHubConfig configSetup(
final @NotNull NakshaContext nakshaContext,
final @Nullable NakshaHubConfig customCfg,
final @Nullable String configId) {
/*
* Config preference, for a given configId (e.g. "custom-config"):
* 1. Custom config - If provided, persist the same in DB, and use the same for NakshaHub
* 2. DB custom config - If Database already has custom config (e.g. "custom-config"), use the same
* 3. DB default config - If Database has default config - "default-config", use the same
* 3. Default config - Fallback to default config from file - "default-config"
*/
try (final IWriteSession admin = getAdminStorage().newWriteSession(nakshaContext, true)) {
if (customCfg != null) {
// Custom config provided. Persist in AdminDB.
final Result wrResult = admin.execute(createFeatureRequest(
NakshaAdminCollection.CONFIGS, customCfg, IfExists.REPLACE, IfConflict.REPLACE));
if (wrResult == null) {
admin.rollback(true);
throw unchecked(new Exception("Unable to add custom config in Admin DB. Null result!"));
} else if (wrResult instanceof ErrorResult er) {
admin.rollback(true);
throw unchecked(
new Exception("Unable to add custom config in Admin DB. " + er.toString(), er.exception));
}
admin.commit(true);
return customCfg;
}
// load custom + default config from DB (if available)
NakshaHubConfig customDbCfg = null, defDbCfg = null;
final List<String> cfgIdList = (configId != null) ? List.of(configId, DEF_CFG_ID) : List.of(DEF_CFG_ID);
final Result rdResult = admin.execute(readFeaturesByIdsRequest(NakshaAdminCollection.CONFIGS, cfgIdList));
if (rdResult instanceof ErrorResult er) {
throw unchecked(new Exception(
"Unable to read custom/default config from Admin DB. " + er.toString(), er.exception));
} else {
try {
List<NakshaHubConfig> nakshaHubConfigs =
ResultHelper.readFeaturesFromResult(rdResult, NakshaHubConfig.class);
for (final NakshaHubConfig cfg : nakshaHubConfigs) {
if (cfg.getId().equals(configId)) {
customDbCfg = cfg;
}
if (cfg.getId().equals(DEF_CFG_ID)) {
defDbCfg = cfg;
}
}
if (customDbCfg != null) {
return customDbCfg; // return custom config from DB
} else if (defDbCfg != null) {
return defDbCfg; // return default config from DB
}
} catch (NoCursor | NoSuchElementException er) {
throw unchecked(new Exception(
"Unable to read custom/default config from Admin DB - ResultCursor has no data"));
}
}
// load default config from file (as DB didn't have custom/default config)
NakshaHubConfig defCfg = null;
try (final Json json = Json.get()) {
final String configJson = IoHelp.readResource("config/" + DEF_CFG_ID + ".json");
defCfg = json.reader(ViewDeserialize.Storage.class)
.forType(NakshaHubConfig.class)
.readValue(configJson);
defCfg.setId(DEF_CFG_ID); // overwrite Id to desired value
} catch (Exception e) {
throw unchecked(new Exception("Unable to read default Config file. " + e.getMessage(), e));
}
// Persist default config in Admin DB
final Result wrResult = admin.execute(createFeatureRequest(NakshaAdminCollection.CONFIGS, defCfg, true));
if (wrResult == null) {
admin.rollback(true);
throw unchecked(new Exception("Unable to add default config in Admin DB. Null result!"));
} else if (wrResult instanceof ErrorResult er) {
admin.rollback(true);
throw unchecked(
new Exception("Unable to add default config in Admin DB. " + er.toString(), er.exception));
}
admin.commit(true);
return defCfg; // return default config obtained from file
}
}
@Override
@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public @NotNull <T extends XyzFeature> T getConfig() {
return (T) this.nakshaHubConfig;
}
@Override
@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public @NotNull IStorage getAdminStorage() {
return this.adminStorageInstance;
}
@Override
@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public @NotNull IStorage getSpaceStorage() {
return this.spaceStorageInstance;
}
@Override
@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public @NotNull IStorage getStorageById(final @NotNull String storageId) {
try (final IReadSession reader = getAdminStorage().newReadSession(NakshaContext.currentContext(), false)) {
final Result result = reader.execute(readFeaturesByIdRequest(NakshaAdminCollection.STORAGES, storageId));
if (result instanceof ErrorResult er) {
throw unchecked(new Exception(
"Exception fetching storage details for id " + storageId + ". " + er.message, er.exception));
}
final Storage storage = Objects.requireNonNull(
readFeatureFromResult(result, Storage.class), "No storage found with id: " + storageId);
return storageInstance(storage);
}
}
private IStorage storageInstance(@NotNull Storage storage) {
Fe1<IStorage, Storage> constructor = getStorageConstructor(storage.getClassName(), Storage.class);
try {
return constructor.call(storage);
} catch (Exception e) {
throw unchecked(e);
}
}
}