/
DataExplorerSchemaManagement.java
196 lines (169 loc) · 6.65 KB
/
DataExplorerSchemaManagement.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/
package org.apache.streampipes.dataexplorer;
import org.apache.streampipes.dataexplorer.api.IDataExplorerSchemaManagement;
import org.apache.streampipes.dataexplorer.utils.DataExplorerUtils;
import org.apache.streampipes.model.datalake.DataLakeMeasure;
import org.apache.streampipes.model.datalake.DataLakeMeasureSchemaUpdateStrategy;
import org.apache.streampipes.model.schema.EventProperty;
import org.apache.streampipes.storage.api.IDataLakeStorage;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DataExplorerSchemaManagement implements IDataExplorerSchemaManagement {
IDataLakeStorage dataLakeStorage;
public DataExplorerSchemaManagement(IDataLakeStorage dataLakeStorage) {
this.dataLakeStorage = dataLakeStorage;
}
@Override
public List<DataLakeMeasure> getAllMeasurements() {
return DataExplorerUtils.getInfos();
}
@Override
public DataLakeMeasure getById(String elementId) {
return dataLakeStorage.findOne(elementId);
}
/**
* For new measurements an entry is generated in the database. For existing measurements the schema is updated
* according to the update strategy defined by the measurement.
*/
@Override
public DataLakeMeasure createOrUpdateMeasurement(DataLakeMeasure measure) {
setDefaultUpdateStrategyIfNoneProvided(measure);
var existingMeasure = getExistingMeasureByName(measure.getMeasureName());
if (existingMeasure.isEmpty()) {
setSchemaVersionAndStoreMeasurement(measure);
} else {
handleExistingMeasurement(measure, existingMeasure.get());
}
return measure;
}
/**
* Distinguishes between the update strategy for existing measurements
*/
private void handleExistingMeasurement(
DataLakeMeasure measure,
DataLakeMeasure existingMeasure
) {
measure.setElementId(existingMeasure.getElementId());
if (DataLakeMeasureSchemaUpdateStrategy.UPDATE_SCHEMA.equals(measure.getSchemaUpdateStrategy())) {
// For the update schema strategy the old schema is overwritten with the new one
updateMeasurement(measure);
} else {
// For the extent existing schema strategy the old schema is merged with the new one
unifyEventSchemaAndUpdateMeasure(measure, existingMeasure);
}
}
/**
* Returns the existing measure that has the provided measure name
*/
private Optional<DataLakeMeasure> getExistingMeasureByName(String measureName) {
return dataLakeStorage.getAllDataLakeMeasures()
.stream()
.filter(m -> m.getMeasureName()
.equals(measureName))
.findFirst();
}
private static void setDefaultUpdateStrategyIfNoneProvided(DataLakeMeasure measure) {
if (measure.getSchemaUpdateStrategy() == null) {
measure.setSchemaUpdateStrategy(DataLakeMeasureSchemaUpdateStrategy.UPDATE_SCHEMA);
}
}
@Override
public void deleteMeasurement(String elementId) {
if (dataLakeStorage.findOne(elementId) != null) {
dataLakeStorage.deleteDataLakeMeasure(elementId);
} else {
throw new IllegalArgumentException("Could not find measure with this ID");
}
}
@Override
public boolean deleteMeasurementByName(String measureName) {
var measureToDeleteOpt = dataLakeStorage.getAllDataLakeMeasures()
.stream()
.filter(measurement -> measurement.getMeasureName()
.equals(measureName))
.findFirst();
return measureToDeleteOpt.map(measure -> {
dataLakeStorage.deleteDataLakeMeasure(measure.getElementId());
return true;
}
).orElse(false);
}
@Override
public void updateMeasurement(DataLakeMeasure measure) {
var existingMeasure = dataLakeStorage.findOne(measure.getElementId());
if (existingMeasure != null) {
measure.setRev(existingMeasure.getRev());
dataLakeStorage.updateDataLakeMeasure(measure);
} else {
dataLakeStorage.storeDataLakeMeasure(measure);
}
}
private void setSchemaVersionAndStoreMeasurement(DataLakeMeasure measure) {
measure.setSchemaVersion(DataLakeMeasure.CURRENT_SCHEMA_VERSION);
dataLakeStorage.storeDataLakeMeasure(measure);
}
/**
* First the event schemas of the measurements are merged and then the measure is updated in the database
*/
private void unifyEventSchemaAndUpdateMeasure(
DataLakeMeasure measure,
DataLakeMeasure existingMeasure
) {
var properties = getUnifiedEventProperties(
existingMeasure,
measure
);
measure
.getEventSchema()
.setEventProperties(properties);
updateMeasurement(measure);
}
/**
* Returns the union of the unique event properties of the two measures.
* They are unique by runtime name.
*/
private List<EventProperty> getUnifiedEventProperties(
DataLakeMeasure measure1,
DataLakeMeasure measure2
) {
// Combine the event properties from both measures into a single Stream
var allMeasurementProperties = Stream.concat(
measure1.getEventSchema()
.getEventProperties()
.stream(),
measure2.getEventSchema()
.getEventProperties()
.stream()
);
// Filter event properties by removing duplicate runtime names
// If there are duplicate keys, choose the first occurrence
var unifiedEventProperties = allMeasurementProperties
.collect(Collectors.toMap(
EventProperty::getRuntimeName,
Function.identity(),
(eventProperty, eventProperty2) -> eventProperty
))
.values();
return new ArrayList<>(unifiedEventProperties);
}
}