1 /*********************************************************************************
2 * Copyright © 2015, highstreet technologies GmbH
5 * http://www.highstreet-technologies.com/
7 * The reproduction, transmission or use of this document or its contents is not
8 * permitted without express written authority. Offenders will be liable for
9 * damages. All rights, including rights created by patent grant or registration
10 * of a utility model or design, are reserved. Technical modifications possible.
11 * Technical specifications and features are binding only insofar as they are
12 * specifically and expressly agreed upon in a written contract.
14 * @author: Martin Skorupski [martin@skorupski.de]
15 *********************************************************************************/
16 package org.opendaylight.mwtn.base.database;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
22 import org.elasticsearch.index.query.QueryBuilder;
23 import org.elasticsearch.search.SearchHit;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Generic class to write lists of model classes to the database.
31 public class HtDataBaseReaderAndWriter<T extends IsEsObject> {
33 private static final Logger log = LoggerFactory.getLogger(HtDataBaseReaderAndWriter.class);
36 private final HtDataBase db;
37 private final String dataTypeName;
38 private final HtMapper<T> mapper;
41 * Class specific access to database
42 * @param db ES database descriptor
43 * @param dataTypeName datatype name
44 * @param clazz class of datatype
46 public HtDataBaseReaderAndWriter(HtDataBase db, String dataTypeName, Class<? extends T> clazz) {
49 this.dataTypeName = dataTypeName;
50 this.mapper = new HtMapper<>( clazz );
55 * Remove Object from database
56 * @param object Object with content
57 * @return true if remove is done
59 public boolean doRemove( T object) {
61 return db.doRemove(dataTypeName, object );
66 * Remove all data that match the filter
67 * @param query to specify data to be deleted
68 * @return number of removed objects
70 public int doRemoveByQuery(QueryBuilder query) {
72 int idx = 0; //Idx for getAll
73 int iterateLength = 100; //Step width for iterate
77 hits = db.doReadByQueryJsonData(idx, iterateLength, dataTypeName, query);
78 log.debug("Found: {} elements: {} Failures: {}",dataTypeName,hits.length, mapper.getMappingFailures());
82 for (SearchHit hit : hits) {
84 object = mapper.getObjectFromJson( hit.getSourceRef() );
86 log.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n Failures: {}", hit.getId(), hit.getSourceAsString(), object, mapper.getMappingFailures());
88 object.setEsId( hit.getId() );
91 log.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString());
94 } while (hits.length == iterateLength); //Do it until end indicated, because less hits than iterateLength allows.
102 * Write one object into Database
103 * @param object Object with content
104 * @return This object for chained call pattern.
106 public T doWrite( T object) {
108 log.debug("doWrite {} {}",object.getClass().getSimpleName(), object.getEsId());
110 String json = mapper.objectToJson(object);
112 String esId = db.doWrite(dataTypeName, object, json);
113 object.setEsId(esId);
114 log.debug("doWrite done for {} {}",object.getClass().getSimpleName(), object.getEsId());
115 return esId == null ? null : object;
117 log.warn("Can not map object and write to database. {}",object.getClass().getSimpleName());
124 * Write a list of Objects to the database.
125 * @param list Object list with content
126 * @return This object for chained call pattern.
128 public HtDataBaseReaderAndWriter<T> doWrite( Collection<T> list) {
131 String indexName = db.getNetworkIndex();
133 log.debug("Write to ES database {}, {} Class: {} {} elements",indexName,dataTypeName, mapper.getClazz().getSimpleName(), list.size());
135 if (indexName == null) {
136 throw new IllegalArgumentException("Missing Index");
139 if (list != null && !list.isEmpty()) {
141 if ( doWrite(s) == null ) {
142 if ( ++writeError > 5 ) {
143 log.warn("Leave because of to >5 write errors");
155 * Read one object via the object class specific ID
156 * @param object Object refrenced by idString
157 * @return The Object if found or null
159 public T doRead( IsEsObject object ) {
160 return mapper.getObjectFromJson( db.doReadJsonData( dataTypeName, object) );
164 * Read all existing objects of a type
165 * @return the list of all objects
167 public List<T> doReadAll() {
169 List<T> res = new ArrayList<>();
170 int idx = 0; //Idx for getAll
171 int iterateLength = 100; //Step width for iterate
177 hits = db.doReadAllJsonData(idx, iterateLength, dataTypeName);
178 log.debug("Read: {} elements: {} Failures: {}",dataTypeName,hits.length, mapper.getMappingFailures());
182 for (SearchHit hit : hits) {
184 object = mapper.getObjectFromJson( hit.getSourceRef() );
186 log.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n Failures: {}", hit.getId(), hit.getSourceAsString(), object, mapper.getMappingFailures());
187 if (object != null) {
188 object.setEsId( hit.getId() );
191 log.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString());
194 } while (hits.length == iterateLength); //Do it until end indicated, because less hits than iterateLength allows.