Example usage for org.apache.lucene.store IOContext IOContext

List of usage examples for org.apache.lucene.store IOContext IOContext

Introduction

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

Prototype

public IOContext() 

Source Link

Usage

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  ww  w.  ja  v  a2 s  .co m
    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.AbstractIndexInputOutputITest.java

License:Apache License

private void verifyData() throws IOException {
    final byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    Assert.assertTrue(jdbcDirectory.fileExists("value1"));
    Assert.assertEquals(36, jdbcDirectory.fileLength("value1"));

    final IndexInput indexInput = jdbcDirectory.openInput("value1", new IOContext());
    Assert.assertEquals(-1, indexInput.readInt());
    Assert.assertEquals(10, indexInput.readLong());
    Assert.assertEquals(0, indexInput.readInt());
    Assert.assertEquals(0, indexInput.readInt());
    indexInput.readBytes(test, 0, 8);//from  w w  w .j  a v  a  2 s. c o m
    Assert.assertEquals((byte) 1, test[0]);
    Assert.assertEquals((byte) 8, test[7]);
    indexInput.readBytes(test, 0, 5);
    Assert.assertEquals((byte) 1, test[0]);
    Assert.assertEquals((byte) 5, test[4]);

    indexInput.seek(28);
    Assert.assertEquals((byte) 1, indexInput.readByte());
    indexInput.seek(30);
    Assert.assertEquals((byte) 3, indexInput.readByte());

    indexInput.close();
}

From source file:com.github.lucene.store.jdbc.JdbcDirectoryGeneralOperationsITest.java

License:Apache License

@Test
public void testList() throws IOException {
    Connection con = DataSourceUtils.getConnection(dataSource);
    jdbcDirectory.create();//  www . ja  v  a2 s .  co m
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    con = DataSourceUtils.getConnection(dataSource);
    String[] list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(0, list.length);

    con = DataSourceUtils.getConnection(dataSource);
    final IndexOutput indexOutput = jdbcDirectory.createOutput("test1", new IOContext());
    indexOutput.writeString("TEST STRING");
    indexOutput.close();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    jdbcTemplate.executeSelect("select * from test", new JdbcTemplate.ExecuteSelectCallback() {

        @Override
        public void fillPrepareStatement(final PreparedStatement ps) throws Exception {
        }

        @Override
        public Object execute(final ResultSet rs) throws Exception {
            Assert.assertTrue(rs.next());
            return null;
        }
    });

    con = DataSourceUtils.getConnection(dataSource);
    list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(1, list.length);

    con = DataSourceUtils.getConnection(dataSource);
    jdbcDirectory.deleteFile("test1");
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    con = DataSourceUtils.getConnection(dataSource);
    list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(0, list.length);
}

From source file:com.github.lucene.store.jdbc.JdbcDirectoryGeneralOperationsITest.java

License:Apache License

@Test
public void testListWithinTransaction() throws IOException {
    final Connection con = DataSourceUtils.getConnection(dataSource);

    jdbcDirectory.create();/*  ww  w. java2 s . c  o  m*/

    String[] list = jdbcDirectory.listAll();
    Assert.assertEquals(0, list.length);

    final IndexOutput indexOutput = jdbcDirectory.createOutput("test1", new IOContext());
    indexOutput.writeString("TEST STRING");
    indexOutput.close();

    jdbcTemplate.executeSelect("select * from test", new JdbcTemplate.ExecuteSelectCallback() {

        @Override
        public void fillPrepareStatement(final PreparedStatement ps) throws Exception {
        }

        @Override
        public Object execute(final ResultSet rs) throws Exception {
            Assert.assertTrue(rs.next());
            return null;
        }
    });

    list = jdbcDirectory.listAll();
    Assert.assertEquals(1, list.length);

    jdbcDirectory.deleteFile("test1");
    list = jdbcDirectory.listAll();
    Assert.assertEquals(0, list.length);

    DataSourceUtils.rollbackConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
}

From source file:com.github.lucene.store.jdbc.JdbcDirectoryGeneralOperationsITest.java

License:Apache License

@Test
public void testDeleteContent() throws IOException {
    Connection con = DataSourceUtils.getConnection(dataSource);
    jdbcDirectory.create();//from  w w  w . j ava2  s . c o  m
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    con = DataSourceUtils.getConnection(dataSource);
    String[] list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(0, list.length);

    con = DataSourceUtils.getConnection(dataSource);
    final IndexOutput indexOutput = jdbcDirectory.createOutput("test1", new IOContext());
    indexOutput.writeString("TEST STRING");
    indexOutput.close();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    con = DataSourceUtils.getConnection(dataSource);
    list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(1, list.length);

    con = DataSourceUtils.getConnection(dataSource);
    jdbcDirectory.deleteContent();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);

    con = DataSourceUtils.getConnection(dataSource);
    list = jdbcDirectory.listAll();
    DataSourceUtils.commitConnectionIfPossible(con);
    DataSourceUtils.releaseConnection(con);
    Assert.assertEquals(0, list.length);
}

From source file:com.xiaomi.linden.hadoop.indexing.reduce.RAMDirectoryUtil.java

License:Apache License

/**
 * Write a number of files from a ram directory to a data output.
 * @param out  the data output/*from   ww  w .  j av a 2 s  . c  om*/
 * @param dir  the ram directory
 * @throws IOException
 */
public static void writeRAMFiles(DataOutput out, RAMDirectory dir) throws IOException {
    String[] names = dir.listAll();
    out.writeInt(names.length);
    for (int i = 0; i < names.length; i++) {
        Text.writeString(out, names[i]);
        long length = dir.fileLength(names[i]);
        out.writeLong(length);

        if (length > 0) {
            // can we avoid the extra copy?
            IndexInput input = null;
            try {
                IOContext context = new IOContext();
                input = dir.openInput(names[i], context);

                int position = 0;
                byte[] buffer = new byte[BUFFER_SIZE];

                while (position < length) {
                    int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
                    input.readBytes(buffer, 0, len);
                    out.write(buffer, 0, len);
                    position += len;
                }
            } finally {
                if (input != null) {
                    input.close();
                }
            }
        }
    }
}

From source file:com.xiaomi.linden.hadoop.indexing.reduce.RAMDirectoryUtil.java

License:Apache License

/**
 * Read a number of files from a data input to a ram directory.
 * @param in  the data input/*from w ww.j av a  2s .  co  m*/
 * @param dir  the ram directory
 * @throws IOException
 */
public static void readRAMFiles(DataInput in, RAMDirectory dir) throws IOException {
    int numFiles = in.readInt();

    for (int i = 0; i < numFiles; i++) {
        String name = Text.readString(in);
        long length = in.readLong();

        if (length > 0) {
            // can we avoid the extra copy?
            IndexOutput output = null;
            try {
                IOContext context = new IOContext();
                output = dir.createOutput(name, context);

                int position = 0;
                byte[] buffer = new byte[BUFFER_SIZE];

                while (position < length) {
                    int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
                    in.readFully(buffer, 0, len);
                    output.writeBytes(buffer, 0, len);
                    position += len;
                }
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        }
    }
}

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryLoader.java

License:Apache License

private synchronized void loadIndex() throws NoIndexAvailableException {
    if (searcher == null) {
        try {/*  w ww . jav  a2s  . c  o  m*/
            logger.info("Loading index from directory " + environment.getLuceneIndexDir().getAbsolutePath());
            NIOFSDirectory directory = new NIOFSDirectory(environment.getLuceneIndexDir());
            ram = new RAMDirectory(directory, new IOContext());
            reader = DirectoryReader.open(ram);
            searcher = new IndexSearcher(reader);
            searcher.setSimilarity(new SimilarityBase() {

                @Override
                public String toString() {
                    return "Constant Similarity";
                }

                @Override
                protected float score(BasicStats stats, float freq, float docLen) {
                    return stats.getTotalBoost();
                }

            });
            directory.close();
            logger.info("Index loaded.");
        } catch (IOException e) {
            throw new NoIndexAvailableException("Failed to load index", e);
        }
    }
}

From source file:io.datalayer.lucene.helper.AosUtil.java

License:Apache License

public void dirCopy() throws Exception {
    Directory otherDir = null;// w ww  .ja va 2s .  c o  m
    Directory ramDir = new RAMDirectory(otherDir, new IOContext());
}

From source file:org.apache.solr.store.blockcache.BlockDirectoryTest.java

License:Apache License

private void testEof(String name, Directory directory, long length) throws IOException {
    IndexInput input = directory.openInput(name, new IOContext());
    try {/*from   w  w w .ja  v a2  s .  c o m*/
        input.seek(length);
        try {
            input.readByte();
            fail("should throw eof");
        } catch (IOException e) {
        }
    } finally {
        input.close();
    }
}