01: /*
02: This file is part of the PolePosition database benchmark
03: http://www.polepos.org
04:
05: This program is free software; you can redistribute it and/or
06: modify it under the terms of the GNU General Public License
07: as published by the Free Software Foundation; either version 2
08: of the License, or (at your option) any later version.
09:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public
16: License along with this program; if not, write to the Free
17: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18: MA 02111-1307, USA. */
19:
20: package org.polepos.teams.hibernate;
21:
22: import java.io.*;
23:
24: import org.polepos.circuits.imola.*;
25: import org.polepos.framework.*;
26: import org.polepos.teams.hibernate.data.*;
27:
28: import net.sf.hibernate.*;
29:
30: public class ImolaHibernate extends HibernateDriver implements
31: ImolaDriver {
32: private final String FROM = "from org.polepos.teams.hibernate.data.HibernateIndexedPilot";
33: private Serializable[] ids;
34: private int step;
35:
36: public void takeSeatIn(Car car, TurnSetup setup)
37: throws CarMotorFailureException {
38: ids = new Serializable[setup.getSelectCount()];
39: super .takeSeatIn(car, setup);
40: }
41:
42: public void store() {
43: try {
44: Transaction tx = db().beginTransaction();
45: for (int i = 1; i <= setup().getObjectCount(); i++) {
46: storePilot(tx, i);
47: }
48: tx.commit();
49: } catch (HibernateException hex) {
50: hex.printStackTrace();
51: }
52: }
53:
54: public void retrieve() {
55: for (Serializable id : ids) {
56: try {
57: HibernateIndexedPilot pilot = (HibernateIndexedPilot) db()
58: .load(HibernateIndexedPilot.class, id);
59: addToCheckSum(pilot.getPoints());
60: if (pilot == null) {
61: System.err.println("Got no pilot by ID.");
62: }
63: } catch (HibernateException e) {
64: e.printStackTrace();
65: }
66: }
67: }
68:
69: private void storePilot(Transaction trans, int idx)
70: throws HibernateException {
71: Serializable id = db().save(
72: new HibernateIndexedPilot("Pilot_" + idx, "Jonny_"
73: + idx, idx, idx));
74: if (isCommitPoint(idx)) {
75: trans.commit();
76: Log.logger.fine("commit while writing at " + idx + 1); //NOI18N
77: }
78: if (idx <= setup().getSelectCount()) {
79: ids[idx - 1] = id;
80: }
81: }
82:
83: private boolean isCommitPoint(int idx) {
84: int commitInterval = setup().getCommitInterval();
85: return commitInterval > 0 && idx % commitInterval == 0
86: && idx < setup().getObjectCount();
87: }
88: }
|