Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat database new #273

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,44 @@
import de.wuespace.telestion.api.message.JsonMessage;
import de.wuespace.telestion.api.config.Config;

/**
* Listener that collects all incoming data configured in listeningAddresses and redirects them to be saved to the
* MongoDatabaseService.
*/
public final class DataListener extends AbstractVerticle {
private final Configuration forcedConfig;
private Configuration config;

private final Logger logger = LoggerFactory.getLogger(DataListener.class);

private final String save = Address.incoming(DataService.class, "save");

public DataListener() {
this.forcedConfig = null;
}
private final String save = Address.incoming(MongoDatabaseService.class, "save");

/**
* This constructor supplies default options.
*
* @param listeningAddresses List of addresses that should be saved
*/
public DataListener(List<String> listeningAddresses) {
this.forcedConfig = new Configuration(listeningAddresses);
}

/**
* If this constructor is used, settings have to be specified in the config file.
*/
public DataListener() {
this.forcedConfig = null;
}

@Override
public void start(Promise<Void> startPromise) throws Exception {
config = Config.get(forcedConfig, config(), Configuration.class);
this.registerConsumers();
startPromise.complete();
}

/**
* Function to register consumers to the eventbus.
*/
private void registerConsumers() {
config.listeningAddresses().forEach(address -> {
vertx.eventBus().consumer(address, document -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import de.wuespace.telestion.api.message.JsonMessage;

public record DataRequest(
@JsonProperty String className,
@JsonProperty JsonObject query,
@JsonProperty String collection,
@JsonProperty String query,
@JsonProperty String operation,
@JsonProperty JsonObject operationParams) implements JsonMessage {
private DataRequest() {
this("", new JsonObject(), "", new JsonObject());
this("", "", "", new JsonObject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public DataService() {
/**
* This constructor supplies default options.
*
* @param dataTypes List of all full class names of the data types
* @param dataOperationMap Map of String->DataOperation for incoming dataRequests
*/
public DataService(Map<String, DataOperation> dataOperationMap) {
Expand Down Expand Up @@ -91,30 +90,24 @@ private void registerConsumers() {
* @param resultHandler Handles the request to the underlying database. Can be failed or succeeded.
*/
private void dataRequestDispatcher(DataRequest request, Handler<AsyncResult<JsonObject>> resultHandler) {
// TODO: If className is empty, check if query exists and just pass the query to the DatabaseClient
try {
var dataType = Class.forName(request.className());
if (request.operation().isEmpty()) {
this.fetchLatestData(dataType, request.query(), res -> {
if (res.failed()) {
resultHandler.handle(Future.failedFuture(res.cause().getMessage()));
return;
}
resultHandler.handle(Future.succeededFuture(res.result()));
});
} else {
var dataOperation = new DataOperation(new JsonObject(), request.operationParams());
this.fetchLatestData(dataType, request.query(), res -> {
if (res.failed()) {
resultHandler.handle(Future.failedFuture(res.cause().getMessage()));
return;
}
dataOperation.data().put("data", res.result());
});
this.applyManipulation(request.operation(), dataOperation, resultHandler);
}
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException: {}", e.getMessage());
if (request.operation().isEmpty()) {
this.fetchLatestData(request.collection(), request.query(), res -> {
if (res.failed()) {
resultHandler.handle(Future.failedFuture(res.cause().getMessage()));
return;
}
resultHandler.handle(Future.succeededFuture(res.result()));
});
} else {
var dataOperation = new DataOperation(new JsonObject(), request.operationParams());
this.fetchLatestData(request.collection(), request.query(), res -> {
if (res.failed()) {
resultHandler.handle(Future.failedFuture(res.cause().getMessage()));
return;
}
dataOperation.data().put("data", res.result());
});
this.applyManipulation(request.operation(), dataOperation, resultHandler);
}
}

Expand Down Expand Up @@ -142,13 +135,13 @@ private void requestResultHandler(
/**
* Method to fetch the latest data of a specified data type.
*
* @param dataType Determines which data type should be fetched.
* @param collection Determines from which collection data should be fetched.
* @param query MongoDB query, can be empty JsonObject if no specific query is needed.
* @param resultHandler Handles the request to the underlying database. Can be failed or succeeded.
*/
private void fetchLatestData(Class<?> dataType, JsonObject query,
private void fetchLatestData(String collection, String query,
Handler<AsyncResult<JsonObject>> resultHandler) {
DbRequest dbRequest = new DbRequest(dataType, query);
DbRequest dbRequest = new DbRequest(collection, query);
this.requestResultHandler(dbFind, dbRequest, resultHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,46 @@
import io.vertx.core.json.JsonObject;
import de.wuespace.telestion.api.message.JsonMessage;

import java.util.Collections;
import java.util.List;

/**
* Record to provide the structure of a database request.
*
* @param dataType class of the data type
* @param query MongoDb query looks like this: { key: value }
* with key meaning the name of the field in the document.
* IN condition: { key: { $in: ["value1", "value2", ...] }}
* AND condition: { key1: "value1", key2: { $lt: value } } with $lt meaning less than
* OR condition: { $or: [{ key1: "value1" }, { key2: { $gt: value2 }}] }
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-documents/">MongoDB manual</a> for more information
* @param collection String of the desired MongoDB collection.
* @param query MongoDB queries are written in JSON.
* This parameter is a String representation of JSONObject. "{ "key": "value" }"
* with key meaning the name of the field in the MongoDB document.
* IN condition: { key: { $in: ["value1", "value2", ...] }}
* AND condition: { key1: "value1", key2: { $lt: value } } with $lt meaning less than
* OR condition: { $or: [{ key1: "value1" }, { key2: { $gt: value2 }}] }
* @param fields List of key Strings in the collection limiting the fields that should be returned.
* @param sort List of key Strings that the returned data should be sorted by.
* @param limit Limits the amount of returned entries. -1 equals all entries found.
* @param skip Specifies how many entries should be skipped. 0 is default, meaning no entries are skipped.
* @param aggregate Field, that should be aggregated.
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-documents/">MongoDB manual</a> for more information.
*
* @author Jan Tischhöfer
* @version 07-05-2021
*/
public record DbRequest(
@JsonProperty Class<?> dataType,
@JsonProperty JsonObject query) implements JsonMessage {
private DbRequest() {
this(null, new JsonObject());
}
@JsonProperty String collection,
@JsonProperty String query,
@JsonProperty List<String> fields,
@JsonProperty List<String> sort,
@JsonProperty int limit,
@JsonProperty int skip,
@JsonProperty String aggregate) implements JsonMessage {
private DbRequest() {
this("", "", Collections.emptyList(), Collections.emptyList(), -1, 0, "");
}

public DbRequest(String collection) {
this(collection, "", Collections.emptyList(), Collections.emptyList(), -1, 0, "");
}

public DbRequest(String collection, String query) {
this(collection, query, Collections.emptyList(), Collections.emptyList(), -1, 0, "");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package de.wuespace.telestion.services.database;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.wuespace.telestion.api.message.JsonMessage;
import io.vertx.core.json.JsonObject;
import java.util.Collections;
import java.util.List;
import de.wuespace.telestion.api.message.JsonMessage;

public record DbResponse(
@JsonProperty Class<?> dataType,
@JsonProperty List<JsonObject> result) implements JsonMessage {
private DbResponse() {
this(null, Collections.emptyList());
this(Collections.emptyList());
}
}
Loading