Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Siena-MongoDB implementation #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@
<artifactId>httpcore</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.3</version>
</dependency>
</dependencies>

<profiles>
Expand Down
21 changes: 21 additions & 0 deletions source/src/main/java/siena/mongodb/MongoDBIndexGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package siena.mongodb;

import java.util.List;

/**
* A generator to process Siena annotated indexes in Mongo.
*
* @author pjarrell
*/
public interface MongoDBIndexGenerator {

/**
* Generate indexes from the supplied model classes.
*
* @param clazzes
* is a list of model classes to process
*/
@SuppressWarnings("rawtypes")
void generate(final List<Class> clazzes);

}
91 changes: 91 additions & 0 deletions source/src/main/java/siena/mongodb/MongoDBIndexGeneratorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package siena.mongodb;

import java.lang.reflect.Field;
import java.util.List;

import siena.ClassInfo;
import siena.Index;
import siena.Model;
import siena.PersistenceManager;
import siena.Unique;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
* A simple generator to help process Siena annotated indexes with Mongo. This generator can be called on each start up since the indexes will only be created if necessary.
*
* @author pjarrell
*/
public class MongoDBIndexGeneratorImpl implements MongoDBIndexGenerator {

private final MongoDBPersistenceManager persistenceManager;

/**
* Create the generator using the Mongo {@link PersistenceManager}.
*
* @param persistenceManager
* is the persistence manager to use to generate indexes
*/
public MongoDBIndexGeneratorImpl(final MongoDBPersistenceManager persistenceManager) {
this.persistenceManager = persistenceManager;
}

/**
* Generate Mongo indexes using the annotations {@link Index} and {@link Unique} from the model classes provided.
*
* @param clazzes
* is a list of Siena {@link Model} classes
*/
@SuppressWarnings("rawtypes")
public void generate(final List<Class> clazzes) {
if (clazzes != null) {
for (final Class<?> clazz : clazzes) {
if (ClassInfo.isModel(clazz)) {
final ClassInfo info = ClassInfo.getClassInfo(clazz);
final DBCollection collection = persistenceManager.getDatabase().getCollection(info.tableName);
for (final Field field : info.updateFields) {

final Index index = field.getAnnotation(Index.class);
if (index != null) {
final String[] names = index.value();
final DBObject newIndex = new BasicDBObject();
for (final String name : names) {
if (newIndex.containsField(name) == false) {
newIndex.put(name, 1);
}
}
collection.ensureIndex(newIndex);
}

final Unique unique = field.getAnnotation(Unique.class);
if (unique != null) {
final String[] names = unique.value();
final DBObject newIndex = new BasicDBObject();
for (final String name : names) {
if (newIndex.containsField(name) == false) {
newIndex.put(name, 1);
}
}
final DBObject uniqueIndex = new BasicDBObject();
uniqueIndex.put("unique", true);
collection.ensureIndex(newIndex, uniqueIndex);
}

}
}
}
}
}

/**
* Gets the persistence manager being used by this generator.
*
* @return the persistenceManager
*/
protected MongoDBPersistenceManager getPersistenceManager() {
return persistenceManager;
}

}
Loading