|
@@ -9,8 +9,6 @@ package org.elasticsearch.xpack.esql.core.util;
|
|
|
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
-import org.elasticsearch.core.Nullable;
|
|
|
-import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
|
|
|
import org.elasticsearch.xpack.esql.core.tree.Location;
|
|
|
import org.elasticsearch.xpack.esql.core.tree.Source;
|
|
|
|
|
@@ -20,74 +18,30 @@ public final class SourceUtils {
|
|
|
|
|
|
private SourceUtils() {}
|
|
|
|
|
|
+ /**
|
|
|
+ * Write a {@link Source} including the text in it.
|
|
|
+ * @deprecated replace with {@link Source#writeTo}.
|
|
|
+ * That's not binary compatible so the replacement is complex.
|
|
|
+ */
|
|
|
+ @Deprecated
|
|
|
public static void writeSource(StreamOutput out, Source source) throws IOException {
|
|
|
- writeSource(out, source, true);
|
|
|
- }
|
|
|
-
|
|
|
- public static void writeSourceNoText(StreamOutput out, Source source) throws IOException {
|
|
|
- writeSource(out, source, false);
|
|
|
- }
|
|
|
-
|
|
|
- public static Source readSource(StreamInput in) throws IOException {
|
|
|
- return readSource(in, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static Source readSourceWithText(StreamInput in, String queryText) throws IOException {
|
|
|
- return readSource(in, queryText);
|
|
|
- }
|
|
|
-
|
|
|
- private static void writeSource(StreamOutput out, Source source, boolean writeText) throws IOException {
|
|
|
out.writeInt(source.source().getLineNumber());
|
|
|
out.writeInt(source.source().getColumnNumber());
|
|
|
- if (writeText) {
|
|
|
- out.writeString(source.text());
|
|
|
- } else {
|
|
|
- out.writeInt(source.text().length());
|
|
|
- }
|
|
|
+ out.writeString(source.text());
|
|
|
}
|
|
|
|
|
|
- private static Source readSource(StreamInput in, @Nullable String queryText) throws IOException {
|
|
|
+ /**
|
|
|
+ * Read a {@link Source} including the text in it.
|
|
|
+ * @deprecated replace with {@link Source#readFrom(StreamInput)}.
|
|
|
+ * That's not binary compatible so the replacement is complex.
|
|
|
+ */
|
|
|
+ @Deprecated
|
|
|
+ public static Source readSource(StreamInput in) throws IOException {
|
|
|
int line = in.readInt();
|
|
|
int column = in.readInt();
|
|
|
int charPositionInLine = column - 1;
|
|
|
|
|
|
- String text;
|
|
|
- if (queryText == null) {
|
|
|
- text = in.readString();
|
|
|
- } else {
|
|
|
- int length = in.readInt();
|
|
|
- text = sourceText(queryText, line, column, length);
|
|
|
- }
|
|
|
+ String text = in.readString();
|
|
|
return new Source(new Location(line, charPositionInLine), text);
|
|
|
}
|
|
|
-
|
|
|
- private static String sourceText(String query, int line, int column, int length) {
|
|
|
- if (line <= 0 || column <= 0 || query.isEmpty()) {
|
|
|
- return StringUtils.EMPTY;
|
|
|
- }
|
|
|
- int offset = textOffset(query, line, column);
|
|
|
- if (offset + length > query.length()) {
|
|
|
- throw new QlIllegalArgumentException(
|
|
|
- "location [@" + line + ":" + column + "] and length [" + length + "] overrun query size [" + query.length() + "]"
|
|
|
- );
|
|
|
- }
|
|
|
- return query.substring(offset, offset + length);
|
|
|
- }
|
|
|
-
|
|
|
- private static int textOffset(String query, int line, int column) {
|
|
|
- int offset = 0;
|
|
|
- if (line > 1) {
|
|
|
- String[] lines = query.split("\n");
|
|
|
- if (line > lines.length) {
|
|
|
- throw new QlIllegalArgumentException(
|
|
|
- "line location [" + line + "] higher than max [" + lines.length + "] in query [" + query + "]"
|
|
|
- );
|
|
|
- }
|
|
|
- for (int i = 0; i < line - 1; i++) {
|
|
|
- offset += lines[i].length() + 1; // +1 accounts for the removed \n
|
|
|
- }
|
|
|
- }
|
|
|
- offset += column - 1; // -1 since column is 1-based indexed
|
|
|
- return offset;
|
|
|
- }
|
|
|
}
|