Skip to content

Commit 185f189

Browse files
j-hajnik9000
authored andcommitted
Adds deprecation logging to ScriptDocValues#getValues. (#34279)
`ScriptDocValues#getValues` was added for backwards compatibility but no longer needed. Scripts using the syntax `doc['foo'].values` when `doc['foo']` is a list should be using `doc['foo']` instead. Closes #22919
1 parent 09a7996 commit 185f189

File tree

24 files changed

+231
-181
lines changed

24 files changed

+231
-181
lines changed

docs/reference/aggregations/bucket/diversified-sampler-aggregation.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ POST /stackoverflow/_search?size=0
108108
"max_docs_per_value" : 3,
109109
"script" : {
110110
"lang": "painless",
111-
"source": "doc['tags'].values.hashCode()"
111+
"source": "doc['tags'].hashCode()"
112112
}
113113
},
114114
"aggs": {

docs/reference/modules/scripting/fields.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ access `text` fields from scripts.
144144

145145
_Stored fields_ -- fields explicitly marked as
146146
<<mapping-store,`"store": true`>> -- can be accessed using the
147-
`_fields['field_name'].value` or `_fields['field_name'].values` syntax.
147+
`_fields['field_name'].value` or `_fields['field_name']` syntax.
148148

149149
The document <<mapping-source-field,`_source`>>, which is really just a
150150
special stored field, can be accessed using the `_source.field_name` syntax.

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/30_search.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
script_fields:
349349
foobar:
350350
script:
351-
source: "doc['f'].values.size()"
351+
source: "doc['f'].size()"
352352
lang: painless
353353

354354

server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java

Lines changed: 54 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ public abstract class ScriptDocValues<T> extends AbstractList<T> {
5858
static final boolean EXCEPTION_FOR_MISSING_VALUE =
5959
parseBoolean(System.getProperty("es.scripting.exception_for_missing_value", "false"));
6060

61+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ScriptDocValues.class));
62+
/**
63+
* Callback for deprecated fields. In production this should always point to
64+
* {@link #deprecationLogger} but tests will override it so they can test
65+
* that we use the required permissions when calling it.
66+
*/
67+
private final BiConsumer<String, String> deprecationCallback;
68+
69+
public ScriptDocValues() {
70+
deprecationCallback = deprecationLogger::deprecatedAndMaybeLog;
71+
}
72+
73+
/**
74+
* Constructor for testing deprecation callback.
75+
*/
76+
ScriptDocValues(BiConsumer<String, String> deprecationCallback) {
77+
this.deprecationCallback = deprecationCallback;
78+
}
79+
6180
/**
6281
* Set the current doc ID.
6382
*/
@@ -67,6 +86,8 @@ public abstract class ScriptDocValues<T> extends AbstractList<T> {
6786
* Return a copy of the list of the values for the current document.
6887
*/
6988
public final List<T> getValues() {
89+
deprecated("ScriptDocValues#getValues", "Deprecated getValues used, the field is a list and should be accessed directly."
90+
+ " For example, use doc['foo'] instead of doc['foo'].values.");
7091
return this;
7192
}
7293

@@ -96,15 +117,25 @@ public final void sort(Comparator<? super T> c) {
96117
throw new UnsupportedOperationException("doc values are unmodifiable");
97118
}
98119

120+
/**
121+
* Log a deprecation log, with the server's permissions and not the permissions
122+
* of the script calling this method. We need to do this to prevent errors
123+
* when rolling the log file.
124+
*/
125+
protected void deprecated(String key, String message) {
126+
AccessController.doPrivileged(new PrivilegedAction<Void>() {
127+
@Override
128+
public Void run() {
129+
deprecationCallback.accept(key, message);
130+
return null;
131+
}
132+
});
133+
}
134+
99135
public static final class Longs extends ScriptDocValues<Long> {
100136
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Longs.class));
101-
private final SortedNumericDocValues in;
102-
/**
103-
* Callback for deprecated fields. In production this should always point to
104-
* {@link #deprecationLogger} but tests will override it so they can test that
105-
* we use the required permissions when calling it.
106-
*/
107137
private final BiConsumer<String, String> deprecationCallback;
138+
private final SortedNumericDocValues in;
108139
private long[] values = new long[0];
109140
private int count;
110141
private Dates dates;
@@ -118,11 +149,12 @@ public Longs(SortedNumericDocValues in) {
118149
}
119150

120151
/**
121-
* Constructor for testing the deprecation callback.
152+
* Constructor for testing deprecation callback.
122153
*/
123154
Longs(SortedNumericDocValues in, BiConsumer<String, String> deprecationCallback) {
124-
this.in = in;
155+
super(deprecationCallback);
125156
this.deprecationCallback = deprecationCallback;
157+
this.in = in;
126158
}
127159

128160
@Override
@@ -198,22 +230,6 @@ public Long get(int index) {
198230
public int size() {
199231
return count;
200232
}
201-
202-
/**
203-
* Log a deprecation log, with the server's permissions, not the permissions of the
204-
* script calling this method. We need to do this to prevent errors when rolling
205-
* the log file.
206-
*/
207-
private void deprecated(String key, String message) {
208-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
209-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
210-
@Override
211-
public Void run() {
212-
deprecationCallback.accept(key, message);
213-
return null;
214-
}
215-
});
216-
}
217233
}
218234

219235
public static final class Dates extends ScriptDocValues<JodaCompatibleZonedDateTime> {
@@ -224,8 +240,6 @@ public static final class Dates extends ScriptDocValues<JodaCompatibleZonedDateT
224240

225241
private final SortedNumericDocValues in;
226242

227-
private final BiConsumer<String, String> deprecationCallback;
228-
229243
/**
230244
* Values wrapped in {@link java.time.ZonedDateTime} objects.
231245
*/
@@ -240,11 +254,11 @@ public Dates(SortedNumericDocValues in) {
240254
}
241255

242256
/**
243-
* Constructor for testing with a deprecation callback.
257+
* Constructor for testing deprecation callback.
244258
*/
245259
Dates(SortedNumericDocValues in, BiConsumer<String, String> deprecationCallback) {
260+
super(deprecationCallback);
246261
this.in = in;
247-
this.deprecationCallback = deprecationCallback;
248262
}
249263

250264
/**
@@ -324,22 +338,6 @@ void refreshArray() throws IOException {
324338
dates[i] = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(in.nextValue()), ZoneOffset.UTC);
325339
}
326340
}
327-
328-
/**
329-
* Log a deprecation log, with the server's permissions, not the permissions of the
330-
* script calling this method. We need to do this to prevent errors when rolling
331-
* the log file.
332-
*/
333-
private void deprecated(String key, String message) {
334-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
335-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
336-
@Override
337-
public Void run() {
338-
deprecationCallback.accept(key, message);
339-
return null;
340-
}
341-
});
342-
}
343341
}
344342

345343
public static final class Doubles extends ScriptDocValues<Double> {
@@ -349,15 +347,14 @@ public static final class Doubles extends ScriptDocValues<Double> {
349347
private int count;
350348

351349
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Doubles.class));
352-
private final BiConsumer<String, String> deprecationCallback;
353350

354351
public Doubles(SortedNumericDoubleValues in) {
355352
this(in, (key, message) -> deprecationLogger.deprecatedAndMaybeLog(key, message));
356353
}
357354

358355
public Doubles(SortedNumericDoubleValues in, BiConsumer<String, String> deprecationCallback) {
356+
super(deprecationCallback);
359357
this.in = in;
360-
this.deprecationCallback = deprecationCallback;
361358
}
362359

363360
@Override
@@ -409,22 +406,6 @@ public Double get(int index) {
409406
public int size() {
410407
return count;
411408
}
412-
413-
/**
414-
* Log a deprecation log, with the server's permissions, not the permissions of the
415-
* script calling this method. We need to do this to prevent errors when rolling
416-
* the log file.
417-
*/
418-
private void deprecated(String key, String message) {
419-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
420-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
421-
@Override
422-
public Void run() {
423-
deprecationCallback.accept(key, message);
424-
return null;
425-
}
426-
});
427-
}
428409
}
429410

430411
public static final class GeoPoints extends ScriptDocValues<GeoPoint> {
@@ -434,15 +415,17 @@ public static final class GeoPoints extends ScriptDocValues<GeoPoint> {
434415
private int count;
435416

436417
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(GeoPoints.class));
437-
private final BiConsumer<String, String> deprecationCallback;
438418

439419
public GeoPoints(MultiGeoPointValues in) {
440420
this(in, (key, message) -> deprecationLogger.deprecatedAndMaybeLog(key, message));
441421
}
442422

443-
public GeoPoints(MultiGeoPointValues in, BiConsumer<String, String> deprecationCallback) {
444-
this.in = in;
445-
this.deprecationCallback = deprecationCallback;
423+
/**
424+
* Constructor for testing deprecation callback.
425+
*/
426+
GeoPoints(MultiGeoPointValues in, BiConsumer<String, String> deprecationCallback) {
427+
super(deprecationCallback);
428+
this.in = in;
446429
}
447430

448431
@Override
@@ -561,22 +544,6 @@ public double geohashDistanceWithDefault(String geohash, double defaultValue) {
561544
}
562545
return geohashDistance(geohash);
563546
}
564-
565-
/**
566-
* Log a deprecation log, with the server's permissions, not the permissions of the
567-
* script calling this method. We need to do this to prevent errors when rolling
568-
* the log file.
569-
*/
570-
private void deprecated(String key, String message) {
571-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
572-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
573-
@Override
574-
public Void run() {
575-
deprecationCallback.accept(key, message);
576-
return null;
577-
}
578-
});
579-
}
580547
}
581548

582549
public static final class Booleans extends ScriptDocValues<Boolean> {
@@ -586,15 +553,14 @@ public static final class Booleans extends ScriptDocValues<Boolean> {
586553
private int count;
587554

588555
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Booleans.class));
589-
private final BiConsumer<String, String> deprecationCallback;
590556

591557
public Booleans(SortedNumericDocValues in) {
592558
this(in, (key, message) -> deprecationLogger.deprecatedAndMaybeLog(key, message));
593559
}
594560

595561
public Booleans(SortedNumericDocValues in, BiConsumer<String, String> deprecationCallback) {
562+
super(deprecationCallback);
596563
this.in = in;
597-
this.deprecationCallback = deprecationCallback;
598564
}
599565

600566
@Override
@@ -651,23 +617,6 @@ private static boolean[] grow(boolean[] array, int minSize) {
651617
} else
652618
return array;
653619
}
654-
655-
/**
656-
* Log a deprecation log, with the server's permissions, not the permissions of the
657-
* script calling this method. We need to do this to prevent errors when rolling
658-
* the log file.
659-
*/
660-
private void deprecated(String key, String message) {
661-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
662-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
663-
@Override
664-
public Void run() {
665-
deprecationCallback.accept(key, message);
666-
return null;
667-
}
668-
});
669-
}
670-
671620
}
672621

673622
abstract static class BinaryScriptDocValues<T> extends ScriptDocValues<T> {
@@ -676,7 +625,8 @@ abstract static class BinaryScriptDocValues<T> extends ScriptDocValues<T> {
676625
protected BytesRefBuilder[] values = new BytesRefBuilder[0];
677626
protected int count;
678627

679-
BinaryScriptDocValues(SortedBinaryDocValues in) {
628+
BinaryScriptDocValues(SortedBinaryDocValues in, BiConsumer<String, String> deprecationCallback) {
629+
super(deprecationCallback);
680630
this.in = in;
681631
}
682632

@@ -720,15 +670,13 @@ public int size() {
720670
public static final class Strings extends BinaryScriptDocValues<String> {
721671

722672
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Strings.class));
723-
private final BiConsumer<String, String> deprecationCallback;
724673

725674
public Strings(SortedBinaryDocValues in) {
726675
this(in, (key, message) -> deprecationLogger.deprecatedAndMaybeLog(key, message));
727676
}
728677

729678
public Strings(SortedBinaryDocValues in, BiConsumer<String, String> deprecationCallback) {
730-
super(in);
731-
this.deprecationCallback = deprecationCallback;
679+
super(in, deprecationCallback);
732680
}
733681

734682
@Override
@@ -750,36 +698,18 @@ public String getValue() {
750698
}
751699
return get(0);
752700
}
753-
754-
/**
755-
* Log a deprecation log, with the server's permissions, not the permissions of the
756-
* script calling this method. We need to do this to prevent errors when rolling
757-
* the log file.
758-
*/
759-
private void deprecated(String key, String message) {
760-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
761-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
762-
@Override
763-
public Void run() {
764-
deprecationCallback.accept(key, message);
765-
return null;
766-
}
767-
});
768-
}
769701
}
770702

771703
public static final class BytesRefs extends BinaryScriptDocValues<BytesRef> {
772704

773705
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Strings.class));
774-
private final BiConsumer<String, String> deprecationCallback;
775706

776707
public BytesRefs(SortedBinaryDocValues in) {
777708
this(in, (key, message) -> deprecationLogger.deprecatedAndMaybeLog(key, message));
778709
}
779710

780711
public BytesRefs(SortedBinaryDocValues in, BiConsumer<String, String> deprecationCallback) {
781-
super(in);
782-
this.deprecationCallback = deprecationCallback;
712+
super(in, deprecationCallback);
783713
}
784714

785715
@Override
@@ -806,22 +736,5 @@ public BytesRef getValue() {
806736
}
807737
return get(0);
808738
}
809-
810-
/**
811-
* Log a deprecation log, with the server's permissions, not the permissions of the
812-
* script calling this method. We need to do this to prevent errors when rolling
813-
* the log file.
814-
*/
815-
private void deprecated(String key, String message) {
816-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
817-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
818-
@Override
819-
public Void run() {
820-
deprecationCallback.accept(key, message);
821-
return null;
822-
}
823-
});
824-
}
825-
826739
}
827740
}

0 commit comments

Comments
 (0)