001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.query.processor;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026:
027: /**
028: *
029: * Join constraint on queries
030: *
031: * @exclude
032: */
033: public class QConJoin extends QCon {
034:
035: // FIELDS MUST BE PUBLIC TO BE REFLECTED ON UNDER JDK <= 1.1
036:
037: public boolean i_and;
038: public QCon i_constraint1;
039: public QCon i_constraint2;
040:
041: public QConJoin() {
042: // C/S
043: }
044:
045: QConJoin(Transaction a_trans, QCon a_c1, QCon a_c2, boolean a_and) {
046: super (a_trans);
047: i_constraint1 = a_c1;
048: i_constraint2 = a_c2;
049: i_and = a_and;
050: }
051:
052: void doNotInclude(QCandidate a_root) {
053: i_constraint1.doNotInclude(a_root);
054: i_constraint2.doNotInclude(a_root);
055: }
056:
057: void exchangeConstraint(QCon a_exchange, QCon a_with) {
058: super .exchangeConstraint(a_exchange, a_with);
059: if (a_exchange == i_constraint1) {
060: i_constraint1 = a_with;
061: }
062: if (a_exchange == i_constraint2) {
063: i_constraint2 = a_with;
064: }
065: }
066:
067: void evaluatePending(QCandidate a_root, QPending a_pending,
068: int a_secondResult) {
069:
070: boolean res = i_evaluator
071: .not(i_and ? ((a_pending._result + a_secondResult) > 0)
072: : (a_pending._result + a_secondResult) > -4);
073:
074: if (hasJoins()) {
075: Iterator4 i = iterateJoins();
076: while (i.moveNext()) {
077: QConJoin qcj = (QConJoin) i.current();
078: if (Debug.queries) {
079: System.out.println("QConJoin creates pending this:"
080: + i_id + " Join:" + qcj.i_id + " res:"
081: + res);
082: }
083: a_root.evaluate(new QPending(qcj, this , res));
084: }
085: } else {
086: if (!res) {
087: if (Debug.queries) {
088: System.out
089: .println("QConJoin evaluatePending FALSE "
090: + i_id + " Calling: "
091: + i_constraint1.i_id + ", "
092: + i_constraint2.i_id);
093: }
094: i_constraint1.doNotInclude(a_root);
095: i_constraint2.doNotInclude(a_root);
096: } else {
097: if (Debug.queries) {
098: System.out.println("QConJoin evaluatePending TRUE "
099: + i_id + " NOT calling: "
100: + i_constraint1.i_id + ", "
101: + i_constraint2.i_id);
102: }
103: }
104:
105: }
106: }
107:
108: public QCon getOtherConstraint(QCon a_constraint) {
109: if (a_constraint == i_constraint1) {
110: return i_constraint2;
111: } else if (a_constraint == i_constraint2) {
112: return i_constraint1;
113: }
114: throw new IllegalArgumentException();
115: }
116:
117: String logObject() {
118: if (Debug.queries) {
119: String msg = i_and ? "&" : "|";
120: return " " + i_constraint1.i_id + msg + i_constraint2.i_id;
121: }
122: return "";
123: }
124:
125: boolean removeForParent(QCon a_constraint) {
126: if (i_and) {
127: QCon other = getOtherConstraint(a_constraint);
128: other.removeJoin(this ); // prevents circular call
129: other.remove();
130: return true;
131: }
132: return false;
133: }
134:
135: public String toString() {
136: if (!Debug4.prettyToStrings) {
137: return super .toString();
138: }
139: String str = "QConJoin " + (i_and ? "AND " : "OR");
140: if (i_constraint1 != null) {
141: str += "\n " + i_constraint1;
142: }
143: if (i_constraint2 != null) {
144: str += "\n " + i_constraint2;
145: }
146: return str;
147: }
148:
149: public boolean isOr() {
150: return !i_and;
151: }
152:
153: }
|