Skip to content

Commit 462c50e

Browse files
more optimizations
1 parent bb1a40b commit 462c50e

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

server/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ private static Boolean valueOrDefault(Boolean value, Boolean globalDefault) {
457457

458458
@Override
459459
public long ramBytesUsed() {
460-
return SHALLOW_SIZE + requests.stream().mapToLong(Accountable::ramBytesUsed).sum();
460+
long sum = SHALLOW_SIZE;
461+
for (DocWriteRequest<?> request : requests) {
462+
sum += request.ramBytesUsed();
463+
}
464+
return sum;
461465
}
462466

463467
public Set<String> getIndices() {

server/src/main/java/org/elasticsearch/action/bulk/BulkRequestParser.java

+34-30
Original file line numberDiff line numberDiff line change
@@ -164,38 +164,14 @@ public void parse(
164164
continue;
165165
}
166166
if (token != XContentParser.Token.START_OBJECT) {
167-
throw new IllegalArgumentException(
168-
"Malformed action/metadata line ["
169-
+ line
170-
+ "], expected "
171-
+ XContentParser.Token.START_OBJECT
172-
+ " but found ["
173-
+ token
174-
+ "]"
175-
);
167+
throwOnMalformedStart(line, token);
176168
}
177169
// Move to FIELD_NAME, that's the action
178-
token = parser.nextToken();
179-
if (token != XContentParser.Token.FIELD_NAME) {
180-
throw new IllegalArgumentException(
181-
"Malformed action/metadata line ["
182-
+ line
183-
+ "], expected "
184-
+ XContentParser.Token.FIELD_NAME
185-
+ " but found ["
186-
+ token
187-
+ "]"
188-
);
189-
}
190-
String action = parser.currentName();
191-
if (SUPPORTED_ACTIONS.contains(action) == false) {
192-
throw new IllegalArgumentException(
193-
"Malformed action/metadata line ["
194-
+ line
195-
+ "], expected field [create], [delete], [index] or [update] but found ["
196-
+ action
197-
+ "]"
198-
);
170+
String action = parser.nextFieldName();
171+
if (action == null) {
172+
throwOnMalformedLine(line, parser);
173+
} else if (SUPPORTED_ACTIONS.contains(action) == false) {
174+
throwOnUnsupportedAction(line, action);
199175
}
200176

201177
String index = defaultIndex;
@@ -444,6 +420,34 @@ public void parse(
444420
}
445421
}
446422

423+
private static void throwOnMalformedStart(int line, XContentParser.Token token) {
424+
throw new IllegalArgumentException(
425+
"Malformed action/metadata line [" + line + "], expected " + XContentParser.Token.START_OBJECT + " but found [" + token + "]"
426+
);
427+
}
428+
429+
private static void throwOnMalformedLine(int line, XContentParser parser) {
430+
throw new IllegalArgumentException(
431+
"Malformed action/metadata line ["
432+
+ line
433+
+ "], expected "
434+
+ XContentParser.Token.FIELD_NAME
435+
+ " but found ["
436+
+ parser.currentToken()
437+
+ "]"
438+
);
439+
}
440+
441+
private static void throwOnUnsupportedAction(int line, String action) {
442+
throw new IllegalArgumentException(
443+
"Malformed action/metadata line ["
444+
+ line
445+
+ "], expected field [create], [delete], [index] or [update] but found ["
446+
+ action
447+
+ "]"
448+
);
449+
}
450+
447451
@UpdateForV9
448452
// Warnings will need to be replaced with XContentEOFException from 9.x
449453
private static void warnBulkActionNotProperlyClosed(String message) {

server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,14 @@ static void prohibitAppendWritesInBackingIndices(DocWriteRequest<?> writeRequest
607607
}
608608

609609
static void prohibitCustomRoutingOnDataStream(DocWriteRequest<?> writeRequest, Metadata metadata) {
610-
IndexAbstraction indexAbstraction = metadata.getIndicesLookup().get(writeRequest.index());
611-
if (indexAbstraction == null) {
612-
return;
613-
}
614-
if (indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM) {
615-
return;
616-
}
617-
618610
if (writeRequest.routing() != null) {
611+
IndexAbstraction indexAbstraction = metadata.getIndicesLookup().get(writeRequest.index());
612+
if (indexAbstraction == null) {
613+
return;
614+
}
615+
if (indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM) {
616+
return;
617+
}
619618
DataStream dataStream = (DataStream) indexAbstraction;
620619
if (dataStream.isAllowCustomRouting() == false) {
621620
throw new IllegalArgumentException(

0 commit comments

Comments
 (0)