001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): _________________________.
021: */package org.continuent.sequoia.controller.scheduler.schema;
022:
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import org.continuent.sequoia.common.sql.schema.DatabaseSchema;
028: import org.continuent.sequoia.common.sql.schema.DatabaseTable;
029:
030: /**
031: * A <code>SchedulerDatabaseSchema</code> describes all the tables and columns
032: * of a database and its associated cache entries.
033: *
034: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
035: * @version 1.0
036: */
037: public class SchedulerDatabaseSchema {
038: /** <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>. */
039: private ArrayList tables;
040:
041: private TransactionExclusiveLock lock = new TransactionExclusiveLock();
042:
043: /**
044: * Creates a new <code>SchedulerDatabaseSchema</code> instance by cloning an
045: * existing <code>DatabaseSchema</code>.
046: *
047: * @param schema the database schema to clone
048: */
049: public SchedulerDatabaseSchema(DatabaseSchema schema) {
050: if (schema == null) {
051: tables = new ArrayList();
052: return;
053: }
054:
055: // Clone the tables
056: Collection origTables = schema.getTables().values();
057: int size = origTables.size();
058: tables = new ArrayList(size);
059: for (Iterator iter = origTables.iterator(); iter.hasNext();)
060: tables.add(new SchedulerDatabaseTable((DatabaseTable) iter
061: .next()));
062: }
063:
064: /**
065: * Adds a <code>SchedulerDatabaseTable</code> describing a table of the
066: * database.
067: *
068: * @param table the table to add
069: */
070: public void addTable(SchedulerDatabaseTable table) {
071: tables.add(table);
072: }
073:
074: /**
075: * Removes a <code>SchedulerDatabaseTable</code> describing a table of the
076: * database.
077: *
078: * @param table the table to remove
079: */
080: public void removeTable(SchedulerDatabaseTable table) {
081: tables.remove(table);
082: }
083:
084: /**
085: * Merge the given schema with the current one. All missing tables are added
086: * if no conflict is detected. An exception is thrown if the given schema
087: * definition conflicts with the current one.
088: *
089: * @param databaseSchema the schema to merge
090: */
091: public void mergeSchema(SchedulerDatabaseSchema databaseSchema) {
092: if (databaseSchema == null)
093: return;
094:
095: ArrayList otherTables = databaseSchema.getTables();
096: if (otherTables == null)
097: return;
098:
099: int size = otherTables.size();
100: for (int i = 0; i < size; i++) {
101: SchedulerDatabaseTable t = (SchedulerDatabaseTable) otherTables
102: .get(i);
103: SchedulerDatabaseTable original = getTable(t.getName());
104: if (original == null)
105: addTable(t);
106: }
107: }
108:
109: /**
110: * Returns an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
111: * objects describing the database.
112: *
113: * @return an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
114: */
115: public ArrayList getTables() {
116: return tables;
117: }
118:
119: /**
120: * Returns the <code>SchedulerDatabaseTable</code> object matching the given
121: * table name or <code>null</code> if not found. Matching is case
122: * insensitive.
123: *
124: * @param tableName the table name to look for
125: * @return a <code>SchedulerDatabaseTable</code> value or null
126: */
127: public SchedulerDatabaseTable getTable(String tableName) {
128: SchedulerDatabaseTable t;
129: int size = tables.size();
130: for (int i = 0; i < size; i++) {
131: t = (SchedulerDatabaseTable) tables.get(i);
132: if (tableName.equalsIgnoreCase(t.getName()))
133: return t;
134: }
135: return null;
136: }
137:
138: /**
139: * Returns <code>true</code> if the given <code>TableName</code> is found
140: * in this schema.
141: *
142: * @param tableName the name of the table you are looking for
143: * @return <code>true</code> if the table has been found
144: */
145: public boolean hasTable(String tableName) {
146: int size = tables.size();
147: for (int i = 0; i < size; i++) {
148: SchedulerDatabaseTable t = (SchedulerDatabaseTable) tables
149: .get(i);
150: if (tableName.equals(t.getName()))
151: return true;
152: }
153: return false;
154: }
155:
156: /**
157: * Returns the lock for this table.
158: *
159: * @return a <code>TransactionExclusiveLock</code> instance
160: * @see TransactionExclusiveLock
161: */
162: public TransactionExclusiveLock getLock() {
163: return lock;
164: }
165:
166: /**
167: * Two <code>SchedulerDatabaseSchema</code> are equals if they have the same
168: * tables.
169: *
170: * @param other the object to compare with
171: * @return true if the objects are the same
172: */
173: public boolean equals(Object other) {
174: if (!(other instanceof SchedulerDatabaseSchema))
175: return false;
176:
177: if (tables == null)
178: return ((SchedulerDatabaseSchema) other).getTables() == null;
179: else
180: return tables.equals(((SchedulerDatabaseSchema) other)
181: .getTables());
182: }
183:
184: /**
185: * Returns information about the database schema.
186: *
187: * @param longFormat <code>true</code> for a <code>long</code> format,
188: * <code>false</code> for a short summary
189: * @return a <code>String</code> value
190: */
191: public String getInformation(boolean longFormat) {
192: StringBuffer result = new StringBuffer();
193: SchedulerDatabaseTable t;
194: int size = tables.size();
195: for (int i = 0; i < size; i++) {
196: t = (SchedulerDatabaseTable) tables.get(i);
197: result.append(t.getInformation(longFormat));
198: result.append(System.getProperty("line.separator"));
199: }
200: return result.toString();
201: }
202: }
|