001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.update;
017:
018: import org.apache.lucene.index.IndexWriter;
019: import org.apache.solr.schema.IndexSchema;
020:
021: import java.util.logging.Logger;
022: import java.io.IOException;
023:
024: /**
025: * An IndexWriter that is configured via Solr config mechanisms.
026: *
027: * @author yonik
028: * @version $Id: SolrIndexWriter.java 533507 2007-04-29 14:05:21Z yonik $
029: * @since solr 0.9
030: */
031:
032: public class SolrIndexWriter extends IndexWriter {
033: private static Logger log = Logger.getLogger(SolrIndexWriter.class
034: .getName());
035:
036: String name;
037: IndexSchema schema;
038:
039: private void init(String name, IndexSchema schema,
040: SolrIndexConfig config) {
041: log.fine("Opened Writer " + name);
042: this .name = name;
043: this .schema = schema;
044: setSimilarity(schema.getSimilarity());
045: // setUseCompoundFile(false);
046:
047: if (config != null) {
048: setUseCompoundFile(config.useCompoundFile);
049: if (config.maxBufferedDocs != -1)
050: setMaxBufferedDocs(config.maxBufferedDocs);
051: if (config.maxMergeDocs != -1)
052: setMaxMergeDocs(config.maxMergeDocs);
053: if (config.mergeFactor != -1)
054: setMergeFactor(config.mergeFactor);
055: if (config.maxFieldLength != -1)
056: setMaxFieldLength(config.maxFieldLength);
057: //if (config.commitLockTimeout != -1) setWriteLockTimeout(config.commitLockTimeout);
058: }
059:
060: }
061:
062: public SolrIndexWriter(String name, String path, boolean create,
063: IndexSchema schema) throws IOException {
064: super (path, schema.getAnalyzer(), create);
065: init(name, schema, null);
066: }
067:
068: public SolrIndexWriter(String name, String path, boolean create,
069: IndexSchema schema, SolrIndexConfig config)
070: throws IOException {
071: super (path, schema.getAnalyzer(), create);
072: init(name, schema, config);
073: }
074:
075: /*** use DocumentBuilder now...
076: private final void addField(Document doc, String name, String val) {
077: SchemaField ftype = schema.getField(name);
078:
079: // we don't check for a null val ourselves because a solr.FieldType
080: // might actually want to map it to something. If createField()
081: // returns null, then we don't store the field.
082:
083: Field field = ftype.createField(val, boost);
084: if (field != null) doc.add(field);
085: }
086:
087:
088: public void addRecord(String[] fieldNames, String[] fieldValues) throws IOException {
089: Document doc = new Document();
090: for (int i=0; i<fieldNames.length; i++) {
091: String name = fieldNames[i];
092: String val = fieldNames[i];
093:
094: // first null is end of list. client can reuse arrays if they want
095: // and just write a single null if there is unused space.
096: if (name==null) break;
097:
098: addField(doc,name,val);
099: }
100: addDocument(doc);
101: }
102: ******/
103:
104: public void close() throws IOException {
105: log.fine("Closing Writer " + name);
106: super .close();
107: }
108:
109: @Override
110: protected void finalize() {
111: try {
112: super .close();
113: } catch (IOException e) {
114: }
115: }
116:
117: }
|