Guest User

Untitled

a guest
Apr 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. package com.luceneserver.core;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.concurrent.Callable;
  6.  
  7. import org.apache.lucene.document.Document;
  8. import org.apache.lucene.document.TextField;
  9. import org.apache.lucene.document.Field.Store;
  10. import org.apache.lucene.index.IndexDeletionPolicy;
  11. import org.apache.lucene.index.IndexWriter;
  12. import org.apache.lucene.index.IndexWriterConfig;
  13. import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
  14. import org.apache.lucene.index.SnapshotDeletionPolicy;
  15. import org.apache.lucene.replicator.IndexReplicationHandler;
  16. import org.apache.lucene.replicator.IndexRevision;
  17. import org.apache.lucene.replicator.LocalReplicator;
  18. import org.apache.lucene.replicator.PerSessionDirectoryFactory;
  19. import org.apache.lucene.replicator.ReplicationClient;
  20. import org.apache.lucene.replicator.ReplicationClient.ReplicationHandler;
  21. import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory;
  22. import org.apache.lucene.replicator.Replicator;
  23. import org.apache.lucene.store.Directory;
  24. import org.apache.lucene.store.FSDirectory;
  25. import org.apache.lucene.store.RAMDirectory;
  26.  
  27. import com.luceneserver.constants.LuceneConstants;
  28.  
  29. public class DemoReplicator implements LuceneConstants {
  30.  
  31. IndexWriter indexWriter;
  32. Replicator replicator;
  33. Directory workingDir;
  34. Directory backupDir;
  35. ReplicationClient client;
  36.  
  37. protected void initializeReplicator() {
  38. replicator = new LocalReplicator();
  39. }
  40.  
  41. protected void initializeDirAndIndices() throws IOException {
  42. workingDir = FSDirectory.open(new File(LUCENE_INDEX_HOME+File.separator+INDEX_NAME).toPath());
  43. IndexWriterConfig indexWriterConfig = new IndexWriterConfig();
  44. IndexDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
  45. indexWriterConfig.setIndexDeletionPolicy(deletionPolicy);
  46. indexWriter = new IndexWriter(workingDir, indexWriterConfig);
  47. }
  48.  
  49. protected void addDocuments() throws IOException {
  50. for (int i = 0; i < 1000000; i++) {
  51. Document document = new Document();
  52. document.add(new TextField("content", String.valueOf(Math.random()), Store.YES));
  53. indexWriter.addDocument(document);
  54. }
  55. indexWriter.commit();
  56. replicator.publish(new IndexRevision(indexWriter));
  57. }
  58.  
  59. protected void initializeReplication() throws IOException {
  60.  
  61. Callable<Boolean> callback = null;
  62. File backupFolder = new File(LUCENE_INDEX_BACKUP_HOME + File.separator + BACKUP_INDEX_NAME);
  63. backupDir = FSDirectory.open(backupFolder.toPath());
  64. ReplicationHandler handler = new IndexReplicationHandler(backupDir, callback);
  65.  
  66.  
  67. SourceDirectoryFactory factory = new PerSessionDirectoryFactory(backupFolder.toPath());
  68. client = new ReplicationClient(replicator, handler, factory);
  69. }
  70.  
  71. protected void doBackup() throws IOException {
  72. System.out.println("Doing a backup");
  73. client.updateNow();
  74. }
  75.  
  76. public static void main(String args[]) throws IOException {
  77. DemoReplicator demo = new DemoReplicator();
  78. demo.initializeDirAndIndices();
  79. demo.initializeReplicator();
  80. demo.initializeReplication();
  81. demo.addDocuments();
  82. demo.doBackup();
  83. }
  84.  
  85. }
Add Comment
Please, Sign In to add comment