Permalink
Browse files

Functional implementation of the berkeleydb integration which does no…

…t manage rollback situations.

This commit includes a tested version that will can be used create and update books.
  • Loading branch information...
1 parent f642b99 commit d2964d6999aadad3a79e2ced127adc96be70f269 @ccorsi committed Sep 15, 2010
@@ -0,0 +1,160 @@
+/**
+
+ BSD License
+
+ Copyright (c) 2010- , Claudio Corsi
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+package books.persistence.manager.berkeleydb;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.Map;
+
+import books.persistence.types.Gender;
+import books.persistence.types.Person;
+import books.persistence.types.PersonType;
+import books.persistence.types.berkeleydb.BookBerkeleyDB;
+import books.persistence.types.berkeleydb.GenderBerkeleyDB;
+import books.persistence.types.berkeleydb.PersonBerkeleyDB;
+
+import com.sleepycat.bind.tuple.TupleBinding;
+import com.sleepycat.bind.tuple.TupleInput;
+import com.sleepycat.bind.tuple.TupleOutput;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.LockMode;
+import com.sleepycat.je.OperationStatus;
+import com.sleepycat.je.Transaction;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class BookTupleBinding extends TupleBinding<BookBerkeleyDB> {
+
+ private Database genderdb;
+ private Map<PersonType,Database> personsdb;
+ private GenderTupleBinding gender_binding;
+ private PersonTupleBinding person_binding;
+ private Transaction txn;
+
+ public BookTupleBinding(Database genderdb, Map<PersonType,Database> personsdb,
+ GenderTupleBinding gender_binding, PersonTupleBinding person_binding,
+ Transaction txn) {
+ this.genderdb = genderdb;
+ this.personsdb = personsdb;
+ this.gender_binding = gender_binding;
+ this.person_binding = person_binding;
+ this.txn = txn;
+ }
+
+ public void setTransaction(Transaction txn) {
+ this.txn = txn;
+ }
+
+ @Override
+ public BookBerkeleyDB entryToObject(TupleInput input) {
+ BookBerkeleyDB book = new BookBerkeleyDB();
+
+ book.setId(input.readLong());
+ boolean hasDate = input.readBoolean();
+ if (hasDate) {
+ book.setDateFinishReading(new Date(input.readLong()));
+ }
+ int pages = input.readInt();
+ if (pages > 0) {
+ book.setPages(pages);
+ }
+ book.setSeries(input.readString());
+ book.setTitle(input.readString());
+ int cnt = input.readInt();
+ while (cnt-- > 0) {
+ book.addSubTitle(input.readString());
+ }
+ cnt = input.readInt();
+ while (cnt-- > 0) {
+ DatabaseEntry key = new DatabaseEntry();
+ TupleOutput output = new TupleOutput();
+ output.writeLong(input.readLong());
+ key.setData(output.getBufferBytes());
+ DatabaseEntry data = new DatabaseEntry();
+ OperationStatus status = genderdb.get(this.txn, key, data, LockMode.READ_COMMITTED);
+ if (status == OperationStatus.SUCCESS) {
+ book.addGender(this.gender_binding.entryToObject(data));
+ }
+ }
+ int size = input.readInt();
+ while (size-- > 0) {
+ String name = input.readString();
+ cnt = input.readInt();
+ PersonType type = PersonType.valueOf(name);
+ Database persondb = this.personsdb.get(type);
+ while (cnt-- > 0) {
+ DatabaseEntry key = new DatabaseEntry();
+ TupleOutput output = new TupleOutput();
+ output.writeLong(input.readLong());
+ key.setData(output.getBufferBytes());
+ DatabaseEntry data = new DatabaseEntry();
+ OperationStatus status = persondb.get(this.txn, key, data, null);
+ if (status == OperationStatus.SUCCESS) {
+ book.addPerson(this.person_binding.entryToObject(data));
+ }
+ }
+ }
+ book.setCopyRight(input.readInt());
+ return book;
+ }
+
+ @Override
+ public void objectToEntry(BookBerkeleyDB book, TupleOutput output) {
+ output.writeLong(book.getId());
+ Date date = book.getDateFinishReading();
+ if (date != null) {
+ output.writeBoolean(true);
+ output.writeLong(date.getTime());
+ } else {
+ output.writeBoolean(false);
+ }
+ output.writeInt(book.getPages());
+ output.writeString(book.getSeries());
+ output.writeString(book.getTitle());
+ Collection<String> subtitles = book.getSubTitles();
+ output.writeInt(subtitles.size());
+ for (String subtitle : subtitles) {
+ output.writeString(subtitle);
+ }
+ Collection<Gender> genders = book.getGenders();
+ output.writeInt(genders.size());
+ for (Gender gender : genders) {
+ output.writeLong(GenderBerkeleyDB.class.cast(gender).getId());
+ }
+ output.writeInt(PersonType.values().length);
+ for (PersonType type : PersonType.values()) {
+ Collection<Person> persons = book.getPersons(type);
+ output.writeString(type.name());
+ output.writeInt(persons.size());
+ for (Person person : persons) {
+ output.writeLong(PersonBerkeleyDB.class.cast(person).getId());
+ }
+ }
+ output.writeInt(book.getCopyRight());
+ }
+
+}
@@ -0,0 +1,54 @@
+/**
+
+ BSD License
+
+ Copyright (c) 2010- , Claudio Corsi
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+package books.persistence.manager.berkeleydb;
+
+import books.persistence.types.berkeleydb.GenderBerkeleyDB;
+
+import com.sleepycat.bind.tuple.TupleBinding;
+import com.sleepycat.bind.tuple.TupleInput;
+import com.sleepycat.bind.tuple.TupleOutput;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class GenderTupleBinding extends TupleBinding<GenderBerkeleyDB> {
+
+ @Override
+ public GenderBerkeleyDB entryToObject(TupleInput input) {
+ GenderBerkeleyDB gender = new GenderBerkeleyDB();
+
+ gender.setId(input.readLong());
+ gender.setName(input.readString());
+
+ return gender;
+ }
+
+ @Override
+ public void objectToEntry(GenderBerkeleyDB gender, TupleOutput output) {
+ output.writeLong(gender.getId());
+ output.writeString(gender.getName());
+ }
+
+}
@@ -0,0 +1,71 @@
+/**
+
+ BSD License
+
+ Copyright (c) 2010- , Claudio Corsi
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+package books.persistence.manager.berkeleydb;
+
+import books.persistence.types.berkeleydb.PersonBerkeleyDB;
+import books.persistence.types.PersonType;
+
+import com.sleepycat.bind.tuple.TupleBinding;
+import com.sleepycat.bind.tuple.TupleInput;
+import com.sleepycat.bind.tuple.TupleOutput;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class PersonTupleBinding extends TupleBinding<PersonBerkeleyDB> {
+
+ @Override
+ public PersonBerkeleyDB entryToObject(TupleInput input) {
+ PersonBerkeleyDB person = new PersonBerkeleyDB();
+
+ person.setId(input.readLong());
+ person.setType(PersonType.valueOf(input.readString()));
+ int cnt = input.readInt();
+ while (cnt-- > 0) {
+ person.addMiddleName(input.readString());
+ }
+ cnt = input.readInt();
+ while (cnt-- > 0) {
+ person.addLastName(input.readString());
+ }
+ return person;
+ }
+
+ @Override
+ public void objectToEntry(PersonBerkeleyDB person, TupleOutput output) {
+ output.writeLong(person.getId());
+ output.writeString(person.getType().name());
+ output.writeString(person.getFirstName());
+ output.writeInt(person.getMiddleNames().size());
+ for (String middlename : person.getMiddleNames()) {
+ output.writeString(middlename);
+ }
+ output.writeInt(person.getLastNames().size());
+ for (String lastname : person.getLastNames()) {
+ output.writeString(lastname);
+ }
+ }
+
+}
@@ -0,0 +1,80 @@
+/**
+
+ BSD License
+
+ Copyright (c) 2010- , Claudio Corsi
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+package books.persistence.manager.berkeleydb.test;
+
+import java.io.File;
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import books.persistence.manager.DatabaseManager;
+import books.persistence.manager.berkeleydb.BerkeleyDBDatabaseManager;
+
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class BookInitTest {
+
+ private final String dbPath = "./berkeleydb/database";
+ private final String dbName = "books";
+ private Properties props;
+ private DatabaseManager dbMgr;
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ File dir = new File(dbPath);
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+ File db = new File(dir,dbName);
+ if (!db.exists()) {
+ db.mkdir();
+ }
+
+ props = new Properties();
+ props.setProperty("dbPath", dbPath);
+ props.setProperty("dbName", dbName);
+ }
+
+ @Test
+ public void BookInit() {
+ dbMgr = new BerkeleyDBDatabaseManager();
+ dbMgr.init(props);
+ }
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ dbMgr.close();
+ }
+
+}
Oops, something went wrong. Retry.

0 comments on commit d2964d6

Please sign in to comment.