Search in sources :

Example 1 with Resolution

use of org.opensearch.index.mapper.DateFieldMapper.Resolution in project OpenSearch by opensearch-project.

the class DocValueFormatTests method testSerialization.

public void testSerialization() throws Exception {
    List<Entry> entries = new ArrayList<>();
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.GEOTILE.getWriteableName(), in -> DocValueFormat.GEOTILE));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.RAW.getWriteableName(), in -> DocValueFormat.RAW));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.BINARY.getWriteableName(), in -> DocValueFormat.BINARY));
    NamedWriteableRegistry registry = new NamedWriteableRegistry(entries);
    BytesStreamOutput out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.BOOLEAN);
    StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.BOOLEAN, in.readNamedWriteable(DocValueFormat.class));
    DocValueFormat.Decimal decimalFormat = new DocValueFormat.Decimal("###.##");
    out = new BytesStreamOutput();
    out.writeNamedWriteable(decimalFormat);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    DocValueFormat vf = in.readNamedWriteable(DocValueFormat.class);
    assertEquals(DocValueFormat.Decimal.class, vf.getClass());
    assertEquals("###.##", ((DocValueFormat.Decimal) vf).pattern);
    DateFormatter formatter = DateFormatter.forPattern("epoch_second");
    DocValueFormat.DateTime dateFormat = new DocValueFormat.DateTime(formatter, ZoneOffset.ofHours(1), Resolution.MILLISECONDS);
    out = new BytesStreamOutput();
    out.writeNamedWriteable(dateFormat);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    vf = in.readNamedWriteable(DocValueFormat.class);
    assertEquals(DocValueFormat.DateTime.class, vf.getClass());
    assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.pattern());
    assertEquals(ZoneOffset.ofHours(1), ((DocValueFormat.DateTime) vf).timeZone);
    assertEquals(Resolution.MILLISECONDS, ((DocValueFormat.DateTime) vf).resolution);
    DocValueFormat.DateTime nanosDateFormat = new DocValueFormat.DateTime(formatter, ZoneOffset.ofHours(1), Resolution.NANOSECONDS);
    out = new BytesStreamOutput();
    out.writeNamedWriteable(nanosDateFormat);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    vf = in.readNamedWriteable(DocValueFormat.class);
    assertEquals(DocValueFormat.DateTime.class, vf.getClass());
    assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.pattern());
    assertEquals(ZoneOffset.ofHours(1), ((DocValueFormat.DateTime) vf).timeZone);
    assertEquals(Resolution.NANOSECONDS, ((DocValueFormat.DateTime) vf).resolution);
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.GEOHASH);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.GEOHASH, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.GEOTILE);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.GEOTILE, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.IP);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.IP, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.RAW);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.RAW, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.BINARY);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.BINARY, in.readNamedWriteable(DocValueFormat.class));
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) InetAddresses(org.opensearch.common.network.InetAddresses) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) BytesRef(org.apache.lucene.util.BytesRef) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Resolution(org.opensearch.index.mapper.DateFieldMapper.Resolution) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) List(java.util.List) ZoneOffset(java.time.ZoneOffset) DateFormatter(org.opensearch.common.time.DateFormatter) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) GeoTileUtils.longEncode(org.opensearch.search.aggregations.bucket.geogrid.GeoTileUtils.longEncode) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) DateFormatter(org.opensearch.common.time.DateFormatter) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)

Aggregations

ZoneOffset (java.time.ZoneOffset)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)1 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.opensearch.common.io.stream.NamedWriteableRegistry.Entry)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 InetAddresses (org.opensearch.common.network.InetAddresses)1 DateFormatter (org.opensearch.common.time.DateFormatter)1 Resolution (org.opensearch.index.mapper.DateFieldMapper.Resolution)1 GeoTileUtils.longEncode (org.opensearch.search.aggregations.bucket.geogrid.GeoTileUtils.longEncode)1 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)1