-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdao_based_solr_config.java
More file actions
542 lines (440 loc) · 20.8 KB
/
dao_based_solr_config.java
File metadata and controls
542 lines (440 loc) · 20.8 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Enhanced Configuration Models for DAO-based data fetching
@Data
public class CollectionUpdateConfig {
private String collectionName;
private IdentifierConfig identifier;
private List<FieldMapping> fieldMappings = new ArrayList<>();
private List<DataSourceConfig> dataSources = new ArrayList<>();
private Map<String, Object> defaultValues = new HashMap<>();
private boolean validateBeforeUpdate = true;
private int batchSize = 1000;
}
@Data
public class DataSourceConfig {
private String name; // Unique name for this data source
private String type; // "DAO", "SERVICE", "REPOSITORY"
private String beanName; // Spring bean name
private String methodName; // Method to call
private List<String> inputFields; // Fields from trigger data needed as input
private String resultType; // "SINGLE", "LIST", "MAP"
private String resultPath; // Path to extract data from result (e.g., "data.records")
private Map<String, String> staticParams = new HashMap<>(); // Static parameters
private boolean required = true; // Fail if this data source fails
private int retryCount = 0; // Number of retries on failure
private long cacheTimeoutMs = 0; // Cache timeout (0 = no cache)
}
@Data
public class FieldMapping {
private String solrField; // Target Solr field
private String dataSource; // Which data source to use
private String sourcePath; // Path in the data source result (e.g., "user.email")
private String dataType = "STRING"; // Data type conversion
private boolean required = false;
private String defaultValue;
private List<String> allowedValues;
private String transformFunction;
// For composite fields
private List<String> sourcePaths; // Multiple paths for composite fields
private String separator = "_";
private String combineFunction;
// For direct value mapping (not from data source)
private String staticValue; // Static value assignment
private String triggerField; // Field from trigger data (CSV/JSON input)
}
@Data
public class IdentifierConfig {
private List<FieldMapping> identifierFields = new ArrayList<>();
private String compositeStrategy = "COMBINE"; // "COMBINE", "HASH", "CUSTOM"
private String compositeFunction; // Custom function for combining identifiers
}
// Updated Service for DAO-based processing
@Service
@Slf4j
public class DaoBasedPartialUpdateService {
private final SolrUpdateConfiguration config;
private final SolrClient solrClient;
private final ApplicationContext applicationContext;
private final DataSourceExecutor dataSourceExecutor;
private final FieldTransformationService transformationService;
private final ResultCacheService cacheService;
public DaoBasedPartialUpdateService(
SolrUpdateConfiguration config,
SolrClient solrClient,
ApplicationContext applicationContext,
DataSourceExecutor dataSourceExecutor,
FieldTransformationService transformationService,
ResultCacheService cacheService) {
this.config = config;
this.solrClient = solrClient;
this.applicationContext = applicationContext;
this.dataSourceExecutor = dataSourceExecutor;
this.transformationService = transformationService;
this.cacheService = cacheService;
}
public void processPartialUpdate(String collectionName, List<Map<String, Object>> triggerData) {
try {
CollectionUpdateConfig collectionConfig = getCollectionConfig(collectionName);
// Process in batches
for (int i = 0; i < triggerData.size(); i += collectionConfig.getBatchSize()) {
int endIndex = Math.min(i + collectionConfig.getBatchSize(), triggerData.size());
List<Map<String, Object>> batch = triggerData.subList(i, endIndex);
processBatch(collectionConfig, batch);
}
solrClient.commit(collectionConfig.getCollectionName());
log.info("Successfully processed {} records for collection {}",
triggerData.size(), collectionName);
} catch (Exception e) {
log.error("Error processing DAO-based update for collection: {}", collectionName, e);
throw new RuntimeException("DAO-based update failed", e);
}
}
private void processBatch(CollectionUpdateConfig config, List<Map<String, Object>> batch)
throws Exception {
Collection<SolrInputDocument> documents = new ArrayList<>();
for (Map<String, Object> triggerRecord : batch) {
try {
// Fetch all required data from configured data sources
Map<String, Object> aggregatedData = fetchDataFromSources(config, triggerRecord);
// Create Solr update document
SolrInputDocument updateDoc = createUpdateDocument(config, triggerRecord, aggregatedData);
if (updateDoc != null) {
documents.add(updateDoc);
}
} catch (Exception e) {
log.warn("Failed to process record: {}, Error: {}", triggerRecord, e.getMessage());
if (config.isValidateBeforeUpdate()) {
throw e; // Fail fast if validation is enabled
}
}
}
if (!documents.isEmpty()) {
solrClient.add(config.getCollectionName(), documents);
}
}
private Map<String, Object> fetchDataFromSources(CollectionUpdateConfig config,
Map<String, Object> triggerRecord) throws Exception {
Map<String, Object> aggregatedData = new HashMap<>();
aggregatedData.put("trigger", triggerRecord); // Include original trigger data
for (DataSourceConfig dataSource : config.getDataSources()) {
try {
Object result = dataSourceExecutor.executeDataSource(dataSource, triggerRecord);
aggregatedData.put(dataSource.getName(), result);
log.debug("Data source '{}' returned: {}", dataSource.getName(),
result != null ? result.getClass().getSimpleName() : "null");
} catch (Exception e) {
log.error("Failed to fetch data from source: {}", dataSource.getName(), e);
if (dataSource.isRequired()) {
throw new RuntimeException("Required data source failed: " + dataSource.getName(), e);
}
// Add null for optional data sources
aggregatedData.put(dataSource.getName(), null);
}
}
return aggregatedData;
}
private SolrInputDocument createUpdateDocument(CollectionUpdateConfig config,
Map<String, Object> triggerRecord, Map<String, Object> aggregatedData) throws Exception {
SolrInputDocument updateDoc = new SolrInputDocument();
// Add identifier fields
addIdentifierFields(updateDoc, config.getIdentifier(), aggregatedData);
// Process field mappings
for (FieldMapping mapping : config.getFieldMappings()) {
processFieldMapping(updateDoc, mapping, aggregatedData);
}
// Add default values
addDefaultValues(updateDoc, config.getDefaultValues());
return updateDoc;
}
private void addIdentifierFields(SolrInputDocument doc, IdentifierConfig identifier,
Map<String, Object> aggregatedData) throws Exception {
List<String> identifierParts = new ArrayList<>();
for (FieldMapping idField : identifier.getIdentifierFields()) {
Object value = extractValue(idField, aggregatedData);
if (value == null) {
throw new IllegalArgumentException("Required identifier field missing: " + idField.getSolrField());
}
identifierParts.add(value.toString());
}
// Combine identifier parts
String compositeId;
if (identifier.getCompositeFunction() != null) {
compositeId = transformationService.combineIdentifierParts(
identifier.getCompositeFunction(), identifierParts);
} else {
compositeId = String.join("_", identifierParts);
}
doc.addField("id", compositeId);
}
private void processFieldMapping(SolrInputDocument doc, FieldMapping mapping,
Map<String, Object> aggregatedData) throws Exception {
Object value = extractValue(mapping, aggregatedData);
if (value == null) {
if (mapping.isRequired()) {
throw new IllegalArgumentException("Required field missing: " + mapping.getSolrField());
}
value = mapping.getDefaultValue();
if (value == null) {
return; // Skip this field
}
}
// Transform value if needed
if (mapping.getTransformFunction() != null) {
value = transformationService.transform(mapping.getTransformFunction(), value.toString());
}
// Convert data type
Object convertedValue = convertDataType(value.toString(), mapping.getDataType());
// Create atomic update operation
Map<String, Object> atomicUpdate = new HashMap<>();
atomicUpdate.put("set", convertedValue);
doc.addField(mapping.getSolrField(), atomicUpdate);
}
private Object extractValue(FieldMapping mapping, Map<String, Object> aggregatedData) {
// Handle static values
if (mapping.getStaticValue() != null) {
return mapping.getStaticValue();
}
// Handle trigger field direct mapping
if (mapping.getTriggerField() != null) {
Map<String, Object> triggerData = (Map<String, Object>) aggregatedData.get("trigger");
return getNestedValue(triggerData, mapping.getTriggerField());
}
// Handle composite fields
if (mapping.getSourcePaths() != null && !mapping.getSourcePaths().isEmpty()) {
List<String> values = new ArrayList<>();
for (String sourcePath : mapping.getSourcePaths()) {
Object value = getValueFromPath(aggregatedData, sourcePath);
values.add(value != null ? value.toString() : "");
}
if (mapping.getCombineFunction() != null) {
return transformationService.combineFields(mapping.getCombineFunction(), values);
} else {
return String.join(mapping.getSeparator(), values);
}
}
// Handle single source path
if (mapping.getSourcePath() != null) {
return getValueFromPath(aggregatedData, mapping.getSourcePath());
}
return null;
}
private Object getValueFromPath(Map<String, Object> data, String path) {
String[] parts = path.split("\\.");
Object current = data;
for (String part : parts) {
if (current == null) return null;
if (current instanceof Map) {
current = ((Map<String, Object>) current).get(part);
} else if (current instanceof List) {
try {
int index = Integer.parseInt(part);
current = ((List<?>) current).get(index);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return null;
}
} else {
// Use reflection for object properties
current = getObjectProperty(current, part);
}
}
return current;
}
private Object getNestedValue(Map<String, Object> data, String path) {
return getValueFromPath(data, path);
}
private Object getObjectProperty(Object obj, String property) {
try {
Field field = obj.getClass().getDeclaredField(property);
field.setAccessible(true);
return field.get(obj);
} catch (Exception e) {
try {
Method getter = obj.getClass().getMethod("get" +
property.substring(0, 1).toUpperCase() + property.substring(1));
return getter.invoke(obj);
} catch (Exception ex) {
log.warn("Could not access property '{}' on object of type {}",
property, obj.getClass().getSimpleName());
return null;
}
}
}
// ... other helper methods remain similar ...
}
// Data Source Executor Service
@Service
@Slf4j
public class DataSourceExecutor {
private final ApplicationContext applicationContext;
private final ResultCacheService cacheService;
public DataSourceExecutor(ApplicationContext applicationContext, ResultCacheService cacheService) {
this.applicationContext = applicationContext;
this.cacheService = cacheService;
}
public Object executeDataSource(DataSourceConfig dataSource, Map<String, Object> triggerData)
throws Exception {
// Check cache first
if (dataSource.getCacheTimeoutMs() > 0) {
String cacheKey = buildCacheKey(dataSource, triggerData);
Object cachedResult = cacheService.get(cacheKey);
if (cachedResult != null) {
log.debug("Returning cached result for data source: {}", dataSource.getName());
return cachedResult;
}
}
// Execute with retry logic
Exception lastException = null;
for (int attempt = 0; attempt <= dataSource.getRetryCount(); attempt++) {
try {
Object result = executeDataSourceInternal(dataSource, triggerData);
// Cache result if configured
if (dataSource.getCacheTimeoutMs() > 0 && result != null) {
String cacheKey = buildCacheKey(dataSource, triggerData);
cacheService.put(cacheKey, result, dataSource.getCacheTimeoutMs());
}
return processResult(result, dataSource);
} catch (Exception e) {
lastException = e;
if (attempt < dataSource.getRetryCount()) {
log.warn("Attempt {} failed for data source {}, retrying...",
attempt + 1, dataSource.getName());
Thread.sleep(1000 * (attempt + 1)); // Exponential backoff
}
}
}
throw new RuntimeException("Data source execution failed after " +
(dataSource.getRetryCount() + 1) + " attempts", lastException);
}
private Object executeDataSourceInternal(DataSourceConfig dataSource, Map<String, Object> triggerData)
throws Exception {
// Get the bean
Object bean = applicationContext.getBean(dataSource.getBeanName());
// Prepare method parameters
Object[] params = prepareMethodParameters(dataSource, triggerData);
// Find and invoke the method
Method method = findMethod(bean.getClass(), dataSource.getMethodName(), params);
log.debug("Executing {}.{}() with {} parameters",
bean.getClass().getSimpleName(), dataSource.getMethodName(), params.length);
return method.invoke(bean, params);
}
private Object[] prepareMethodParameters(DataSourceConfig dataSource, Map<String, Object> triggerData) {
List<Object> params = new ArrayList<>();
// Add input fields from trigger data
for (String inputField : dataSource.getInputFields()) {
Object value = getNestedValue(triggerData, inputField);
params.add(value);
}
// Add static parameters
for (Map.Entry<String, String> staticParam : dataSource.getStaticParams().entrySet()) {
params.add(staticParam.getValue());
}
return params.toArray();
}
private Method findMethod(Class<?> clazz, String methodName, Object[] params)
throws NoSuchMethodException {
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName) &&
method.getParameterCount() == params.length) {
return method;
}
}
throw new NoSuchMethodException("Method " + methodName + " with " +
params.length + " parameters not found in " + clazz.getSimpleName());
}
private Object processResult(Object result, DataSourceConfig dataSource) {
if (result == null) return null;
// Extract data based on result path
if (dataSource.getResultPath() != null && !dataSource.getResultPath().isEmpty()) {
return extractFromResult(result, dataSource.getResultPath());
}
return result;
}
private Object extractFromResult(Object result, String path) {
// Similar to getValueFromPath but for method results
String[] parts = path.split("\\.");
Object current = result;
for (String part : parts) {
if (current == null) return null;
if (current instanceof Map) {
current = ((Map<String, Object>) current).get(part);
} else if (current instanceof List) {
try {
int index = Integer.parseInt(part);
current = ((List<?>) current).get(index);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return null;
}
} else {
current = getObjectProperty(current, part);
}
}
return current;
}
private Object getNestedValue(Map<String, Object> data, String path) {
String[] parts = path.split("\\.");
Object current = data;
for (String part : parts) {
if (current instanceof Map) {
current = ((Map<String, Object>) current).get(part);
} else {
return null;
}
}
return current;
}
private Object getObjectProperty(Object obj, String property) {
try {
Field field = obj.getClass().getDeclaredField(property);
field.setAccessible(true);
return field.get(obj);
} catch (Exception e) {
try {
Method getter = obj.getClass().getMethod("get" +
property.substring(0, 1).toUpperCase() + property.substring(1));
return getter.invoke(obj);
} catch (Exception ex) {
return null;
}
}
}
private String buildCacheKey(DataSourceConfig dataSource, Map<String, Object> triggerData) {
StringBuilder keyBuilder = new StringBuilder();
keyBuilder.append(dataSource.getName()).append(":");
for (String inputField : dataSource.getInputFields()) {
Object value = getNestedValue(triggerData, inputField);
keyBuilder.append(inputField).append("=").append(value).append(";");
}
return keyBuilder.toString();
}
}
// Simple cache service for DAO results
@Service
public class ResultCacheService {
private final Map<String, CacheEntry> cache = new ConcurrentHashMap<>();
public void put(String key, Object value, long timeoutMs) {
long expiryTime = System.currentTimeMillis() + timeoutMs;
cache.put(key, new CacheEntry(value, expiryTime));
}
public Object get(String key) {
CacheEntry entry = cache.get(key);
if (entry == null) return null;
if (System.currentTimeMillis() > entry.expiryTime) {
cache.remove(key);
return null;
}
return entry.value;
}
@Scheduled(fixedRate = 60000) // Clean expired entries every minute
public void cleanExpiredEntries() {
long now = System.currentTimeMillis();
cache.entrySet().removeIf(entry -> now > entry.getValue().expiryTime);
}
private static class CacheEntry {
final Object value;
final long expiryTime;
CacheEntry(Object value, long expiryTime) {
this.value = value;
this.expiryTime = expiryTime;
}
}
}