Skip to content

Commit

Permalink
adding support for BigDecimal/BigInteger storage mechanism and also s…
Browse files Browse the repository at this point in the history
…aving the meta data now and have a test for that
  • Loading branch information
Dean Hiller committed Jul 26, 2012
1 parent 150131d commit f051acf
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 53 deletions.
4 changes: 3 additions & 1 deletion bldfiles/configDesign.xml
Expand Up @@ -79,7 +79,9 @@

<!-- This section is just the database adapters depending on the spi -->
<package name="cass" package="com.alvazan.orm.layer3.spi.db.cassandra">
<depends>RawNoSqlSpi</depends>
<depends>RawNoSqlSpi</depends> <!-- implements this SPI -->
<depends>api</depends> <!-- ironically uses not implements -->
<depends>NoSqlApi</depends>
<depends>astyanax</depends>
</package>
<package name="hadoop" package="com.alvazan.orm.layer3.spi.db.hadoop">
Expand Down
14 changes: 9 additions & 5 deletions input/javasrc/com/alvazan/orm/api/base/AbstractBootstrap.java
Expand Up @@ -2,18 +2,22 @@

import java.util.Map;

import com.alvazan.orm.api.spi.layer2.Converter;

@SuppressWarnings("rawtypes")
public abstract class AbstractBootstrap {

public synchronized static NoSqlEntityManagerFactory create(DbTypeEnum type, Map<String, String> properties) {
return create(type, "com.alvazan.orm.impl.bindings.Bootstrap", properties);
public synchronized static NoSqlEntityManagerFactory create(DbTypeEnum type, Map<String, String> properties, Map<Class, Converter> converters) {
return create(type, "com.alvazan.orm.impl.bindings.Bootstrap", properties, converters);
}

public synchronized static NoSqlEntityManagerFactory create(DbTypeEnum type, String impl, Map<String, String> properties) {
public synchronized static NoSqlEntityManagerFactory create(DbTypeEnum type, String impl, Map<String, String> properties, Map<Class, Converter> converters) {
try {
Class<?> clazz = Class.forName(impl);
AbstractBootstrap newInstance = (AbstractBootstrap) clazz.newInstance();
return newInstance.createInstance(type, properties);
NoSqlEntityManagerFactory inst = newInstance.createInstance(type, properties, converters);

return inst;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
Expand All @@ -23,6 +27,6 @@ public synchronized static NoSqlEntityManagerFactory create(DbTypeEnum type, Str
}
}

protected abstract NoSqlEntityManagerFactory createInstance(DbTypeEnum type, Map<String, String> properties);
protected abstract NoSqlEntityManagerFactory createInstance(DbTypeEnum type, Map<String, String> properties, Map<Class, Converter> converters);

}
Expand Up @@ -9,6 +9,8 @@
@ImplementedBy(BaseEntityManagerImpl.class)
public interface NoSqlEntityManager {

public static final String META_DB_KEY = "nosqlorm";

/**
* Retrieve underlying interface to write raw columns to. This works the same as the NoSqlEntityManager
* in that you must call flush to execute all the calls to persist.
Expand Down
@@ -1,27 +1,12 @@
package com.alvazan.orm.api.base;

import java.util.Map;

import com.alvazan.orm.api.spi.layer2.Converter;
import com.alvazan.orm.layer1.base.BaseEntityManagerFactoryImpl;
import com.google.inject.ImplementedBy;

@ImplementedBy(BaseEntityManagerFactoryImpl.class)
public interface NoSqlEntityManagerFactory {
/**
* setup scans all classes in a folder or jar with the class nosql.Persistence. EVERY
* jar or folder with that class is scanned not just the first one on the classpath!!!
* are specified, it scans only the jars with those packages. The converters is
* a Map of converters you can supply. When a field is found, we first check
* if the field has it's own converter then check this Map below for converters
* then we check in the standard converters Map which has converters for all
* primitives, etc. etc.
*
* @param converters
* @param packages
*/
@SuppressWarnings("rawtypes")
public void setup(Map<Class, Converter> converters);

public String AUTO_CREATE_KEY = "autoCreateKey";

public NoSqlEntityManager createEntityManager();

Expand Down
Expand Up @@ -21,7 +21,7 @@ public interface NoSqlRawSession {
* in the order we are given here
* @param actions
*/
public void sendChanges(List<Action> actions);
public void sendChanges(List<Action> actions, Object session);

public void clearDatabaseIfInMemoryType();

Expand Down
@@ -1,13 +1,14 @@
package com.alvazan.orm.api.spi.layer2;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.alvazan.orm.api.base.anno.Id;
import com.alvazan.orm.api.base.anno.NoSqlEntity;
import com.alvazan.orm.api.base.anno.OneToMany;

@NoSqlEntity
@NoSqlEntity()
public class DboDatabaseMeta {

@Id(usegenerator=false)
Expand All @@ -32,4 +33,7 @@ public DboTableMeta getMeta(String tableName) {
return colFamilyToMeta.get(tableName);
}

public Collection<DboTableMeta> getAllTables() {
return colFamilyToMeta.values();
}
}
@@ -1,5 +1,6 @@
package com.alvazan.orm.api.spi.layer2;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -80,4 +81,7 @@ public void setForeignKeyToExtensions(String foreignKeyToExtensions) {
this.foreignKeyToExtensions = foreignKeyToExtensions;
}

public Collection<DboColumnMeta> getAllColumns() {
return nameToField.values();
}
}
Expand Up @@ -76,4 +76,6 @@ public interface NoSqlSession {
public Iterable<Column> columnRangeScan(String colFamily, byte[] rowKey,
byte[] from, byte[] to, int batchSize);

public void setOrmSessionForMeta(Object session);

}
10 changes: 8 additions & 2 deletions input/javasrc/com/alvazan/orm/impl/bindings/Bootstrap.java
Expand Up @@ -6,6 +6,7 @@
import com.alvazan.orm.api.base.DbTypeEnum;
import com.alvazan.orm.api.base.NoSqlEntityManagerFactory;
import com.alvazan.orm.api.spi.db.NoSqlRawSession;
import com.alvazan.orm.api.spi.layer2.Converter;
import com.alvazan.orm.api.spi.layer2.DboDatabaseMeta;
import com.alvazan.orm.api.spi.layer2.NoSqlSessionFactory;
import com.alvazan.orm.layer1.base.BaseEntityManagerFactoryImpl;
Expand All @@ -14,8 +15,9 @@

public class Bootstrap extends AbstractBootstrap {

@SuppressWarnings("rawtypes")
@Override
public NoSqlEntityManagerFactory createInstance(DbTypeEnum type, Map<String, String> properties) {
public NoSqlEntityManagerFactory createInstance(DbTypeEnum type, Map<String, String> properties, Map<Class, Converter> converters) {
Injector injector = Guice.createInjector(new ProductionBindings(type));
NoSqlEntityManagerFactory factory = injector.getInstance(NoSqlEntityManagerFactory.class);

Expand All @@ -24,7 +26,11 @@ public NoSqlEntityManagerFactory createInstance(DbTypeEnum type, Map<String, Str

BaseEntityManagerFactoryImpl impl = (BaseEntityManagerFactoryImpl)factory;
impl.setInjector(injector);
return factory;

//The expensive scan all entities occurs here...
impl.setup(properties, converters);

return impl;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions input/javasrc/com/alvazan/orm/layer1/base/AutoCreateEnum.java
@@ -0,0 +1,28 @@
package com.alvazan.orm.layer1.base;

import java.util.HashMap;
import java.util.Map;

public enum AutoCreateEnum {
UPDATE("update"), FAIL_IF_NOT_VALID("validate"), CREATE_ONLY("create");

private static Map<String, AutoCreateEnum> mapping = new HashMap<String, AutoCreateEnum>();
static {
for(AutoCreateEnum a : AutoCreateEnum.values()) {
mapping.put(a.getValue(), a);
}
}

private String value;
private AutoCreateEnum(String val) {
this.value = val;
}

public String getValue() {
return value;
}

public static AutoCreateEnum translate(String val) {
return mapping.get(val);
}
}
Expand Up @@ -18,6 +18,9 @@
import com.alvazan.orm.api.base.anno.NoSqlQueries;
import com.alvazan.orm.api.base.anno.NoSqlQuery;
import com.alvazan.orm.api.spi.layer2.Converter;
import com.alvazan.orm.api.spi.layer2.DboColumnMeta;
import com.alvazan.orm.api.spi.layer2.DboDatabaseMeta;
import com.alvazan.orm.api.spi.layer2.DboTableMeta;
import com.alvazan.orm.api.spi.layer2.MetaQuery;
import com.alvazan.orm.api.spi.layer2.NoSqlSessionFactory;
import com.alvazan.orm.impl.meta.data.MetaClass;
Expand All @@ -43,7 +46,9 @@ public class BaseEntityManagerFactoryImpl implements NoSqlEntityManagerFactory {
private boolean isScanned;
@Inject
private MetaInfo metaInfo;

@Inject
private DboDatabaseMeta databaseInfo;

private Object injector;

@Override
Expand All @@ -54,11 +59,17 @@ public NoSqlEntityManager createEntityManager() {
}

@SuppressWarnings("rawtypes")
@Override
public void setup(Map<Class, Converter> converters) {
public void setup(Map<String, String> properties, Map<Class, Converter> converters) {
if(isScanned)
throw new IllegalStateException("scanForEntities can only be called once");

String val = properties.get(NoSqlEntityManagerFactory.AUTO_CREATE_KEY);
if(val == null)
throw new IllegalArgumentException("Must provide property with key NoSqlEntityManagerFactory.AUTO_CREATE_KEY so we know to update or validate existing schema");
AutoCreateEnum autoCreate = AutoCreateEnum.translate(val);
if(autoCreate == null)
throw new IllegalArgumentException("Property NoSqlEntityManagerFactory.AUTO_CREATE_KEY can only have values validate,update, or create");

inspectorField.setCustomConverters(converters);

log.info("Begin scanning for jars with nosql.Persistence.class");
Expand All @@ -84,8 +95,40 @@ public void setup(Map<Class, Converter> converters) {
setupQueryStuff(meta);
}

log.info("Finished scanning classes");
log.info("Finished scanning classes, saving meta data");
isScanned = true;

if(AutoCreateEnum.CREATE_ONLY != autoCreate)
throw new UnsupportedOperationException("not implemented yet");


NoSqlEntityManager tempMgr = createEntityManager();

saveMetaData(tempMgr);

tempMgr.flush();
log.info("Finished saving meta data, complelety done initializing");
}

private void saveMetaData(NoSqlEntityManager tempMgr) {
DboDatabaseMeta existing = tempMgr.find(DboDatabaseMeta.class, NoSqlEntityManager.META_DB_KEY);
if(existing != null)
throw new IllegalStateException("Your property NoSqlEntityManagerFactory.AUTO_CREATE_KEY which only creates meta data if none exist already but meta already exists");

for(DboTableMeta table : databaseInfo.getAllTables()) {

for(DboColumnMeta col : table.getAllColumns()) {
tempMgr.put(col);
}

tempMgr.put(table.getIdColumnMeta());

tempMgr.put(table);
}

databaseInfo.setId(NoSqlEntityManager.META_DB_KEY);

tempMgr.put(databaseInfo);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down
Expand Up @@ -117,6 +117,7 @@ public <T> T getReference(Class<T> entityType, Object key) {

@Override
public NoSqlSession getSession() {
session.setOrmSessionForMeta(this);
return session;
}

Expand Down
Expand Up @@ -84,5 +84,10 @@ public Iterable<Column> columnRangeScan(String colFamily, byte[] rowKey,
return session.columnRangeScan(colFamily, rowKey, from, to, batchSize);
}

@Override
public void setOrmSessionForMeta(Object ormSession) {
session.setOrmSessionForMeta(ormSession);
}


}
Expand Up @@ -29,6 +29,7 @@ public class NoSqlWriteCacheImpl implements NoSqlSession {
private Map<String, List<? extends IndexRemove>> removeFromIndex = new HashMap<String, List<? extends IndexRemove>>();
private List<Action> actions = new ArrayList<Action>();
private Map<String, List<IndexAdd>> addToIndex = new HashMap<String, List<IndexAdd>>();
private Object ormSession;

@Override
public void persist(String colFamily, byte[] rowKey, List<Column> columns) {
Expand Down Expand Up @@ -73,7 +74,7 @@ public void flush() {
//REMOVE from index BEFORE removing the entity. ie. If we do the reverse, you do a query and get
//an id of entity that is about to be removed and if removed before you, you don't get the entity
indexWriter.sendRemoves(removeFromIndex);
rawSession.sendChanges(actions);
rawSession.sendChanges(actions, ormSession);
indexWriter.sendAdds(addToIndex);
}

Expand Down Expand Up @@ -151,4 +152,9 @@ public Iterable<Column> columnRangeScan(String colFamily, byte[] rowKey,
return rawSession.columnRangeScan(colFamily, rowKey, from, to, batchSize);
}

@Override
public void setOrmSessionForMeta(Object session) {
this.ormSession = session;
}

}

0 comments on commit f051acf

Please sign in to comment.