|
@@ -6,13 +6,12 @@
|
|
package org.elasticsearch.xpack.sql.session;
|
|
package org.elasticsearch.xpack.sql.session;
|
|
|
|
|
|
import org.elasticsearch.Version;
|
|
import org.elasticsearch.Version;
|
|
|
|
+import org.elasticsearch.common.collect.Tuple;
|
|
import org.elasticsearch.common.io.stream.NamedWriteable;
|
|
import org.elasticsearch.common.io.stream.NamedWriteable;
|
|
-import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
|
|
|
|
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
|
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
|
-import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
|
|
|
|
-import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
|
-import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
|
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
|
|
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
|
|
|
|
+import org.elasticsearch.xpack.sql.common.io.SqlStreamInput;
|
|
|
|
+import org.elasticsearch.xpack.sql.common.io.SqlStreamOutput;
|
|
import org.elasticsearch.xpack.sql.execution.search.CompositeAggregationCursor;
|
|
import org.elasticsearch.xpack.sql.execution.search.CompositeAggregationCursor;
|
|
import org.elasticsearch.xpack.sql.execution.search.ScrollCursor;
|
|
import org.elasticsearch.xpack.sql.execution.search.ScrollCursor;
|
|
import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractors;
|
|
import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractors;
|
|
@@ -22,11 +21,9 @@ import org.elasticsearch.xpack.sql.expression.literal.Literals;
|
|
import org.elasticsearch.xpack.sql.plugin.TextFormatterCursor;
|
|
import org.elasticsearch.xpack.sql.plugin.TextFormatterCursor;
|
|
import org.elasticsearch.xpack.sql.util.StringUtils;
|
|
import org.elasticsearch.xpack.sql.util.StringUtils;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.time.ZoneId;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
-import java.util.Base64;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -35,6 +32,7 @@ import java.util.List;
|
|
public final class Cursors {
|
|
public final class Cursors {
|
|
|
|
|
|
private static final NamedWriteableRegistry WRITEABLE_REGISTRY = new NamedWriteableRegistry(getNamedWriteables());
|
|
private static final NamedWriteableRegistry WRITEABLE_REGISTRY = new NamedWriteableRegistry(getNamedWriteables());
|
|
|
|
+ private static final Version VERSION = Version.CURRENT;
|
|
|
|
|
|
private Cursors() {}
|
|
private Cursors() {}
|
|
|
|
|
|
@@ -65,17 +63,20 @@ public final class Cursors {
|
|
/**
|
|
/**
|
|
* Write a {@linkplain Cursor} to a string for serialization across xcontent.
|
|
* Write a {@linkplain Cursor} to a string for serialization across xcontent.
|
|
*/
|
|
*/
|
|
- public static String encodeToString(Version version, Cursor info) {
|
|
|
|
|
|
+ public static String encodeToString(Cursor info, ZoneId zoneId) {
|
|
|
|
+ return encodeToString(info, VERSION, zoneId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static String encodeToString(Cursor info, Version version, ZoneId zoneId) {
|
|
if (info == Cursor.EMPTY) {
|
|
if (info == Cursor.EMPTY) {
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
}
|
|
}
|
|
- try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
|
|
|
- try (OutputStream base64 = Base64.getEncoder().wrap(os); StreamOutput out = new OutputStreamStreamOutput(base64)) {
|
|
|
|
- Version.writeVersion(version, out);
|
|
|
|
- out.writeNamedWriteable(info);
|
|
|
|
- }
|
|
|
|
- return os.toString(StandardCharsets.UTF_8.name());
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
|
|
+ try (SqlStreamOutput output = new SqlStreamOutput(version, zoneId)) {
|
|
|
|
+ output.writeNamedWriteable(info);
|
|
|
|
+ output.close();
|
|
|
|
+ // return the string only after closing the resource
|
|
|
|
+ return output.streamAsString();
|
|
|
|
+ } catch (IOException ex) {
|
|
throw new SqlIllegalArgumentException("Unexpected failure retrieving next page", ex);
|
|
throw new SqlIllegalArgumentException("Unexpected failure retrieving next page", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -84,22 +85,23 @@ public final class Cursors {
|
|
/**
|
|
/**
|
|
* Read a {@linkplain Cursor} from a string.
|
|
* Read a {@linkplain Cursor} from a string.
|
|
*/
|
|
*/
|
|
- public static Cursor decodeFromString(String info) {
|
|
|
|
- if (info.isEmpty()) {
|
|
|
|
- return Cursor.EMPTY;
|
|
|
|
|
|
+ public static Cursor decodeFromString(String base64) {
|
|
|
|
+ return decodeFromStringWithZone(base64).v1();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Read a {@linkplain Cursor} from a string.
|
|
|
|
+ */
|
|
|
|
+ public static Tuple<Cursor, ZoneId> decodeFromStringWithZone(String base64) {
|
|
|
|
+ if (base64.isEmpty()) {
|
|
|
|
+ return new Tuple<>(Cursor.EMPTY, null);
|
|
}
|
|
}
|
|
- byte[] bytes = info.getBytes(StandardCharsets.UTF_8);
|
|
|
|
- try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(Base64.getDecoder().decode(bytes)), WRITEABLE_REGISTRY)) {
|
|
|
|
- Version version = Version.readVersion(in);
|
|
|
|
- if (version.after(Version.CURRENT)) {
|
|
|
|
- throw new SqlIllegalArgumentException("Unsupported cursor version " + version);
|
|
|
|
- }
|
|
|
|
- in.setVersion(version);
|
|
|
|
- return in.readNamedWriteable(Cursor.class);
|
|
|
|
- } catch (SqlIllegalArgumentException ex) {
|
|
|
|
- throw ex;
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
|
|
+ try (SqlStreamInput in = new SqlStreamInput(base64, WRITEABLE_REGISTRY, VERSION)) {
|
|
|
|
+ Cursor cursor = in.readNamedWriteable(Cursor.class);
|
|
|
|
+ return new Tuple<>(cursor, in.zoneId());
|
|
|
|
+ } catch (IOException ex) {
|
|
throw new SqlIllegalArgumentException("Unexpected failure decoding cursor", ex);
|
|
throw new SqlIllegalArgumentException("Unexpected failure decoding cursor", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|