Skip to content

Commit 54cae36

Browse files
Move ID generation to common util
Signed-off-by: Varun Bharadwaj <varunbharadwaj1995@gmail.com>
1 parent 8965dc7 commit 54cae36

5 files changed

Lines changed: 60 additions & 39 deletions

File tree

server/src/main/java/org/opensearch/action/index/IndexRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
import org.opensearch.cluster.metadata.MappingMetadata;
4747
import org.opensearch.cluster.metadata.Metadata;
4848
import org.opensearch.common.Nullable;
49-
import org.opensearch.common.UUIDs;
5049
import org.opensearch.common.annotation.PublicApi;
5150
import org.opensearch.common.lucene.uid.Versions;
51+
import org.opensearch.common.util.RequestUtils;
5252
import org.opensearch.common.xcontent.XContentHelper;
5353
import org.opensearch.common.xcontent.XContentType;
5454
import org.opensearch.core.common.Strings;
@@ -625,7 +625,7 @@ public void process(Version indexCreatedVersion, @Nullable MappingMetadata mappi
625625
assert ifSeqNo == UNASSIGNED_SEQ_NO;
626626
assert ifPrimaryTerm == UNASSIGNED_PRIMARY_TERM;
627627
autoGeneratedTimestamp = Math.max(0, System.currentTimeMillis()); // extra paranoia
628-
id(UUIDs.base64UUID());
628+
id(RequestUtils.generateID());
629629
}
630630
}
631631

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.util;
10+
11+
import org.opensearch.common.UUIDs;
12+
13+
/**
14+
* Common utility methods for request handling.
15+
*
16+
* @opensearch.internal
17+
*/
18+
public final class RequestUtils {
19+
20+
private RequestUtils() {}
21+
22+
/**
23+
* Generates a new ID field for new documents.
24+
*/
25+
public static String generateID() {
26+
return UUIDs.base64UUID();
27+
}
28+
}

server/src/main/java/org/opensearch/indices/pollingingest/IngestionUtils.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

server/src/main/java/org/opensearch/indices/pollingingest/MessageProcessorRunnable.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import org.opensearch.action.DocWriteRequest;
1616
import org.opensearch.common.lucene.uid.Versions;
1717
import org.opensearch.common.metrics.CounterMetric;
18+
import org.opensearch.common.util.RequestUtils;
1819
import org.opensearch.common.xcontent.XContentFactory;
20+
import org.opensearch.common.xcontent.XContentHelper;
1921
import org.opensearch.core.common.Strings;
22+
import org.opensearch.core.common.bytes.BytesArray;
2023
import org.opensearch.core.common.bytes.BytesReference;
2124
import org.opensearch.core.xcontent.MediaTypeRegistry;
2225
import org.opensearch.index.IngestionShardConsumer;
@@ -142,7 +145,7 @@ protected void process(Message message, IngestionShardPointer pointer) {
142145
* @return the engine operation
143146
*/
144147
protected Engine.Operation getOperation(byte[] payload, IngestionShardPointer pointer) throws IOException {
145-
Map<String, Object> payloadMap = IngestionUtils.getParsedPayloadMap(payload);
148+
Map<String, Object> payloadMap = getParsedPayloadMap(payload);
146149

147150
if (payloadMap.containsKey(OP_TYPE) && !(payloadMap.get(OP_TYPE) instanceof String)) {
148151
// TODO: add metric
@@ -154,7 +157,7 @@ protected Engine.Operation getOperation(byte[] payload, IngestionShardPointer po
154157
long autoGeneratedIdTimestamp = UNSET_AUTO_GENERATED_TIMESTAMP;
155158
if (Strings.isNullOrEmpty(id)) {
156159
// auto generate ID for the message
157-
id = IngestionUtils.generateID();
160+
id = RequestUtils.generateID();
158161
payloadMap.put(ID, id);
159162
autoGeneratedIdTimestamp = System.currentTimeMillis();
160163
}
@@ -233,6 +236,12 @@ protected Engine.Operation getOperation(byte[] payload, IngestionShardPointer po
233236

234237
return operation;
235238
}
239+
240+
private Map<String, Object> getParsedPayloadMap(byte[] payload) {
241+
BytesReference payloadBR = new BytesArray(payload);
242+
Map<String, Object> payloadMap = XContentHelper.convertToMap(payloadBR, false, MediaTypeRegistry.xContentType(payloadBR)).v2();
243+
return payloadMap;
244+
}
236245
}
237246

238247
private static BytesReference convertToBytes(Object object) throws IOException {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.util;
10+
11+
import org.opensearch.core.common.Strings;
12+
import org.opensearch.test.OpenSearchTestCase;
13+
14+
public class RequestUtilsTests extends OpenSearchTestCase {
15+
16+
public void testGenerateID() {
17+
assertTrue(Strings.hasText(RequestUtils.generateID()));
18+
}
19+
}

0 commit comments

Comments
 (0)