001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.hibernate;
021:
022: import org.polepos.circuits.sepang.*;
023: import org.polepos.teams.hibernate.data.*;
024:
025: import net.sf.hibernate.*;
026:
027: /**
028: * @author Herkules
029: */
030: public class SepangHibernate extends HibernateDriver implements
031: SepangDriver {
032:
033: private long treeRootID; // TODO: get and use for findRoot
034:
035: public void write() {
036: try {
037: Transaction tx = db().beginTransaction();
038: HibernateTree tree = HibernateTree.createTree(setup()
039: .getTreeDepth());
040: HibernateTree.traverse(tree, new HibernateTreeVisitor() {
041: public void visit(HibernateTree tree) {
042: try {
043: db().save(tree);
044: } catch (HibernateException e) {
045: e.printStackTrace();
046: }
047: }
048: });
049: tx.commit();
050:
051: } catch (HibernateException hex) {
052: hex.printStackTrace();
053: }
054: }
055:
056: public void read() {
057: try {
058: HibernateTree tree = findRoot();
059: HibernateTree.traverse(tree, new HibernateTreeVisitor() {
060: public void visit(HibernateTree tree) {
061: addToCheckSum(tree.getDepth());
062: }
063: });
064: } catch (HibernateException hex) {
065: hex.printStackTrace();
066: }
067: }
068:
069: public void read_hot() {
070: read();
071: }
072:
073: public void delete() {
074: try {
075: Transaction tx = db().beginTransaction();
076: HibernateTree tree = findRoot();
077: HibernateTree.traverse(tree, new HibernateTreeVisitor() {
078: public void visit(HibernateTree tree) {
079: try {
080: db().delete(tree);
081: } catch (HibernateException e) {
082: e.printStackTrace();
083: }
084: }
085: });
086: tx.commit();
087: } catch (HibernateException hex) {
088: hex.printStackTrace();
089: }
090: }
091:
092: /**
093: * Finds the root of the tree.
094: */
095: private HibernateTree findRoot() throws HibernateException {
096: Query q = db()
097: .createQuery(
098: "select tree from tree in class org.polepos.teams.hibernate.data.HibernateTree where tree.name='root'");
099: HibernateTree tree = (HibernateTree) q.list().get(0);
100: return tree;
101: }
102:
103: }
|