List of usage examples for org.apache.lucene.store IndexInput length
public abstract long length();
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public static void commitSegments(String segmentsNew, Connection conn, Application app, Directory dir) { byte[] segmentContents = null; if (segmentsNew == null) { segmentsNew = TransFSDirectory.SEGMENTS_NEW;//TODO:IndexFileNames.getSegmentsNewFileName(); }//from w ww. j av a 2 s .com IndexInput input = null; try { input = dir.openInput(segmentsNew); int length = (int) input.length(); segmentContents = new byte[length]; try { input.readBytes(segmentContents, 0, length); } catch (IOException ioe) { segmentContents = null; } } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(LuceneManager.class, "commitSegments"), ex); throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + ex.getMessage()); } finally { if (input != null) { try { input.close(); } catch (Exception ignore) { } input = null; } } if (segmentContents == null || segmentContents.length == 0) { throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + "The segments.new file does not contain any data to save."); } PreparedStatement pstmt = null; ByteArrayInputStream bais = null; boolean exceptionOccured = false; try { String sql = "UPDATE Lucene SET valid = ?, version = ? " + "WHERE valid = ? AND db_home = ?"; pstmt = conn.prepareStatement(sql); int count = 1; pstmt.setBoolean(count++, false); pstmt.setInt(count++, getLuceneVersion()); pstmt.setBoolean(count++, true); pstmt.setString(count++, app.getDbDir().getName()); pstmt.executeUpdate(); pstmt.close(); pstmt = null; sql = "INSERT INTO Lucene (valid, db_home, segments, version) " + "VALUES (?,?,?,?)"; pstmt = conn.prepareStatement(sql); count = 1; pstmt.setBoolean(count++, true); pstmt.setString(count++, app.getDbDir().getName()); bais = new ByteArrayInputStream(segmentContents); pstmt.setBinaryStream(count++, bais, segmentContents.length); pstmt.setInt(count++, getLuceneVersion()); int rows = pstmt.executeUpdate(); if (rows < 1) { throw new Exception( "LuceneTransactionManager.executeTransaction(): update didn't affect any rows in the database"); } } catch (Exception ex) { exceptionOccured = true; throw new TransactionException(ex.getMessage()); } finally { try { dir.deleteFile(segmentsNew); } catch (IOException ioex) { // i guess its okay if a random segments.new file is lying around, itll // get overwritten on the next lucene write operation anyway app.logEvent(ErrorReporter.warningMsg(LuceneManager.class, "commitSegments") + "Could not delete " + segmentsNew); } if (bais != null) { try { bais.close(); } catch (Exception ignoreit) { } bais = null; } segmentContents = null; if (pstmt != null) { try { pstmt.close(); } catch (SQLException sqle) { if (!exceptionOccured) { throw new TransactionException(sqle.getMessage()); } } pstmt = null; } } }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public static void commitSegments(String segmentsNew, Connection conn, File dbhome, Directory dir) { byte[] segmentContents = null; if (segmentsNew == null) { segmentsNew = TransFSDirectory.SEGMENTS_NEW;//TODO:IndexFileNames.getSegmentsNewFileName(); }/* w w w . j av a 2s . c o m*/ IndexInput input = null; try { input = dir.openInput(segmentsNew); int length = (int) input.length(); segmentContents = new byte[length]; try { input.readBytes(segmentContents, 0, length); } catch (IOException ioe) { segmentContents = null; } } catch (Exception ex) { throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + ex.getMessage()); } finally { if (input != null) { try { input.close(); } catch (Exception ignore) { } input = null; } } if (segmentContents == null || segmentContents.length == 0) { throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + "The segments.new file does not contain any data to save."); } PreparedStatement pstmt = null; ByteArrayInputStream bais = null; boolean exceptionOccured = false; try { String sql = "UPDATE Lucene SET valid = ?, version = ? " + "WHERE valid = ? AND db_home = ?"; pstmt = conn.prepareStatement(sql); int count = 1; pstmt.setBoolean(count++, false); pstmt.setInt(count++, getLuceneVersion()); pstmt.setBoolean(count++, true); pstmt.setString(count++, dbhome.getName()); pstmt.executeUpdate(); pstmt.close(); pstmt = null; sql = "INSERT INTO Lucene (valid, db_home, segments, version) " + "VALUES (?,?,?,?)"; pstmt = conn.prepareStatement(sql); count = 1; pstmt.setBoolean(count++, true); pstmt.setString(count++, dbhome.getName()); bais = new ByteArrayInputStream(segmentContents); pstmt.setBinaryStream(count++, bais, segmentContents.length); pstmt.setInt(count++, getLuceneVersion()); int rows = pstmt.executeUpdate(); System.out.println("EXECUTE update was a SUCCESS!!"); if (rows < 1) { throw new Exception( "LuceneTransactionManager.executeTransaction(): update didn't affect any rows in the database"); } } catch (Exception ex) { exceptionOccured = true; throw new TransactionException(ex.getMessage()); } finally { try { dir.deleteFile(segmentsNew); } catch (IOException ioex) { // i guess its okay if a random segments.new file is lying around, itll // get overwritten on the next lucene write operation anyway } if (bais != null) { try { bais.close(); } catch (Exception ignoreit) { } bais = null; } segmentContents = null; if (pstmt != null) { try { pstmt.close(); } catch (SQLException sqle) { if (!exceptionOccured) { throw new TransactionException(sqle.getMessage()); } } pstmt = null; } } }
From source file:collene.ColDirectory.java
License:Apache License
/** @inheritDoc */ @Override/*from w w w .j a v a2 s .co m*/ public IndexInput openInput(String name, IOContext context) throws IOException { IndexInput input = new RowIndexInput(name, new RowReader(name, indexIO, meta)); // we cannot read a file that does not exist. Lucene relies on the fact that this method will throw an exception // when a file is not present. try { input.length(); } catch (NullPointerException ex) { throw new FileNotFoundException(name + " does not exist"); } return input; }
From source file:com.bah.lucene.BaseDirectoryTestSuite.java
License:Apache License
private void assertInputsEquals(String name, Directory fsDir, Directory hdfs) throws IOException { int reads = random.nextInt(MAX_NUMBER_OF_READS); IndexInput fsInput = fsDir.openInput(name, IOContext.DEFAULT); IndexInput hdfsInput = hdfs.openInput(name, IOContext.DEFAULT); assertEquals(fsInput.length(), hdfsInput.length()); int fileLength = (int) fsInput.length(); for (int i = 0; i < reads; i++) { byte[] fsBuf = new byte[random.nextInt(Math.min(MAX_BUFFER_SIZE - MIN_BUFFER_SIZE, fileLength)) + MIN_BUFFER_SIZE];//w w w .j a va2s. c o m byte[] hdfsBuf = new byte[fsBuf.length]; int offset = random.nextInt(fsBuf.length); int length = random.nextInt(fsBuf.length - offset); int pos = random.nextInt(fileLength - length); fsInput.seek(pos); fsInput.readBytes(fsBuf, offset, length); hdfsInput.seek(pos); hdfsInput.readBytes(hdfsBuf, offset, length); for (int f = offset; f < length; f++) { if (fsBuf[f] != hdfsBuf[f]) { fail(); } } } fsInput.close(); hdfsInput.close(); }
From source file:com.bah.lucene.blockcache.BlockDirectoryTest.java
License:Apache License
private void assertInputsEquals(String name, Directory fsDir, Directory hdfs) throws IOException { int reads = random.nextInt(MAX_NUMBER_OF_READS); IndexInput fsInput = fsDir.openInput(name, IOContext.DEFAULT); IndexInput hdfsInput = hdfs.openInput(name, IOContext.DEFAULT); assertEquals(fsInput.length(), hdfsInput.length()); int fileLength = (int) fsInput.length(); if (fileLength != 0) { for (int i = 0; i < reads; i++) { byte[] fsBuf = new byte[random.nextInt(Math.min(MAX_BUFFER_SIZE - MIN_BUFFER_SIZE, fileLength)) + MIN_BUFFER_SIZE];//from w w w . j a v a 2 s.co m byte[] hdfsBuf = new byte[fsBuf.length]; int offset = random.nextInt(fsBuf.length); int length = random.nextInt(fsBuf.length - offset); int pos = random.nextInt(fileLength - length); fsInput.seek(pos); fsInput.readBytes(fsBuf, offset, length); hdfsInput.seek(pos); hdfsInput.readBytes(hdfsBuf, offset, length); for (int f = offset; f < length; f++) { if (fsBuf[f] != hdfsBuf[f]) { fail(Long.toString(seed) + " read [" + i + "]"); } } } } fsInput.close(); hdfsInput.close(); }
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; }/* w w w . j a v a 2s . c om*/ 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.CacheIndexInput.java
License:Apache License
public CacheIndexInput(CacheDirectory directory, String fileName, IndexInput indexInput, Cache cache) throws IOException { super(fileName); _directory = directory;//from w ww .ja v a2 s . co m _fileName = fileName; _indexInput = indexInput; _fileLength = indexInput.length(); _cache = cache; _fileId = _cache.getFileId(_directory, _fileName); _cacheBlockSize = _cache.getCacheBlockSize(_directory, _fileName); _bufferSize = _cache.getFileBufferSize(_directory, _fileName); _quiet = _cache.shouldBeQuiet(_directory, _fileName); _key.setFileId(_fileId); _isClosed = false; }
From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java
License:Apache License
public static void readRandomData(IndexInput baseInput, IndexInput testInput, Random random, int sampleSize, int maxBufSize, int maxOffset) throws IOException { assertEquals(baseInput.length(), testInput.length()); int fileLength = (int) baseInput.length(); for (int i = 0; i < sampleSize; i++) { int position = random.nextInt(fileLength - maxBufSize); int bufSize = random.nextInt(maxBufSize - maxOffset) + 1; byte[] buf1 = new byte[bufSize]; byte[] buf2 = new byte[bufSize]; int offset = random.nextInt(Math.min(maxOffset, bufSize)); int len = Math.min(random.nextInt(bufSize - offset), fileLength - position); baseInput.seek(position);/*from w w w. j a v a2 s.c om*/ baseInput.readBytes(buf1, offset, len); testInput.seek(position); testInput.readBytes(buf2, offset, len); assertArrayEquals("Read [" + i + "] The position is [" + position + "] and bufSize [" + bufSize + "]", buf1, buf2); } }
From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java
License:Apache License
public static void readRandomDataInt(IndexInput baseInput, IndexInput testInput, Random random, int sampleSize) throws IOException { assertEquals(baseInput.length(), testInput.length()); int fileLength = (int) baseInput.length(); for (int i = 0; i < sampleSize; i++) { int position = random.nextInt(fileLength - 4); baseInput.seek(position);// ww w.j a v a 2 s. c o m int i1 = baseInput.readInt(); testInput.seek(position); int i2 = testInput.readInt(); assertEquals("Read [" + i + "] The position is [" + position + "]", i1, i2); } }
From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java
License:Apache License
public static void readRandomDataShort(IndexInput baseInput, IndexInput testInput, Random random, int sampleSize) throws IOException { assertEquals(baseInput.length(), testInput.length()); int fileLength = (int) baseInput.length(); for (int i = 0; i < sampleSize; i++) { int position = random.nextInt(fileLength - 2); baseInput.seek(position);/*from w ww. j a va 2s .c o m*/ short i1 = baseInput.readShort(); testInput.seek(position); short i2 = testInput.readShort(); assertEquals("Read [" + i + "] The position is [" + position + "]", i1, i2); } }