Example usage for org.apache.lucene.store IndexOutput writeBytes

List of usage examples for org.apache.lucene.store IndexOutput writeBytes

Introduction

In this page you can find the example usage for org.apache.lucene.store IndexOutput writeBytes.

Prototype

public void writeBytes(byte[] b, int length) throws IOException 

Source Link

Document

Writes an array of bytes.

Usage

From source file:com.bah.lucene.blockcache_v2.CacheDirectoryTest.java

License:Apache License

@Test
public void test2() throws IOException {
    IndexOutput output = _cacheDirectory.createOutput("test.file", IOContext.DEFAULT);
    byte[] buf = new byte[9000];
    for (int i = 0; i < buf.length; i++) {
        buf[i] = (byte) i;
    }// ww w.  java  2s  .  c  o  m
    output.writeBytes(buf, buf.length);
    output.close();

    IndexInput input = _cacheDirectory.openInput("test.file", IOContext.DEFAULT);
    assertEquals(9000, input.length());
    input.close();
}

From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java

License:Apache License

@Test
public void test1() throws IOException {
    RAMDirectory directory = new RAMDirectory();

    String name = "test";

    IndexOutput output = directory.createOutput(name, IOContext.DEFAULT);
    byte[] bs = "hello world".getBytes();
    output.writeBytes(bs, bs.length);
    output.close();/*  www.  j a  v  a2  s . c o  m*/

    IndexInput input = directory.openInput(name, IOContext.DEFAULT);
    Cache cache = getCache();
    CacheIndexInput cacheInput = new CacheIndexInput(null, name, input, cache);
    byte[] buf = new byte[bs.length];
    cacheInput.readBytes(buf, 0, buf.length);
    cacheInput.close();

    assertArrayEquals(bs, buf);
    directory.close();
}

From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java

License:Apache License

public static void writeRandomData(long size, Random random, IndexOutput... outputs) throws IOException {
    byte[] buf = new byte[1024];
    for (long l = 0; l < size; l += buf.length) {
        random.nextBytes(buf);/*from  w ww . ja  v a  2s.c o m*/
        int length = (int) Math.min(buf.length, size - l);
        for (IndexOutput output : outputs) {
            output.writeBytes(buf, length);
        }
    }
}

From source file:com.browseengine.bobo.geosearch.solo.impl.IDGeoRecordSerializer.java

License:Apache License

@Override
public void writeGeoRecord(IndexOutput output, IDGeoRecord record, int recordByteCount) throws IOException {
    if (record.id.length != recordByteCount - INTERLACE_BYTES) {
        throw new IllegalArgumentException(
                "Incorrect number of id bytes given.  " + "This is most likely a bug!  ExpectedBytes="
                        + (recordByteCount - INTERLACE_BYTES) + "; receivedBytes=" + record.id.length);
    }// w  w  w  .j  a v a 2s . c  o  m

    output.writeLong(record.highOrder);
    output.writeInt(record.lowOrder);
    output.writeBytes(record.id, record.id.length);
}

From source file:com.devwebsphere.wxslucene.GridDirectory.java

License:Open Source License

public static void copy(Directory src, GridDirectory dest, boolean closeDirSrc) throws IOException {
    final String[] files = src.listAll();
    IndexFileNameFilter filter = IndexFileNameFilter.getFilter();

    byte[] buf = new byte[COPY_BUFFER_SIZE];
    for (int i = 0; i < files.length; i++) {

        if (!filter.accept(null, files[i]))
            continue;

        IndexOutput os = null;
        ChecksumIndexInput is = null;//from   ww w .  ja  v a 2 s . co  m
        try {
            // create file in dest directory
            os = dest.createOutput(files[i]);
            // read current file
            is = new ChecksumIndexInput(src.openInput(files[i]));
            // and copy to dest directory
            long len = is.length();
            long readCount = 0;
            while (readCount < len) {
                int toRead = readCount + COPY_BUFFER_SIZE > len ? (int) (len - readCount) : COPY_BUFFER_SIZE;
                is.readBytes(buf, 0, toRead);
                os.writeBytes(buf, toRead);
                readCount += toRead;
            }
            long src_sum = is.getChecksum();
            os.flush();

            // this code can just compare the new file with the old one
            // to make sure it's copied correctly
            ChecksumIndexInput dst_check_stream = new ChecksumIndexInput(dest.openInput(files[i]));

            len = dst_check_stream.length();
            readCount = 0;
            while (readCount < len) {
                int toRead = readCount + COPY_BUFFER_SIZE > len ? (int) (len - readCount) : COPY_BUFFER_SIZE;
                dst_check_stream.readBytes(buf, 0, toRead);
                readCount += toRead;
            }
            long dst_sum = dst_check_stream.getChecksum();
            if (dst_sum == src_sum) {
                logger.log(Level.INFO, "Verify " + files[i] + " was successful");
            } else {
                logger.log(Level.INFO, "Verify " + files[i] + " failed");
                throw new IllegalStateException("File " + files[i] + " failed verification");
            }
        } finally {
            // graceful cleanup
            try {
                if (os != null)
                    os.close();
            } finally {
                if (is != null)
                    is.close();
            }
        }
    }
    if (closeDirSrc)
        src.close();
}

From source file:com.github.lucene.store.jdbc.index.AbstractIndexInputOutputITest.java

License:Apache License

private void insertData() throws IOException {
    final byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    final IndexOutput indexOutput = jdbcDirectory.createOutput("value1", new IOContext());
    indexOutput.writeInt(-1);//from www. j  av a2s . c  om
    indexOutput.writeLong(10);
    indexOutput.writeInt(0);
    indexOutput.writeInt(0);
    indexOutput.writeBytes(test, 8);
    indexOutput.writeBytes(test, 5);
    indexOutput.writeByte((byte) 8);
    indexOutput.writeBytes(new byte[] { 1, 2 }, 2);
    indexOutput.close();
}

From source file:com.github.lucene.store.jdbc.index.RAMJdbcIndexOutput.java

License:Apache License

public void flushToIndexOutput(final IndexOutput indexOutput) throws IOException {
    super.flush();
    if (file.buffers.size() == 0) {
        return;/*ww w .j ava  2s. c o  m*/
    }
    if (file.buffers.size() == 1) {
        indexOutput.writeBytes(file.buffers.get(0), (int) file.length);
        return;
    }
    final int tempSize = file.buffers.size() - 1;
    int i;
    for (i = 0; i < tempSize; i++) {
        indexOutput.writeBytes(file.buffers.get(i), bufferSize);
    }
    final int leftOver = (int) (file.length % bufferSize);
    if (leftOver == 0) {
        indexOutput.writeBytes(file.buffers.get(i), bufferSize);
    } else {
        indexOutput.writeBytes(file.buffers.get(i), leftOver);
    }
}

From source file:com.liferay.portal.search.lucene.dump.IndexCommitSerializationUtil.java

License:Open Source License

private static void deserializeSegment(InputStream inputStream, long length, IndexOutput indexOutput)
        throws IOException {

    try {//from  www. j a  v a2 s  .c  o m
        indexOutput.setLength(length);

        byte[] buffer = new byte[_BUFFER_SIZE];

        long received = 0;

        while (received < length) {
            int bufferSize = _BUFFER_SIZE;

            if ((received + _BUFFER_SIZE) > length) {
                bufferSize = (int) (length - received);
            }

            int actualSize = inputStream.read(buffer, 0, bufferSize);

            indexOutput.writeBytes(buffer, actualSize);

            received += actualSize;
        }
    } finally {
        indexOutput.close();
    }
}

From source file:com.lucure.core.codec.ForUtil.java

License:Apache License

/**
 * Write a block of data (<code>For</code> format).
 *
 * @param data     the data to write/*w ww  .  j  a v a 2 s .c  o  m*/
 * @param encoded  a buffer to use to encode data
 * @param out      the destination output
 * @throws IOException If there is a low-level I/O error
 */
void writeBlock(int[] data, byte[] encoded, IndexOutput out) throws IOException {
    if (isAllEqual(data)) {
        out.writeByte((byte) ALL_VALUES_EQUAL);
        out.writeVInt(data[0]);
        return;
    }

    final int numBits = bitsRequired(data);
    assert numBits > 0 && numBits <= 32 : numBits;
    final PackedInts.Encoder encoder = encoders[numBits];
    final int iters = iterations[numBits];
    assert iters * encoder.byteValueCount() >= BLOCK_SIZE;
    final int encodedSize = encodedSizes[numBits];
    assert iters * encoder.byteBlockCount() >= encodedSize;

    out.writeByte((byte) numBits);

    encoder.encode(data, 0, encoded, 0, iters);
    out.writeBytes(encoded, encodedSize);
}

From source file:com.zimbra.cs.index.LuceneDirectoryTest.java

License:Open Source License

@Test
public void write() throws IOException {
    long count = ZimbraPerf.COUNTER_IDX_BYTES_WRITTEN.getCount();
    long total = ZimbraPerf.COUNTER_IDX_BYTES_WRITTEN.getTotal();
    LuceneDirectory dir = LuceneDirectory.open(new File("/tmp"));
    IndexOutput out = dir.createOutput("write");
    out.writeBytes(new byte[] { 0, 1, 2 }, 3);
    out.close();/*from   w w w  .ja v a  2s  .  com*/

    Assert.assertEquals(1, ZimbraPerf.COUNTER_IDX_BYTES_WRITTEN.getCount() - count);
    Assert.assertEquals(3, ZimbraPerf.COUNTER_IDX_BYTES_WRITTEN.getTotal() - total);
}