List of usage examples for org.apache.lucene.store IndexOutput writeByte
public abstract void writeByte(byte b) throws IOException;
From source file:com.bah.lucene.BaseDirectoryTestSuite.java
License:Apache License
private Directory getControlDir(final Directory control, final Directory test) { return new Directory() { @Override// w w w. j av a 2 s.co m public Lock makeLock(String name) { return control.makeLock(name); } @Override public void clearLock(String name) throws IOException { control.clearLock(name); } @Override public void setLockFactory(LockFactory lockFactory) throws IOException { control.setLockFactory(lockFactory); } @Override public LockFactory getLockFactory() { return control.getLockFactory(); } @Override public String getLockID() { return control.getLockID(); } @Override public void copy(Directory to, String src, String dest, IOContext context) throws IOException { control.copy(to, src, dest, context); } @Override public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException { return control.createSlicer(name, context); } @Override public IndexOutput createOutput(final String name, IOContext context) throws IOException { final IndexOutput testOutput = test.createOutput(name, context); final IndexOutput controlOutput = control.createOutput(name, context); return new IndexOutput() { @Override public void flush() throws IOException { testOutput.flush(); controlOutput.flush(); } @Override public void close() throws IOException { testOutput.close(); controlOutput.close(); } @Override public long getFilePointer() { long filePointer = testOutput.getFilePointer(); long controlFilePointer = controlOutput.getFilePointer(); if (controlFilePointer != filePointer) { System.err.println("Output Name [" + name + "] with filePointer [" + filePointer + "] and control filePointer [" + controlFilePointer + "] does not match"); } return filePointer; } @SuppressWarnings("deprecation") @Override public void seek(long pos) throws IOException { testOutput.seek(pos); controlOutput.seek(pos); } @Override public long length() throws IOException { long length = testOutput.length(); long controlLength = controlOutput.length(); if (controlLength != length) { System.err.println("Ouput Name [" + name + "] with length [" + length + "] and control length [" + controlLength + "] does not match"); } return length; } @Override public void writeByte(byte b) throws IOException { testOutput.writeByte(b); controlOutput.writeByte(b); } @Override public void writeBytes(byte[] b, int offset, int length) throws IOException { testOutput.writeBytes(b, offset, length); controlOutput.writeBytes(b, offset, length); } }; } @Override public IndexInput openInput(final String name, IOContext context) throws IOException { final IndexInput testInput = test.openInput(name, context); final IndexInput controlInput = control.openInput(name, context); return new IndexInputCompare(name, testInput, controlInput); } @Override public String[] listAll() throws IOException { return test.listAll(); } @Override public boolean fileExists(String name) throws IOException { return test.fileExists(name); } @Override public void deleteFile(String name) throws IOException { test.deleteFile(name); control.deleteFile(name); } @Override public long fileLength(String name) throws IOException { long fileLength = test.fileLength(name); long controlFileLength = control.fileLength(name); if (controlFileLength != fileLength) { System.err.println("Input Name [" + name + "] with length [" + fileLength + "] and control length [" + controlFileLength + "] does not match"); } return fileLength; } @Override public void sync(Collection<String> names) throws IOException { test.sync(names); test.sync(names); } @Override public void close() throws IOException { test.close(); control.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 w ww .j av a2 s . com 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.lucure.core.codec.ForUtil.java
License:Apache License
/** * Write a block of data (<code>For</code> format). * * @param data the data to write/* w w w. jav a 2 s . c om*/ * @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:net.mongonet.lucene.store.je.JEMongoStoreTest.java
License:Apache License
public void testBytes() throws Exception { final int count = 250; final int LENGTH_MASK = 0xffff; Random r = random;//from w w w. j a v a 2s. co m final long seed = r.nextLong(); Random gen = new Random(seed); int totalLength = 0; int duration; Date end; Date veryStart = new Date(); Date start = new Date(); JEMongoDirectory store = null; if (VERBOSE) System.out.println("Writing files byte by byte"); try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); for (int i = 0; i < count; i++) { String name = i + ".dat"; int length = gen.nextInt() & LENGTH_MASK; IndexOutput file = store.createOutput(name); totalLength += length; for (int j = 0; j < length; j++) { byte b = (byte) (gen.nextInt() & 0x7F); file.writeByte(b); } file.close(); } store.commitTransaction(); } catch (IOException e) { store.abortTransaction(); } finally { store.close(); } end = new Date(); if (VERBOSE) { duration = (int) (end.getTime() - start.getTime()); System.out.print(duration); System.out.print(" total milliseconds to create, "); System.out.print(totalLength / duration); System.out.println(" kb/s"); } try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); gen = new Random(seed); start = new Date(); for (int i = 0; i < count; i++) { String name = i + ".dat"; int length = gen.nextInt() & LENGTH_MASK; IndexInput file = store.openInput(name); if (file.length() != length) throw new Exception("length incorrect"); for (int j = 0; j < length; j++) { byte b = (byte) (gen.nextInt() & 0x7F); if (file.readByte() != b) throw new Exception("contents incorrect"); } file.close(); } } catch (IOException e) { store.abortTransaction(); throw e; } catch (DatabaseException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { duration = (int) (end.getTime() - start.getTime()); System.out.print(duration); System.out.print(" total milliseconds to read, "); System.out.print(totalLength / duration); System.out.println(" kb/s"); } try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); gen = new Random(seed); start = new Date(); for (int i = 0; i < count; i++) { String name = i + ".dat"; store.deleteFile(name); } } catch (IOException e) { store.abortTransaction(); throw e; } catch (DatabaseException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { System.out.print(end.getTime() - start.getTime()); System.out.println(" total milliseconds to delete"); System.out.print(end.getTime() - veryStart.getTime()); System.out.println(" total milliseconds"); } }
From source file:net.mongonet.lucene.store.je.JEMongoStoreTest.java
License:Apache License
public void testDelete() throws Exception { final int count = 250; final int LENGTH_MASK = 0xffff; Random r = random;/* w ww .j a v a 2 s .com*/ final long seed = r.nextLong(); Random gen = new Random(seed); int totalLength = 0; int duration; Date end; Date veryStart = new Date(); Date start = new Date(); JEMongoDirectory store = null; if (VERBOSE) System.out.println("Writing files byte by byte"); try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); for (int i = 0; i < count; i++) { String name = i + ".dat"; int length = gen.nextInt() & LENGTH_MASK; IndexOutput file = store.createOutput(name); totalLength += length; for (int j = 0; j < length; j++) { byte b = (byte) (gen.nextInt() & 0x7F); file.writeByte(b); } file.close(); } } catch (IOException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { duration = (int) (end.getTime() - start.getTime()); System.out.print(duration); System.out.print(" total milliseconds to read, "); System.out.print(totalLength / duration); System.out.println(" kb/s"); } try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); gen = new Random(seed); start = new Date(); for (int i = 0; i < count; i++) { if (i % 2 == 0) { String name = i + ".dat"; store.deleteFile(name); } } } catch (IOException e) { store.abortTransaction(); throw e; } catch (DatabaseException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { System.out.print(end.getTime() - start.getTime()); System.out.println(" total milliseconds to delete even files"); duration = (int) (end.getTime() - start.getTime()); System.out.print(duration); System.out.print(" total milliseconds to create, "); System.out.print(totalLength / duration); System.out.println(" kb/s"); } try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); gen = new Random(seed); start = new Date(); for (int i = 0; i < count; i++) { int length = gen.nextInt() & LENGTH_MASK; if (i % 2 != 0) { String name = i + ".dat"; IndexInput file = store.openInput(name); if (file.length() != length) throw new Exception("length incorrect"); for (int j = 0; j < length; j++) { byte b = (byte) (gen.nextInt() & 0x7F); if (file.readByte() != b) throw new Exception("contents incorrect"); } file.close(); } else { for (int j = 0; j < length; j++) { gen.nextInt(); } } } } catch (IOException e) { store.abortTransaction(); throw e; } catch (DatabaseException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { duration = (int) (end.getTime() - start.getTime()); System.out.print(duration); System.out.print(" total milliseconds to read, "); System.out.print(totalLength / duration); System.out.println(" kb/s"); } try { store = new JEMongoDirectory(env, dbConfig); store.beginTransaction(); gen = new Random(seed); start = new Date(); for (int i = 0; i < count; i++) { if (i % 2 != 0) { String name = i + ".dat"; store.deleteFile(name); } } } catch (IOException e) { store.abortTransaction(); throw e; } catch (DatabaseException e) { store.abortTransaction(); throw e; } finally { store.commitTransaction(); store.close(); } end = new Date(); if (VERBOSE) { System.out.print(end.getTime() - start.getTime()); System.out.println(" total milliseconds to delete"); System.out.print(end.getTime() - veryStart.getTime()); System.out.println(" total milliseconds"); } Cursor cursor = null; try { store = new JEMongoDirectory(env, dbConfig); try { cursor = store.getIndex().openCursor(null, null); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundData = new DatabaseEntry(); if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { fail("index database is not empty"); } } catch (DatabaseException e) { throw e; } finally { if (cursor != null) cursor.close(); } cursor = null; try { cursor = store.getBlocks().openCursor(null, null); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundData = new DatabaseEntry(); if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { fail("blocks database is not empty"); } } catch (DatabaseException e) { throw e; } finally { if (cursor != null) cursor.close(); } } catch (DatabaseException e) { throw e; } finally { store.close(); } }
From source file:org.apache.blur.lucene.codec.Blur022SegmentInfoWriter.java
License:Apache License
@Override public void write(Directory dir, SegmentInfo si, FieldInfos fis, IOContext ioContext) throws IOException { final String fileName = IndexFileNames.segmentFileName(si.name, "", Blur022SegmentInfoFormat.SI_EXTENSION); si.addFile(fileName);/*from w w w.j a v a 2 s. c o m*/ final IndexOutput output = dir.createOutput(fileName, ioContext); boolean success = false; try { CodecUtil.writeHeader(output, Blur022SegmentInfoFormat.CODEC_NAME, Blur022SegmentInfoFormat.VERSION_CURRENT); output.writeString(si.getVersion()); output.writeInt(si.getDocCount()); output.writeByte((byte) (si.getUseCompoundFile() ? SegmentInfo.YES : SegmentInfo.NO)); output.writeStringStringMap(si.getDiagnostics()); Map<String, String> attributes = si.attributes(); TreeMap<String, String> newAttributes = new TreeMap<String, String>(); if (attributes != null) { newAttributes.putAll(attributes); } newAttributes.put(Blur022StoredFieldsFormat.STORED_FIELDS_FORMAT_CHUNK_SIZE, Integer.toString(_compressionChunkSize)); newAttributes.put(Blur022StoredFieldsFormat.STORED_FIELDS_FORMAT_COMPRESSION_MODE, _compressionMode); output.writeStringStringMap(newAttributes); output.writeStringSet(si.files()); success = true; } finally { if (!success) { IOUtils.closeWhileHandlingException(output); si.dir.deleteFile(fileName); } else { output.close(); } } }
From source file:org.apache.blur.store.BaseDirectoryTestSuite.java
License:Apache License
private String writeFile(Directory dir, long length) throws IOException { IndexOutput output = dir.createOutput(OUT_DAT, IOContext.DEFAULT); for (long l = 0; l < length; l++) { output.writeByte((byte) 1); }// w ww. ja va 2 s.c o m output.close(); return OUT_DAT; }
From source file:org.compass.core.test.localcache.MemoryLocalCacheTests.java
License:Apache License
private void insertData(MemoryDirectoryCache dir) throws IOException { byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; IndexOutput indexOutput = dir.createOutput("value1"); indexOutput.writeBytes(new byte[] { 2, 4, 6, 7, 8 }, 5); indexOutput.writeInt(-1);/*from www. j ava 2s. co m*/ indexOutput.writeLong(10); indexOutput.writeInt(0); indexOutput.writeInt(0); indexOutput.writeBytes(test, 8); indexOutput.writeBytes(test, 5); indexOutput.seek(0); indexOutput.writeByte((byte) 8); if (dir.getBucketSize() > 4) { indexOutput.seek(2); indexOutput.writeBytes(new byte[] { 1, 2 }, 2); } indexOutput.close(); }
From source file:org.compass.needle.coherence.AbstractCoherenceDirectoryTests.java
License:Apache License
private void insertData(CoherenceDirectory dir, String fileName) throws IOException { byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; IndexOutput indexOutput = dir.createOutput(fileName); indexOutput.writeBytes(new byte[] { 2, 4, 6, 7, 8 }, 5); indexOutput.writeInt(-1);//from ww w . ja va 2 s. c o m indexOutput.writeLong(10); indexOutput.writeInt(0); indexOutput.writeInt(0); indexOutput.writeBytes(test, 8); indexOutput.writeBytes(test, 5); indexOutput.seek(0); indexOutput.writeByte((byte) 8); if (dir.getBucketSize() > 4) { indexOutput.seek(2); indexOutput.writeBytes(new byte[] { 1, 2 }, 2); } indexOutput.close(); }
From source file:org.compass.needle.gae.GoogleAppEngineDirectoryTests.java
License:Apache License
private void insertData(GoogleAppEngineDirectory dir, String fileName) throws IOException { byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; IndexOutput indexOutput = dir.createOutput(fileName); indexOutput.writeBytes(new byte[] { 2, 4, 6, 7, 8 }, 5); indexOutput.writeInt(-1);/* ww w . j av a 2 s . c o m*/ indexOutput.writeLong(10); indexOutput.writeInt(0); indexOutput.writeInt(0); indexOutput.writeBytes(test, 8); indexOutput.writeBytes(test, 5); indexOutput.seek(0); indexOutput.writeByte((byte) 8); if (dir.getBucketSize() > 4) { indexOutput.seek(2); indexOutput.writeBytes(new byte[] { 1, 2 }, 2); } indexOutput.close(); }