This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial entity schema management service
- Loading branch information
zznate
committed
Jan 24, 2011
1 parent
c311129
commit 33d7497
Showing
7 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
object-mapper/src/test/java/me/prettyprint/hom/SimpleRelationshipBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
object-mapper/src/test/java/me/prettyprint/hom/service/EntitySchemaManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters