List of usage examples for org.apache.lucene.store ByteArrayDataOutput ByteArrayDataOutput
public ByteArrayDataOutput(byte[] bytes)
From source file:org.elasticsearch.common.util.ByteUtilsTests.java
License:Apache License
public void testVLong() throws IOException { final long[] data = new long[atLeast(1000)]; for (int i = 0; i < data.length; ++i) { switch (randomInt(4)) { case 0:/*www .ja v a2 s .co m*/ data[i] = 0; break; case 1: data[i] = Long.MAX_VALUE; break; case 2: data[i] = Long.MIN_VALUE; break; case 3: data[i] = randomInt(1 << randomIntBetween(2, 30)); break; case 4: data[i] = randomLong(); break; default: throw new AssertionError(); } } final byte[] encoded = new byte[ByteUtils.MAX_BYTES_VLONG * data.length]; ByteArrayDataOutput out = new ByteArrayDataOutput(encoded); for (int i = 0; i < data.length; ++i) { final int pos = out.getPosition(); ByteUtils.writeVLong(out, data[i]); if (data[i] < 0) { assertEquals(ByteUtils.MAX_BYTES_VLONG, out.getPosition() - pos); } } final ByteArrayDataInput in = new ByteArrayDataInput(encoded); for (int i = 0; i < data.length; ++i) { assertEquals(data[i], ByteUtils.readVLong(in)); } }
From source file:org.elasticsearch.index.mapper.BinaryRangeUtil.java
License:Apache License
static BytesRef encodeLongRanges(Set<RangeFieldMapper.Range> ranges) throws IOException { List<RangeFieldMapper.Range> sortedRanges = new ArrayList<>(ranges); Comparator<RangeFieldMapper.Range> fromComparator = Comparator .comparingLong(range -> ((Number) range.from).longValue()); Comparator<RangeFieldMapper.Range> toComparator = Comparator .comparingLong(range -> ((Number) range.to).longValue()); sortedRanges.sort(fromComparator.thenComparing(toComparator)); final byte[] encoded = new byte[5 + (9 * 2) * sortedRanges.size()]; ByteArrayDataOutput out = new ByteArrayDataOutput(encoded); out.writeVInt(sortedRanges.size());//from ww w .ja v a 2s.c om for (RangeFieldMapper.Range range : sortedRanges) { byte[] encodedFrom = encodeLong(((Number) range.from).longValue()); out.writeBytes(encodedFrom, encodedFrom.length); byte[] encodedTo = encodeLong(((Number) range.to).longValue()); out.writeBytes(encodedTo, encodedTo.length); } return new BytesRef(encoded, 0, out.getPosition()); }
From source file:org.elasticsearch.index.mapper.BinaryRangeUtil.java
License:Apache License
static BytesRef encodeDoubleRanges(Set<RangeFieldMapper.Range> ranges) throws IOException { List<RangeFieldMapper.Range> sortedRanges = new ArrayList<>(ranges); Comparator<RangeFieldMapper.Range> fromComparator = Comparator .comparingDouble(range -> ((Number) range.from).doubleValue()); Comparator<RangeFieldMapper.Range> toComparator = Comparator .comparingDouble(range -> ((Number) range.to).doubleValue()); sortedRanges.sort(fromComparator.thenComparing(toComparator)); final byte[] encoded = new byte[5 + (8 * 2) * sortedRanges.size()]; ByteArrayDataOutput out = new ByteArrayDataOutput(encoded); out.writeVInt(sortedRanges.size());/*from w ww. jav a2s. c o m*/ for (RangeFieldMapper.Range range : sortedRanges) { byte[] encodedFrom = encodeDouble(((Number) range.from).doubleValue()); out.writeBytes(encodedFrom, encodedFrom.length); byte[] encodedTo = encodeDouble(((Number) range.to).doubleValue()); out.writeBytes(encodedTo, encodedTo.length); } return new BytesRef(encoded, 0, out.getPosition()); }
From source file:org.elasticsearch.index.mapper.BinaryRangeUtil.java
License:Apache License
static BytesRef encodeFloatRanges(Set<RangeFieldMapper.Range> ranges) throws IOException { List<RangeFieldMapper.Range> sortedRanges = new ArrayList<>(ranges); Comparator<RangeFieldMapper.Range> fromComparator = Comparator .comparingDouble(range -> ((Number) range.from).floatValue()); Comparator<RangeFieldMapper.Range> toComparator = Comparator .comparingDouble(range -> ((Number) range.to).floatValue()); sortedRanges.sort(fromComparator.thenComparing(toComparator)); final byte[] encoded = new byte[5 + (4 * 2) * sortedRanges.size()]; ByteArrayDataOutput out = new ByteArrayDataOutput(encoded); out.writeVInt(sortedRanges.size());/*www . j ava2s . co m*/ for (RangeFieldMapper.Range range : sortedRanges) { byte[] encodedFrom = encodeFloat(((Number) range.from).floatValue()); out.writeBytes(encodedFrom, encodedFrom.length); byte[] encodedTo = encodeFloat(((Number) range.to).floatValue()); out.writeBytes(encodedTo, encodedTo.length); } return new BytesRef(encoded, 0, out.getPosition()); }
From source file:org.elasticsearch.index.translog.Checkpoint.java
License:Apache License
private void write(FileChannel channel) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; final ByteArrayDataOutput out = new ByteArrayDataOutput(buffer); write(out);// w w w. j a v a 2s . c om Channels.writeToChannel(buffer, channel); }
From source file:org.elasticsearch.index.translog.TranslogDeletionPolicyTests.java
License:Apache License
private Tuple<List<TranslogReader>, TranslogWriter> createReadersAndWriter(final long now) throws IOException { final Path tempDir = createTempDir(); Files.createFile(tempDir.resolve(Translog.CHECKPOINT_FILE_NAME)); TranslogWriter writer = null;//from w w w . java 2 s . co m List<TranslogReader> readers = new ArrayList<>(); final int numberOfReaders = randomIntBetween(0, 10); final String translogUUID = UUIDs.randomBase64UUID(random()); for (long gen = 1; gen <= numberOfReaders + 1; gen++) { if (writer != null) { final TranslogReader reader = Mockito.spy(writer.closeIntoReader()); Mockito.doReturn(writer.getLastModifiedTime()).when(reader).getLastModifiedTime(); readers.add(reader); } writer = TranslogWriter.create(new ShardId("index", "uuid", 0), translogUUID, gen, tempDir.resolve(Translog.getFilename(gen)), FileChannel::open, TranslogConfig.DEFAULT_BUFFER_SIZE, () -> 1L, 1L, () -> 1L); writer = Mockito.spy(writer); Mockito.doReturn(now - (numberOfReaders - gen + 1) * 1000).when(writer).getLastModifiedTime(); byte[] bytes = new byte[4]; ByteArrayDataOutput out = new ByteArrayDataOutput(bytes); for (int ops = randomIntBetween(0, 20); ops > 0; ops--) { out.reset(bytes); out.writeInt(ops); writer.add(new BytesArray(bytes), ops); } } return new Tuple<>(readers, writer); }
From source file:org.elasticsearch.index.translog.TranslogTests.java
License:Apache License
public void testTranslogWriter() throws IOException { final TranslogWriter writer = translog.createWriter(0); final int numOps = randomIntBetween(10, 100); byte[] bytes = new byte[4]; ByteArrayDataOutput out = new ByteArrayDataOutput(bytes); for (int i = 0; i < numOps; i++) { out.reset(bytes);/*from www. j av a2 s.c om*/ out.writeInt(i); writer.add(new BytesArray(bytes)); } writer.sync(); final BaseTranslogReader reader = randomBoolean() ? writer : translog.openReader(writer.path(), Checkpoint.read(translog.location().resolve(Translog.CHECKPOINT_FILE_NAME))); for (int i = 0; i < numOps; i++) { ByteBuffer buffer = ByteBuffer.allocate(4); reader.readBytes(buffer, reader.getFirstOperationOffset() + 4 * i); buffer.flip(); final int value = buffer.getInt(); assertEquals(i, value); } out.reset(bytes); out.writeInt(2048); writer.add(new BytesArray(bytes)); if (reader instanceof TranslogReader) { ByteBuffer buffer = ByteBuffer.allocate(4); try { reader.readBytes(buffer, reader.getFirstOperationOffset() + 4 * numOps); fail("read past EOF?"); } catch (EOFException ex) { // expected } ((TranslogReader) reader).close(); } else { // live reader! ByteBuffer buffer = ByteBuffer.allocate(4); final long pos = reader.getFirstOperationOffset() + 4 * numOps; reader.readBytes(buffer, pos); buffer.flip(); final int value = buffer.getInt(); assertEquals(2048, value); } IOUtils.close(writer); }
From source file:org.elasticsearch.index.translog.TranslogTests.java
License:Apache License
public void testFailWriterWhileClosing() throws IOException { Path tempDir = createTempDir(); final FailSwitch fail = new FailSwitch(); fail.failNever();//from ww w.j av a2 s.co m TranslogConfig config = getTranslogConfig(tempDir); try (Translog translog = getFailableTranslog(fail, config)) { final TranslogWriter writer = translog.createWriter(0); final int numOps = randomIntBetween(10, 100); byte[] bytes = new byte[4]; ByteArrayDataOutput out = new ByteArrayDataOutput(bytes); for (int i = 0; i < numOps; i++) { out.reset(bytes); out.writeInt(i); writer.add(new BytesArray(bytes)); } writer.sync(); try { fail.failAlways(); writer.closeIntoReader(); fail(); } catch (MockDirectoryWrapper.FakeIOException ex) { } try (TranslogReader reader = translog.openReader(writer.path(), Checkpoint.read(translog.location().resolve(Translog.CHECKPOINT_FILE_NAME)))) { for (int i = 0; i < numOps; i++) { ByteBuffer buffer = ByteBuffer.allocate(4); reader.readBytes(buffer, reader.getFirstOperationOffset() + 4 * i); buffer.flip(); final int value = buffer.getInt(); assertEquals(i, value); } } } }