public
Description: semantic repository service is intended for indexing content from different sources and maintain multi indexes for different types of content and perform different types of search. yet another solr type indexing service on top of lucene but it will gradually support content versioning and more semantic search result.
Homepage: http://hasan.we4tech.com
Clone URL: git://github.com/we4tech/semantic-repository.git
semantic-repository / development / idea-repository-core / src / main / java / com / ideabase / repository / core / index / impl / RepositoryItemIndexImpl.java
100644 137 lines (122 sloc) 4.195 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* $Id:RepositoryItemIndexImpl.java 249 2007-12-02 08:32:47Z hasan $
******************************************************************************
* Copyright (C) 2007 somewhere in .Net ltd.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from somewhere in .Net LTD. This notice must be included on
* all copies, modifications and derivatives of this work.
******************************************************************************
* $LastChangedBy:hasan $
* $LastChangedDate:2007-12-02 14:32:47 +0600 (Sun, 02 Dec 2007) $
* $LastChangedRevision:249 $
******************************************************************************
*/
package com.ideabase.repository.core.index.impl;
 
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
 
import org.springmodules.lucene.index.support.LuceneIndexSupport;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import com.ideabase.repository.core.index.RepositoryItemIndex;
 
/**
* Implementation of {@code RepositoryItemIndex}.
* @author <a href="mailto:hasan@somewherein.net">nhm tanveer hossain khan (hasan)</a>
*/
public class RepositoryItemIndexImpl extends LuceneIndexSupport
    implements RepositoryItemIndex {
 
  private final Logger LOG =
      LogManager.getLogger(RepositoryItemIndexImpl.class);
 
  /**
* Executor service is used to process all same index related work in a
* queued manner. so the caller won't wait for the processing instead it
* will do it afterwords.
*/
  private Executor mIndexTaskExecutor =
      Executors.newSingleThreadExecutor();
 
  private boolean mThreadedTaskExecution = true;
 
  public void setThreadedTaskExecution(final boolean pThreadedTaskExecution) {
    mThreadedTaskExecution = pThreadedTaskExecution;
  }
 
  public void setTaskExecutor(final Executor pExecutor) {
    mIndexTaskExecutor = pExecutor;
  }
 
  /**
* {@inheritDoc}
* @param pDocument {@inheritDoc}
*/
  public void addDocument(final Document pDocument) {
    final Runnable runnable = new Runnable() {
      public void run() {
        if (getAnalyzer() == null) {
          getLuceneIndexTemplate().addDocument(pDocument);
        } else {
          getLuceneIndexTemplate().addDocument(pDocument, getAnalyzer());
        }
      }
    };
    if (mThreadedTaskExecution) {
      mIndexTaskExecutor.execute(runnable);
    } else {
      runnable.run();
    }
  }
 
  /**
* {@inheritDoc}
*/
  public void updateDocument(final Term pTerm,
                             final Document pDocument) {
    final Runnable runnable = new Runnable() {
      public void run() {
        getLuceneIndexTemplate().deleteDocuments(pTerm);
        if (getAnalyzer() == null) {
          getLuceneIndexTemplate().addDocument(pDocument);
        } else {
          getLuceneIndexTemplate().addDocument(pDocument, getAnalyzer());
        }
      }
    };
    if (mThreadedTaskExecution) {
      mIndexTaskExecutor.execute(runnable);
    } else {
      runnable.run();
    }
  }
 
  /**
* {@inheritDoc}
*/
  public void deleteDocument(final Term pIdTerm) {
    final Runnable runnable = new Runnable() {
      public void run() {
        getLuceneIndexTemplate().deleteDocuments(pIdTerm);
      }
    };
    if (mThreadedTaskExecution) {
      mIndexTaskExecutor.execute(runnable);
    } else {
      runnable.run();
    }
 
  }
 
  /**
* {@inheritDoc}
*/
  public void optimize() {
    final Runnable runnable = new Runnable() {
      public void run() {
        getLuceneIndexTemplate().optimize();
      }
    };
    if (mThreadedTaskExecution) {
      mIndexTaskExecutor.execute(runnable);
    } else {
      runnable.run();
    }
  }
 
  public void deleteIndexFiles(final boolean pConfirmation) throws IOException {
    throw new UnsupportedOperationException(
              "This method is not yet implemented.");
  }
}