mirror of https://github.com/apache/lucene.git
Replace OutputStream with IndexOutput and BufferedIndexOutput to permit unbuffered Directory implementations.
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150536 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1b8843fa04
commit
79892c1f40
|
@ -58,8 +58,9 @@ $Id$
|
|||
|
||||
12. Permit unbuffered Directory implementations (e.g., using mmap).
|
||||
InputStream is replaced by the new classes IndexInput and
|
||||
BufferedIndexInput. InputStream is now deprecated and FSDirectory
|
||||
is now subclassable. (cutting)
|
||||
BufferedIndexInput. OutputStream is replaced by the new classes
|
||||
IndexOutput and BufferedIndexOutput. InputStream and OutputStream
|
||||
are now deprecated and FSDirectory is now subclassable. (cutting)
|
||||
|
||||
13. Fixed bug #31241: Sorting could lead to incorrect results (documents
|
||||
missing, others duplicated) if the sort keys were not unique and there
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.lucene.index;
|
|||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.BufferedIndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.Lock;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
|
@ -171,7 +171,7 @@ class CompoundFileReader extends Directory {
|
|||
|
||||
/** Not implemented
|
||||
* @throws UnsupportedOperationException */
|
||||
public OutputStream createFile(String name)
|
||||
public IndexOutput createOutput(String name)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ package org.apache.lucene.index;
|
|||
*/
|
||||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import java.util.LinkedList;
|
||||
import java.util.HashSet;
|
||||
|
@ -140,9 +140,9 @@ final class CompoundFileWriter {
|
|||
merged = true;
|
||||
|
||||
// open the compound stream
|
||||
OutputStream os = null;
|
||||
IndexOutput os = null;
|
||||
try {
|
||||
os = directory.createFile(fileName);
|
||||
os = directory.createOutput(fileName);
|
||||
|
||||
// Write the number of entries
|
||||
os.writeVInt(entries.size());
|
||||
|
@ -180,7 +180,7 @@ final class CompoundFileWriter {
|
|||
// close so that if an exception occurs during the close, the
|
||||
// finally clause below will not attempt to close the stream
|
||||
// the second time.
|
||||
OutputStream tmp = os;
|
||||
IndexOutput tmp = os;
|
||||
os = null;
|
||||
tmp.close();
|
||||
|
||||
|
@ -193,7 +193,7 @@ final class CompoundFileWriter {
|
|||
* provided output stream. Use the provided buffer for moving data
|
||||
* to reduce memory allocation.
|
||||
*/
|
||||
private void copyFile(FileEntry source, OutputStream os, byte buffer[])
|
||||
private void copyFile(FileEntry source, IndexOutput os, byte buffer[])
|
||||
throws IOException
|
||||
{
|
||||
IndexInput is = null;
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.apache.lucene.analysis.Analyzer;
|
|||
import org.apache.lucene.analysis.TokenStream;
|
||||
import org.apache.lucene.analysis.Token;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.search.Similarity;
|
||||
|
||||
final class DocumentWriter {
|
||||
|
@ -247,13 +247,13 @@ final class DocumentWriter {
|
|||
|
||||
private final void writePostings(Posting[] postings, String segment)
|
||||
throws IOException {
|
||||
OutputStream freq = null, prox = null;
|
||||
IndexOutput freq = null, prox = null;
|
||||
TermInfosWriter tis = null;
|
||||
TermVectorsWriter termVectorWriter = null;
|
||||
try {
|
||||
//open files for inverse index storage
|
||||
freq = directory.createFile(segment + ".frq");
|
||||
prox = directory.createFile(segment + ".prx");
|
||||
freq = directory.createOutput(segment + ".frq");
|
||||
prox = directory.createOutput(segment + ".prx");
|
||||
tis = new TermInfosWriter(directory, segment, fieldInfos);
|
||||
TermInfo ti = new TermInfo();
|
||||
String currentField = null;
|
||||
|
@ -321,7 +321,7 @@ final class DocumentWriter {
|
|||
FieldInfo fi = fieldInfos.fieldInfo(n);
|
||||
if(fi.isIndexed){
|
||||
float norm = fieldBoosts[n] * similarity.lengthNorm(fi.name, fieldLengths[n]);
|
||||
OutputStream norms = directory.createFile(segment + ".f" + n);
|
||||
IndexOutput norms = directory.createOutput(segment + ".f" + n);
|
||||
try {
|
||||
norms.writeByte(Similarity.encodeNorm(norm));
|
||||
} finally {
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.apache.lucene.document.Document;
|
|||
import org.apache.lucene.document.Field;
|
||||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
|
||||
/** Access to the Field Info file that describes document fields and whether or
|
||||
|
@ -167,7 +167,7 @@ final class FieldInfos {
|
|||
}
|
||||
|
||||
public void write(Directory d, String name) throws IOException {
|
||||
OutputStream output = d.createFile(name);
|
||||
IndexOutput output = d.createOutput(name);
|
||||
try {
|
||||
write(output);
|
||||
} finally {
|
||||
|
@ -175,7 +175,7 @@ final class FieldInfos {
|
|||
}
|
||||
}
|
||||
|
||||
public void write(OutputStream output) throws IOException {
|
||||
public void write(IndexOutput output) throws IOException {
|
||||
output.writeVInt(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
FieldInfo fi = fieldInfo(i);
|
||||
|
|
|
@ -22,20 +22,20 @@ import java.util.Enumeration;
|
|||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
|
||||
final class FieldsWriter
|
||||
{
|
||||
private FieldInfos fieldInfos;
|
||||
|
||||
private OutputStream fieldsStream;
|
||||
private IndexOutput fieldsStream;
|
||||
|
||||
private OutputStream indexStream;
|
||||
private IndexOutput indexStream;
|
||||
|
||||
FieldsWriter(Directory d, String segment, FieldInfos fn) throws IOException {
|
||||
fieldInfos = fn;
|
||||
fieldsStream = d.createFile(segment + ".fdt");
|
||||
indexStream = d.createFile(segment + ".fdx");
|
||||
fieldsStream = d.createOutput(segment + ".fdt");
|
||||
indexStream = d.createOutput(segment + ".fdx");
|
||||
}
|
||||
|
||||
final void close() throws IOException {
|
||||
|
@ -80,4 +80,4 @@ final class FieldsWriter
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.lucene.store.RAMDirectory;
|
|||
import org.apache.lucene.store.FSDirectory;
|
||||
import org.apache.lucene.store.Lock;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.search.Similarity;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
|
@ -617,7 +617,7 @@ public class IndexWriter {
|
|||
}
|
||||
|
||||
private final void writeDeleteableFiles(Vector files) throws IOException {
|
||||
OutputStream output = directory.createFile("deleteable.new");
|
||||
IndexOutput output = directory.createOutput("deleteable.new");
|
||||
try {
|
||||
output.writeInt(files.size());
|
||||
for (int i = 0; i < files.size(); i++)
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.util.Vector;
|
|||
import java.io.IOException;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
|
||||
final class SegmentInfos extends Vector {
|
||||
|
||||
|
@ -70,7 +70,7 @@ final class SegmentInfos extends Vector {
|
|||
}
|
||||
|
||||
public final void write(Directory directory) throws IOException {
|
||||
OutputStream output = directory.createFile("segments.new");
|
||||
IndexOutput output = directory.createOutput("segments.new");
|
||||
try {
|
||||
output.writeInt(FORMAT); // write FORMAT
|
||||
output.writeLong(++version); // every write changes the index
|
||||
|
|
|
@ -21,7 +21,7 @@ import java.util.Iterator;
|
|||
import java.io.IOException;
|
||||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.RAMOutputStream;
|
||||
|
||||
/**
|
||||
|
@ -225,16 +225,16 @@ final class SegmentMerger {
|
|||
}
|
||||
}
|
||||
|
||||
private OutputStream freqOutput = null;
|
||||
private OutputStream proxOutput = null;
|
||||
private IndexOutput freqOutput = null;
|
||||
private IndexOutput proxOutput = null;
|
||||
private TermInfosWriter termInfosWriter = null;
|
||||
private int skipInterval;
|
||||
private SegmentMergeQueue queue = null;
|
||||
|
||||
private final void mergeTerms() throws IOException {
|
||||
try {
|
||||
freqOutput = directory.createFile(segment + ".frq");
|
||||
proxOutput = directory.createFile(segment + ".prx");
|
||||
freqOutput = directory.createOutput(segment + ".frq");
|
||||
proxOutput = directory.createOutput(segment + ".prx");
|
||||
termInfosWriter =
|
||||
new TermInfosWriter(directory, segment, fieldInfos);
|
||||
skipInterval = termInfosWriter.skipInterval;
|
||||
|
@ -404,7 +404,7 @@ final class SegmentMerger {
|
|||
for (int i = 0; i < fieldInfos.size(); i++) {
|
||||
FieldInfo fi = fieldInfos.fieldInfo(i);
|
||||
if (fi.isIndexed) {
|
||||
OutputStream output = directory.createFile(segment + ".f" + i);
|
||||
IndexOutput output = directory.createOutput(segment + ".f" + i);
|
||||
try {
|
||||
for (int j = 0; j < readers.size(); j++) {
|
||||
IndexReader reader = (IndexReader) readers.elementAt(j);
|
||||
|
|
|
@ -26,7 +26,7 @@ import java.util.Vector;
|
|||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BitVector;
|
||||
|
||||
|
@ -69,7 +69,7 @@ class SegmentReader extends IndexReader {
|
|||
|
||||
private void reWrite() throws IOException {
|
||||
// NOTE: norms are re-written in regular directory, not cfs
|
||||
OutputStream out = directory().createFile(segment + ".tmp");
|
||||
IndexOutput out = directory().createOutput(segment + ".tmp");
|
||||
try {
|
||||
out.writeBytes(bytes, maxDoc());
|
||||
} finally {
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.lucene.index;
|
|||
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.StringHelper;
|
||||
|
||||
|
@ -30,7 +30,7 @@ final class TermInfosWriter {
|
|||
public static final int FORMAT = -2;
|
||||
|
||||
private FieldInfos fieldInfos;
|
||||
private OutputStream output;
|
||||
private IndexOutput output;
|
||||
private Term lastTerm = new Term("", "");
|
||||
private TermInfo lastTi = new TermInfo();
|
||||
private long size = 0;
|
||||
|
@ -77,7 +77,7 @@ final class TermInfosWriter {
|
|||
boolean isi) throws IOException {
|
||||
fieldInfos = fis;
|
||||
isIndex = isi;
|
||||
output = directory.createFile(segment + (isIndex ? ".tii" : ".tis"));
|
||||
output = directory.createOutput(segment + (isIndex ? ".tii" : ".tis"));
|
||||
output.writeInt(FORMAT); // write format
|
||||
output.writeLong(0); // leave space for size
|
||||
output.writeInt(indexInterval); // write indexInterval
|
||||
|
|
|
@ -17,7 +17,7 @@ package org.apache.lucene.index;
|
|||
*/
|
||||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.util.StringHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -58,7 +58,7 @@ final class TermVectorsWriter {
|
|||
public static final String TVX_EXTENSION = ".tvx";
|
||||
public static final String TVD_EXTENSION = ".tvd";
|
||||
public static final String TVF_EXTENSION = ".tvf";
|
||||
private OutputStream tvx = null, tvd = null, tvf = null;
|
||||
private IndexOutput tvx = null, tvd = null, tvf = null;
|
||||
private Vector fields = null;
|
||||
private Vector terms = null;
|
||||
private FieldInfos fieldInfos;
|
||||
|
@ -77,11 +77,11 @@ final class TermVectorsWriter {
|
|||
FieldInfos fieldInfos)
|
||||
throws IOException {
|
||||
// Open files for TermVector storage
|
||||
tvx = directory.createFile(segment + TVX_EXTENSION);
|
||||
tvx = directory.createOutput(segment + TVX_EXTENSION);
|
||||
tvx.writeInt(FORMAT_VERSION);
|
||||
tvd = directory.createFile(segment + TVD_EXTENSION);
|
||||
tvd = directory.createOutput(segment + TVD_EXTENSION);
|
||||
tvd.writeInt(FORMAT_VERSION);
|
||||
tvf = directory.createFile(segment + TVF_EXTENSION);
|
||||
tvf = directory.createOutput(segment + TVF_EXTENSION);
|
||||
tvf.writeInt(FORMAT_VERSION);
|
||||
|
||||
this.fieldInfos = fieldInfos;
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.io.IOException;
|
|||
|
||||
/** Base implementation class for buffered {@link IndexInput}. */
|
||||
public abstract class BufferedIndexInput extends IndexInput {
|
||||
static final int BUFFER_SIZE = OutputStream.BUFFER_SIZE;
|
||||
static final int BUFFER_SIZE = BufferedIndexOutput.BUFFER_SIZE;
|
||||
|
||||
private byte[] buffer;
|
||||
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
package org.apache.lucene.store;
|
||||
|
||||
/**
|
||||
* Copyright 2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** Base implementation class for buffered {@link IndexOutput}. */
|
||||
public abstract class BufferedIndexOutput extends IndexOutput {
|
||||
static final int BUFFER_SIZE = 1024;
|
||||
|
||||
private final byte[] buffer = new byte[BUFFER_SIZE];
|
||||
private long bufferStart = 0; // position in file of buffer
|
||||
private int bufferPosition = 0; // position in buffer
|
||||
|
||||
/** Writes a single byte.
|
||||
* @see InputStream#readByte()
|
||||
*/
|
||||
public void writeByte(byte b) throws IOException {
|
||||
if (bufferPosition >= BUFFER_SIZE)
|
||||
flush();
|
||||
buffer[bufferPosition++] = b;
|
||||
}
|
||||
|
||||
/** Writes an array of bytes.
|
||||
* @param b the bytes to write
|
||||
* @param length the number of bytes to write
|
||||
* @see InputStream#readBytes(byte[],int,int)
|
||||
*/
|
||||
public void writeBytes(byte[] b, int length) throws IOException {
|
||||
for (int i = 0; i < length; i++)
|
||||
writeByte(b[i]);
|
||||
}
|
||||
|
||||
/** Forces any buffered output to be written. */
|
||||
public void flush() throws IOException {
|
||||
flushBuffer(buffer, bufferPosition);
|
||||
bufferStart += bufferPosition;
|
||||
bufferPosition = 0;
|
||||
}
|
||||
|
||||
/** Expert: implements buffer write. Writes bytes at the current position in
|
||||
* the output.
|
||||
* @param b the bytes to write
|
||||
* @param len the number of bytes to write
|
||||
*/
|
||||
protected abstract void flushBuffer(byte[] b, int len) throws IOException;
|
||||
|
||||
/** Closes this stream to further operations. */
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
}
|
||||
|
||||
/** Returns the current position in this file, where the next write will
|
||||
* occur.
|
||||
* @see #seek(long)
|
||||
*/
|
||||
public long getFilePointer() {
|
||||
return bufferStart + bufferPosition;
|
||||
}
|
||||
|
||||
/** Sets current position in this file, where the next write will occur.
|
||||
* @see #getFilePointer()
|
||||
*/
|
||||
public void seek(long pos) throws IOException {
|
||||
flush();
|
||||
bufferStart = pos;
|
||||
}
|
||||
|
||||
/** The number of bytes in the file. */
|
||||
public abstract long length() throws IOException;
|
||||
|
||||
|
||||
}
|
|
@ -62,10 +62,18 @@ public abstract class Directory {
|
|||
public abstract long fileLength(String name)
|
||||
throws IOException;
|
||||
|
||||
/** @deprecated use {@link #createOutput(String)} */
|
||||
public OutputStream createFile(String name) throws IOException {
|
||||
return (OutputStream)createOutput(name);
|
||||
}
|
||||
|
||||
/** Creates a new, empty file in the directory with the given name.
|
||||
Returns a stream writing this file. */
|
||||
public abstract OutputStream createFile(String name)
|
||||
throws IOException;
|
||||
public IndexOutput createOutput(String name) throws IOException {
|
||||
// default implementation for back compatibility
|
||||
// this method should be abstract
|
||||
return (IndexOutput)createFile(name);
|
||||
}
|
||||
|
||||
/** @deprecated use {@link #openInput(String)} */
|
||||
public InputStream openFile(String name) throws IOException {
|
||||
|
|
|
@ -26,7 +26,7 @@ public abstract class IndexInput implements Cloneable {
|
|||
private char[] chars; // used by readString()
|
||||
|
||||
/** Reads and returns a single byte.
|
||||
* @see OutputStream#writeByte(byte)
|
||||
* @see IndexOutput#writeByte(byte)
|
||||
*/
|
||||
public abstract byte readByte() throws IOException;
|
||||
|
||||
|
@ -34,13 +34,13 @@ public abstract class IndexInput implements Cloneable {
|
|||
* @param b the array to read bytes into
|
||||
* @param offset the offset in the array to start storing bytes
|
||||
* @param len the number of bytes to read
|
||||
* @see OutputStream#writeBytes(byte[],int)
|
||||
* @see IndexOutput#writeBytes(byte[],int)
|
||||
*/
|
||||
public abstract void readBytes(byte[] b, int offset, int len)
|
||||
throws IOException;
|
||||
|
||||
/** Reads four bytes and returns an int.
|
||||
* @see OutputStream#writeInt(int)
|
||||
* @see IndexOutput#writeInt(int)
|
||||
*/
|
||||
public int readInt() throws IOException {
|
||||
return ((readByte() & 0xFF) << 24) | ((readByte() & 0xFF) << 16)
|
||||
|
@ -50,7 +50,7 @@ public abstract class IndexInput implements Cloneable {
|
|||
/** Reads an int stored in variable-length format. Reads between one and
|
||||
* five bytes. Smaller values take fewer bytes. Negative numbers are not
|
||||
* supported.
|
||||
* @see OutputStream#writeVInt(int)
|
||||
* @see IndexOutput#writeVInt(int)
|
||||
*/
|
||||
public int readVInt() throws IOException {
|
||||
byte b = readByte();
|
||||
|
@ -63,7 +63,7 @@ public abstract class IndexInput implements Cloneable {
|
|||
}
|
||||
|
||||
/** Reads eight bytes and returns a long.
|
||||
* @see OutputStream#writeLong(long)
|
||||
* @see IndexOutput#writeLong(long)
|
||||
*/
|
||||
public long readLong() throws IOException {
|
||||
return (((long)readInt()) << 32) | (readInt() & 0xFFFFFFFFL);
|
||||
|
@ -83,7 +83,7 @@ public abstract class IndexInput implements Cloneable {
|
|||
}
|
||||
|
||||
/** Reads a string.
|
||||
* @see OutputStream#writeString(String)
|
||||
* @see IndexOutput#writeString(String)
|
||||
*/
|
||||
public String readString() throws IOException {
|
||||
int length = readVInt();
|
||||
|
@ -97,7 +97,7 @@ public abstract class IndexInput implements Cloneable {
|
|||
* @param buffer the array to read characters into
|
||||
* @param start the offset in the array to start storing characters
|
||||
* @param length the number of characters to read
|
||||
* @see OutputStream#writeChars(String,int,int)
|
||||
* @see IndexOutput#writeChars(String,int,int)
|
||||
*/
|
||||
public void readChars(char[] buffer, int start, int length)
|
||||
throws IOException {
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
package org.apache.lucene.store;
|
||||
|
||||
/**
|
||||
* Copyright 2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** Abstract base class for output to a file in a Directory. A random-access
|
||||
* output stream. Used for all Lucene index output operations.
|
||||
* @see Directory
|
||||
* @see InputStream
|
||||
*/
|
||||
public abstract class IndexOutput {
|
||||
|
||||
/** Writes a single byte.
|
||||
* @see InputStream#readByte()
|
||||
*/
|
||||
public abstract void writeByte(byte b) throws IOException;
|
||||
|
||||
/** Writes an array of bytes.
|
||||
* @param b the bytes to write
|
||||
* @param length the number of bytes to write
|
||||
* @see InputStream#readBytes(byte[],int,int)
|
||||
*/
|
||||
public abstract void writeBytes(byte[] b, int length) throws IOException;
|
||||
|
||||
/** Writes an int as four bytes.
|
||||
* @see InputStream#readInt()
|
||||
*/
|
||||
public void writeInt(int i) throws IOException {
|
||||
writeByte((byte)(i >> 24));
|
||||
writeByte((byte)(i >> 16));
|
||||
writeByte((byte)(i >> 8));
|
||||
writeByte((byte) i);
|
||||
}
|
||||
|
||||
/** Writes an int in a variable-length format. Writes between one and
|
||||
* five bytes. Smaller values take fewer bytes. Negative numbers are not
|
||||
* supported.
|
||||
* @see InputStream#readVInt()
|
||||
*/
|
||||
public void writeVInt(int i) throws IOException {
|
||||
while ((i & ~0x7F) != 0) {
|
||||
writeByte((byte)((i & 0x7f) | 0x80));
|
||||
i >>>= 7;
|
||||
}
|
||||
writeByte((byte)i);
|
||||
}
|
||||
|
||||
/** Writes a long as eight bytes.
|
||||
* @see InputStream#readLong()
|
||||
*/
|
||||
public void writeLong(long i) throws IOException {
|
||||
writeInt((int) (i >> 32));
|
||||
writeInt((int) i);
|
||||
}
|
||||
|
||||
/** Writes an long in a variable-length format. Writes between one and five
|
||||
* bytes. Smaller values take fewer bytes. Negative numbers are not
|
||||
* supported.
|
||||
* @see InputStream#readVLong()
|
||||
*/
|
||||
public void writeVLong(long i) throws IOException {
|
||||
while ((i & ~0x7F) != 0) {
|
||||
writeByte((byte)((i & 0x7f) | 0x80));
|
||||
i >>>= 7;
|
||||
}
|
||||
writeByte((byte)i);
|
||||
}
|
||||
|
||||
/** Writes a string.
|
||||
* @see InputStream#readString()
|
||||
*/
|
||||
public void writeString(String s) throws IOException {
|
||||
int length = s.length();
|
||||
writeVInt(length);
|
||||
writeChars(s, 0, length);
|
||||
}
|
||||
|
||||
/** Writes a sequence of UTF-8 encoded characters from a string.
|
||||
* @param s the source of the characters
|
||||
* @param start the first character in the sequence
|
||||
* @param length the number of characters in the sequence
|
||||
* @see InputStream#readChars(char[],int,int)
|
||||
*/
|
||||
public void writeChars(String s, int start, int length)
|
||||
throws IOException {
|
||||
final int end = start + length;
|
||||
for (int i = start; i < end; i++) {
|
||||
final int code = (int)s.charAt(i);
|
||||
if (code >= 0x01 && code <= 0x7F)
|
||||
writeByte((byte)code);
|
||||
else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
|
||||
writeByte((byte)(0xC0 | (code >> 6)));
|
||||
writeByte((byte)(0x80 | (code & 0x3F)));
|
||||
} else {
|
||||
writeByte((byte)(0xE0 | (code >>> 12)));
|
||||
writeByte((byte)(0x80 | ((code >> 6) & 0x3F)));
|
||||
writeByte((byte)(0x80 | (code & 0x3F)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Forces any buffered output to be written. */
|
||||
public abstract void flush() throws IOException;
|
||||
|
||||
/** Closes this stream to further operations. */
|
||||
public abstract void close() throws IOException;
|
||||
|
||||
/** Returns the current position in this file, where the next write will
|
||||
* occur.
|
||||
* @see #seek(long)
|
||||
*/
|
||||
public abstract long getFilePointer();
|
||||
|
||||
/** Sets current position in this file, where the next write will occur.
|
||||
* @see #getFilePointer()
|
||||
*/
|
||||
public abstract void seek(long pos) throws IOException;
|
||||
|
||||
/** The number of bytes in the file. */
|
||||
public abstract long length() throws IOException;
|
||||
|
||||
|
||||
}
|
|
@ -18,151 +18,7 @@ package org.apache.lucene.store;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
/** Abstract class for output to a file in a Directory. A random-access output
|
||||
* stream. Used for all Lucene index output operations.
|
||||
* @see Directory
|
||||
* @see InputStream
|
||||
*/
|
||||
public abstract class OutputStream {
|
||||
static final int BUFFER_SIZE = 1024;
|
||||
|
||||
private final byte[] buffer = new byte[BUFFER_SIZE];
|
||||
private long bufferStart = 0; // position in file of buffer
|
||||
private int bufferPosition = 0; // position in buffer
|
||||
|
||||
/** Writes a single byte.
|
||||
* @see InputStream#readByte()
|
||||
*/
|
||||
public final void writeByte(byte b) throws IOException {
|
||||
if (bufferPosition >= BUFFER_SIZE)
|
||||
flush();
|
||||
buffer[bufferPosition++] = b;
|
||||
}
|
||||
|
||||
/** Writes an array of bytes.
|
||||
* @param b the bytes to write
|
||||
* @param length the number of bytes to write
|
||||
* @see InputStream#readBytes(byte[],int,int)
|
||||
*/
|
||||
public final void writeBytes(byte[] b, int length) throws IOException {
|
||||
for (int i = 0; i < length; i++)
|
||||
writeByte(b[i]);
|
||||
}
|
||||
|
||||
/** Writes an int as four bytes.
|
||||
* @see InputStream#readInt()
|
||||
*/
|
||||
public final void writeInt(int i) throws IOException {
|
||||
writeByte((byte)(i >> 24));
|
||||
writeByte((byte)(i >> 16));
|
||||
writeByte((byte)(i >> 8));
|
||||
writeByte((byte) i);
|
||||
}
|
||||
|
||||
/** Writes an int in a variable-length format. Writes between one and
|
||||
* five bytes. Smaller values take fewer bytes. Negative numbers are not
|
||||
* supported.
|
||||
* @see InputStream#readVInt()
|
||||
*/
|
||||
public final void writeVInt(int i) throws IOException {
|
||||
while ((i & ~0x7F) != 0) {
|
||||
writeByte((byte)((i & 0x7f) | 0x80));
|
||||
i >>>= 7;
|
||||
}
|
||||
writeByte((byte)i);
|
||||
}
|
||||
|
||||
/** Writes a long as eight bytes.
|
||||
* @see InputStream#readLong()
|
||||
*/
|
||||
public final void writeLong(long i) throws IOException {
|
||||
writeInt((int) (i >> 32));
|
||||
writeInt((int) i);
|
||||
}
|
||||
|
||||
/** Writes an long in a variable-length format. Writes between one and five
|
||||
* bytes. Smaller values take fewer bytes. Negative numbers are not
|
||||
* supported.
|
||||
* @see InputStream#readVLong()
|
||||
*/
|
||||
public final void writeVLong(long i) throws IOException {
|
||||
while ((i & ~0x7F) != 0) {
|
||||
writeByte((byte)((i & 0x7f) | 0x80));
|
||||
i >>>= 7;
|
||||
}
|
||||
writeByte((byte)i);
|
||||
}
|
||||
|
||||
/** Writes a string.
|
||||
* @see InputStream#readString()
|
||||
*/
|
||||
public final void writeString(String s) throws IOException {
|
||||
int length = s.length();
|
||||
writeVInt(length);
|
||||
writeChars(s, 0, length);
|
||||
}
|
||||
|
||||
/** Writes a sequence of UTF-8 encoded characters from a string.
|
||||
* @param s the source of the characters
|
||||
* @param start the first character in the sequence
|
||||
* @param length the number of characters in the sequence
|
||||
* @see InputStream#readChars(char[],int,int)
|
||||
*/
|
||||
public final void writeChars(String s, int start, int length)
|
||||
throws IOException {
|
||||
final int end = start + length;
|
||||
for (int i = start; i < end; i++) {
|
||||
final int code = (int)s.charAt(i);
|
||||
if (code >= 0x01 && code <= 0x7F)
|
||||
writeByte((byte)code);
|
||||
else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
|
||||
writeByte((byte)(0xC0 | (code >> 6)));
|
||||
writeByte((byte)(0x80 | (code & 0x3F)));
|
||||
} else {
|
||||
writeByte((byte)(0xE0 | (code >>> 12)));
|
||||
writeByte((byte)(0x80 | ((code >> 6) & 0x3F)));
|
||||
writeByte((byte)(0x80 | (code & 0x3F)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Forces any buffered output to be written. */
|
||||
protected final void flush() throws IOException {
|
||||
flushBuffer(buffer, bufferPosition);
|
||||
bufferStart += bufferPosition;
|
||||
bufferPosition = 0;
|
||||
}
|
||||
|
||||
/** Expert: implements buffer write. Writes bytes at the current position in
|
||||
* the output.
|
||||
* @param b the bytes to write
|
||||
* @param len the number of bytes to write
|
||||
*/
|
||||
protected abstract void flushBuffer(byte[] b, int len) throws IOException;
|
||||
|
||||
/** Closes this stream to further operations. */
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
}
|
||||
|
||||
/** Returns the current position in this file, where the next write will
|
||||
* occur.
|
||||
* @see #seek(long)
|
||||
*/
|
||||
public final long getFilePointer() {
|
||||
return bufferStart + bufferPosition;
|
||||
}
|
||||
|
||||
/** Sets current position in this file, where the next write will occur.
|
||||
* @see #getFilePointer()
|
||||
*/
|
||||
public void seek(long pos) throws IOException {
|
||||
flush();
|
||||
bufferStart = pos;
|
||||
}
|
||||
|
||||
/** The number of bytes in the file. */
|
||||
public abstract long length() throws IOException;
|
||||
|
||||
|
||||
/** @deprecated Use {@link IndexOutput} or {@link BufferedIndexOutput}
|
||||
* instead.*/
|
||||
public abstract class OutputStream extends BufferedIndexOutput {
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.lucene.store;
|
|||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A memory-resident {@link OutputStream} implementation.
|
||||
* A memory-resident {@link IndexOutput} implementation.
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
|
@ -38,7 +38,7 @@ public class RAMOutputStream extends OutputStream {
|
|||
}
|
||||
|
||||
/** Copy the current contents of this buffer to the named output. */
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
public void writeTo(IndexOutput out) throws IOException {
|
||||
flush();
|
||||
final long end = file.length;
|
||||
long pos = 0;
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.io.IOException;
|
|||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
|
||||
/** Optimized implementation of a vector of bits. This is more-or-less like
|
||||
java.util.BitSet, but also includes the following:
|
||||
|
@ -108,7 +108,7 @@ public final class BitVector {
|
|||
<code>d</code>, in a format that can be read by the constructor {@link
|
||||
#BitVector(Directory, String)}. */
|
||||
public final void write(Directory d, String name) throws IOException {
|
||||
OutputStream output = d.createFile(name);
|
||||
IndexOutput output = d.createOutput(name);
|
||||
try {
|
||||
output.writeInt(size()); // write size
|
||||
output.writeInt(count()); // write count
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.lucene;
|
|||
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.FSDirectory;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
|
||||
|
@ -57,7 +57,7 @@ class StoreTest {
|
|||
byte b = (byte)(gen.nextInt() & 0x7F);
|
||||
//System.out.println("filling " + name + " with " + length + " of " + b);
|
||||
|
||||
OutputStream file = store.createFile(name);
|
||||
IndexOutput file = store.createOutput(name);
|
||||
|
||||
for (int j = 0; j < length; j++)
|
||||
file.writeByte(b);
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.io.File;
|
|||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.textui.TestRunner;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.FSDirectory;
|
||||
|
@ -66,7 +66,7 @@ public class TestCompoundFile extends TestCase
|
|||
private void createRandomFile(Directory dir, String name, int size)
|
||||
throws IOException
|
||||
{
|
||||
OutputStream os = dir.createFile(name);
|
||||
IndexOutput os = dir.createOutput(name);
|
||||
for (int i=0; i<size; i++) {
|
||||
byte b = (byte) (Math.random() * 256);
|
||||
os.writeByte(b);
|
||||
|
@ -84,7 +84,7 @@ public class TestCompoundFile extends TestCase
|
|||
int size)
|
||||
throws IOException
|
||||
{
|
||||
OutputStream os = dir.createFile(name);
|
||||
IndexOutput os = dir.createOutput(name);
|
||||
for (int i=0; i < size; i++) {
|
||||
os.writeByte(start);
|
||||
start ++;
|
||||
|
@ -313,7 +313,7 @@ public class TestCompoundFile extends TestCase
|
|||
throws IOException
|
||||
{
|
||||
// Setup the test file - we need more than 1024 bytes
|
||||
OutputStream os = fsdir.createFile(file);
|
||||
IndexOutput os = fsdir.createOutput(file);
|
||||
for(int i=0; i<2000; i++) {
|
||||
os.writeByte((byte) i);
|
||||
}
|
||||
|
|
|
@ -63,16 +63,16 @@ public class TestDoc extends TestCase {
|
|||
directory.close();
|
||||
|
||||
files = new LinkedList();
|
||||
files.add(createFile("test.txt",
|
||||
files.add(createOutput("test.txt",
|
||||
"This is the first test file"
|
||||
));
|
||||
|
||||
files.add(createFile("test2.txt",
|
||||
files.add(createOutput("test2.txt",
|
||||
"This is the second test file"
|
||||
));
|
||||
}
|
||||
|
||||
private File createFile(String name, String text) throws IOException {
|
||||
private File createOutput(String name, String text) throws IOException {
|
||||
FileWriter fw = null;
|
||||
PrintWriter pw = null;
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ package org.apache.lucene.index;
|
|||
import junit.framework.TestCase;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
import org.apache.lucene.store.OutputStream;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -25,7 +25,7 @@ public class TestFieldInfos extends TestCase {
|
|||
protected void tearDown() {
|
||||
}
|
||||
|
||||
public void test() {
|
||||
public void test() throws IOException {
|
||||
//Positive test of FieldInfos
|
||||
assertTrue(testDoc != null);
|
||||
FieldInfos fieldInfos = new FieldInfos();
|
||||
|
@ -34,7 +34,7 @@ public class TestFieldInfos extends TestCase {
|
|||
assertTrue(fieldInfos.size() == 7); //this is 7 b/c we are using the no-arg constructor
|
||||
RAMDirectory dir = new RAMDirectory();
|
||||
String name = "testFile";
|
||||
OutputStream output = dir.createFile(name);
|
||||
IndexOutput output = dir.createOutput(name);
|
||||
assertTrue(output != null);
|
||||
//Use a RAMOutputStream
|
||||
|
||||
|
|
Loading…
Reference in New Issue