Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
197 additions
and
22 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...r-core/src/main/java/com/nearinfinity/blur/lucene/index/TimeBasedIndexDeletionPolicy.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,55 @@ | ||
package com.nearinfinity.blur.lucene.index; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.lucene.index.IndexCommit; | ||
import org.apache.lucene.index.IndexDeletionPolicy; | ||
|
||
public class TimeBasedIndexDeletionPolicy implements IndexDeletionPolicy { | ||
|
||
private static final Log LOG = LogFactory.getLog(TimeBasedIndexDeletionPolicy.class); | ||
|
||
private long maxAge; | ||
|
||
public TimeBasedIndexDeletionPolicy(long maxAge) { | ||
this.maxAge = maxAge; | ||
} | ||
|
||
@Override | ||
public void onInit(List<? extends IndexCommit> commits) throws IOException { | ||
onCommit(commits); | ||
} | ||
|
||
@Override | ||
public void onCommit(List<? extends IndexCommit> commits) throws IOException { | ||
IndexCommit current = commits.get(commits.size() - 1); | ||
int length; | ||
if (isTooOld(current.getTimestamp())) { | ||
// the current index is old enough that following generation can be | ||
// removed | ||
length = commits.size() - 1; | ||
} else { | ||
// the current index is NOT old enough, so therefore the next generation | ||
// (no matter the old can be removed) | ||
length = commits.size() - 2; | ||
} | ||
for (int i = 0; i < length; i++) { | ||
IndexCommit commit = commits.get(i); | ||
if (isTooOld(commit.getTimestamp())) { | ||
LOG.info("Removing old generation [" + commit.getGeneration() + "] for directory [" + commit.getDirectory() + "]"); | ||
commit.delete(); | ||
} | ||
} | ||
} | ||
|
||
private boolean isTooOld(long timestamp) { | ||
if (timestamp + maxAge < System.currentTimeMillis()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
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
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
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
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
58 changes: 58 additions & 0 deletions
58
...re/src/test/java/com/nearinfinity/blur/lucene/index/TimeBasedIndexDeletionPolicyTest.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,58 @@ | ||
package com.nearinfinity.blur.lucene.index; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.lucene.analysis.KeywordAnalyzer; | ||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.document.Field.Index; | ||
import org.apache.lucene.document.Field.Store; | ||
import org.apache.lucene.index.CorruptIndexException; | ||
import org.apache.lucene.index.IndexDeletionPolicy; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.IndexWriterConfig; | ||
import org.apache.lucene.store.LockObtainFailedException; | ||
import org.apache.lucene.store.RAMDirectory; | ||
import org.apache.lucene.util.Version; | ||
import org.junit.Test; | ||
|
||
import com.nearinfinity.blur.index.IndexWriter; | ||
|
||
public class TimeBasedIndexDeletionPolicyTest { | ||
|
||
@Test | ||
public void testTimeBasedIndexDeletionPolicy() throws IOException, InterruptedException { | ||
TimeBasedIndexDeletionPolicy indexDeletionPolicy = new TimeBasedIndexDeletionPolicy(3000); | ||
RAMDirectory directory = new RAMDirectory(); | ||
addAndCommit(directory, indexDeletionPolicy); | ||
addAndCommit(directory, indexDeletionPolicy); | ||
addAndCommit(directory, indexDeletionPolicy); | ||
Thread.sleep(1000); | ||
assertEquals(3, IndexReader.listCommits(directory).size()); | ||
Thread.sleep(4000); | ||
addAndCommit(directory, indexDeletionPolicy); | ||
assertEquals(2, IndexReader.listCommits(directory).size()); | ||
Thread.sleep(4000); | ||
openClose(directory, indexDeletionPolicy); | ||
assertEquals(1, IndexReader.listCommits(directory).size()); | ||
} | ||
|
||
private void openClose(RAMDirectory directory, TimeBasedIndexDeletionPolicy indexDeletionPolicy) throws CorruptIndexException, LockObtainFailedException, IOException { | ||
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, new KeywordAnalyzer()); | ||
conf.setIndexDeletionPolicy(indexDeletionPolicy); | ||
new IndexWriter(directory, conf).close(); | ||
} | ||
|
||
private void addAndCommit(RAMDirectory directory, IndexDeletionPolicy indexDeletionPolicy) throws CorruptIndexException, LockObtainFailedException, IOException { | ||
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, new KeywordAnalyzer()); | ||
conf.setIndexDeletionPolicy(indexDeletionPolicy); | ||
IndexWriter writer = new IndexWriter(directory, conf); | ||
Document doc = new Document(); | ||
doc.add(new Field("id", "1", Store.YES, Index.ANALYZED_NO_NORMS)); | ||
writer.addDocument(doc); | ||
writer.close(); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/blur-thrift/src/main/java/com/nearinfinity/blur/thrift/BlurClient.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,67 @@ | ||
package com.nearinfinity.blur.thrift; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.thrift.TException; | ||
|
||
import com.nearinfinity.blur.thrift.commands.BlurCommand; | ||
import com.nearinfinity.blur.thrift.generated.Blur.Client; | ||
import com.nearinfinity.blur.thrift.generated.Blur.Iface; | ||
import com.nearinfinity.blur.thrift.generated.BlurException; | ||
|
||
public class BlurClient { | ||
|
||
static class BlurClientInvocationHandler implements InvocationHandler { | ||
|
||
private List<Connection> connections; | ||
|
||
public BlurClientInvocationHandler(List<Connection> connections) { | ||
this.connections = connections; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable { | ||
return BlurClientManager.execute(connections, new BlurCommand<Object>() { | ||
@Override | ||
public Object call(Client client) throws BlurException, TException { | ||
try { | ||
return method.invoke(client, args); | ||
} catch (IllegalArgumentException e) { | ||
throw new RuntimeException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (InvocationTargetException e) { | ||
Throwable targetException = e.getTargetException(); | ||
if (targetException instanceof BlurException) { | ||
throw (BlurException) targetException; | ||
} | ||
if (targetException instanceof TException) { | ||
throw (TException) targetException; | ||
} | ||
throw new RuntimeException(targetException); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
public static Iface getClient(String connectionStr) { | ||
List<Connection> connections = BlurClientManager.getConnections(connectionStr); | ||
return getClient(connections); | ||
} | ||
|
||
public static Iface getClient(Connection connection) { | ||
return getClient(Arrays.asList(connection)); | ||
} | ||
|
||
public static Iface getClient(List<Connection> connections) { | ||
return (Iface) Proxy.newProxyInstance(Iface.class.getClassLoader(), new Class[] { Iface.class }, new BlurClientInvocationHandler(connections)); | ||
} | ||
|
||
} |
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
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