Skip to content

Commit

Permalink
finish testIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Huai Jiang authored and Huai Jiang committed Jul 14, 2012
1 parent 9edeba6 commit e248c1e
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 76 deletions.
3 changes: 2 additions & 1 deletion bldfiles/configDesign.xml
Expand Up @@ -34,7 +34,7 @@
</package>

<package name="allapis" package="com.alvazan.orm.api" subpackages="include"/>

<package name ="exceptions" package="com.alvazan.orm.api.base.exc" subpackages="include"/>
<package name="ourParser" package="com.alvazan.orm.parser.antlr">
<depends>antlr</depends>
</package>
Expand Down Expand Up @@ -79,6 +79,7 @@
</package>
<package name="inMemDb" package="com.alvazan.orm.layer3.spi.db.inmemory">
<depends>RawNoSqlSpi</depends>
<depends>exceptions</depends>

</package>

Expand Down
Expand Up @@ -4,7 +4,7 @@

public interface SpiQueryAdapter {

public void setParameter(String parameterName, String value);
public void setParameter(String parameterName, Object value);

@SuppressWarnings("rawtypes")
public List getResultList();
Expand Down
Expand Up @@ -35,17 +35,13 @@ public void setup(Field field2, String colName, Converter converter) {
@Override
public Object translateToIndexFormat(OWNER entity) {
Object value = ReflectionUtil.fetchFieldValue(entity, field);
String indexValue = converter.convertToIndexFormat(value);
return indexValue;
return value;
}

public Class<?> getFieldType(){
return this.field.getType();
}

@Override
public String translateIfEntity(Object value) {
return value+"";
}


}
2 changes: 0 additions & 2 deletions input/javasrc/com/alvazan/orm/impl/meta/data/MetaField.java
Expand Up @@ -16,8 +16,6 @@ public interface MetaField<OWNER> {

public Class<?> getFieldType();

//TODO: should be Object instead of String probably later so we can return int, double, etc.
public String translateIfEntity(Object value);
//TODO: should be Map<String, Object> so we can return int, double, etc. etc.
public Object translateToIndexFormat(OWNER entity);

Expand Down
Expand Up @@ -136,10 +136,7 @@ public Class<?> getFieldType() {
throw new UnsupportedOperationException("not done yet");
}

@Override
public String translateIfEntity(Object value) {
throw new UnsupportedOperationException("not done yet");
}


@Override
public Object translateToIndexFormat(OWNER entity) {
Expand Down
15 changes: 3 additions & 12 deletions input/javasrc/com/alvazan/orm/impl/meta/data/MetaProxyField.java
Expand Up @@ -2,7 +2,6 @@

import java.lang.reflect.Field;

import com.alvazan.orm.api.base.Converter;
import com.alvazan.orm.api.base.exc.ChildWithNoPkException;
import com.alvazan.orm.api.spi.db.Column;
import com.alvazan.orm.api.spi.layer2.MetaTableDbo;
Expand Down Expand Up @@ -60,23 +59,15 @@ public void setup(Field field2, String colName, MetaClass<PROXY> classMeta) {

@Override
public Object translateToIndexFormat(OWNER entity) {
String idStr = translateIfEntity(entity);
return idStr;
Object value = ReflectionUtil.fetchFieldValue(entity, field);
return value;
}

@Override
public Class<?> getFieldType() {
return this.field.getType();
}

@SuppressWarnings("unchecked")
@Override
public String translateIfEntity(Object entity) {
PROXY value = (PROXY) ReflectionUtil.fetchFieldValue(entity, field);
MetaIdField<PROXY> idField = classMeta.getIdField();
Converter converter = idField.getConverter();
String idStr = converter.convertToIndexFormat(value);
return idStr;
}


}
Expand Up @@ -10,6 +10,7 @@
import com.alvazan.orm.api.base.Index;
import com.alvazan.orm.api.base.KeyValue;
import com.alvazan.orm.api.base.NoSqlEntityManager;
import com.alvazan.orm.api.base.exc.StorageMissingEntitesException;
import com.alvazan.orm.api.spi.db.Row;
import com.alvazan.orm.api.spi.layer2.NoSqlSession;
import com.alvazan.orm.impl.meta.data.MetaClass;
Expand Down Expand Up @@ -66,9 +67,23 @@ public <T> List<KeyValue<T>> findAll(Class<T> entityType, List<Object> keys) {

//NOTE: It is WAY more efficient to find ALL keys at once then it is to
//find one at a time. You would rather have 1 find than 1000 if network latency was 1 ms ;).
List<Row> rows = session.find(meta.getColumnFamily(), noSqlKeys);
try{
List<Row> rows = session.find(meta.getColumnFamily(), noSqlKeys);
return getKeyValues( meta,keys,rows);
}catch (StorageMissingEntitesException e) {
List<Row> rows = e.getFoundElements();
List<KeyValue<T>> keyValues = getKeyValues( meta,keys,rows);
StorageMissingEntitesException kExeception= new StorageMissingEntitesException(keyValues,e.getMessage());
throw kExeception;
}



}

private <T> List<KeyValue<T>> getKeyValues(MetaClass<T> meta,List<Object> keys,List<Row> rows){
List<KeyValue<T>> keyValues = new ArrayList<KeyValue<T>>();

for(int i = 0; i < rows.size(); i++) {
Row row = rows.get(i);
Object key = keys.get(i);
Expand Down
42 changes: 29 additions & 13 deletions input/javasrc/com/alvazan/orm/layer1/base/QueryAdapter.java
Expand Up @@ -7,6 +7,7 @@

import com.alvazan.orm.api.base.KeyValue;
import com.alvazan.orm.api.base.Query;
import com.alvazan.orm.api.base.exc.StorageMissingEntitesException;
import com.alvazan.orm.api.base.exc.TooManyResultException;
import com.alvazan.orm.api.base.exc.TypeMismatchException;
import com.alvazan.orm.api.spi.index.SpiQueryAdapter;
Expand Down Expand Up @@ -53,17 +54,19 @@ public void setParameter(String name, Object value) {
//Are actual type will never be a primitive because of autoboxing. When the param
//is passed in, it becomes an Long, Integer, etc. so we need to convert here
Class objectFieldType = MetaColumnDbo.convertIfPrimitive(fieldType);
Class actualType = value.getClass();

if(!objectFieldType.isAssignableFrom(actualType)){
throw new TypeMismatchException("value [" + value
+ "] is not the correct type for the parameter='"+name+"' from inspecting the Entity. Type should be=["
+ fieldType + "]");
}

String newValue = metaField.translateIfEntity(value);
if(value!=null){
Class actualType = value.getClass();

if(!objectFieldType.isAssignableFrom(actualType)){
throw new TypeMismatchException("value [" + value
+ "] is not the correct type for the parameter='"+name+"' from inspecting the Entity. Type should be=["
+ fieldType + "]");
}
}



indexQuery.setParameter(name, newValue);
indexQuery.setParameter(name, value);
}


Expand All @@ -84,13 +87,26 @@ public List<T> getResultList() {
List primaryKeys = indexQuery.getResultList();

//HERE we need to query the nosql database with the primary keys from the index
List<KeyValue<T>> all = session.findAll(metaClass.getMetaClass(), primaryKeys);
try{
List<KeyValue<T>> all = session.findAll(metaClass.getMetaClass(), primaryKeys);
return getEntities(all);
}catch(StorageMissingEntitesException e){
List<KeyValue<T>> partial = e.getFoundElements();
List<T> entities = getEntities(partial);
throw new StorageMissingEntitesException(entities,e.getMessage());
}



}

private List<T> getEntities(List<KeyValue<T>> keyValues){
List<T> entities = new ArrayList<T>();
for(KeyValue<T> keyVal : all) {
for(KeyValue<T> keyVal : keyValues) {
entities.add(keyVal.getValue());
}

return entities;
}

}
Expand Up @@ -6,6 +6,7 @@

import javax.inject.Inject;

import com.alvazan.orm.api.base.exc.StorageMissingEntitesException;
import com.alvazan.orm.api.spi.db.Action;
import com.alvazan.orm.api.spi.db.Column;
import com.alvazan.orm.api.spi.db.NoSqlRawSession;
Expand All @@ -21,9 +22,18 @@ public class InMemorySession implements NoSqlRawSession {
@Override
public List<Row> find(String colFamily, List<byte[]> keys) {
List<Row> rows = new ArrayList<Row>();
StringBuilder missingKey= new StringBuilder();
for(byte[] key : keys) {
Row row = findRow(colFamily, key);
rows.add(row);
try{
Row row = findRow(colFamily, key);
rows.add(row);
}catch (IllegalArgumentException e) {
missingKey.append(e.getLocalizedMessage()+" ");
}

}
if(missingKey.length()>0){
throw new StorageMissingEntitesException(rows,missingKey.toString());
}
return rows;
}
Expand Down
Expand Up @@ -32,6 +32,10 @@ public void removeRow(byte[] rowKey) {

public Row getRow(byte[] rowKey) {
String strValue = convert(rowKey);
return keyToRow.get(strValue);
Row row = keyToRow.get(strValue);
if(row==null){
throw new IllegalArgumentException(strValue);
}
return row;
}
}
Expand Up @@ -20,6 +20,8 @@
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alvazan.orm.api.spi.index.IndexAdd;
import com.alvazan.orm.api.spi.index.IndexReaderWriter;
Expand All @@ -30,7 +32,7 @@
import com.alvazan.orm.api.spi.index.exc.IndexErrorInfo;

public class MemoryIndexWriter implements IndexReaderWriter {

private static final Logger log = LoggerFactory.getLogger(MemoryIndexWriter.class);
@Inject
private Indice indice;
@Inject
Expand Down Expand Up @@ -109,6 +111,7 @@ public void sendAdds(Map<String, List<IndexAdd>> addToIndex) {
adds = entry.getValue();
addToIndex(key, adds);
} catch (Exception e) {
log.error("creating index error ",e);
exceptions.add(create(adds, e));
}
}
Expand Down Expand Up @@ -170,30 +173,36 @@ private static Document createDocument(String idValue, Map<String, Object> map)
for (Entry<String, Object> item : map.entrySet()) {
String key = item.getKey();
Object value = item.getValue();
if(value==null) continue;
if(value==null) {
doc.add(new Field(key, "__impossible__value__", Field.Store.NO,
Field.Index.ANALYZED));
continue;
}
// FIXME stupid if else if

if (value instanceof String)
if (value instanceof String){
doc.add(new Field(key, (String) value, Field.Store.NO,
Field.Index.ANALYZED));
}
else if (value instanceof Number) {
NumericField numericField = new NumericField(key,
Field.Store.NO, true);
if (value.getClass() == int.class
|| value.getClass() == Integer.class) {
if (value.getClass() == Integer.class) {
numericField.setIntValue((Integer) value);
} else if (value.getClass() == long.class
|| value.getClass() == Long.class) {
} else if ( value.getClass() == Long.class) {
numericField.setLongValue((Long) value);
} else if (value.getClass() == double.class
|| value.getClass() == Double.class) {
} else if (value.getClass() == Double.class) {
numericField.setDoubleValue((Double) value);
} else if (value.getClass() == float.class
|| value.getClass() == Float.class) {
} else if (value.getClass() == Float.class) {
numericField.setFloatValue((Float) value);
}
doc.add(numericField);
} else {
}
else if (value instanceof Boolean){
doc.add(new Field(key, value+"", Field.Store.NO,
Field.Index.ANALYZED));
}
else {
throw new RuntimeException("Unsupport field " + key + " type "
+ value.getClass());
}
Expand Down
Expand Up @@ -38,7 +38,7 @@ public void setup(String indexName, SpiMetaQueryImpl spiQuery) {
}

@Override
public void setParameter(String parameterName, String value) {
public void setParameter(String parameterName, Object value) {
log.info("set param for query "+ parameterName +"="+value);
parameterValues.put(parameterName, value);
}
Expand Down

0 comments on commit e248c1e

Please sign in to comment.