Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Starting mongodb implementation. Bootstrap, Tables and Columns creation

  • Loading branch information...
commit d8f885cfdd56a7f739f0586de67b1a2cca495383 1 parent 644081a
@easility easility authored
View
1  build.gradle
@@ -32,6 +32,7 @@ dependencies {
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.1'
compile group: 'com.eaio.uuid', name: 'uuid', version: '3.2'
compile group: 'joda-time', name: 'joda-time', version: '2.0'
+ compile group: 'org.mongodb', name: 'mongo-java-driver', version: '2.10.1'
compile 'org.antlr:antlr-runtime:3.2'
antlr3 'org.antlr:antlr:3.2'
antcp "ant-contrib:ant-contrib:1.0b3"
View
6 gradle/configDesign.xml
@@ -21,6 +21,7 @@
<!-- external packages we limit the use of so we can isolate to just one or two packages that depend on this technology -->
<package name="antlr" package="org.antlr.runtime" subpackages="include"/>
<package name="astyanax" package="com.netflix.astyanax" subpackages="include"/>
+<package name="mongodb" package="com.mongodb" subpackages="include"/>
<package name="cassandra" package="org.apache.cassandra" subpackages="include"/>
<package name="args" package="org.kohsuke.args4j"/>
<package name="logback" package="ch.qos.logback" subpackages="include"/>
@@ -128,7 +129,10 @@
<package name="inMemDb" package="com.alvazan.orm.layer9z.spi.db.inmemory">
<depends>allapis</depends>
</package>
-
+<package name="mongo" package="com.alvazan.orm.layer9z.spi.db.mongodb">
+ <depends>api8</depends>
+ <depends>mongodb</depends>
+</package>
<package name="monitorapi" package="org.playorm.monitor.api"></package>
<package name="monitorimpl" package="org.playorm.monitor.impl" subpackages="include">
View
13 src/main/java/com/alvazan/orm/api/base/Bootstrap.java
@@ -17,6 +17,10 @@
public static final String CASSANDRA_KEYSPACE = "nosql.cassandra.keyspace";
public static final String CASSANDRA_SEEDS = "nosql.cassandra.seeds";
+ public static final String MONGODB_CLUSTERNAME = "nosql.mongodb.clusterName";
+ public static final String MONGODB_KEYSPACE = "nosql.mongodb.keyspace";
+ public static final String MONGODB_SEEDS = "nosql.mongodb.seeds";
+
private static final String OUR_IMPL = "com.alvazan.orm.impl.bindings.BootstrapImpl";
public static final String SPI_IMPL = "nosql.spi.implementation";
@@ -83,4 +87,13 @@ public static void createAndAddBestCassandraConfiguration(
protected abstract void createBestCassandraConfig(Map<String, Object> properties,
String clusterName, String keyspace2, String seeds2);
+
+ public static void createAndAddBestMongoDbConfiguration(Map<String, Object> properties, String clusterName, String keyspace, String seeds) {
+ Bootstrap bootstrap = createInstance(OUR_IMPL);
+ bootstrap.createBestMongoDbConfig(properties, clusterName, keyspace, seeds);
+ }
+
+ protected abstract void createBestMongoDbConfig(Map<String, Object> properties,
+ String clusterName, String keyspace2, String seeds2);
+
}
View
2  src/main/java/com/alvazan/orm/api/base/DbTypeEnum.java
@@ -4,7 +4,7 @@
import java.util.Map;
public enum DbTypeEnum {
- CASSANDRA("cassandra"), IN_MEMORY("inmemory");
+ CASSANDRA("cassandra"), IN_MEMORY("inmemory"), MONGODB("mongodb");
private static final Map<String, DbTypeEnum> LOOKUP = new HashMap<String, DbTypeEnum>();
View
7 src/main/java/com/alvazan/orm/impl/bindings/BootstrapImpl.java
@@ -97,5 +97,12 @@ protected void createBestCassandraConfig(Map<String, Object> properties,
properties.put(Bootstrap.CASSANDRA_BUILDER, builder);
}
+ @Override
+ protected void createBestMongoDbConfig(Map<String, Object> properties,
+ String clusterName, String keyspace2, String seeds2) {
+ properties.put(Bootstrap.MONGODB_CLUSTERNAME, clusterName);
+ properties.put(Bootstrap.MONGODB_KEYSPACE, keyspace2);
+ properties.put(Bootstrap.MONGODB_SEEDS, seeds2);
+ }
}
View
4 src/main/java/com/alvazan/orm/impl/bindings/ProductionBindings.java
@@ -10,6 +10,7 @@
import com.alvazan.orm.layer5.nosql.cache.NoSqlWriteCacheImpl;
import com.alvazan.orm.layer9z.spi.db.cassandra.CassandraSession;
import com.alvazan.orm.layer9z.spi.db.inmemory.InMemorySession;
+import com.alvazan.orm.layer9z.spi.db.mongodb.MongoDbSession;
import com.alvazan.orm.logging.NoSqlDevLogger;
import com.alvazan.orm.logging.NoSqlRawLogger;
import com.google.inject.Binder;
@@ -56,6 +57,9 @@ private void bindRawSession(String name, Binder binder) {
case IN_MEMORY:
binder.bind(NoSqlRawSession.class).annotatedWith(Names.named(name)).to(InMemorySession.class).asEagerSingleton();
break;
+ case MONGODB:
+ binder.bind(NoSqlRawSession.class).annotatedWith(Names.named(name)).to(MongoDbSession.class).asEagerSingleton();
+ break;
default:
throw new RuntimeException("bug, unsupported database type="+type);
}
View
281 src/main/java/com/alvazan/orm/layer9z/spi/db/mongodb/CursorKeysToRowsMDB.java
@@ -0,0 +1,281 @@
+package com.alvazan.orm.layer9z.spi.db.mongodb;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import com.alvazan.orm.api.z8spi.BatchListener;
+import com.alvazan.orm.api.z8spi.Cache;
+import com.alvazan.orm.api.z8spi.KeyValue;
+import com.alvazan.orm.api.z8spi.Row;
+import com.alvazan.orm.api.z8spi.RowHolder;
+import com.alvazan.orm.api.z8spi.conv.ByteArray;
+import com.alvazan.orm.api.z8spi.iter.AbstractCursor;
+import com.alvazan.orm.api.z8spi.iter.DirectCursor;
+import com.alvazan.orm.api.z8spi.iter.StringLocal;
+import com.alvazan.orm.api.z8spi.meta.DboTableMeta;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+
+public class CursorKeysToRowsMDB extends AbstractCursor<KeyValue<Row>> {
+
+ private DirectCursor<byte[]> rowKeys;
+ private BatchListener list;
+ private ListIterator<KeyValue<Row>> cachedRows;
+ private Cache cache;
+ private DboTableMeta colFamily;
+ private DB database;
+
+ public CursorKeysToRowsMDB(DboTableMeta colFamily, DirectCursor<byte[]> rowKeys, BatchListener list, DB database, Cache cache2) {
+ this.colFamily = colFamily;
+ this.cache = cache2;
+ this.database = database;
+ this.rowKeys = rowKeys;
+ this.list = list;
+ beforeFirst();
+ }
+
+ @Override
+ public void beforeFirst() {
+ rowKeys.beforeFirst();
+ cachedRows = null;
+ }
+
+ @Override
+ public void afterLast() {
+ rowKeys.afterLast();
+ cachedRows = null;
+ }
+
+ @Override
+ public com.alvazan.orm.api.z8spi.iter.AbstractCursor.Holder<KeyValue<Row>> nextImpl() {
+ loadCache();
+ if(cachedRows == null || !cachedRows.hasNext())
+ return null;
+
+ return new Holder<KeyValue<Row>>(cachedRows.next());
+ }
+
+ @Override
+ public com.alvazan.orm.api.z8spi.iter.AbstractCursor.Holder<KeyValue<Row>> previousImpl() {
+ loadCacheBackward();
+ if(cachedRows == null || !cachedRows.hasPrevious())
+ return null;
+
+ return new Holder<KeyValue<Row>>(cachedRows.previous());
+ }
+
+ private void loadCache() {
+ byte[] nextKey = null;
+ Holder<byte[]> keyHolder = rowKeys.nextImpl();
+ if (keyHolder != null)
+ nextKey = keyHolder.getValue();
+
+ if(cachedRows != null && cachedRows.hasNext())
+ return; //There are more rows so return and the code will return the next result from cache
+ else if(nextKey == null)
+ return;
+
+ List<RowHolder<Row>> results = new ArrayList<RowHolder<Row>>();
+ List<byte[]> keysToLookup = new ArrayList<byte[]>();
+ while(nextKey != null) {
+ RowHolder<Row> result = cache.fromCache(colFamily, nextKey);
+ if(result == null)
+ keysToLookup.add(nextKey);
+
+ results.add(result);
+ nextKey = null;
+ keyHolder = rowKeys.nextImpl();
+ if (keyHolder != null)
+ nextKey = keyHolder.getValue();
+ }
+
+ List<KeyValue<Row>> rows = new ArrayList<KeyValue<Row>>();
+ if(keysToLookup.size() > 0) {
+ if(list != null)
+ list.beforeFetchingNextBatch();
+ rows = fetchRows();
+ if(list != null)
+ list.afterFetchingNextBatch(rows.size());
+ }
+
+ Iterator<KeyValue<Row>> resultingRows = rows.iterator();
+
+ Map<ByteArray, KeyValue<Row>> map = new HashMap<ByteArray, KeyValue<Row>>();
+ while(resultingRows.hasNext()) {
+ KeyValue<Row> kv = resultingRows.next();
+ byte[] key = (byte[]) kv.getKey();
+ ByteArray b = new ByteArray(key);
+ map.put(b, kv);
+ cache.cacheRow(colFamily, key, kv.getValue());
+ }
+
+ //UNFORTUNATELY, astyanax's result is NOT ORDERED by the keys we provided so, we need to iterate over the whole thing here
+ //into our own List :( :( .....okay, well, we are now in memory and would have been ordered but oh well....rework this later
+
+ List<KeyValue<Row>> finalRes = new ArrayList<KeyValue<Row>>();
+ Iterator<byte[]> keyIter = keysToLookup.iterator();
+ for(RowHolder<Row> r : results) {
+ if(r == null) {
+ byte[] key = keyIter.next();
+ ByteArray b = new ByteArray(key);
+ KeyValue<Row> kv = map.get(b);
+ finalRes.add(kv);
+ } else {
+ Row row = r.getValue();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(r.getKey());
+ kv.setValue(row);
+ finalRes.add(kv);
+ }
+ }
+
+ cachedRows = finalRes.listIterator();
+ }
+
+
+ private void loadCacheBackward() {
+ byte[] previousKey = null;
+ Holder<byte[]> keyHolder = rowKeys.previousImpl();
+ if (keyHolder != null)
+ previousKey = keyHolder.getValue();
+
+ if(cachedRows != null && cachedRows.hasPrevious())
+ return; //There are more rows so return and the code will return the next result from cache
+ else if(previousKey == null)
+ return;
+
+ List<RowHolder<Row>> results = new ArrayList<RowHolder<Row>>();
+ ArrayList<byte[]> keysToLookup = new ArrayList<byte[]>();
+ while(previousKey != null) {
+ RowHolder<Row> result = cache.fromCache(colFamily, previousKey);
+ if(result == null)
+ keysToLookup.add(0, previousKey);
+
+ results.add(result);
+ previousKey = null;
+ keyHolder = rowKeys.previousImpl();
+ if (keyHolder != null)
+ previousKey = keyHolder.getValue();
+ }
+
+ List<KeyValue<Row>> rows = new ArrayList<KeyValue<Row>>();
+ if(keysToLookup.size() > 0) {
+ if(list != null)
+ list.beforeFetchingNextBatch();
+ rows = fetchRowsBackward();
+ if(list != null)
+ list.afterFetchingNextBatch(rows.size());
+ }
+
+ Iterator<KeyValue<Row>> resultingRows = rows.iterator();
+
+ Map<ByteArray, KeyValue<Row>> map = new HashMap<ByteArray, KeyValue<Row>>();
+ while(resultingRows.hasNext()) {
+ KeyValue<Row> kv = resultingRows.next();
+ byte[] key = (byte[]) kv.getKey();
+ ByteArray b = new ByteArray(key);
+ map.put(b, kv);
+ cache.cacheRow(colFamily, key, kv.getValue());
+ }
+
+ //UNFORTUNATELY, astyanax's result is NOT ORDERED by the keys we provided so, we need to iterate over the whole thing here
+ //into our own List :( :( .....okay, well, we are now in memory and would have been ordered but oh well....rework this later
+
+ List<KeyValue<Row>> finalRes = new ArrayList<KeyValue<Row>>();
+ Iterator<byte[]> keyIter = keysToLookup.iterator();
+ for(RowHolder<Row> r : results) {
+ if(r == null) {
+ byte[] key = keyIter.next();
+ ByteArray b = new ByteArray(key);
+ KeyValue<Row> kv = map.get(b);
+ finalRes.add(kv);
+ } else {
+ Row row = r.getValue();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(r.getKey());
+ kv.setValue(row);
+ finalRes.add(kv);
+ }
+ }
+
+ cachedRows = finalRes.listIterator();
+ while (cachedRows.hasNext()) cachedRows.next();
+ }
+
+
+
+ public List<KeyValue<Row>> fetchRows() {
+ List<KeyValue<Row>> rows = new ArrayList<KeyValue<Row>>();
+ DBCollection table = database.getCollection(colFamily.getColumnFamily());
+ rowKeys.beforeFirst();
+ Holder<byte[]> keyHolder = rowKeys.nextImpl();
+ if (keyHolder != null) {
+ byte[] key = keyHolder.getValue();
+ while(key != null) {
+ Row row = findRow(table, key);
+ Row newRow = null;
+ if(row != null)
+ newRow = row.deepCopy();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(key);
+ kv.setValue(newRow);
+ //This add null if there is no row to the list on purpose
+ rows.add(kv);
+ keyHolder = rowKeys.nextImpl();
+ key = null;
+ if (keyHolder != null)
+ key = keyHolder.getValue();
+ }
+ }
+ return rows;
+ }
+
+ public List<KeyValue<Row>> fetchRowsBackward() {
+ List<KeyValue<Row>> rows = new ArrayList<KeyValue<Row>>();
+ DBCollection table = database.getCollection(colFamily.getColumnFamily());
+ rowKeys.afterLast();
+ Holder<byte[]> keyHolder = rowKeys.previousImpl();
+ if (keyHolder != null) {
+ byte[] key = keyHolder.getValue();
+ while(key != null) {
+ Row row = findRow(table, key);
+ Row newRow = null;
+ if(row != null)
+ newRow = row.deepCopy();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(key);
+ kv.setValue(newRow);
+ //This add null if there is no row to the list on purpose
+ rows.add(0, kv);
+ keyHolder = rowKeys.previousImpl();
+ key = null;
+ if (keyHolder != null)
+ key = keyHolder.getValue();
+ }
+ }
+ return rows;
+ }
+
+ private Row findRow(DBCollection table, byte[] key) {
+ if(table == null)
+ return null;
+ //return table.getRow(key);
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ String tabs = StringLocal.getAndAdd();
+ String keys = ""+rowKeys;
+ if(rowKeys instanceof List)
+ keys = "List"+keys;
+ String retVal = "CursorKeysToRows(inmemoryRowLookup)["+tabs+keys+tabs+"]";
+ StringLocal.set(tabs.length());
+ return retVal;
+ }
+
+}
View
53 src/main/java/com/alvazan/orm/layer9z/spi/db/mongodb/CursorReturnsEmptyRows2.java
@@ -0,0 +1,53 @@
+package com.alvazan.orm.layer9z.spi.db.mongodb;
+
+import com.alvazan.orm.api.z8spi.KeyValue;
+import com.alvazan.orm.api.z8spi.Row;
+import com.alvazan.orm.api.z8spi.iter.AbstractCursor;
+import com.alvazan.orm.api.z8spi.iter.DirectCursor;
+
+public class CursorReturnsEmptyRows2 extends AbstractCursor<KeyValue<Row>> {
+
+ private DirectCursor<byte[]> keys;
+ public CursorReturnsEmptyRows2(DirectCursor<byte[]> keys) {
+ if(keys == null)
+ throw new IllegalArgumentException("keys cannot be null");
+ this.keys = keys;
+ beforeFirst();
+ }
+
+ @Override
+ public void beforeFirst() {
+ keys.beforeFirst();
+ }
+
+ @Override
+ public void afterLast() {
+ keys.afterLast();
+ }
+
+ @Override
+ public com.alvazan.orm.api.z8spi.iter.AbstractCursor.Holder<KeyValue<Row>> nextImpl() {
+ Holder<byte[]> holder = keys.nextImpl();
+ if (holder == null)
+ return null;
+
+ byte[] key = holder.getValue();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(key);
+
+ return new Holder<KeyValue<Row>>(kv);
+ }
+
+ @Override
+ public com.alvazan.orm.api.z8spi.iter.AbstractCursor.Holder<KeyValue<Row>> previousImpl() {
+ Holder<byte[]> holder = keys.previousImpl();
+ if (holder == null)
+ return null;
+
+ byte[] key = holder.getValue();
+ KeyValue<Row> kv = new KeyValue<Row>();
+ kv.setKey(key);
+
+ return new Holder<KeyValue<Row>>(kv);
+ }
+}
View
38 src/main/java/com/alvazan/orm/layer9z/spi/db/mongodb/Info.java
@@ -0,0 +1,38 @@
+package com.alvazan.orm.layer9z.spi.db.mongodb;
+
+import com.alvazan.orm.api.z8spi.ColumnType;
+import com.alvazan.orm.api.z8spi.conv.StorageTypeEnum;
+import com.mongodb.DBCollection;
+
+public class Info {
+
+ private StorageTypeEnum rowKeyType;
+ private ColumnType columnType;
+ private DBCollection columnFamilyObj;
+
+ public ColumnType getColumnType() {
+ return columnType;
+ }
+
+ public void setColumnType(ColumnType columnType) {
+ this.columnType = columnType;
+ }
+
+ public void setColumnFamilyObj(DBCollection cf) {
+ this.columnFamilyObj = cf;
+ }
+
+
+ public DBCollection getColumnFamilyObj() {
+ return columnFamilyObj;
+ }
+
+ public StorageTypeEnum getRowKeyType() {
+ return rowKeyType;
+ }
+
+ public void setRowKeyType(StorageTypeEnum rowKeyType) {
+ this.rowKeyType = rowKeyType;
+ }
+
+}
View
295 src/main/java/com/alvazan/orm/layer9z/spi/db/mongodb/MongoDbSession.java
@@ -0,0 +1,295 @@
+package com.alvazan.orm.layer9z.spi.db.mongodb;
+
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Inject;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.alvazan.orm.api.base.Bootstrap;
+import com.alvazan.orm.api.z8spi.BatchListener;
+import com.alvazan.orm.api.z8spi.Cache;
+import com.alvazan.orm.api.z8spi.Key;
+import com.alvazan.orm.api.z8spi.KeyValue;
+import com.alvazan.orm.api.z8spi.MetaLookup;
+import com.alvazan.orm.api.z8spi.NoSqlRawSession;
+import com.alvazan.orm.api.z8spi.Row;
+import com.alvazan.orm.api.z8spi.ScanInfo;
+import com.alvazan.orm.api.z8spi.action.Action;
+import com.alvazan.orm.api.z8spi.action.Column;
+import com.alvazan.orm.api.z8spi.action.IndexColumn;
+import com.alvazan.orm.api.z8spi.action.Persist;
+import com.alvazan.orm.api.z8spi.action.PersistIndex;
+import com.alvazan.orm.api.z8spi.action.Remove;
+import com.alvazan.orm.api.z8spi.action.RemoveColumn;
+import com.alvazan.orm.api.z8spi.action.RemoveIndex;
+import com.alvazan.orm.api.z8spi.conv.StandardConverters;
+import com.alvazan.orm.api.z8spi.iter.AbstractCursor;
+import com.alvazan.orm.api.z8spi.iter.DirectCursor;
+import com.alvazan.orm.api.z8spi.meta.DboDatabaseMeta;
+import com.alvazan.orm.api.z8spi.meta.DboTableMeta;
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.MongoClient;
+
+public class MongoDbSession implements NoSqlRawSession {
+
+ private static final Logger log = LoggerFactory.getLogger(MongoDbSession.class);
+
+ private DB db;
+ private MongoClient mongoClient;
+
+ @Inject
+ private DboDatabaseMeta dbMetaFromOrmOnly;
+
+ private Map<String, String> virtualToCfName = new HashMap<String, String>();
+
+ public DB getDb() {
+ return db;
+ }
+
+ public void setDb(DB db) {
+ this.db = db;
+ }
+
+ public MongoClient getMongoClient() {
+ return mongoClient;
+ }
+
+ public void setMongoClient(MongoClient mongoClient) {
+ this.mongoClient = mongoClient;
+ }
+
+ @Override
+ public void sendChanges(List<Action> actions, MetaLookup ormSession) {
+ sendChangesImpl(actions, ormSession);
+ }
+
+ public void sendChangesImpl(List<Action> actions, MetaLookup ormSession) {
+ for(Action action : actions) {
+ if(action instanceof Persist) {
+ persist((Persist)action, ormSession);
+ } else if(action instanceof Remove) {
+ remove((Remove)action, ormSession);
+ } else if(action instanceof PersistIndex) {
+ persistIndex((PersistIndex) action, ormSession);
+ } else if(action instanceof RemoveIndex) {
+ removeIndex((RemoveIndex) action, ormSession);
+ } else if(action instanceof RemoveColumn) {
+ removeColumn((RemoveColumn) action, ormSession);
+ }
+ }
+ }
+
+
+ @Override
+ public void clearDatabase() {
+ throw new UnsupportedOperationException("Not supported by actual databases. Only can be used with in-memory db.");
+ }
+
+ @Override
+ public void start(Map<String, Object> properties) {
+ properties.keySet();
+ try {
+ mongoClient = new MongoClient();
+ } catch (UnknownHostException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ String keySpace = properties.get(Bootstrap.MONGODB_KEYSPACE).toString();
+ db = mongoClient.getDB(keySpace);
+ }
+
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public void readMetaAndCreateTable(MetaLookup ormSession, String colFamily) {
+
+ }
+
+ @Override
+ public AbstractCursor<Column> columnSlice(DboTableMeta colFamily, byte[] rowKey,
+ byte[] from, byte[] to, Integer batchSize, BatchListener l,
+ MetaLookup mgr) {
+ return null;
+ }
+
+ @Override
+ public AbstractCursor<IndexColumn> scanIndex(ScanInfo scan, Key from,
+ Key to, Integer batchSize, BatchListener l, MetaLookup mgr) {
+ return null;
+ }
+
+ @Override
+ public AbstractCursor<IndexColumn> scanIndex(ScanInfo scanInfo,
+ List<byte[]> values, BatchListener list, MetaLookup mgr) {
+ return null;
+ }
+
+ @Override
+ public AbstractCursor<KeyValue<Row>> find(DboTableMeta colFamily,
+ DirectCursor<byte[]> rowKeys, Cache cache, int batchSize,
+ BatchListener list, MetaLookup mgr) {
+ return new CursorReturnsEmptyRows2(rowKeys);
+ //return null;
+ }
+
+ private void persistIndex(PersistIndex action, MetaLookup ormSession) {
+ String colFamily = action.getIndexCfName();
+ DBCollection table = lookupColFamily(colFamily, ormSession);
+ byte[] rowKey = action.getRowKey();
+ IndexColumn column = action.getColumn();
+ //IndexedRow row = (IndexedRow) DBCollection.findOrCreateRow(rowKey);
+ //row.addIndexedColumn(column.copy());
+ }
+
+
+
+ private void removeIndex(RemoveIndex action, MetaLookup ormSession) {
+ String colFamily = action.getIndexCfName();
+ if (colFamily.equalsIgnoreCase("BytesIndice"))
+ return;
+ DBCollection table = lookupColFamily(colFamily, ormSession);
+
+ byte[] rowKey = action.getRowKey();
+ IndexColumn column = action.getColumn();
+ //IndexedRow row = (IndexedRow) table.findOrCreateRow(rowKey);
+ //row.removeIndexedColumn(column.copy());
+ }
+
+ private DBCollection lookupColFamily(String colFamily, MetaLookup mgr) {
+ DBCollection table;
+ if(db.collectionExists(colFamily)) {
+ table = db.getCollection(colFamily);
+ return table;
+ }
+
+ log.info("CREATING column family="+colFamily+" in the MongDB nosql store");
+
+ DboTableMeta cf = dbMetaFromOrmOnly.getMeta(colFamily);
+ if(cf == null) {
+ //check the database now for the meta since it was not found in the ORM meta data. This is for
+ //those that are modifying meta data themselves
+ //DboDatabaseMeta db = mgr.find(DboDatabaseMeta.class, DboDatabaseMeta.META_DB_ROWKEY);
+ cf = mgr.find(DboTableMeta.class, colFamily);
+ log.info("cf from db="+cf);
+ }
+
+ if(cf == null) {
+ throw new IllegalStateException("Column family='"+colFamily+"' was not found AND we looked up meta data for this column" +
+ " family to create it AND we could not find that data so we can't create it for you");
+ }
+
+ /*SortType sortType;
+ StorageTypeEnum prefixType = cf.getColNamePrefixType();
+ if(prefixType == null) {
+ switch (cf.getNameStorageType()) {
+ case BYTES:
+ sortType = SortType.BYTES;
+ break;
+ case DECIMAL:
+ sortType = SortType.DECIMAL;
+ break;
+ case INTEGER:
+ sortType = SortType.INTEGER;
+ break;
+ case STRING:
+ sortType = SortType.UTF8;
+ break;
+ default:
+ throw new UnsupportedOperationException("type not supported="+cf.getNameStorageType());
+ }
+ } else {
+ switch(prefixType) {
+ case DECIMAL:
+ sortType = SortType.DECIMAL_PREFIX;
+ break;
+ case INTEGER:
+ sortType = SortType.INTEGER_PREFIX;
+ break;
+ case STRING:
+ sortType = SortType.UTF8_PREFIX;
+ break;
+ default:
+ throw new UnsupportedOperationException("type not supported="+prefixType);
+ }
+ }*/
+ table = db.createCollection(colFamily, new BasicDBObject());
+
+ return table;
+ }
+
+ private void remove(Remove action, MetaLookup ormSession) {
+ String colFamily = action.getColFamily().getColumnFamily();
+ DBCollection table = lookupColFamily(colFamily, ormSession);
+ if(action.getAction() == null)
+ throw new IllegalArgumentException("action param is missing ActionEnum so we know to remove entire row or just columns in the row");
+ switch(action.getAction()) {
+ case REMOVE_ENTIRE_ROW:
+ //table.remove(action.getRowKey());
+ break;
+ case REMOVE_COLUMNS_FROM_ROW:
+ removeColumns(action, table);
+ break;
+ default:
+ throw new RuntimeException("bug, unknown remove action="+action.getAction());
+ }
+ }
+
+ private void removeColumns(Remove action, DBCollection table) {
+/* Row row = table.g.getRow(action.getRowKey());
+ if(row == null)
+ return;
+
+ for(byte[] name : action.getColumns()) {
+ row.remove(name);
+ }*/
+ }
+
+ private void removeColumn(RemoveColumn action, MetaLookup ormSession) {
+
+ String colFamily = action.getColFamily().getColumnFamily();
+ DBCollection table = lookupColFamily(colFamily, ormSession);
+/* Row row = table.getRow(action.getRowKey());
+ if(row == null)
+ return;
+ byte[] name = action.getColumn();
+ row.remove(name);*/
+ }
+
+ private void persist(Persist action, MetaLookup ormSession) {
+ String colFamily = action.getColFamily().getColumnFamily();
+ DBCollection table = lookupColFamily(colFamily, ormSession);
+ BasicDBObject doc = findOrCreateRow(table, action.getRowKey());
+ for(Column col : action.getColumns()) {
+ Integer theTime = null;
+ Long time = col.getTimestamp();
+ if(time != null)
+ theTime = (int)time.longValue();
+ byte[] value = new byte[0];
+ if(col.getValue() != null)
+ value = col.getValue();
+ doc.append(StandardConverters.convertToString(col.getName()), value);
+ }
+ }
+
+ public BasicDBObject findOrCreateRow(DBCollection table, byte[] key) {
+ //ByteArray array = new ByteArray(key);
+ DBObject row = table.findOne(key);
+ if(row == null) {
+ BasicDBObject basicRow = new BasicDBObject();
+ basicRow.append("_id", key);
+ table.insert(basicRow);
+ return basicRow;
+ }
+ else return (BasicDBObject)row;
+ }
+}
View
9 src/main/java/com/alvazan/ssql/cmdline/PlayOrm.java
@@ -84,9 +84,14 @@ public static void main(String[] args) {
}
Map<String, Object> properties = new HashMap<String, Object>();
- Bootstrap.createAndAddBestCassandraConfiguration(properties , "", bean.getKeyspace(), bean.getSeeds());
+ if(storeType == DbTypeEnum.CASSANDRA) {
+ Bootstrap.createAndAddBestCassandraConfiguration(properties , "", bean.getKeyspace(), bean.getSeeds());
+ }
+ else if (storeType == DbTypeEnum.MONGODB) {
+ Bootstrap.createAndAddBestMongoDbConfiguration(properties ,"", bean.getKeyspace(), bean.getSeeds());
+ }
properties.put(Bootstrap.AUTO_CREATE_KEY, "create");
-
+
NoSqlEntityManagerFactory factory = Bootstrap.create(storeType, properties);
new PlayOrm(factory).start();
}
Please sign in to comment.
Something went wrong with that request. Please try again.