Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
initial entity schema management service
Browse files Browse the repository at this point in the history
  • Loading branch information
zznate committed Jan 24, 2011
1 parent c311129 commit 33d7497
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 3 deletions.
@@ -1,6 +1,7 @@
package me.prettyprint.hom.openjpa;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -73,7 +74,15 @@ class ColumnMeta<V> {
ColumnMeta(int fieldId, Serializer<V> serializer) {
this.fieldId = fieldId;
this.serializer = serializer;
}

}
}

@Override
public String toString() {
return String.format("EntityFacade[class: %s, columnFamily: %s, columnNames: %s]",
clazz.getName(),
columnFamilyName, Arrays.toString(getColumnNames()));
}


}
@@ -0,0 +1,45 @@
package me.prettyprint.hom.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.service.ThriftCfDef;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hom.openjpa.EntityFacade;
import me.prettyprint.hom.service.EntitySchemaStatus.SchemaResult;

public class EntitySchemaManager {

private static Logger log = LoggerFactory.getLogger(EntitySchemaManager.class);

private Cluster cluster;
private Keyspace keyspace;

public EntitySchemaManager(Cluster cluster, Keyspace keyspace) {
this.cluster = cluster;
this.keyspace = keyspace;
}

public EntitySchemaStatus createSchema(EntityFacade entityFacade) {
BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
columnFamilyDefinition.setKeyspaceName(keyspace.getKeyspaceName());
columnFamilyDefinition.setName(entityFacade.getColumnFamilyName());

ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);
EntitySchemaStatus entitySchemaStatus;
try {
cluster.addColumnFamily(cfDef);
entitySchemaStatus = new EntitySchemaStatus(SchemaResult.CREATED);
} catch (HInvalidRequestException ire) {
entitySchemaStatus = new EntitySchemaStatus(SchemaResult.NOT_MODIFIED);
}
return entitySchemaStatus;

}


}
@@ -0,0 +1,21 @@
package me.prettyprint.hom.service;

public class EntitySchemaStatus {

private final SchemaResult schemaResult;

public EntitySchemaStatus(SchemaResult result) {
this.schemaResult = result;
}


public SchemaResult getSchemaResult() {
return schemaResult;
}

public enum SchemaResult {
CREATED,
UPDATED,
NOT_MODIFIED
}
}
Expand Up @@ -24,6 +24,7 @@
public class CassandraTestBase {
protected static boolean cassandraStarted = false;
protected static Keyspace keyspace;
protected static Cluster cluster;

public static void startCassandraInstance(String pathToDataDir) throws TTransportException, IOException,
InterruptedException, SecurityException, IllegalArgumentException, NoSuchMethodException,
Expand Down Expand Up @@ -101,7 +102,7 @@ public static void setupKeyspace() throws TTransportException,
.setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400));
cfDefList.add(new CfDef("TestKeyspace", "SimpleTestBeanColumnFamily").setComparator_type(BytesType.class.getSimpleName())
.setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400));
Cluster cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9161");
cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9161");
createKeyspace(cluster, "TestKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, cfDefList);
keyspace = HFactory.createKeyspace("TestKeyspace", cluster);
}
Expand Down
@@ -0,0 +1,35 @@
package me.prettyprint.hom;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class SimpleRelationshipBean {
@Id
private UUID baseId;

@Column(name = "myType")
private String myType;

public UUID getBaseId() {
return baseId;
}

public void setBaseId(UUID baseId) {
this.baseId = baseId;
}

public String getMyType() {
return myType;
}

public void setMyType(String myType) {
this.myType = myType;
}



}
@@ -0,0 +1,42 @@
package me.prettyprint.hom.service;

import static org.junit.Assert.assertEquals;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import me.prettyprint.hom.CassandraTestBase;
import me.prettyprint.hom.SimpleRelationshipBean;
import me.prettyprint.hom.openjpa.EntityFacade;
import me.prettyprint.hom.service.EntitySchemaStatus.SchemaResult;

import org.apache.openjpa.persistence.JPAFacadeHelper;
import org.junit.BeforeClass;
import org.junit.Test;

public class EntitySchemaManagerTest extends CassandraTestBase {

private static EntitySchemaManager entitySchemaManager;
private static EntityManagerFactory entityManagerFactory;
private static EntityFacade entityFacade;

@BeforeClass
public static void setup() {
entitySchemaManager = new EntitySchemaManager(cluster, keyspace);
entityManagerFactory = Persistence.createEntityManagerFactory("openjpa");
}

@Test
public void testCreateSchemaFromEntity() {
entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleRelationshipBean.class));
EntitySchemaStatus ecs = entitySchemaManager.createSchema(entityFacade);
assertEquals(SchemaResult.CREATED, ecs.getSchemaResult());
}

@Test
public void testCreateSchemaFailExisting() {
entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleRelationshipBean.class));
EntitySchemaStatus ecs = entitySchemaManager.createSchema(entityFacade);
assertEquals(SchemaResult.NOT_MODIFIED, ecs.getSchemaResult());
}
}
1 change: 1 addition & 0 deletions object-mapper/src/test/resources/META-INF/persistence.xml
Expand Up @@ -16,6 +16,7 @@
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

<class>me.prettyprint.hom.beans.SimpleTestBean</class>
<class>me.prettyprint.hom.beans.SimpleRelationshipBean</class>
<!-- <class>me.prettyprint.hom.beans.MyCustomIdBean</class>
<class>me.prettyprint.hom.beans.MyBlueTestBean</class>
<class>me.prettyprint.hom.beans.MyRedTestBean</class>
Expand Down

0 comments on commit 33d7497

Please sign in to comment.