9
9
package org .opensearch .script ;
10
10
11
11
import org .apache .lucene .index .LeafReaderContext ;
12
+ import org .opensearch .common .collect .Tuple ;
12
13
import org .opensearch .index .fielddata .ScriptDocValues ;
13
14
import org .opensearch .search .lookup .LeafSearchLookup ;
14
15
import org .opensearch .search .lookup .SearchLookup ;
15
16
import org .opensearch .search .lookup .SourceLookup ;
16
17
17
18
import java .io .IOException ;
19
+ import java .nio .charset .StandardCharsets ;
18
20
import java .util .ArrayList ;
19
21
import java .util .HashMap ;
20
22
import java .util .List ;
@@ -31,6 +33,7 @@ public abstract class DerivedFieldScript {
31
33
32
34
public static final String [] PARAMETERS = {};
33
35
public static final ScriptContext <Factory > CONTEXT = new ScriptContext <>("derived_field" , Factory .class );
36
+ private static final int MAX_BYTE_SIZE = 1024 * 1024 ; // Maximum allowed byte size (1 MB)
34
37
35
38
private static final Map <String , Function <Object , Object >> PARAMS_FUNCTIONS = Map .of (
36
39
"doc" ,
@@ -54,18 +57,22 @@ public abstract class DerivedFieldScript {
54
57
*/
55
58
private List <Object > emittedValues ;
56
59
60
+ private int totalByteSize ;
61
+
57
62
public DerivedFieldScript (Map <String , Object > params , SearchLookup lookup , LeafReaderContext leafContext ) {
58
63
Map <String , Object > parameters = new HashMap <>(params );
59
64
this .leafLookup = lookup .getLeafSearchLookup (leafContext );
60
65
parameters .putAll (leafLookup .asMap ());
61
66
this .params = new DynamicMap (parameters , PARAMS_FUNCTIONS );
62
67
this .emittedValues = new ArrayList <>();
68
+ this .totalByteSize = 0 ;
63
69
}
64
70
65
71
public DerivedFieldScript () {
66
72
this .params = null ;
67
73
this .leafLookup = null ;
68
74
this .emittedValues = new ArrayList <>();
75
+ this .totalByteSize = 0 ;
69
76
}
70
77
71
78
/**
@@ -95,11 +102,38 @@ public List<Object> getEmittedValues() {
95
102
*/
96
103
public void setDocument (int docid ) {
97
104
this .emittedValues = new ArrayList <>();
105
+ this .totalByteSize = 0 ;
98
106
leafLookup .setDocument (docid );
99
107
}
100
108
101
109
public void addEmittedValue (Object o ) {
102
- emittedValues .add (o );
110
+ int byteSize = getObjectByteSize (o );
111
+ int newTotalByteSize = totalByteSize + byteSize ;
112
+ if (newTotalByteSize <= MAX_BYTE_SIZE ) {
113
+ emittedValues .add (o );
114
+ totalByteSize = newTotalByteSize ;
115
+ } else {
116
+ throw new IllegalStateException ("Exceeded maximum allowed byte size for emitted values" );
117
+ }
118
+ }
119
+
120
+ private int getObjectByteSize (Object obj ) {
121
+ if (obj instanceof String ) {
122
+ return ((String ) obj ).getBytes (StandardCharsets .UTF_8 ).length ;
123
+ } else if (obj instanceof Integer ) {
124
+ return Integer .BYTES ;
125
+ } else if (obj instanceof Long ) {
126
+ return Long .BYTES ;
127
+ } else if (obj instanceof Double ) {
128
+ return Double .BYTES ;
129
+ } else if (obj instanceof Boolean ) {
130
+ return Byte .BYTES ; // Assuming 1 byte for boolean
131
+ } else if (obj instanceof Tuple ) {
132
+ // Assuming each element in the tuple is a double for GeoPoint case
133
+ return Double .BYTES * 2 ;
134
+ } else {
135
+ throw new IllegalArgumentException ("Unsupported object type passed in emit()" );
136
+ }
103
137
}
104
138
105
139
public void execute () {}
0 commit comments