Package org.apache.xindice.core.indexer

Source Code of org.apache.xindice.core.indexer.NameIndexer

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: NameIndexer.java 571949 2007-09-02 10:52:38Z vgritsenko $
*/

package org.apache.xindice.core.indexer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.core.data.Value;
import org.apache.xindice.core.filer.BTree;
import org.apache.xindice.core.filer.BTreeCallback;
import org.apache.xindice.core.filer.BTreeCorruptException;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.xml.SymbolTable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* NameIndexer is a basic implementation of the Indexer interface.
* It is used for maintaining element and element@attribute unique
* indexes.
*
* @version $Revision: 571949 $, $Date: 2007-09-02 06:52:38 -0400 (Sun, 02 Sep 2007) $
*/
public final class NameIndexer extends BTree implements Indexer {

    private static final Log log = LogFactory.getLog(NameIndexer.class);

    private static final IndexMatch[] EmptyMatches = new IndexMatch[0];

    private static final String NAME = "name";
    private static final String PATTERN = "pattern";

    private Collection collection;
    private SymbolTable symbols;

    private String name;
    private IndexPattern pattern;
    private boolean wildcard;
    private IndexerEventHandler handler;


    public NameIndexer() {
    }

    public void setCollection(Collection collection) {
        this.collection = collection;
        this.symbols = collection.getSymbols();
    }

    private void setLocation(String location) {
        setFile(new File(collection.getCollectionRoot(), location + ".idx"));
    }

    public void setConfig(Configuration config) {
        super.setConfig(config);
        try {
            name = config.getAttribute(NAME);

            String pattern = config.getAttribute(PATTERN);
            wildcard = pattern.indexOf('*') != -1;
            this.pattern = new IndexPattern(symbols, pattern, null);

            setLocation(name);
            setupHandler();
        } catch (Exception e) {
            if (log.isWarnEnabled()) {
                log.warn("ignored exception", e);
            }
        }
    }

    private void setupHandler() {
        handler = new BasicIndexerEventHandler() {
            public void onNameAdded(IndexPattern pattern, Key key, short elemID, short attrID) throws DBException {
                try {
                    addValue(getCombinedValue(key, elemID, attrID), 0);
                } catch (IOException e) {
                    throw new BTreeCorruptException("Corruption detected on add", e);
                }
            }

            public void onNameDeleted(IndexPattern pattern, Key key, short elemID, short attrID) throws DBException {
                try {
                    removeValue(getCombinedValue(key, elemID, attrID));
                } catch (IOException e) {
                    throw new BTreeCorruptException("Corruption detected on remove", e);
                }
            }
        };
    }

    public String getName() {
        return name;
    }

    public String getIndexStyle() {
        return STYLE_NODENAME;
    }

    public IndexPattern[] getPatterns() {
        return new IndexPattern[] { pattern };
    }

    private Value getCombinedValue(Key key, short elemID, short attrID) {
        int l = key.getLength();
        byte[] b = new byte[l + 5];

        // Write the key
        key.copyTo(b, 0, l);
        b[l] = 0;

        // Write the elemID
        b[l + 1] = (byte) ((elemID >>> 8) & 0xFF);
        b[l + 2] = (byte) ( elemID        & 0xFF);

        // Write the attrID
        b[l + 3] = (byte) ((attrID >>> 8) & 0xFF);
        b[l + 4] = (byte) ( attrID        & 0xFF);

        return new Value(b);
    }

    private IndexMatch getIndexMatch(Value v) {
        int l = v.getLength() - 5;
        Key key = v.keyAt(0, l);
        short elemID = v.shortAt(l + 1);
        short attrID = v.shortAt(l + 3);

        return new IndexMatch(key, elemID, attrID);
    }

    public IndexMatch[] queryMatches(final IndexQuery query) throws DBException {
        final List results = new ArrayList();
        final IndexPattern pattern = query.getPattern();

        try {
            query(query, new BTreeCallback() {
                public boolean indexInfo(Value value, long pos) {
                    IndexMatch match = getIndexMatch(value);
                    if (wildcard) {
                        IndexPattern pt = new IndexPattern(symbols, match.getElement(), match.getAttribute());
                        if (pt.getMatchLevel(pattern) > 0) {
                            results.add(match);
                        }
                    } else {
                        results.add(match);
                    }
                    return true;
                }
            });
        } catch (DBException e) {
            throw e;
        } catch (Exception e) {
            if (log.isWarnEnabled()) {
                log.warn("ignored exception", e);
            }
        }

        return (IndexMatch[]) results.toArray(EmptyMatches);
    }

    public IndexerEventHandler getIndexerEventHandler() {
        return handler;
    }
}
TOP

Related Classes of org.apache.xindice.core.indexer.NameIndexer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.