package com.dbxml.db.common.indexers;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: NameIndexer.java,v 1.5 2006/02/02 18:53:52 bradford Exp $
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.dbxml.db.common.btree.BTree;
import com.dbxml.db.common.btree.BTreeCallback;
import com.dbxml.db.common.btree.BTreeCorruptException;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.data.Value;
import com.dbxml.db.core.indexer.IndexMatch;
import com.dbxml.db.core.indexer.IndexPattern;
import com.dbxml.db.core.indexer.IndexQuery;
import com.dbxml.db.core.indexer.Indexer;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.Configuration;
import com.dbxml.xml.SymbolTable;
import com.dbxml.util.dbXMLException;
/**
* NameIndexer is a basic implementation of the Indexer interface.
* It is used for maintaining element and element@attribute unique
* indexes.
*/
public final class NameIndexer extends BTree implements Indexer {
private static final IndexMatch[] EmptyMatches = new IndexMatch[0];
private static final String NAME = "name";
private static final String PATTERN = "pattern";
private static final String PAGESIZE = "pagesize";
private Collection collection;
private SymbolTable symbols;
private String name;
private String pattern;
private boolean wildcard;
private FileHeader fileHeader;
public NameIndexer() {
super(false);
setTransactionSupported(true);
fileHeader = getFileHeader();
}
public void setConfig(Configuration config) throws dbXMLException {
super.setConfig(config);
try {
name = config.getAttribute(NAME);
pattern = config.getAttribute(PATTERN);
wildcard = pattern.indexOf('*') != -1;
fileHeader.setPageSize(config.getIntAttribute(PAGESIZE, fileHeader.getPageSize()));
setLocation(name);
}
catch ( Exception e ) {
e.printStackTrace(System.err);
}
}
public String getName() {
return name;
}
public void setLocation(String location) {
setFile(new File(collection.getCollectionRoot(), location + ".idx"));
}
public void setCollection(Collection collection) {
try {
this.collection = collection;
symbols = collection.getSymbols();
}
catch ( Exception e ) {
e.printStackTrace(System.err);
}
}
public String getIndexStyle() {
return STYLE_NODENAME;
}
public String getPattern() {
return pattern;
}
public void remove(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
try {
removeValue(tx, key);
}
catch ( IOException e ) {
throw new BTreeCorruptException("Corruption detected on remove", e);
}
}
public void add(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
try {
addValue(tx, key, 0);
}
catch ( IOException e ) {
throw new BTreeCorruptException("Corruption detected on add", e);
}
}
public IndexMatch[] queryMatches(Transaction tx, final IndexQuery query) throws DBException {
final List results = new ArrayList(128);
final IndexPattern pattern = query.getPattern();
try {
query(tx, query, new BTreeCallback() {
public void indexInfo(Value value, Value extra) {
results.add(new IndexMatch(new Key(value), pattern));
}
});
}
catch ( DBException d ) {
throw d;
}
catch ( Exception e ) {
e.printStackTrace(System.err);
}
return (IndexMatch[])results.toArray(EmptyMatches);
}
}