forked from apache/lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow FST builder to use different writer (apache#12543)
- Loading branch information
Showing
12 changed files
with
761 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lucene/core/src/java/org/apache/lucene/util/fst/ByteBlockPoolReverseBytesReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.util.fst; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.util.ByteBlockPool; | ||
|
||
/** Reads in reverse from a ByteBlockPool. */ | ||
final class ByteBlockPoolReverseBytesReader extends FST.BytesReader { | ||
|
||
private final ByteBlockPool buf; | ||
// the difference between the FST node address and the hash table copied node address | ||
private long posDelta; | ||
private long pos; | ||
|
||
public ByteBlockPoolReverseBytesReader(ByteBlockPool buf) { | ||
this.buf = buf; | ||
} | ||
|
||
@Override | ||
public byte readByte() { | ||
return buf.readByte(pos--); | ||
} | ||
|
||
@Override | ||
public void readBytes(byte[] b, int offset, int len) { | ||
for (int i = 0; i < len; i++) { | ||
b[offset + i] = buf.readByte(pos--); | ||
} | ||
} | ||
|
||
@Override | ||
public void skipBytes(long numBytes) throws IOException { | ||
pos -= numBytes; | ||
} | ||
|
||
@Override | ||
public long getPosition() { | ||
return pos + posDelta; | ||
} | ||
|
||
@Override | ||
public void setPosition(long pos) { | ||
this.pos = pos - posDelta; | ||
} | ||
|
||
@Override | ||
public boolean reversed() { | ||
return true; | ||
} | ||
|
||
public void setPosDelta(long posDelta) { | ||
this.posDelta = posDelta; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
lucene/core/src/java/org/apache/lucene/util/fst/DataOutputFSTWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.util.fst; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import org.apache.lucene.store.DataOutput; | ||
import org.apache.lucene.util.Accountable; | ||
import org.apache.lucene.util.RamUsageEstimator; | ||
|
||
/** | ||
* An {@link FSTWriter} which write to a DataOutput | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public class DataOutputFSTWriter implements FSTWriter { | ||
|
||
private static final long BASE_RAM_BYTES_USED = | ||
RamUsageEstimator.shallowSizeOfInstance(DataOutputFSTWriter.class); | ||
|
||
private final DataOutput dataOutput; | ||
|
||
protected boolean finish = false; | ||
|
||
private long size = 0L; | ||
|
||
/** | ||
* ctor | ||
* | ||
* @param dataOutput the data output to write to | ||
*/ | ||
public DataOutputFSTWriter(DataOutput dataOutput) { | ||
this.dataOutput = dataOutput; | ||
} | ||
|
||
@Override | ||
public long ramBytesUsed() { | ||
long size = BASE_RAM_BYTES_USED; | ||
if (dataOutput instanceof Accountable) { | ||
size += ((Accountable) dataOutput).ramBytesUsed(); | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
public long size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public void writeByte(byte b) throws IOException { | ||
size++; | ||
dataOutput.writeByte(b); | ||
} | ||
|
||
@Override | ||
public void writeBytes(byte[] b, int offset, int length) throws IOException { | ||
size += length; | ||
dataOutput.writeBytes(b, offset, length); | ||
} | ||
|
||
@Override | ||
public void finish() throws IOException { | ||
finish = true; | ||
if (dataOutput instanceof Closeable) { | ||
((Closeable) dataOutput).close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(DataOutput out) throws IOException { | ||
// Technically we can support this method, as the DataOutput by this time has already been | ||
// closed. | ||
// But allow the FST which is already written to a DataOutput to be saved to another DataOutput | ||
// would be rather a strange use case | ||
throw new UnsupportedOperationException("writeTo(DataOutput) is not supported by this class"); | ||
} | ||
|
||
@Override | ||
public FST.BytesReader getReverseBytesReader() { | ||
// Technically we can support this method, as the DataOutput by this time has already been | ||
// closed. | ||
// However, I think ideally we would want the FSTWriter/FSTCompiler to only write the FST to the | ||
// DataOutput, and | ||
// some process later on can construct the FST using the FSTStore method. | ||
throw new UnsupportedOperationException( | ||
"getReverseBytesReader() is not supported by this class"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.