Skip to content

Commit

Permalink
Added some unit test for cell mappings. Fix minor bugs in query boost…
Browse files Browse the repository at this point in the history
… processing.
  • Loading branch information
Andrés de la Peña committed Mar 17, 2014
1 parent b93e74a commit e9c43c6
Show file tree
Hide file tree
Showing 49 changed files with 2,184 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.cassandra.db.index.stratio.query.PrefixQuery;
import org.apache.cassandra.db.index.stratio.query.RangeQuery;
import org.apache.cassandra.db.index.stratio.query.WildcardQuery;
import org.apache.cassandra.db.index.stratio.schema.CellsMapper;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.cassandra.db.filter.QueryFilter;
import org.apache.cassandra.db.filter.SliceQueryFilter;
import org.apache.cassandra.db.index.stratio.RowDirectory.ScoredDocument;
import org.apache.cassandra.db.index.stratio.schema.CellsMapper;
import org.apache.cassandra.db.index.stratio.util.ByteBufferUtils;
import org.apache.cassandra.db.index.stratio.util.Log;
import org.apache.cassandra.db.marshal.CompositeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.index.stratio.schema.CellsMapper;

/**
* Class for building {@link RowService} instances.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

/**
* A cell of a CQL3 logic {@link Cell}, which in most cases is different from a storage engine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

import java.nio.ByteBuffer;

import org.apache.cassandra.db.index.stratio.MappingException;
import org.apache.cassandra.db.index.stratio.query.AbstractQuery;
import org.apache.cassandra.db.index.stratio.query.BooleanQuery;
import org.apache.cassandra.db.index.stratio.query.FuzzyQuery;
import org.apache.cassandra.db.index.stratio.query.MatchQuery;
import org.apache.cassandra.db.index.stratio.query.PhraseQuery;
Expand Down Expand Up @@ -66,6 +69,29 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
*/
public abstract Field field(String name, Object value);

public Query query(AbstractQuery abstractQuery) {
Query query = null;
if (abstractQuery instanceof MatchQuery) {
query = query((MatchQuery) abstractQuery);
} else if (abstractQuery instanceof PrefixQuery) {
query = query((PrefixQuery) abstractQuery);
} else if (abstractQuery instanceof WildcardQuery) {
query = query((WildcardQuery) abstractQuery);
} else if (abstractQuery instanceof PhraseQuery) {
query = query((PhraseQuery) abstractQuery);
} else if (abstractQuery instanceof FuzzyQuery) {
query = query((FuzzyQuery) abstractQuery);
} else if (abstractQuery instanceof RangeQuery) {
query = query((RangeQuery) abstractQuery);
} else if (abstractQuery instanceof BooleanQuery) {
query = query((BooleanQuery) abstractQuery);
} else {
throw new MappingException();
}
query.setBoost(abstractQuery.getBoost());
return query;
}

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link MatchQuery}.
Expand All @@ -75,7 +101,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link MatchQuery}.
*/
public abstract Query query(MatchQuery matchQuery);
protected abstract Query query(MatchQuery matchQuery);

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
Expand All @@ -86,7 +112,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link PrefixQuery}.
*/
public abstract Query query(PrefixQuery prefixQuery);
protected abstract Query query(PrefixQuery prefixQuery);

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
Expand All @@ -97,7 +123,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link WildcardQuery}.
*/
public abstract Query query(WildcardQuery wildcardQuery);
protected abstract Query query(WildcardQuery wildcardQuery);

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
Expand All @@ -108,7 +134,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link PhraseQuery}.
*/
public abstract Query query(PhraseQuery phraseQuery);
protected abstract Query query(PhraseQuery phraseQuery);

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
Expand All @@ -119,7 +145,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link FuzzyQuery}.
*/
public abstract Query query(FuzzyQuery fuzzyQuery);
protected abstract Query query(FuzzyQuery fuzzyQuery);

/**
* Returns the Lucene's {@link org.apache.lucene.search.Query} represented by the specified
Expand All @@ -130,7 +156,7 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* @return The Lucene's {@link org.apache.lucene.search.Query} represented by the specified
* {@link RangeQuery}.
*/
public abstract Query query(RangeQuery rangeQuery);
protected abstract Query query(RangeQuery rangeQuery);

/**
* Returns the cell value resulting from the mapping of the specified object.
Expand All @@ -139,5 +165,5 @@ public static Cell cell(String name, ByteBuffer value, AbstractType<?> type) {
* The object to be mapped.
* @return The cell value resulting from the mapping of the specified object.
*/
protected abstract BASE value(Object value);
public abstract BASE value(Object value);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

import org.apache.cassandra.db.index.stratio.MappingException;
import org.apache.cassandra.db.index.stratio.query.FuzzyQuery;
import org.apache.cassandra.db.index.stratio.query.MatchQuery;
import org.apache.cassandra.db.index.stratio.query.PhraseQuery;
Expand Down Expand Up @@ -41,7 +42,7 @@ public Field field(String name, Object value) {
}

@Override
protected String value(Object value) {
public String value(Object value) {
if (value == null) {
return null;
} else if (value instanceof Boolean) {
Expand All @@ -58,41 +59,41 @@ protected String value(Object value) {
}

@Override
public Query query(MatchQuery matchQuery) {
protected Query query(MatchQuery matchQuery) {
String name = matchQuery.getField();
String value = value(matchQuery.getValue());
Term term = new Term(name, value);
return new TermQuery(term);
}

@Override
public Query query(PrefixQuery prefixQuery) {
protected Query query(PrefixQuery prefixQuery) {
String name = prefixQuery.getField();
String value = value(prefixQuery.getValue());
Term term = new Term(name, value);
return new org.apache.lucene.search.PrefixQuery(term);
}

@Override
public Query query(WildcardQuery wildcardQuery) {
protected Query query(WildcardQuery wildcardQuery) {
String name = wildcardQuery.getField();
String value = value(wildcardQuery.getValue());
Term term = new Term(name, value);
return new org.apache.lucene.search.WildcardQuery(term);
}

@Override
public Query query(PhraseQuery phraseQuery) {
protected Query query(PhraseQuery phraseQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(FuzzyQuery fuzzyQuery) {
protected Query query(FuzzyQuery fuzzyQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(RangeQuery rangeQuery) {
protected Query query(RangeQuery rangeQuery) {
String name = rangeQuery.getField();
String lowerValue = value(rangeQuery.getLowerValue());
String upperValue = value(rangeQuery.getUpperValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.cassandra.db.index.stratio.MappingException;
import org.apache.cassandra.db.index.stratio.query.FuzzyQuery;
import org.apache.cassandra.db.index.stratio.query.MatchQuery;
import org.apache.cassandra.db.index.stratio.query.PhraseQuery;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Field field(String name, Object value) {
}

@Override
protected Long value(Object value) {
public Long value(Object value) {
if (value == null) {
return null;
} else if (value instanceof Date) {
Expand All @@ -78,34 +79,36 @@ protected Long value(Object value) {
}

@Override
public Query query(MatchQuery matchQuery) {
protected Query query(MatchQuery matchQuery) {
String name = matchQuery.getField();
Long value = value(matchQuery.getValue());
return NumericRangeQuery.newLongRange(name, value, value, true, true);
Query query = NumericRangeQuery.newLongRange(name, value, value, true, true);
query.setBoost(matchQuery.getBoost());
return query;
}

@Override
public Query query(PrefixQuery prefixQuery) {
protected Query query(PrefixQuery prefixQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(WildcardQuery wildcardQuery) {
protected Query query(WildcardQuery wildcardQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(PhraseQuery phraseQuery) {
protected Query query(PhraseQuery phraseQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(FuzzyQuery fuzzyQuery) {
protected Query query(FuzzyQuery fuzzyQuery) {
throw new UnsupportedOperationException();
}

@Override
public Query query(RangeQuery rangeQuery) {
protected Query query(RangeQuery rangeQuery) {
String name = rangeQuery.getField();
Long lowerValue = value(rangeQuery.getLowerValue());
Long upperValue = value(rangeQuery.getUpperValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

import org.apache.cassandra.db.index.stratio.MappingException;
import org.apache.cassandra.db.index.stratio.query.FuzzyQuery;
import org.apache.cassandra.db.index.stratio.query.MatchQuery;
import org.apache.cassandra.db.index.stratio.query.PhraseQuery;
Expand Down Expand Up @@ -46,7 +47,7 @@ public Field field(String name, Object value) {
}

@Override
protected Double value(Object value) {
public Double value(Object value) {
if (value == null) {
return null;
} else if (value instanceof Number) {
Expand All @@ -59,34 +60,36 @@ protected Double value(Object value) {
}

@Override
public Query query(MatchQuery matchQuery) {
protected Query query(MatchQuery matchQuery) {
String name = matchQuery.getField();
Double value = value(matchQuery.getValue());
return NumericRangeQuery.newDoubleRange(name, value, value, true, true);
Query query = NumericRangeQuery.newDoubleRange(name, value, value, true, true);
query.setBoost(matchQuery.getBoost());
return query;
}

@Override
public Query query(PrefixQuery prefixQuery) {
throw new UnsupportedOperationException();
protected Query query(PrefixQuery prefixQuery) {
throw new MappingException();
}

@Override
public Query query(WildcardQuery wildcardQuery) {
throw new UnsupportedOperationException();
protected Query query(WildcardQuery wildcardQuery) {
throw new MappingException();
}

@Override
public Query query(PhraseQuery phraseQuery) {
throw new UnsupportedOperationException();
protected Query query(PhraseQuery phraseQuery) {
throw new MappingException();
}

@Override
public Query query(FuzzyQuery fuzzyQuery) {
throw new UnsupportedOperationException();
protected Query query(FuzzyQuery fuzzyQuery) {
throw new MappingException();
}

@Override
public Query query(RangeQuery rangeQuery) {
protected Query query(RangeQuery rangeQuery) {
String name = rangeQuery.getField();
Double lowerValue = value(rangeQuery.getLowerValue());
Double upperValue = value(rangeQuery.getUpperValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.cassandra.db.index.stratio;
package org.apache.cassandra.db.index.stratio.schema;

import org.apache.cassandra.db.index.stratio.MappingException;
import org.apache.cassandra.db.index.stratio.query.FuzzyQuery;
import org.apache.cassandra.db.index.stratio.query.MatchQuery;
import org.apache.cassandra.db.index.stratio.query.PhraseQuery;
Expand Down Expand Up @@ -46,47 +47,49 @@ public Field field(String name, Object value) {
}

@Override
protected Float value(Object value) {
public Float value(Object value) {
if (value == null) {
return null;
} else if (value instanceof Number) {
return ((Number) value).floatValue();
} else if (value instanceof String) {
return Float.valueOf(value.toString());
return Double.valueOf(value.toString()).floatValue();
} else {
throw new MappingException("Value '%s' cannot be cast to Float", value);
}
}

@Override
public Query query(MatchQuery matchQuery) {
protected Query query(MatchQuery matchQuery) {
String name = matchQuery.getField();
Float value = value(matchQuery.getValue());
return NumericRangeQuery.newFloatRange(name, value, value, true, true);
Query query = NumericRangeQuery.newFloatRange(name, value, value, true, true);
query.setBoost(matchQuery.getBoost());
return query;
}

@Override
public Query query(PrefixQuery prefixQuery) {
throw new UnsupportedOperationException();
protected Query query(PrefixQuery prefixQuery) {
throw new MappingException();
}

@Override
public Query query(WildcardQuery wildcardQuery) {
throw new UnsupportedOperationException();
protected Query query(WildcardQuery wildcardQuery) {
throw new MappingException();
}

@Override
public Query query(PhraseQuery phraseQuery) {
throw new UnsupportedOperationException();
protected Query query(PhraseQuery phraseQuery) {
throw new MappingException();
}

@Override
public Query query(FuzzyQuery fuzzyQuery) {
throw new UnsupportedOperationException();
protected Query query(FuzzyQuery fuzzyQuery) {
throw new MappingException();
}

@Override
public Query query(RangeQuery rangeQuery) {
protected Query query(RangeQuery rangeQuery) {
String name = rangeQuery.getField();
Float lowerValue = value(rangeQuery.getLowerValue());
Float upperValue = value(rangeQuery.getUpperValue());
Expand Down
Loading

0 comments on commit e9c43c6

Please sign in to comment.