changeset 1271:48f302bdc187

fix indexed_only_field
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 21:19:52 -0700
parents d410747a671a
children 95f6540b27f9
files src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java
diffstat 2 files changed, 19 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/lucene/Lucene.luan	Mon Nov 12 20:02:50 2018 -0700
+++ b/src/luan/modules/lucene/Lucene.luan	Mon Nov 12 21:19:52 2018 -0700
@@ -51,9 +51,6 @@
 		return java_index.setIndexedFieldParser(key,value)
 	end
 
-	-- index.indexed_only_fields[type][field] = fn(doc)
-	index.indexed_only_fields = java_index.indexed_only_fields
-
 	index.to_string = java_index.to_string
 --	index.backup = java_index.backup
 	index.snapshot = java_index.snapshot
@@ -67,6 +64,7 @@
 	index.ensure_open = java_index.ensure_open
 	index.next_id = java_index.nextId
 	index.highlighter = java_index.highlighter
+	index.indexed_only_field = java_index.indexed_only_field
 
 	Lucene.instances[index] = true
 
--- a/src/luan/modules/lucene/LuceneIndex.java	Mon Nov 12 20:02:50 2018 -0700
+++ b/src/luan/modules/lucene/LuceneIndex.java	Mon Nov 12 21:19:52 2018 -0700
@@ -72,6 +72,7 @@
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
+import luan.LuanCloner;
 import luan.LuanException;
 import luan.LuanRuntimeException;
 import org.slf4j.Logger;
@@ -94,7 +95,6 @@
 	private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>();
 	private boolean isClosed = true;
 	private final MultiFieldParser mfp;
-	public final LuanTable indexed_only_fields;
 	private final Analyzer analyzer;
 	private final Exception created = new Exception("created");
 
@@ -102,10 +102,13 @@
 	private File fileDir;
 	private int writeCount;
 
+	private final ConcurrentMap<String,Map<String,LuanFunction>> indexedOnlyFields = new ConcurrentHashMap<String,Map<String,LuanFunction>>();
+	private final LuanState luanMine = new LuanState();
+	private final LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
+
 	public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields)
 		throws LuanException, IOException
 	{
-		indexed_only_fields = new LuanTable(luan);  // not good, not thread safe
 		mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields);
 		mfp.fields.put( "type", STRING_FIELD_PARSER );
 		mfp.fields.put( "id", NumberFieldParser.LONG );
@@ -178,6 +181,13 @@
 		}
 	}
 
+	public void indexed_only_field(String type,String field,LuanFunction fn) {
+		fn = (LuanFunction)cloner.get(fn);
+		indexedOnlyFields.putIfAbsent(type,new ConcurrentHashMap<String,LuanFunction>());
+		Map<String,LuanFunction> map = indexedOnlyFields.get(type);
+		map.put(field,fn);
+	}
+
 	public void save(LuanState luan,LuanTable doc) throws LuanException, IOException {
 		Set indexedOnlySet = new HashSet();
 		Object typeObj = doc.get("type");
@@ -186,21 +196,12 @@
 		if( !(typeObj instanceof String) )
 			throw new LuanException("type must be string");
 		String type = (String)typeObj;
-		Object indexedOnlyObj = indexed_only_fields.get(type);
-		if( indexedOnlyObj != null ) {
-			if( !(indexedOnlyObj instanceof LuanTable) )
-				throw new LuanException("indexed_only_fields elements must be tables");
-			LuanTable indexedOnly = (LuanTable)indexedOnlyObj;
-			for( Map.Entry<Object,Object> entry : indexedOnly.iterable() ) {
-				Object key = entry.getKey();
-				if( !(key instanceof String) )
-					throw new LuanException("indexed_only_fields."+type+" entries must be strings");
-				String name = (String)key;
-				Object value = entry.getValue();
-				if( !(value instanceof LuanFunction) )
-					throw new LuanException("indexed_only_fields."+type+" values must be functions");
-				LuanFunction fn = (LuanFunction)value;
-				value = Luan.first(fn.call(luan,new Object[]{doc}));
+		Map<String,LuanFunction> map = indexedOnlyFields.get(type);
+		if( map != null ) {
+			for( Map.Entry<String,LuanFunction> entry : map.entrySet() ) {
+				String name = entry.getKey();
+				LuanFunction fn = entry.getValue();
+				Object value = Luan.first(fn.call(luan,new Object[]{doc}));
 				doc.put( name, value );
 				indexedOnlySet.add(name);
 			}