01: /**
02: * Copyright (C) 2006 NetMind Consulting Bt.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 3 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package hu.netmind.persistence.node;
18:
19: import hu.netmind.persistence.StoreContext;
20: import org.apache.log4j.Logger;
21: import java.util.*;
22:
23: /**
24: * This class tracks current commits and queries.
25: * @author Brautigam Robert
26: * @version Revision: $Revision$
27: */
28: public class CommitAndQueryTracker {
29: private static Logger logger = Logger
30: .getLogger(CommitAndQueryTracker.class);
31:
32: private HashMap commitsByIndex;
33: private SortedSet commitSerials;
34: private StoreContext context;
35:
36: public CommitAndQueryTracker(StoreContext context) {
37: this .context = context;
38: commitsByIndex = new HashMap();
39: commitSerials = new TreeSet();
40: }
41:
42: public synchronized Long startCommit(int index) {
43: // Get serial and register it
44: Long serial = context.getSerialTracker().getNextSerial();
45: Set serials = (Set) commitsByIndex.get(new Integer(index));
46: if (serials == null) {
47: serials = new HashSet();
48: commitsByIndex.put(new Integer(index), serials);
49: }
50: serials.add(serial);
51: commitSerials.add(serial);
52: // Return serial
53: return serial;
54: }
55:
56: public synchronized void endCommit(int index, Long serial) {
57: // Remove the serial
58: Set serials = (Set) commitsByIndex.get(new Integer(index));
59: if (serials == null)
60: return;
61: serials.remove(serial);
62: if (serials.size() == 0)
63: commitsByIndex.remove(new Integer(index));
64: commitSerials.remove(serial);
65: notifyAll(); // Notify waiting queries to re-check commit serials
66: }
67:
68: public synchronized void endAllCommits(int index) {
69: Set serials = (Set) commitsByIndex.remove(new Integer(index));
70: if (serials == null)
71: return;
72: Iterator serialIterator = serials.iterator();
73: while (serialIterator.hasNext())
74: commitSerials.remove(serialIterator.next());
75: notifyAll(); // Notify waiting queries to re-check commit serials
76: }
77:
78: /**
79: * This method must block until all commits which are lower
80: * serials are finished.
81: */
82: public synchronized void waitForQuery(Long serial) {
83: // If there are no serials, return immediately
84: if (commitSerials.size() == 0)
85: return;
86: // Get the lowest commit serial. If that is higher than this
87: // serial, than all commits are finished before this query.
88: Long lowest = null;
89: while ((lowest = (Long) commitSerials.first())
90: .compareTo(serial) <= 0) {
91: try {
92: wait(); // Wait until some commit finishes
93: } catch (InterruptedException e) {
94: logger.warn("query wait was interrupted", e);
95: }
96: }
97: }
98: }
|