comparison lucene/src/luan/modules/lucene/LuceneWriter.java @ 230:4438cb2e04d0

start lucene git-svn-id: https://luan-java.googlecode.com/svn/trunk@231 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 30 Sep 2014 20:03:56 +0000
parents
children ef39bc4d3f70
comparison
equal deleted inserted replaced
229:2a54cb7d1cf4 230:4438cb2e04d0
1 package luan.modules.lucene;
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.Set;
6 import java.util.List;
7 import java.util.ArrayList;
8 import org.apache.lucene.index.IndexableField;
9 import org.apache.lucene.index.Term;
10 import luan.Luan;
11 import luan.LuanState;
12 import luan.LuanTable;
13 import luan.LuanJavaFunction;
14 import luan.LuanException;
15
16
17 public final class LuceneWriter {
18 public static final String FLD_TYPE = "type index";
19 public static final String FLD_ID = "id index";
20
21 private final LuceneIndex index;
22
23 LuceneWriter(LuceneIndex index) {
24 index.writeLock.lock();
25 this.index = index;
26 }
27
28 // call in finally block
29 void close() {
30 index.writeLock.unlock();
31 }
32
33 void commit() throws IOException {
34 index.writer.commit();
35 }
36
37 void addDocument(LuanTable doc) throws IOException {
38 index.writer.addDocument(LuceneDocument.toLucene(doc));
39 }
40
41 void updateDocument(Term term,LuanTable doc) throws IOException {
42 index.writer.updateDocument(term,LuceneDocument.toLucene(doc));
43 }
44
45 public void delete_documents(LuanState luan,LuanTable tblTerms) throws LuanException, IOException {
46 List<Term> list = new ArrayList<Term>();
47 for( Map.Entry<Object,Object> entry : tblTerms ) {
48 Object key = entry.getKey();
49 Object value = entry.getValue();
50 if( !(key instanceof String) )
51 throw luan.exception("key must be a string but got "+key.getClass().getSimpleName());
52 if( !(value instanceof String) )
53 throw luan.exception("value must be a string but got "+value.getClass().getSimpleName());
54 list.add( new Term( (String)key, (String)value ) );
55 }
56 index.writer.deleteDocuments(list.toArray(new Term[list.size()]));
57 }
58
59 String nextId() {
60 return index.nextId();
61 }
62
63 public void save_document(LuanTable doc) throws IOException {
64 if( doc.get(FLD_TYPE)==null )
65 throw new RuntimeException("missing '"+FLD_TYPE+"'");
66 String id = (String)doc.get(FLD_ID);
67 if( id == null ) {
68 id = nextId();
69 doc.put(FLD_ID,id);
70 addDocument(doc);
71 } else {
72 updateDocument(new Term(FLD_ID,id),doc);
73 }
74 }
75
76 // luan
77
78 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
79 t.put( method, new LuanJavaFunction(LuceneWriter.class.getMethod(method,parameterTypes),this) );
80 }
81
82 LuanTable table() {
83 LuanTable tbl = Luan.newTable();
84 try {
85 add( tbl, "save_document", LuanTable.class );
86 add( tbl, "delete_documents", LuanState.class, LuanTable.class );
87 } catch(NoSuchMethodException e) {
88 throw new RuntimeException(e);
89 }
90 return tbl;
91 }
92
93 }