comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 547:0be287ab0309

add lucene/Versioning and simplify Lucene fn names
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 15 Jun 2015 01:22:58 -0600
parents eaef1005ab87
children f1601a4ce1aa
comparison
equal deleted inserted replaced
546:eaef1005ab87 547:0be287ab0309
112 } finally { 112 } finally {
113 writeLock.unlock(); 113 writeLock.unlock();
114 } 114 }
115 } 115 }
116 116
117 private static Term term(String key,int value) {
118 BytesRef br = new BytesRef();
119 NumericUtils.intToPrefixCoded(value,0,br);
120 return new Term(key,br);
121 }
122
123 private static Term term(String key,long value) { 117 private static Term term(String key,long value) {
124 BytesRef br = new BytesRef(); 118 BytesRef br = new BytesRef();
125 NumericUtils.longToPrefixCoded(value,0,br); 119 NumericUtils.longToPrefixCoded(value,0,br);
126 return new Term(key,br); 120 return new Term(key,br);
127 } 121 }
128 122
129 private static Term term(LuanState luan,String key,Object value) throws LuanException { 123 public void delete(LuanState luan,String queryStr) throws LuanException, IOException, ParseException {
130 if( value instanceof String ) 124 Query query = SaneQueryParser.parseQuery(mfp,queryStr);
131 return new Term( key, (String)value );
132 if( value instanceof Integer )
133 return term( key, (Integer)value );
134 if( value instanceof Long )
135 return term( key, (Long)value );
136 if( value instanceof Float )
137 return term( key, NumericUtils.floatToSortableInt((Float)value) );
138 if( value instanceof Double )
139 return term( key, NumericUtils.doubleToSortableLong((Double)value) );
140 throw luan.exception("invalid value type '"+value.getClass().getSimpleName()+"' for key '"+key+"'");
141 }
142
143 public void delete_documents(LuanState luan,LuanTable tblTerms) throws LuanException, IOException {
144 List<Term> list = new ArrayList<Term>();
145 for( Map.Entry<Object,Object> entry : tblTerms.iterable(luan) ) {
146 Object key = entry.getKey();
147 Object value = entry.getValue();
148 if( !(key instanceof String) )
149 throw luan.exception("key must be a string but got "+key.getClass().getSimpleName());
150 list.add( term( luan, (String)key, value ) );
151 }
152 125
153 boolean commit = !writeLock.isHeldByCurrentThread(); 126 boolean commit = !writeLock.isHeldByCurrentThread();
154 writeLock.lock(); 127 writeLock.lock();
155 try { 128 try {
156 writer.deleteDocuments(list.toArray(new Term[list.size()])); 129 writer.deleteDocuments(query);
157 if(commit) writer.commit(); 130 if(commit) writer.commit();
158 } finally { 131 } finally {
159 writeLock.unlock(); 132 writeLock.unlock();
160 } 133 }
161 } 134 }
162 135
163 public void save_document(LuanState luan,LuanTable doc) throws LuanException, IOException { 136 public void save(LuanState luan,LuanTable doc) throws LuanException, IOException {
164 if( doc.get(luan,"type")==null ) 137 if( doc.get(luan,"type")==null )
165 throw luan.exception("missing 'type' field"); 138 throw luan.exception("missing 'type' field");
166 Long id = (Long)doc.get(luan,"id"); 139 Long id = (Long)doc.get(luan,"id");
167 140
168 boolean commit = !writeLock.isHeldByCurrentThread(); 141 boolean commit = !writeLock.isHeldByCurrentThread();
197 private long idLim = 0; 170 private long idLim = 0;
198 private final int idBatch = 10; 171 private final int idBatch = 10;
199 172
200 private void initId(LuanState luan) throws LuanException, IOException { 173 private void initId(LuanState luan) throws LuanException, IOException {
201 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); 174 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1);
175
176 // tmp hack
177 if( td.totalHits == 0 ) {
178 td = searcher.search(new TermQuery(new Term("type index","next_id")),1);
179 if( td.totalHits == 1 ) {
180 long idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue();
181 LuanTable doc = new LuanTable();
182 doc.rawPut( "type", "next_id" );
183 doc.rawPut( FLD_NEXT_ID, idLim );
184 writer.addDocument(toLucene(luan,doc));
185 writer.commit();
186 }
187 }
188
202 switch(td.totalHits) { 189 switch(td.totalHits) {
203 case 0: 190 case 0:
204 break; // do nothing 191 break; // do nothing
205 case 1: 192 case 1:
206 idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); 193 idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue();
325 throw luan.exception("sort must be nil when n is nil"); 312 throw luan.exception("sort must be nil when n is nil");
326 final DocFn docFn = new DocFn(searcher); 313 final DocFn docFn = new DocFn(searcher);
327 MyCollector col = new MyCollector() { 314 MyCollector col = new MyCollector() {
328 @Override public void collect(int doc) { 315 @Override public void collect(int doc) {
329 try { 316 try {
330 docFn.docID = doc; 317 docFn.docID = docBase + doc;
331 luan.call(fn,new Object[]{++i,docFn}); 318 luan.call(fn,new Object[]{++i,docFn});
332 } catch(LuanException e) { 319 } catch(LuanException e) {
333 throw new LuanRuntimeException(e); 320 throw new LuanRuntimeException(e);
334 } 321 }
335 } 322 }