Permalink
Browse files

Initial attempt at making this integration work but alas this current…

… implementation is not functional.
  • Loading branch information...
1 parent ac040de commit f642b991bfec8ea9e007b474757a18e7664773ec @ccorsi committed Sep 14, 2010
View
1 persistence/books/berkeleydb/src/META-INF/services/books.persistence.manager.databasemanager
@@ -0,0 +1 @@
+books.persistence.manager.berkeleydb.BerkeleyDBDatabaseManager # berkeleydb databaase manager for the books project.
View
105 .../books/berkeleydb/src/books/persistence/manager/berkeleydb/BerkeleyDBDatabaseManager.java
@@ -25,7 +25,10 @@
import java.io.File;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Properties;
import java.util.Set;
@@ -46,6 +49,7 @@
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.persist.EntityStore;
+import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
/**
@@ -59,9 +63,9 @@
private Transaction txn;
private BookAccessor accessor;
private EntityStore store;
- private Set<BookBerkeleyDB> books = new HashSet<BookBerkeleyDB>();
- private Set<PersonBerkeleyDB> persons = new HashSet<PersonBerkeleyDB>();
- private Set<GenderBerkeleyDB> genders = new HashSet<GenderBerkeleyDB>();
+ private Collection<BookBerkeleyDB> books = Collections.emptyList();
+ private Collection<PersonBerkeleyDB> persons = Collections.emptyList();
+ private Collection<GenderBerkeleyDB> genders = Collections.emptyList();
/*
* (non-Javadoc)
@@ -151,21 +155,27 @@ public Person createPerson(PersonType type) {
*
* @see books.persistence.manager.DatabaseManager#getBooks()
*/
+ @SuppressWarnings("unchecked")
@Override
public Collection<Book> getBooks() {
// TODO Auto-generated method stub
- return null;
+ Collection<? extends Book> books = this.accessor.booksIdx.map()
+ .values();
+ return (Collection<Book>) books;
}
/*
* (non-Javadoc)
*
* @see books.persistence.manager.DatabaseManager#getGenders()
*/
+ @SuppressWarnings("unchecked")
@Override
public Collection<Gender> getGenders() {
- // TODO Auto-generated method stub
- return null;
+ Collection<? extends Gender> genders = accessor.genderIdx.map().values();
+/* ObjectsContainerManager objects = getObjectsContainerManager();
+ Collection<Gender> genders = objects.getGenders();
+*/ return (Collection<Gender>) genders;
}
/*
@@ -176,8 +186,7 @@ public Person createPerson(PersonType type) {
*/
@Override
public String getIdentifier(Object object) {
- // TODO Auto-generated method stub
- return null;
+ return object.toString();
}
/*
@@ -187,10 +196,14 @@ public String getIdentifier(Object object) {
* books.persistence.manager.DatabaseManager#getPersons(books.persistence
* .types.PersonType)
*/
+ @SuppressWarnings("unchecked")
@Override
public Collection<Person> getPersons(PersonType type) {
- // TODO Auto-generated method stub
- return null;
+/* ObjectsContainerManager objects = getObjectsContainerManager();
+ Collection<Person> personsType = objects.getPersons(type);
+*/
+ Collection<? extends Person> personsType = accessor.personsMap.get(type).map().values();
+ return (Collection<Person>) personsType;
}
/*
@@ -205,8 +218,17 @@ public void init(Properties properties) {
"/berkeleydb/databases"), properties.getProperty("dbName",
"books"));
+ if (!dbFile.exists()) {
+ dbFile.mkdirs();
+ } else if (!dbFile.isDirectory()) {
+ // What this is completely wrong....hmmm which exception should be
+ // thrown in this case...
+ // TODO: Determine which exception to be thrown...
+ }
+
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
+ envConfig.setAllowCreate(true);
env = new Environment(dbFile, envConfig);
StoreConfig storeConfig = new StoreConfig();
@@ -233,24 +255,67 @@ public void init(Properties properties) {
@Override
public void save(Book book) {
- // Add the book instance to the book index...
- accessor.booksIdx.put(BookBerkeleyDB.class.cast(book));
-
+ // Add the genders to the collection of genders within the database....
+ for (Gender gender : book.getGenders()) {
+ accessor.genderIdx.putNoReturn(GenderBerkeleyDB.class.cast(gender));
+ }
+
// Add the person instances to the database...
- for (Person person : book.getPersons(PersonType.Author)) {
- ObjectsContainerManager objects = accessor.allIdx.get(1L);
- if (objects == null) {
- objects = new ObjectsContainerManager(1L);
+ for (PersonType type : PersonType.values()) {
+ PrimaryIndex<Long, PersonBerkeleyDB> idx = accessor.personsMap.get(type);
+ for (Person person : book.getPersons(type)) {
+ idx.putNoReturn(PersonBerkeleyDB.class.cast(person));
+ }
+ }
+
+ // Add the book instance to the database....
+ this.accessor.booksIdx.putNoReturn(BookBerkeleyDB.class.cast(book));
+
+/* // Add the person instances to the database...
+ for (PersonType type : PersonType.values()) {
+ for (Person person : book.getPersons(type)) {
+ ObjectsContainerManager objects = getObjectsContainerManager();
+
+ PersonBerkeleyDB persondb = PersonBerkeleyDB.class.cast(person);
+
+ if (!objects.getPersons(persondb.getType()).contains(persondb)) {
+ // Person does not exists within the database....
+ objects.addPerson(persondb);
+ }
+
+ System.out.println(objects);
}
+ }
+
+ for (Gender gender : book.getGenders()) {
+ ObjectsContainerManager objects = getObjectsContainerManager();
+
+ objects.addGender(GenderBerkeleyDB.class.cast(gender));
+ }
- objects.addPerson(PersonBerkeleyDB.class.cast(person));
+ // Add the book instance to the book index...
+ accessor.booksIdx.put(BookBerkeleyDB.class.cast(book));
+*/ }
+
+ private static final Long zero = Long.valueOf(0);
+
+ private ObjectsContainerManager getObjectsContainerManager() {
+ ObjectsContainerManager mgr = accessor.allIdx.get(zero);
+ System.out.println(mgr);
+ if (mgr == null) {
+ System.out.println("Creating an ObjectsContainerManager instance");
+ mgr = new ObjectsContainerManager();
+ accessor.allIdx.put(mgr);
+ mgr = accessor.allIdx.get(Long.valueOf(0));
+ System.out.println(mgr);
}
+ return mgr;
}
@Override
public void update(Book book) {
- // TODO Auto-generated method stub
- throw new RuntimeException("Method has not been implemented");
+ // Forward the updating of objects to the save method since it will perform the same task...
+ this.save(book);
}
}
View
21 persistence/books/berkeleydb/src/books/persistence/manager/berkeleydb/BookAccessor.java
@@ -23,6 +23,9 @@
*/
package books.persistence.manager.berkeleydb;
+import java.util.HashMap;
+import java.util.Map;
+
import books.persistence.types.PersonType;
import books.persistence.types.berkeleydb.BookBerkeleyDB;
import books.persistence.types.berkeleydb.GenderBerkeleyDB;
@@ -40,19 +43,25 @@
class BookAccessor {
private EntityStore store;
+
PrimaryIndex<Long, BookBerkeleyDB> booksIdx;
- PrimaryIndex<String, GenderBerkeleyDB> genderIdx;
- PrimaryIndex<Long, PersonBerkeleyDB> personIdx;
- SecondaryIndex<PersonType, Long, PersonBerkeleyDB> personSecIdx;
+ PrimaryIndex<Long, GenderBerkeleyDB> genderIdx;
+// PrimaryIndex<Long, PersonBerkeleyDB> personIdx;
+// SecondaryIndex<PersonType, Long, PersonBerkeleyDB> personSecIdx;
PrimaryIndex<Long, ObjectsContainerManager> allIdx;
+ Map<PersonType, PrimaryIndex<Long, PersonBerkeleyDB>> personsMap = new HashMap<PersonType, PrimaryIndex<Long, PersonBerkeleyDB>>();
BookAccessor(EntityStore store) {
this.store = store;
booksIdx = this.store.getPrimaryIndex(Long.class, BookBerkeleyDB.class);
- genderIdx = this.store.getPrimaryIndex(String.class, GenderBerkeleyDB.class);
- personIdx = this.store.getPrimaryIndex(Long.class, PersonBerkeleyDB.class);
- personSecIdx = this.store.getSecondaryIndex(personIdx, PersonType.class, "type");
+ for (PersonType type : PersonType.values()) {
+ personsMap.put(type, this.store.getPrimaryIndex(Long.class, PersonBerkeleyDB.class));
+ }
+// TODO: Determine if we can create indexes for persistent types...
+ genderIdx = this.store.getPrimaryIndex(Long.class, GenderBerkeleyDB.class);
+// personIdx = this.store.getPrimaryIndex(Long.class, PersonBerkeleyDB.class);
+// personSecIdx = this.store.getSecondaryIndex(personIdx, PersonType.class, "type");
allIdx = this.store.getPrimaryIndex(Long.class, ObjectsContainerManager.class);
}
View
22 persistence/books/berkeleydb/src/books/persistence/types/berkeleydb/BookBerkeleyDB.java
@@ -31,6 +31,9 @@
import java.util.Set;
import com.sleepycat.persist.model.Entity;
+import com.sleepycat.persist.model.PrimaryKey;
+import com.sleepycat.persist.model.Relationship;
+import com.sleepycat.persist.model.SecondaryKey;
import books.persistence.types.Book;
import books.persistence.types.Gender;
@@ -45,17 +48,36 @@
public class BookBerkeleyDB implements Book {
+ @Override
+ public String toString() {
+ return "[BookBerkeleyDB: " + id + "]";
+ }
+
// These collection types should reference the same object used by one or more books.
+ @SecondaryKey(relate=Relationship.MANY_TO_MANY, relatedEntity=GenderBerkeleyDB.class)
private List<Gender> genders = new LinkedList<Gender>();
+ @SecondaryKey(relate=Relationship.MANY_TO_MANY, relatedEntity=PersonBerkeleyDB.class)
private List<Person> persons = new LinkedList<Person>();
private Set<String> subtitles = new HashSet<String>();
+ @SecondaryKey(relate=Relationship.ONE_TO_MANY, relatedEntity=PersonBerkeleyDB.class)
+ private List<Long> personsKey = new LinkedList<Long>();
+
+ @PrimaryKey(sequence="bookId")
+ private long id;
private int copyright;
private Date finishDate;
private int pages;
private String series;
private String title;
+ /**
+ * @return
+ */
+ public long getId() {
+ return id;
+ }
+
/* (non-Javadoc)
* @see books.persistence.types.Book#addGender(books.persistence.types.Gender)
*/
View
26 persistence/books/berkeleydb/src/books/persistence/types/berkeleydb/GenderBerkeleyDB.java
@@ -23,19 +23,26 @@
*/
package books.persistence.types.berkeleydb;
-import com.sleepycat.persist.model.Persistent;
-import com.sleepycat.persist.model.PrimaryKey;
-
import books.persistence.types.Gender;
+import com.sleepycat.persist.model.Entity;
+import com.sleepycat.persist.model.PrimaryKey;
+
/**
- * @author ccorsi
+ * @author Claudio Corsi
*
*/
-@Persistent
+@Entity
public class GenderBerkeleyDB implements Gender {
- @PrimaryKey
+ @Override
+ public String toString() {
+ return "[GenderBerkeleyDB: " + id + "]";
+ }
+
+ @PrimaryKey(sequence="genderid")
+ private long id;
+
private String name;
/* (non-Javadoc)
@@ -54,4 +61,11 @@ public void setName(String name) {
this.name = name;
}
+ /**
+ * @return
+ */
+ public long getId() {
+ return id;
+ }
+
}
View
38 ...ence/books/berkeleydb/src/books/persistence/types/berkeleydb/ObjectsContainerManager.java
@@ -23,11 +23,15 @@
*/
package books.persistence.types.berkeleydb;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import books.persistence.types.Gender;
+import books.persistence.types.Person;
import books.persistence.types.PersonType;
import com.sleepycat.persist.model.Entity;
@@ -44,28 +48,23 @@
@PrimaryKey
private long id;
- private Map<PersonType,List<PersonBerkeleyDB>> persons;
- private List<GenderBerkeleyDB> genders;
+ private Map<PersonType,Collection<Person>> persons;
+ private Collection<Gender> genders;
- ObjectsContainerManager() {
- this.persons = new HashMap<PersonType, List<PersonBerkeleyDB>>();
- this.genders = new LinkedList<GenderBerkeleyDB>();
+ public ObjectsContainerManager() {
+ this.persons = new HashMap<PersonType, Collection<Person>>();
+ this.genders = new LinkedList<Gender>();
}
- public ObjectsContainerManager(long id) {
- this();
- this.id = id;
- }
-
public long getId() {
return this.id;
}
public void addPerson(PersonBerkeleyDB person) {
- List<PersonBerkeleyDB> personTypes = persons.get(person.getType());
+ Collection<Person> personTypes = persons.get(person.getType());
if(personTypes == null) {
- personTypes = new LinkedList<PersonBerkeleyDB>();
+ personTypes = new LinkedList<Person>();
persons.put(person.getType(), personTypes);
}
@@ -76,11 +75,20 @@ public void addGender(GenderBerkeleyDB gender) {
genders.add(gender);
}
- public List<PersonBerkeleyDB> getPersons(PersonType type) {
- return persons.get(type);
+ public Collection<Person> getPersons(PersonType type) {
+ Collection<Person> personsType = persons.get(type);
+ if (personsType == null) {
+ personsType = Collections.emptyList();
+ }
+ return personsType;
}
- public List<GenderBerkeleyDB> getGenders() {
+ public Collection<Gender> getGenders() {
return genders;
}
+
+ public String toString() {
+ return "Persons: [" + this.persons + "], Genders: [" + this.genders + "]";
+ }
+
}
View
13 persistence/books/berkeleydb/src/books/persistence/types/berkeleydb/PersonBerkeleyDB.java
@@ -30,19 +30,24 @@
import books.persistence.types.Person;
import books.persistence.types.PersonType;
-import com.sleepycat.persist.model.Persistent;
+import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
/**
* @author ccorsi
*
*
*/
-@Persistent
+@Entity
public class PersonBerkeleyDB implements Person {
- @PrimaryKey(sequence="ID")
- long id;
+ @Override
+ public String toString() {
+ return "[PersonBerkeleyDB: " + id + "]";
+ }
+
+ @PrimaryKey(sequence="personId")
+ private long id;
private Set<String> lastnames = new HashSet<String>();
private String name;
View
187 persistence/books/berkeleydb/src/books/persistence/types/berkeleydb/test/BooksTest.java
@@ -24,15 +24,29 @@
package books.persistence.types.berkeleydb.test;
+import java.io.File;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Properties;
+import java.util.concurrent.atomic.AtomicLong;
import org.junit.After;
import org.junit.AfterClass;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
+import org.junit.Test;
+import org.valhalla.services.Service;
+import org.valhalla.services.ServiceFactory;
import books.persistence.manager.DatabaseManager;
-import books.persistence.manager.DatabaseManagerFactory;
+import books.persistence.types.Book;
+import books.persistence.types.Gender;
+import books.persistence.types.Person;
+import books.persistence.types.PersonType;
/**
* @author ccorsi
@@ -47,11 +61,50 @@
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- dbMgr = DatabaseManagerFactory.createDatabaseManager();
+ System
+ .setProperty("books.persistence.manager.databasemanager",
+ "books.persistence.manager.berkeleydb.BerkeleyDBDatabaseManager");
+
+ Service<DatabaseManager> service = ServiceFactory.createService(
+ "books.persistence.manager.databasemanager",
+ "books.persistence.manager.databasemanager",
+ DatabaseManager.class);
+
+ dbMgr = service.create();
+
+ File dir = new File("./berkeleydb/databases");
+ deleteFiles(dir);
Properties properties = new Properties();
+ properties.setProperty("dbPath", dir.getAbsolutePath());
dbMgr.init(properties);
}
+ private static void deleteFiles(File dir) {
+ // TODO Auto-generated method stub
+ if (!dir.exists()) {
+ // do nothing...
+ return;
+ } else if (dir.isFile()) {
+ // this is a file then just delete the file...
+ dir.delete();
+ return;
+ } else if (dir.isDirectory()) {
+ // Iterator over all file names and delete each one...
+ List<File> dirs = new LinkedList<File>();
+ for (File file : dir.listFiles()) {
+ if (file.isDirectory()) {
+ dirs.add(file);
+ } else {
+ file.delete();
+ }
+ }
+ // Iterate over each directory....
+ for (File subdir : dirs) {
+ deleteFiles(subdir);
+ }
+ }
+ }
+
/**
* @throws java.lang.Exception
*/
@@ -60,6 +113,136 @@ public static void tearDownAfterClass() throws Exception {
dbMgr.close();
}
+ private Book createBook() {
+ Book book = dbMgr.createBook();
+ long currentCount = System.currentTimeMillis();
+ String title = "Title" + currentCount;
+ book.setTitle(title);
+ book.setCopyRight(2010);
+ book.setPages(101);
+ book.setSeries("Series" + currentCount);
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.HOUR_OF_DAY,0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ cal.set(Calendar.MILLISECOND,0);
+
+ book.setDateFinishReading(cal.getTime());
+ return book;
+ }
+
+ private Person addPerson(Book book, PersonType type) {
+ Person person = dbMgr.createPerson(type);
+ long tag = System.currentTimeMillis();
+ person.setFirstName("FirstName" + tag);
+ person.addMiddleName("MiddleName" + tag);
+ person.addLastName("LastName" + tag);
+ book.addPerson(person);
+ return person;
+ }
+
+ private void checkForBook(Book book) {
+ Collection<Book> books = dbMgr.getBooks();
+ Assert.assertNotNull("An non-null books collection should always be returned", books);
+ Assert.assertFalse("Books collection contains no book elements", books.isEmpty());
+ Assert.assertTrue("Created book does not exist in database", books.contains(book));
+ System.out.println("Added book instance: " + book);
+ for (Book bookdb : books) {
+ System.out.println(bookdb);
+ }
+ }
+
+ private void checkForPerson(Book book, Person person, PersonType type) {
+ Collection<Person> persons = book.getPersons(type);
+ Assert.assertNotNull("null persons instance returned is not valid", persons);
+ Assert.assertFalse("No persons aasociated to created book", persons.isEmpty());
+ Assert.assertTrue("Added person is not part of the created book instance", persons.contains(person));
+ for (Person persondb : persons) {
+ System.out.println(persondb);
+ }
+ }
+
+ @Test
+ public void createSingleBook() {
+ System.out.println("INSIDE createSingleBook");
+ // Create an instance of book and store it in the database...
+ dbMgr.beginTx();
+ Book book = this.createBook();
+ System.out.println("Created book: " + book);
+ book.setTitle("The Title");
+ book.setCopyRight(2010);
+ dbMgr.save(book);
+ dbMgr.commitTx();
+
+ // Retreive the list of books in the database and determine if the created book exists...
+ checkForBook(book);
+ }
+
+ @Test
+ public void createSingleBookWithAuthor() {
+ System.out.println("INSIDE createSingleBookWithAuthor");
+ dbMgr.beginTx();
+ Book book = this.createBook();
+ Person person = this.addPerson(book, PersonType.Author);
+ dbMgr.save(book);
+ dbMgr.commitTx();
+
+ // Retreive the list of books in the database and determine if the created book exists...
+ checkForBook(book);
+
+ checkForPerson(book, person, PersonType.Author);
+
+ // Retrieve the list of persons in the database and determine if the created person exists...
+ Collection<Person> persons = dbMgr.getPersons(PersonType.Author);
+ Assert.assertNotNull("A non-null persons collection should always be returned", persons);
+ Assert.assertFalse("Persons collection contains no person elements", persons.isEmpty());
+ Assert.assertTrue("Created person instance does not exists in collection", persons.contains(person));
+ for (Person persondb : persons) {
+ System.out.println(persondb);
+ }
+ }
+
+ @Test
+ public void createMultipleBooks() {
+ System.out.println("INSIDE createMultipleBooks");
+ dbMgr.beginTx();
+ for (int idx=0 ; idx < 15 ; idx++) {
+ Book book = this.createBook();
+ System.out.println("Created book: " + book.getTitle());
+ dbMgr.save(book);
+ }
+ dbMgr.commitTx();
+
+ for(Book book : dbMgr.getBooks()) {
+ System.out.println("Container contains book: " + book.getTitle());
+ }
+ }
+
+ @Test
+ public void createBookWithGender() {
+ System.out.println("INSIDE createBookWithGender");
+ dbMgr.beginTx();
+ Book book = this.createBook();
+ Gender gender = this.addGender(book);
+ dbMgr.save(book);
+ dbMgr.commitTx();
+ System.out.println(book);
+ System.out.println(gender);
+
+ for (Book bookdb : dbMgr.getBooks()) {
+ System.out.println("Book: " + bookdb + ", genders: " + bookdb.getGenders());
+ }
+
+ System.out.println("Genders: " + dbMgr.getGenders());
+ }
+
+ private Gender addGender(Book book) {
+ Gender gender = dbMgr.createGender();
+ gender.setName("Gender" + System.currentTimeMillis());
+ book.addGender(gender);
+ return gender;
+ }
+
/**
* @throws java.lang.Exception
*/

0 comments on commit f642b99

Please sign in to comment.