001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.core.query;
020:
021: import java.util.Vector;
022: import java.util.WeakHashMap;
023:
024: import com.mobixess.jodb.util.ArrayUtils;
025:
026: public class SortDataCache {
027: private WeakHashMap<Long, SortingDataContainer> _sortValues = new WeakHashMap<Long, SortingDataContainer>();
028:
029: private Vector<SortNodeRecord> _sortNodesCache = new Vector<SortNodeRecord>();
030: private SortNodeRecord[] _nodes;
031: private Long _currentCandidateID;
032: private Object[] _curretCandidateValues;
033: private int _currentCandidatesAccumValTotal;
034:
035: public void clearAll() {
036: _sortNodesCache.clear();
037: _nodes = null;
038: _currentCandidateID = null;
039: _curretCandidateValues = null;
040: _currentCandidatesAccumValTotal = 0;
041: }
042:
043: public void addNode(int sortNodeId, String fullPath,
044: boolean isAscending) {
045: SortNodeRecord newNode = new SortNodeRecord(sortNodeId,
046: fullPath, isAscending);
047: for (int i = 0; i < _sortNodesCache.size(); i++) {
048: SortNodeRecord currentNode = _sortNodesCache.elementAt(i);
049: int compareResult = newNode.compareTo(currentNode);
050: if (compareResult < 0) {
051: _sortNodesCache.insertElementAt(newNode, i);
052: _currentCandidatesAccumValTotal++;
053: return;
054: } else if (compareResult == 0) {
055: System.err.println("sort node already added ");//TODO log
056: return;
057: }
058: }
059: _sortNodesCache.add(newNode);
060: _currentCandidatesAccumValTotal++;
061: }
062:
063: public int getSortNodesCacheSize() {
064: return _sortNodesCache.size();
065: }
066:
067: public SortNodeRecord[] getSortNodes() {
068: return _nodes;
069: }
070:
071: public void setValuesAccumulationMode() {
072: _nodes = new SortNodeRecord[_sortNodesCache.size()];
073: for (int i = 0; i < _nodes.length; i++) {
074: _nodes[i] = _sortNodesCache.elementAt(i);
075: }
076: _sortNodesCache.clear();
077: }
078:
079: public void setCandidateID(long id) {
080: _currentCandidateID = id;
081: _currentCandidatesAccumValTotal = 0;
082: if (_curretCandidateValues == null) {
083: _curretCandidateValues = new Object[_nodes.length];
084: } else {
085: ArrayUtils.clear(_curretCandidateValues);
086: }
087: }
088:
089: public SortingDataContainer acceptCandidate() {
090: SortingDataContainer result = new SortingDataContainer(
091: _curretCandidateValues);
092: _sortValues.put(_currentCandidateID, result);
093: _currentCandidateID = null;
094: _curretCandidateValues = null;
095: _currentCandidatesAccumValTotal = 0;
096: return result;
097: }
098:
099: public boolean isValuesAccumulationComplete() {
100: return _currentCandidatesAccumValTotal == _curretCandidateValues.length;
101: }
102:
103: public void addSortValue(int nodeID, Object value) {
104: int nodeIndex = getNodeIndex(nodeID);
105: if (nodeIndex != -1) {
106: _curretCandidateValues[nodeIndex] = value;
107: return;
108: }
109: System.err.println("illegal sort value " + nodeID);
110: }
111:
112: private int getNodeIndex(int nodeID) {
113: for (int i = 0; i < _nodes.length; i++) {
114: if (_nodes[i]._nodeID == nodeID) {
115: return i;
116: }
117: }
118: return -1;
119: }
120:
121: public Object getValue(long candidateId, int sortNodeId) {
122: SortingDataContainer values = _sortValues.get(candidateId);
123: if (values == null) {
124: return null;
125: }
126: int index = getNodeIndex(sortNodeId);
127: if (index == -1) {
128: System.err.println(" requesting unknown node ");
129: return null;
130: } else {
131: return values._data[index];
132: }
133: }
134:
135: public SortingDataContainer getSortDataForCandidateId(
136: long candidateId) {
137: return _sortValues.get(candidateId);
138: }
139:
140: static class SortNodeRecord implements Comparable {
141:
142: int _nodeID;
143: String _fullPath;
144: boolean _orderAscending;
145:
146: /**
147: * @param nodeIndex
148: * @param field
149: */
150: public SortNodeRecord(int nodeIndex, String fullPath,
151: boolean isAscending) {
152: _nodeID = nodeIndex;
153: _fullPath = fullPath;
154: _orderAscending = isAscending;
155: }
156:
157: public int compareTo(Object o) {
158: SortNodeRecord o1 = (SortNodeRecord) o;
159: if (_nodeID < o1._nodeID) {
160: return -1;
161: } else if (_nodeID > o1._nodeID) {
162: return 1;
163: } else {
164: return 0;
165: }
166: }
167:
168: public String getFullPath() {
169: return _fullPath;
170: }
171:
172: public boolean isOrderAscending() {
173: return _orderAscending;
174: }
175: }
176: }
|