Mercurial Hosting > luan
annotate src/luan/modules/lucene/SupplementingConfig.java @ 1578:c922446f53aa
immutable threading
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 08 Feb 2021 14:16:19 -0700 |
parents | 8fbcc4747091 |
children | 46cf5137cb6b |
rev | line source |
---|---|
1528 | 1 package luan.modules.lucene; |
2 | |
3 import java.util.Map; | |
1529 | 4 import java.util.LinkedHashMap; |
5 import java.util.List; | |
1528 | 6 import java.util.Collections; |
7 import org.apache.lucene.index.IndexWriterConfig; | |
8 import org.apache.lucene.index.SnapshotDeletionPolicy; | |
9 import org.apache.lucene.util.Version; | |
10 import goodjava.lucene.queryparser.MultiFieldParser; | |
11 import goodjava.lucene.api.MultiFieldParserConfig; | |
12 import goodjava.lucene.api.MoreFieldInfo; | |
1529 | 13 import luan.Luan; |
1528 | 14 import luan.LuanFunction; |
15 import luan.LuanTable; | |
1578 | 16 import luan.LuanMutable; |
1528 | 17 import luan.LuanException; |
18 import luan.LuanRuntimeException; | |
19 | |
20 | |
21 final class SupplementingConfig extends MultiFieldParserConfig { | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
22 private final Luan luan; |
1528 | 23 private final LuanFunction supplementer; |
24 | |
1578 | 25 SupplementingConfig(Version luceneVersion,MultiFieldParser mfp,Luan luan,LuanFunction supplementer) throws LuanException { |
1528 | 26 super(luceneVersion,mfp); |
1578 | 27 this.luan = new Luan(luan); |
28 LuanMutable.makeImmutable(supplementer); | |
29 this.supplementer = supplementer; | |
1528 | 30 } |
31 | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
32 @Override public IndexWriterConfig newLuceneConfig() { |
1528 | 33 IndexWriterConfig luceneConfig = super.newLuceneConfig(); |
34 SnapshotDeletionPolicy snapshotDeletionPolicy = new SnapshotDeletionPolicy(luceneConfig.getIndexDeletionPolicy()); | |
35 luceneConfig.setIndexDeletionPolicy(snapshotDeletionPolicy); | |
36 return luceneConfig; | |
37 } | |
38 | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
39 @Override public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) { |
1528 | 40 if( supplementer == null ) |
41 return super.getMoreFieldInfo(storedFields); | |
42 try { | |
1562 | 43 LuanTable tbl = toTable(storedFields); |
1578 | 44 synchronized(luan) { |
45 tbl = (LuanTable)supplementer.call(luan,tbl); | |
46 } | |
1528 | 47 if( tbl == null ) { |
48 return super.getMoreFieldInfo(storedFields); | |
49 } else { | |
1529 | 50 return new MoreFieldInfo(toLucene(tbl),Collections.emptyMap()); |
1528 | 51 } |
52 } catch(LuanException e) { | |
53 throw new LuanRuntimeException(e); | |
54 } | |
55 } | |
1529 | 56 |
1562 | 57 static LuanTable toTable(Map map) throws LuanException { |
58 LuanTable table = new LuanTable(); | |
1529 | 59 for( Object obj : map.entrySet() ) { |
60 Map.Entry entry = (Map.Entry)obj; | |
61 Object value = entry.getValue(); | |
62 if( value instanceof List ) | |
1562 | 63 value = new LuanTable((List)value); |
1529 | 64 table.rawPut( entry.getKey(), value ); |
65 } | |
66 return table; | |
67 } | |
68 | |
69 static Map<String,Object> toLucene(LuanTable table) throws LuanException { | |
70 Map<String,Object> map = new LinkedHashMap<String,Object>(); | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
71 for( Map.Entry<Object,Object> entry : table.rawIterable() ) { |
1529 | 72 String name = (String)entry.getKey(); |
73 Object value = entry.getValue(); | |
74 if( value instanceof LuanTable ) { | |
75 LuanTable list = (LuanTable)value; | |
76 if( !list.isList() ) | |
77 throw new LuanException("table value for '"+name+"' must be a list"); | |
78 value = list.asList(); | |
79 } | |
80 map.put(name,value); | |
81 } | |
82 return map; | |
83 } | |
84 | |
1528 | 85 } |