[mongodb] Collection per Item (#10202)
Put Item in separate Collections Signed-off-by: Marc <iseli_marc@hotmail.com>
This commit is contained in:
@@ -11,6 +11,9 @@ This service can be configured in the file `services/mongodb.cfg`.
|
|||||||
| ---------- | ------- | :------: | ---------------------------------------------------------------------------- |
|
| ---------- | ------- | :------: | ---------------------------------------------------------------------------- |
|
||||||
| url | | Yes | connection URL to address MongoDB. For example, `mongodb://localhost:27017` |
|
| url | | Yes | connection URL to address MongoDB. For example, `mongodb://localhost:27017` |
|
||||||
| database | | Yes | database name |
|
| database | | Yes | database name |
|
||||||
| collection | | Yes | collection name |
|
| collection | | Yes | set collection to "" if it shall generate a collection per item |
|
||||||
|
|
||||||
All item and event related configuration is done in the file `persistence/mongodb.persist`.
|
If you have a username and password it looks like this: url = mongodb://[username]:[password]@[localhost]:27017/[database]
|
||||||
|
The database is required: http://mongodb.github.io/mongo-java-driver/3.9/javadoc/com/mongodb/MongoClientURI.html
|
||||||
|
|
||||||
|
All item and event related configuration is done in the file `persistence/mongodb.persist`.
|
||||||
@@ -87,6 +87,7 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
private @NonNullByDefault({}) String url;
|
private @NonNullByDefault({}) String url;
|
||||||
private @NonNullByDefault({}) String db;
|
private @NonNullByDefault({}) String db;
|
||||||
private @NonNullByDefault({}) String collection;
|
private @NonNullByDefault({}) String collection;
|
||||||
|
private boolean collectionPerItem;
|
||||||
|
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
|
|
||||||
@@ -117,9 +118,9 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
collection = (String) config.get("collection");
|
collection = (String) config.get("collection");
|
||||||
logger.debug("MongoDB collection {}", collection);
|
logger.debug("MongoDB collection {}", collection);
|
||||||
if (collection == null || collection.isBlank()) {
|
if (collection == null || collection.isBlank()) {
|
||||||
logger.warn(
|
collectionPerItem = false;
|
||||||
"The MongoDB database collection is missing - please configure the mongodb:collection parameter.");
|
} else {
|
||||||
return;
|
collectionPerItem = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectFromDatabase();
|
disconnectFromDatabase();
|
||||||
@@ -172,6 +173,12 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String realName = item.getName();
|
String realName = item.getName();
|
||||||
|
|
||||||
|
// If collection Per Item is active, connect to the item Collection
|
||||||
|
if (collectionPerItem) {
|
||||||
|
connectToCollection(realName);
|
||||||
|
}
|
||||||
|
|
||||||
String name = (alias != null) ? alias : realName;
|
String name = (alias != null) ? alias : realName;
|
||||||
Object value = this.convertValue(item.getState());
|
Object value = this.convertValue(item.getState());
|
||||||
|
|
||||||
@@ -183,6 +190,11 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
obj.put(FIELD_VALUE, value);
|
obj.put(FIELD_VALUE, value);
|
||||||
this.mongoCollection.save(obj);
|
this.mongoCollection.save(obj);
|
||||||
|
|
||||||
|
// If collection Per Item is active, disconnect after save.
|
||||||
|
if (collectionPerItem) {
|
||||||
|
disconnectFromCollection();
|
||||||
|
}
|
||||||
|
|
||||||
logger.debug("MongoDB save {}={}", name, value);
|
logger.debug("MongoDB save {}={}", name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,11 +241,14 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
try {
|
try {
|
||||||
logger.debug("Connect MongoDB");
|
logger.debug("Connect MongoDB");
|
||||||
this.cl = new MongoClient(new MongoClientURI(this.url));
|
this.cl = new MongoClient(new MongoClientURI(this.url));
|
||||||
mongoCollection = cl.getDB(this.db).getCollection(this.collection);
|
if (collectionPerItem) {
|
||||||
|
mongoCollection = cl.getDB(this.db).getCollection(this.collection);
|
||||||
|
|
||||||
|
BasicDBObject idx = new BasicDBObject();
|
||||||
|
idx.append(FIELD_TIMESTAMP, 1).append(FIELD_ITEM, 1);
|
||||||
|
this.mongoCollection.createIndex(idx);
|
||||||
|
}
|
||||||
|
|
||||||
BasicDBObject idx = new BasicDBObject();
|
|
||||||
idx.append(FIELD_TIMESTAMP, 1).append(FIELD_ITEM, 1);
|
|
||||||
this.mongoCollection.createIndex(idx);
|
|
||||||
logger.debug("Connect MongoDB ... done");
|
logger.debug("Connect MongoDB ... done");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Failed to connect to database {}", this.url);
|
logger.error("Failed to connect to database {}", this.url);
|
||||||
@@ -241,6 +256,29 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects to the Collection
|
||||||
|
*/
|
||||||
|
private void connectToCollection(String collectionName) {
|
||||||
|
try {
|
||||||
|
mongoCollection = cl.getDB(this.db).getCollection(collectionName);
|
||||||
|
|
||||||
|
BasicDBObject idx = new BasicDBObject();
|
||||||
|
idx.append(FIELD_TIMESTAMP, 1).append(FIELD_ITEM, 1);
|
||||||
|
this.mongoCollection.createIndex(idx);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to connect to collection {}", collectionName);
|
||||||
|
throw new RuntimeException("Cannot connect to collection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnects from the Collection
|
||||||
|
*/
|
||||||
|
private void disconnectFromCollection() {
|
||||||
|
this.mongoCollection = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnects from the database
|
* Disconnects from the database
|
||||||
*/
|
*/
|
||||||
@@ -267,6 +305,11 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String name = filter.getItemName();
|
String name = filter.getItemName();
|
||||||
|
|
||||||
|
// If collection Per Item is active, connect to the item Collection
|
||||||
|
if (collectionPerItem) {
|
||||||
|
connectToCollection(name);
|
||||||
|
}
|
||||||
Item item = getItem(name);
|
Item item = getItem(name);
|
||||||
|
|
||||||
List<HistoricItem> items = new ArrayList<>();
|
List<HistoricItem> items = new ArrayList<>();
|
||||||
@@ -315,6 +358,10 @@ public class MongoDBPersistenceService implements QueryablePersistenceService {
|
|||||||
ZonedDateTime.ofInstant(obj.getDate(FIELD_TIMESTAMP).toInstant(), ZoneId.systemDefault())));
|
ZonedDateTime.ofInstant(obj.getDate(FIELD_TIMESTAMP).toInstant(), ZoneId.systemDefault())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If collection Per Item is active, disconnect after save.
|
||||||
|
if (collectionPerItem) {
|
||||||
|
disconnectFromCollection();
|
||||||
|
}
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user