/*
* 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: ValueIndexerTest.java 530223 2007-04-19 01:48:06Z vgritsenko $
*/
package org.apache.xindice.core.indexer;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.Database;
import org.apache.xindice.core.DatabaseTest;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.xml.dom.DOMParser;
import junit.framework.TestCase;
import org.w3c.dom.Document;
/**
* Tests indexed queries
*
* @version $Revision: 530223 $, $Date: 2007-04-18 21:48:06 -0400 (Wed, 18 Apr 2007) $
*/
public class ValueIndexerTest extends TestCase {
private Database db;
private Collection collection;
protected String indexClass;
public ValueIndexerTest(String name) {
super(name);
indexClass = "org.apache.xindice.core.indexer.ValueIndexer";
}
public void setUp() throws Exception {
String name = getClass().getName();
db = new Database();
db.setConfig(new Configuration(DOMParser.toDocument(DatabaseTest.DATABASE)));
collection = db.createCollection(name, new Configuration(
DOMParser.toDocument(
"<collection compressed='true' name='" + name + "' inline-metadata='true'>" +
"<filer class='org.apache.xindice.core.filer.BTreeFiler'/>" +
"</collection>"), false
));
}
public void tearDown() throws Exception {
db.dropCollection(collection);
db.close();
}
private Indexer createIndex(String name, String pattern, String type) throws Exception {
String config = "<index name='" + name + "' " +
"class='" + indexClass + "' " +
"pattern='" + pattern + "' type='" + type + "'/>";
Indexer ind = collection.createIndexer(new Configuration(DOMParser.toDocument(config)));
Thread.sleep(100);
return ind;
}
private IndexMatch[] query(Indexer ind, String pattern, String value, int op) throws DBException {
IndexPattern indPattern = new IndexPattern(collection.getSymbols(), pattern, null);
IndexQuery query = new IndexQuery(indPattern, op, value);
return ind.queryMatches(query);
}
public void testStringIndex() throws Exception {
Indexer ind = createIndex("StrIndex", "test@value", "string");
Document document = DOMParser.toDocument("<test value='abc'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='abe'/>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<test value='bcd'/>");
collection.insertDocument("key3", document);
document = DOMParser.toDocument("<test value='aac'/>");
collection.insertDocument("key", document);
IndexMatch[] match = query(ind, "test@pattern", "ab", IndexQuery.SW);
assertEquals(2, match.length);
}
public void testLongIntIndex() throws Exception {
Indexer ind = createIndex("IntIndex", "test@value", "long");
Document document = DOMParser.toDocument("<test value='1050687000291'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='1049903940000'/>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<test value='-1050687000291'/>");
collection.insertDocument("key3", document);
IndexMatch[] match = query(ind, "test@pattern", "1049903940000", IndexQuery.GT);
assertEquals(1, match.length);
}
public void testDoubleFloatIndex() throws Exception {
Indexer ind = createIndex("FloatIndex", "test@value", "float");
Document document = DOMParser.toDocument("<test value='71.4894'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='211.499539'/>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<test value='-211.499539'/>");
collection.insertDocument("key3", document);
document = DOMParser.toDocument("<test value='-391.958379'/>");
collection.insertDocument("key4", document);
document = DOMParser.toDocument("<test value='0.00499539'/>");
collection.insertDocument("key5", document);
document = DOMParser.toDocument("<test value='0'/>");
collection.insertDocument("key6", document);
document = DOMParser.toDocument("<test value='" + Double.NEGATIVE_INFINITY + "'/>");
collection.insertDocument("key7", document);
document = DOMParser.toDocument("<test value='" + Double.POSITIVE_INFINITY + "'/>");
collection.insertDocument("key8", document);
IndexMatch[] match = query(ind, "test@pattern", "71.48941", IndexQuery.LT);
assertEquals(6, match.length);
match = query(ind, "test@pattern", "-211.499539", IndexQuery.LT);
assertEquals(2, match.length);
}
public void testByteIndex() throws Exception {
Indexer ind = createIndex("ByteIndex", "test@value", "byte");
Document document = DOMParser.toDocument("<test value='20'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='-112'/>");
collection.insertDocument("key2", document);
IndexMatch[] match = query(ind, "test@pattern", "-112", IndexQuery.GT);
assertEquals(1, match.length);
}
public void testCharIndex() throws Exception {
Indexer ind = createIndex("CharIndex", "test@value", "char");
Document document = DOMParser.toDocument("<test value='z'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='\u00FF'/>");
collection.insertDocument("key2", document);
IndexMatch[] match = query(ind, "test@pattern", "z", IndexQuery.EQ);
assertEquals(1, match.length);
}
public void testBooleanIndex() throws Exception {
Indexer ind = createIndex("BooleanIndex", "test@value", "boolean");
Document document = DOMParser.toDocument("<test value='false'/>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value='true'/>");
collection.insertDocument("key2", document);
IndexMatch[] match = query(ind, "test@pattern", "false", IndexQuery.EQ);
assertEquals(1, match.length);
}
public void testMixedValues() throws Exception {
Indexer ind = createIndex("MixIndex", "test@value", "string");
byte[] value = "<test value='1'/>".getBytes();
collection.insertBinary("key1", value);
Document document = DOMParser.toDocument("<test value='1'/>");
collection.insertDocument("key1", document);
IndexMatch[] match = query(ind, "test@value", "1", IndexQuery.EQ);
assertEquals("XML document did not get indexed: ", 1, match.length);
collection.insertBinary("key1", value);
match = query(ind, "test@value", "1", IndexQuery.EQ);
assertEquals("Found previous XML document in the index: ", 0, match.length);
document = DOMParser.toDocument("<test value='2'/>");
collection.insertDocument("key1", document);
match = query(ind, "test@value", "1", IndexQuery.EQ);
assertEquals("Found previous XML document in the index: ", 0, match.length);
match = query(ind, "test@value", "2", IndexQuery.EQ);
assertEquals("Did not find new XML document in the index: ", 1, match.length);
}
}