Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.luceneserver.core;
- import java.io.File;
- import java.io.IOException;
- import java.util.concurrent.Callable;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.TextField;
- import org.apache.lucene.document.Field.Store;
- import org.apache.lucene.index.IndexDeletionPolicy;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
- import org.apache.lucene.index.SnapshotDeletionPolicy;
- import org.apache.lucene.replicator.IndexReplicationHandler;
- import org.apache.lucene.replicator.IndexRevision;
- import org.apache.lucene.replicator.LocalReplicator;
- import org.apache.lucene.replicator.PerSessionDirectoryFactory;
- import org.apache.lucene.replicator.ReplicationClient;
- import org.apache.lucene.replicator.ReplicationClient.ReplicationHandler;
- import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory;
- import org.apache.lucene.replicator.Replicator;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.store.RAMDirectory;
- import com.luceneserver.constants.LuceneConstants;
- public class DemoReplicator implements LuceneConstants {
- IndexWriter indexWriter;
- Replicator replicator;
- Directory workingDir;
- Directory backupDir;
- ReplicationClient client;
- protected void initializeReplicator() {
- replicator = new LocalReplicator();
- }
- protected void initializeDirAndIndices() throws IOException {
- workingDir = FSDirectory.open(new File(LUCENE_INDEX_HOME+File.separator+INDEX_NAME).toPath());
- IndexWriterConfig indexWriterConfig = new IndexWriterConfig();
- IndexDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
- indexWriterConfig.setIndexDeletionPolicy(deletionPolicy);
- indexWriter = new IndexWriter(workingDir, indexWriterConfig);
- }
- protected void addDocuments() throws IOException {
- for (int i = 0; i < 1000000; i++) {
- Document document = new Document();
- document.add(new TextField("content", String.valueOf(Math.random()), Store.YES));
- indexWriter.addDocument(document);
- }
- indexWriter.commit();
- replicator.publish(new IndexRevision(indexWriter));
- }
- protected void initializeReplication() throws IOException {
- Callable<Boolean> callback = null;
- File backupFolder = new File(LUCENE_INDEX_BACKUP_HOME + File.separator + BACKUP_INDEX_NAME);
- backupDir = FSDirectory.open(backupFolder.toPath());
- ReplicationHandler handler = new IndexReplicationHandler(backupDir, callback);
- SourceDirectoryFactory factory = new PerSessionDirectoryFactory(backupFolder.toPath());
- client = new ReplicationClient(replicator, handler, factory);
- }
- protected void doBackup() throws IOException {
- System.out.println("Doing a backup");
- client.updateNow();
- }
- public static void main(String args[]) throws IOException {
- DemoReplicator demo = new DemoReplicator();
- demo.initializeDirAndIndices();
- demo.initializeReplicator();
- demo.initializeReplication();
- demo.addDocuments();
- demo.doBackup();
- }
- }
Add Comment
Please, Sign In to add comment