diff src/luan/modules/lucene/LuceneIndex.java @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents 475905984870
children 48f302bdc187
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java	Sun Sep 30 19:10:48 2018 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Mon Nov 12 02:10:41 2018 -0700
@@ -94,7 +94,7 @@
 	private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>();
 	private boolean isClosed = true;
 	private final MultiFieldParser mfp;
-	public final LuanTable indexed_only_fields = new LuanTable();
+	public final LuanTable indexed_only_fields;
 	private final Analyzer analyzer;
 	private final Exception created = new Exception("created");
 
@@ -102,7 +102,10 @@
 	private File fileDir;
 	private int writeCount;
 
-	public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException {
+	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 );
@@ -177,18 +180,18 @@
 
 	public void save(LuanState luan,LuanTable doc) throws LuanException, IOException {
 		Set indexedOnlySet = new HashSet();
-		Object typeObj = doc.get(luan,"type");
+		Object typeObj = doc.get("type");
 		if( typeObj==null )
 			throw new LuanException("missing 'type' field");
 		if( !(typeObj instanceof String) )
 			throw new LuanException("type must be string");
 		String type = (String)typeObj;
-		Object indexedOnlyObj = indexed_only_fields.get(luan,type);
+		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(luan) ) {
+			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");
@@ -198,11 +201,11 @@
 					throw new LuanException("indexed_only_fields."+type+" values must be functions");
 				LuanFunction fn = (LuanFunction)value;
 				value = Luan.first(fn.call(luan,new Object[]{doc}));
-				doc.put(luan, name, value );
+				doc.put( name, value );
 				indexedOnlySet.add(name);
 			}
 		}
-		Object obj = doc.get(luan,"id");
+		Object obj = doc.get("id");
 		Long id;
 		try {
 			id = (Long)obj;
@@ -215,10 +218,10 @@
 		try {
 			if( id == null ) {
 				id = nextId(luan);
-				doc.put(luan,"id",id);
-				writer.addDocument(toLucene(luan,doc,indexedOnlySet));
+				doc.put("id",id);
+				writer.addDocument(toLucene(doc,indexedOnlySet));
 			} else {
-				writer.updateDocument( term("id",id), toLucene(luan,doc,indexedOnlySet) );
+				writer.updateDocument( term("id",id), toLucene(doc,indexedOnlySet) );
 			}
 			if(commit) writer.commit();
 		} finally {
@@ -277,10 +280,10 @@
 	public synchronized long nextId(LuanState luan) throws LuanException, IOException {
 		if( ++id > idLim ) {
 			idLim += idBatch;
-			LuanTable doc = new LuanTable();
+			LuanTable doc = new LuanTable(luan);
 			doc.rawPut( "type", "next_id" );
 			doc.rawPut( FLD_NEXT_ID, idLim );
-			writer.updateDocument(new Term("type","next_id"),toLucene(luan,doc,Collections.EMPTY_SET));
+			writer.updateDocument(new Term("type","next_id"),toLucene(doc,Collections.EMPTY_SET));
 			wrote();
 		}
 		return id;
@@ -314,7 +317,7 @@
 		IndexCommit ic = snapshotDeletionPolicy.snapshot();
 		try {
 			String dir = fileDir.toString();
-			LuanTable fileNames = new LuanTable(new ArrayList(ic.getFileNames()));
+			LuanTable fileNames = new LuanTable(luan,new ArrayList(ic.getFileNames()));
 			return fn.call(luan,new Object[]{dir,fileNames});
 		} finally {
 			snapshotDeletionPolicy.release(ic);
@@ -355,7 +358,7 @@
 
 		@Override public Object call(LuanState luan,Object[] args) throws LuanException {
 			try {
-				return toTable(searcher.doc(docID));
+				return toTable(luan,searcher.doc(docID));
 			} catch(IOException e) {
 				throw new LuanException(e);
 			}
@@ -518,10 +521,10 @@
 			throw new LuanException("invalid value type "+value.getClass()+"' for '"+name+"'");
 	}
 
-	private Document toLucene(LuanState luan,LuanTable table,Set indexOnly) throws LuanException {
+	private Document toLucene(LuanTable table,Set indexOnly) throws LuanException {
 		Set<String> indexed = mfp.fields.keySet();
 		Document doc = new Document();
-		for( Map.Entry<Object,Object> entry : table.iterable(luan) ) {
+		for( Map.Entry<Object,Object> entry : table.iterable() ) {
 			Object key = entry.getKey();
 			if( !(key instanceof String) )
 				throw new LuanException("key must be string");
@@ -553,10 +556,10 @@
 		throw new LuanException("invalid field type for "+ifld);
 	}
 
-	private static LuanTable toTable(Document doc) throws LuanException {
+	private static LuanTable toTable(LuanState luan,Document doc) throws LuanException {
 		if( doc==null )
 			return null;
-		LuanTable table = new LuanTable();
+		LuanTable table = new LuanTable(luan);
 		for( IndexableField ifld : doc ) {
 			String name = ifld.name();
 			Object value = getValue(ifld);
@@ -568,7 +571,7 @@
 				if( old instanceof LuanTable ) {
 					list = (LuanTable)old;
 				} else {
-					list = new LuanTable();
+					list = new LuanTable(luan);
 					list.rawPut(1,old);
 					table.rawPut(name,list);
 				}