comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 578:60c549d43988

remove LuanState.exception()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jul 2015 17:40:48 -0600
parents 7c3ad6db8ac3
children 790d5de23042
comparison
equal deleted inserted replaced
577:d7a85fbe15f1 578:60c549d43988
133 } 133 }
134 } 134 }
135 135
136 public void save(LuanState luan,LuanTable doc) throws LuanException, IOException { 136 public void save(LuanState luan,LuanTable doc) throws LuanException, IOException {
137 if( doc.get(luan,"type")==null ) 137 if( doc.get(luan,"type")==null )
138 throw luan.exception("missing 'type' field"); 138 throw new LuanException(luan,"missing 'type' field");
139 Long id = (Long)doc.get(luan,"id"); 139 Long id = (Long)doc.get(luan,"id");
140 140
141 boolean commit = !writeLock.isHeldByCurrentThread(); 141 boolean commit = !writeLock.isHeldByCurrentThread();
142 writeLock.lock(); 142 writeLock.lock();
143 try { 143 try {
210 } 210 }
211 211
212 212
213 public void backup(LuanState luan,String zipFile) throws LuanException, IOException { 213 public void backup(LuanState luan,String zipFile) throws LuanException, IOException {
214 if( !zipFile.endsWith(".zip") ) 214 if( !zipFile.endsWith(".zip") )
215 throw luan.exception("file "+zipFile+" doesn't end with '.zip'"); 215 throw new LuanException(luan,"file "+zipFile+" doesn't end with '.zip'");
216 IndexCommit ic = snapshotDeletionPolicy.snapshot(); 216 IndexCommit ic = snapshotDeletionPolicy.snapshot();
217 try { 217 try {
218 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); 218 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
219 for( String fileName : ic.getFileNames() ) { 219 for( String fileName : ic.getFileNames() ) {
220 out.putNextEntry(new ZipEntry(fileName)); 220 out.putNextEntry(new ZipEntry(fileName));
263 263
264 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 264 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
265 try { 265 try {
266 return toTable(luan,searcher.doc(docID)); 266 return toTable(luan,searcher.doc(docID));
267 } catch(IOException e) { 267 } catch(IOException e) {
268 throw luan.exception(e); 268 throw new LuanException(luan,e);
269 } 269 }
270 } 270 }
271 } 271 }
272 272
273 private static abstract class MyCollector extends Collector { 273 private static abstract class MyCollector extends Collector {
307 if( !inTransaction ) 307 if( !inTransaction )
308 searcher = openSearcher(); 308 searcher = openSearcher();
309 try { 309 try {
310 if( fn!=null && n==null ) { 310 if( fn!=null && n==null ) {
311 if( sortStr != null ) 311 if( sortStr != null )
312 throw luan.exception("sort must be nil when n is nil"); 312 throw new LuanException(luan,"sort must be nil when n is nil");
313 final DocFn docFn = new DocFn(searcher); 313 final DocFn docFn = new DocFn(searcher);
314 MyCollector col = new MyCollector() { 314 MyCollector col = new MyCollector() {
315 @Override public void collect(int doc) { 315 @Override public void collect(int doc) {
316 try { 316 try {
317 docFn.docID = docBase + doc; 317 docFn.docID = docBase + doc;
348 } 348 }
349 } 349 }
350 350
351 public Object search_in_transaction(LuanState luan,LuanFunction fn) throws LuanException, IOException { 351 public Object search_in_transaction(LuanState luan,LuanFunction fn) throws LuanException, IOException {
352 if( threadLocalSearcher.get() != null ) 352 if( threadLocalSearcher.get() != null )
353 throw luan.exception("can't nest search_in_transaction calls"); 353 throw new LuanException(luan,"can't nest search_in_transaction calls");
354 IndexSearcher searcher = openSearcher(); 354 IndexSearcher searcher = openSearcher();
355 threadLocalSearcher.set(searcher); 355 threadLocalSearcher.set(searcher);
356 try { 356 try {
357 return fn.call(luan); 357 return fn.call(luan);
358 } finally { 358 } finally {
373 return mfp.fields.get(key); 373 return mfp.fields.get(key);
374 } 374 }
375 375
376 @Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { 376 @Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
377 if( !(key instanceof String) ) 377 if( !(key instanceof String) )
378 throw luan.exception("key must be string"); 378 throw new LuanException(luan,"key must be string");
379 String field = (String)key; 379 String field = (String)key;
380 if( value==null ) { // delete 380 if( value==null ) { // delete
381 mfp.fields.remove(field); 381 mfp.fields.remove(field);
382 return; 382 return;
383 } 383 }
384 if( !(value instanceof FieldParser) ) 384 if( !(value instanceof FieldParser) )
385 throw luan.exception("value must be FieldParser like the values of Lucene.type"); 385 throw new LuanException(luan,"value must be FieldParser like the values of Lucene.type");
386 FieldParser parser = (FieldParser)value; 386 FieldParser parser = (FieldParser)value;
387 mfp.fields.put( field, parser ); 387 mfp.fields.put( field, parser );
388 } 388 }
389 389
390 @Override public final Iterator keys(LuanTable tbl) { 390 @Override public final Iterator keys(LuanTable tbl) {
404 Set<String> indexed = mfp.fields.keySet(); 404 Set<String> indexed = mfp.fields.keySet();
405 Document doc = new Document(); 405 Document doc = new Document();
406 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) { 406 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) {
407 Object key = entry.getKey(); 407 Object key = entry.getKey();
408 if( !(key instanceof String) ) 408 if( !(key instanceof String) )
409 throw luan.exception("key must be string"); 409 throw new LuanException(luan,"key must be string");
410 String name = (String)key; 410 String name = (String)key;
411 Object value = entry.getValue(); 411 Object value = entry.getValue();
412 if( value instanceof String ) { 412 if( value instanceof String ) {
413 String s = (String)value; 413 String s = (String)value;
414 if( indexed.contains(name) ) { 414 if( indexed.contains(name) ) {
439 } 439 }
440 } else if( value instanceof byte[] ) { 440 } else if( value instanceof byte[] ) {
441 byte[] b = (byte[])value; 441 byte[] b = (byte[])value;
442 doc.add(new StoredField(name, b)); 442 doc.add(new StoredField(name, b));
443 } else 443 } else
444 throw luan.exception("invalid value type "+value.getClass()+"' for '"+name+"'"); 444 throw new LuanException(luan,"invalid value type "+value.getClass()+"' for '"+name+"'");
445 } 445 }
446 return doc; 446 return doc;
447 } 447 }
448 448
449 private static LuanTable toTable(LuanState luan,Document doc) throws LuanException { 449 private static LuanTable toTable(LuanState luan,Document doc) throws LuanException {
465 String s = ifld.stringValue(); 465 String s = ifld.stringValue();
466 if( s != null ) { 466 if( s != null ) {
467 table.rawPut(name,s); 467 table.rawPut(name,s);
468 continue; 468 continue;
469 } 469 }
470 throw luan.exception("invalid field type for "+ifld); 470 throw new LuanException(luan,"invalid field type for "+ifld);
471 } 471 }
472 return table; 472 return table;
473 } 473 }
474 474
475 } 475 }