Skip to content

Commit

Permalink
Merge pull request #3 from romanpierson/fixIndexPrefixCache
Browse files Browse the repository at this point in the history
Fix index prefix cache
  • Loading branch information
romanpierson committed Dec 10, 2023
2 parents 57f913b + 5c22f6d commit c7d6475
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# vertx-elasticsearch-indexer

A verticle that receives index data via event bus and indexes to the corresponding ElasticSearch instance(s). The whole configuration is maintained on the verticle itself.
Also Axiom.co is supoorted in latest version.
Also Axiom.co is supported in latest version.

## Technical Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public class ElasticSearchIndexerVerticle extends AbstractVerticle {
private final DateFormat indexTimeStampPattern;

private final String newLine = "\n";
private String cachedStaticIndexPrefix;
private Map<String, String> cachedDynamicIndexPrefix = new HashMap<>();
private Map<String, String> cachedIndexPrefix = new HashMap<>();

public enum IndexFlavour{

Expand Down Expand Up @@ -331,50 +330,56 @@ private HttpRequest<Buffer> getRequestFor(final ElasticSearchIndexerConfiguratio
return request;
}

private String getIndexPrefixString(final ElasticSearchIndexerConfiguration indexerConfiguration,
final long eventTimestamp) {
private String getIndexPrefixString(final ElasticSearchIndexerConfiguration indexerConfiguration, final long eventTimestamp) {

boolean isDynamicCacheIndex = false;
String cacheKey = indexerConfiguration.getIdentifier();

if (IndexMode.DATE_PATTERN_EVENT_TIMESTAMP.equals(indexerConfiguration.getIndexMode())
|| IndexMode.DATE_PATTERN_INDEX_TIMESTAMP.equals(indexerConfiguration.getIndexMode())) {


isDynamicCacheIndex = true;

long timestamp = IndexMode.DATE_PATTERN_EVENT_TIMESTAMP.equals(indexerConfiguration.getIndexMode())
? eventTimestamp
: System.currentTimeMillis();

final String cacheKey = indexerConfiguration.getIdentifier() + indexDateModePattern.format(timestamp);

if (!this.cachedDynamicIndexPrefix.containsKey(cacheKey)) {

cacheKey = indexerConfiguration.getIdentifier() + indexDateModePattern.format(timestamp);

}

if (!this.cachedIndexPrefix.containsKey(cacheKey)) {

//We still need to create that entry
// For static its straight
String formattedIndexPattern = indexerConfiguration.getIndexNameOrPattern();

if(isDynamicCacheIndex) {

long timestamp = IndexMode.DATE_PATTERN_EVENT_TIMESTAMP.equals(indexerConfiguration.getIndexMode())
? eventTimestamp
: System.currentTimeMillis();

ZonedDateTime tsDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), TIMEZONE_ID_UTC);
// Explicitly not using a DateTimeFormatter as this would require escaping the
// whole pattern
String formattedIndexPattern = indexerConfiguration.getIndexNameOrPattern()
formattedIndexPattern = indexerConfiguration.getIndexNameOrPattern()
.replaceAll("yyyy", String.format("%04d", tsDateTime.getYear()))
.replaceAll("MM", String.format("%02d", tsDateTime.getMonthValue()))
.replaceAll("dd", String.format("%02d", tsDateTime.getDayOfMonth()));

String formattedIndexPrefix = String.format(
"{ \"index\" : { \"_index\" : \"%s\" } }%s", formattedIndexPattern,
this.newLine);

this.cachedDynamicIndexPrefix.put(cacheKey, formattedIndexPrefix);

}

return this.cachedDynamicIndexPrefix.get(cacheKey);

} else {

if (this.cachedStaticIndexPrefix == null) {

this.cachedStaticIndexPrefix = String.format(
"{ \"index\" : { \"_index\" : \"%s\" } }%s",
indexerConfiguration.getIndexNameOrPattern(), this.newLine);

}

return this.cachedStaticIndexPrefix;

// Now create the formatted index prefix fragment
String formattedIndexPrefix = String.format("{ \"index\" : { \"_index\" : \"%s\" } }%s", formattedIndexPattern, this.newLine);

// And cache it
this.cachedIndexPrefix.put(cacheKey, formattedIndexPrefix);
}

// Now we should be able to read it from cache regardless if its static or dynamic
return this.cachedIndexPrefix.get(cacheKey);


}

Expand Down

0 comments on commit c7d6475

Please sign in to comment.