Skip to content

Commit

Permalink
Merge pull request #5 from cloudant-labs/close-after-delete
Browse files Browse the repository at this point in the history
Fix close after delete
  • Loading branch information
rnewson authored Sep 19, 2019
2 parents ee3c848 + 318d01b commit 35b4d3e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
15 changes: 3 additions & 12 deletions src/main/java/com/cloudant/fdblucene/FDBDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public final class FDBDirectory extends Directory {

private static class FileMetaData {
static class FileMetaData {

private final Tuple asTuple;

Expand Down Expand Up @@ -241,7 +241,8 @@ public IndexOutput createOutput(final String name, final IOContext context) thro
}

final String resourceDescription = String.format("FDBIndexOutput(name=%s,number=%d)", name, fileNumber);
return new FDBIndexOutput(this, resourceDescription, name, txc, fileSubspace(fileNumber), pageSize, txnSize);
return new FDBIndexOutput(this, resourceDescription, name, txc, metaKey(name), fileSubspace(fileNumber),
pageSize, txnSize);
}

/**
Expand Down Expand Up @@ -296,16 +297,6 @@ public long fileLength(final String name) throws IOException {
return meta.getFileLength();
}

void setFileLength(final TransactionContext txc, final String name, final long length) {
final byte[] metaKey = metaKey(name);
txc.run(txn -> {
final byte[] value = txn.get(metaKey).join();
final FileMetaData meta = new FileMetaData(value).setFileLength(length);
txn.set(metaKey, meta.pack());
return null;
});
}

@Override
public String[] listAll() throws IOException {
final Range metaRange = metaRange();
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/com/cloudant/fdblucene/FDBIndexOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.zip.CRC32;

import org.apache.lucene.store.IndexOutput;
Expand All @@ -25,6 +26,7 @@
import com.apple.foundationdb.TransactionContext;
import com.apple.foundationdb.async.AsyncUtil;
import com.apple.foundationdb.subspace.Subspace;
import com.cloudant.fdblucene.FDBDirectory.FileMetaData;

public final class FDBIndexOutput extends IndexOutput {

Expand Down Expand Up @@ -59,6 +61,7 @@ private static byte[] pageKey(final Subspace subspace, final long pos, final int

private final FDBDirectory dir;
private final TransactionContext txc;
private final byte[] metaKey;
private final Subspace subspace;
private byte[] txnBuffer;

Expand All @@ -74,10 +77,12 @@ private static byte[] pageKey(final Subspace subspace, final long pos, final int
private final int txnSize;

FDBIndexOutput(final FDBDirectory dir, final String resourceDescription, final String name,
final TransactionContext txc, final Subspace subspace, final int pageSize, final int txnSize) {
final TransactionContext txc, final byte[] metaKey, final Subspace subspace, final int pageSize,
final int txnSize) {
super(resourceDescription, name);
this.dir = dir;
this.txc = txc;
this.metaKey = metaKey;
this.subspace = subspace;
this.readVersionCache = new ReadVersionCache();
this.pageSize = pageSize;
Expand All @@ -97,7 +102,7 @@ public void close() throws IOException {
flushTxnBuffer(subspace, txn, txnBuffer, txnBufferOffset, pointer, pageSize);
txn.options().setNextWriteNoWriteConflictRange();

dir.setFileLength(txn, getName(), pointer);
setFileLength(txn, pointer);
return null;
});
}
Expand Down Expand Up @@ -149,7 +154,9 @@ private void flushTxnBuffer() {
lastFlushFuture = txc.runAsync(txn -> {
readVersionCache.setReadVersion(txn);
txn.options().setTransactionLoggingEnable(String.format("%s,out,flush,%d", getName(), pointer));
flushTxnBuffer(subspace, txn, txnBuffer, txnBufferOffset, pointer, pageSize);
applyIfExists(txn, value -> {
flushTxnBuffer(subspace, txn, txnBuffer, txnBufferOffset, pointer, pageSize);
});
return AsyncUtil.DONE;
});
}
Expand All @@ -160,4 +167,23 @@ private void flushTxnBufferIfFull() {
}
}

private void setFileLength(final TransactionContext txc, final long length) {
txc.run(txn -> {
applyIfExists(txn, value -> {
final FileMetaData meta = new FileMetaData(value).setFileLength(length);
txn.set(metaKey, meta.pack());
});
return null;
});
}

private void applyIfExists(final Transaction txn, final Consumer<byte[]> fun) {
txn.get(metaKey).thenApply(value -> {
if (value != null) {
fun.accept(value);
}
return null;
}).join();
}

}
13 changes: 13 additions & 0 deletions src/test/java/com/cloudant/fdblucene/SimpleFDBDirectoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ public void addIndexes() throws Exception {
}
}

@Test
public void testCloseAfterDeleteShouldntThrow() throws Exception {
final Directory dir = FDBDirectory.open(DB, FileSystems.getDefault().getPath("lucene", "test1"));
assertCloseDoesntThrowOnDeletedFile(dir);
}

private void assertCloseDoesntThrowOnDeletedFile(final Directory dir) throws Exception {
cleanupDir(dir);
final IndexOutput out = dir.createOutput("foo", null);
dir.deleteFile("foo");
out.close();
}

private void addDocument(final IndexWriter writer, final String docId) throws IOException {
final Document doc = new Document();
doc.add(new TextField("foo", "hello everybody", Store.NO));
Expand Down

0 comments on commit 35b4d3e

Please sign in to comment.