Mercurial Hosting > luan
annotate src/luan/modules/lucene/SupplementingConfig.java @ 1563:8fbcc4747091
remove LuanFunction.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Nov 2020 01:37:57 -0700 |
parents | b89212fd04b5 |
children | c922446f53aa |
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; | |
16 import luan.LuanCloner; | |
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 | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
25 SupplementingConfig(Version luceneVersion,MultiFieldParser mfp,Luan luan,LuanFunction supplementer) { |
1528 | 26 super(luceneVersion,mfp); |
27 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
28 this.luan = (Luan)cloner.get(luan); |
1528 | 29 this.supplementer = (LuanFunction)cloner.get(supplementer); |
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); |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
44 tbl = (LuanTable)supplementer.call(luan,tbl); |
1528 | 45 if( tbl == null ) { |
46 return super.getMoreFieldInfo(storedFields); | |
47 } else { | |
1529 | 48 return new MoreFieldInfo(toLucene(tbl),Collections.emptyMap()); |
1528 | 49 } |
50 } catch(LuanException e) { | |
51 throw new LuanRuntimeException(e); | |
52 } | |
53 } | |
1529 | 54 |
1562 | 55 static LuanTable toTable(Map map) throws LuanException { |
56 LuanTable table = new LuanTable(); | |
1529 | 57 for( Object obj : map.entrySet() ) { |
58 Map.Entry entry = (Map.Entry)obj; | |
59 Object value = entry.getValue(); | |
60 if( value instanceof List ) | |
1562 | 61 value = new LuanTable((List)value); |
1529 | 62 table.rawPut( entry.getKey(), value ); |
63 } | |
64 return table; | |
65 } | |
66 | |
67 static Map<String,Object> toLucene(LuanTable table) throws LuanException { | |
68 Map<String,Object> map = new LinkedHashMap<String,Object>(); | |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
69 for( Map.Entry<Object,Object> entry : table.rawIterable() ) { |
1529 | 70 String name = (String)entry.getKey(); |
71 Object value = entry.getValue(); | |
72 if( value instanceof LuanTable ) { | |
73 LuanTable list = (LuanTable)value; | |
74 if( !list.isList() ) | |
75 throw new LuanException("table value for '"+name+"' must be a list"); | |
76 value = list.asList(); | |
77 } | |
78 map.put(name,value); | |
79 } | |
80 return map; | |
81 } | |
82 | |
1528 | 83 } |