root / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / PDataSource.java @ 466
History | View | Annotate | Download (5.82 KB)
1 |
package com.hardcode.gdbms.engine; |
---|---|
2 |
|
3 |
import com.hardcode.gdbms.engine.data.DataSource; |
4 |
import com.hardcode.gdbms.engine.data.DriverException; |
5 |
import com.hardcode.gdbms.engine.data.FieldNameAccessSupport; |
6 |
import com.hardcode.gdbms.engine.data.ReadDriver; |
7 |
import com.hardcode.gdbms.engine.values.Value; |
8 |
|
9 |
|
10 |
/**
|
11 |
* Clase que representa el producto cartesiano de dos o m?s tablas. El
|
12 |
* almacenamiento de dicha tabla se realiza en las propias tablas sobre las
|
13 |
* que se opera, haciendo los c?lculos en cada acceso para saber en qu? tabla
|
14 |
* y en qu? posici?n de la tabla se encuentra el dato buscado
|
15 |
*
|
16 |
* @author Fernando Gonz?lez Cort?s
|
17 |
*/
|
18 |
public class PDataSource implements DataSource { |
19 |
private DataSource[] tables; |
20 |
private long tablesArity; |
21 |
private FieldNameAccessSupport fnaSupport = new FieldNameAccessSupport(this); |
22 |
|
23 |
/**
|
24 |
* Creates a new PDataSource object.
|
25 |
*
|
26 |
* @param tables Array de tablas que forman el producto
|
27 |
*/
|
28 |
public PDataSource(DataSource[] tables) { |
29 |
this.tables = tables;
|
30 |
} |
31 |
|
32 |
/**
|
33 |
* Dado un ?ndice de campo en la tabla producto, devuelve el ?ndice en la
|
34 |
* tabla operando a la cual pertenence el campo
|
35 |
*
|
36 |
* @param fieldId ?ndice en la tabla producto
|
37 |
*
|
38 |
* @return ?ndice en la tabla operando
|
39 |
*
|
40 |
* @throws DriverException Si se prouce alg?n error accediendo a la tabla
|
41 |
* operando
|
42 |
*/
|
43 |
private int getFieldIndex(int fieldId) throws DriverException { |
44 |
int table = 0; |
45 |
|
46 |
while (fieldId >= tables[table].getFieldCount()) {
|
47 |
fieldId -= tables[table].getFieldCount(); |
48 |
table++; |
49 |
} |
50 |
|
51 |
return fieldId;
|
52 |
} |
53 |
|
54 |
/**
|
55 |
* Dado un ?ndice de campo en la tabla producto, devuelve el ?ndice en el
|
56 |
* array de tablas de la tabla operando que contiene dicho campo
|
57 |
*
|
58 |
* @param fieldId ?ndice del campo en la tabla producto
|
59 |
*
|
60 |
* @return ?ndice de la tabla en el array de tablas
|
61 |
*
|
62 |
* @throws DriverException Si se prouce alg?n error accediendo a la tabla
|
63 |
* operando
|
64 |
*/
|
65 |
private int getTableIndexByFieldId(int fieldId) throws DriverException { |
66 |
int table = 0; |
67 |
|
68 |
while (fieldId >= tables[table].getFieldCount()) {
|
69 |
fieldId -= tables[table].getFieldCount(); |
70 |
table++; |
71 |
} |
72 |
|
73 |
return table;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Devuelve la fila de la tabla operando con ?ndice tableIndex que contiene
|
78 |
* la informaci?n de la fila rowIndex en la tabla producto
|
79 |
*
|
80 |
* @param rowIndex fila en la tabla producto a la que se quiere acceder
|
81 |
* @param tableIndex ?ndice de la tabla
|
82 |
*
|
83 |
* @return fila en la tabla operando de ?ndice tableIndex que se quiere
|
84 |
* acceder
|
85 |
*
|
86 |
* @throws DriverException Si se prouce alg?n error accediendo a la tabla
|
87 |
* operando
|
88 |
* @throws ArrayIndexOutOfBoundsException Si la fila que se pide (rowIndex)
|
89 |
* supera el n?mero de filas de la tabla producto
|
90 |
*/
|
91 |
private long getTableRowIndexByTablePosition(long rowIndex, int tableIndex) |
92 |
throws DriverException {
|
93 |
if (rowIndex >= tablesArity) {
|
94 |
throw new ArrayIndexOutOfBoundsException(); |
95 |
} |
96 |
|
97 |
int arity = 1; |
98 |
|
99 |
for (int i = tableIndex + 1; i < tables.length; i++) { |
100 |
arity *= tables[i].getRowCount(); |
101 |
} |
102 |
|
103 |
long selfArity = tables[tableIndex].getRowCount();
|
104 |
|
105 |
return (rowIndex / arity) % selfArity;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see com.hardcode.gdbms.data.DataSource#getFieldName(int)
|
110 |
*/
|
111 |
public String getFieldName(int fieldId) throws DriverException { |
112 |
return tables[getTableIndexByFieldId(fieldId)].getFieldName(getFieldIndex(
|
113 |
fieldId)); |
114 |
} |
115 |
|
116 |
/**
|
117 |
* @see com.hardcode.gdbms.data.DataSource#getIntFieldValue(int, int)
|
118 |
*/
|
119 |
public Value getFieldValue(long rowIndex, int fieldId) |
120 |
throws DriverException {
|
121 |
int tableIndex = getTableIndexByFieldId(fieldId);
|
122 |
|
123 |
return tables[tableIndex].getFieldValue(getTableRowIndexByTablePosition(
|
124 |
rowIndex, tableIndex), getFieldIndex(fieldId)); |
125 |
} |
126 |
|
127 |
/**
|
128 |
* @see com.hardcode.gdbms.data.DataSource#getFieldCount()
|
129 |
*/
|
130 |
public int getFieldCount() throws DriverException { |
131 |
int ret = 0; |
132 |
|
133 |
for (int i = 0; i < tables.length; i++) { |
134 |
ret += tables[i].getFieldCount(); |
135 |
} |
136 |
|
137 |
return ret;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.hardcode.gdbms.data.DataSource#getRowCount()
|
142 |
*/
|
143 |
public long getRowCount() { |
144 |
return tablesArity;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* @see com.hardcode.gdbms.data.DataSource#open(java.io.File)
|
149 |
*/
|
150 |
public void start() throws DriverException { |
151 |
for (int i = 0; i < tables.length; i++) { |
152 |
try {
|
153 |
tables[i].start(); |
154 |
} catch (DriverException e) {
|
155 |
for (int j = 0; j < i; j++) { |
156 |
tables[i].stop(); |
157 |
} |
158 |
|
159 |
throw e;
|
160 |
} |
161 |
} |
162 |
|
163 |
tablesArity = 1;
|
164 |
|
165 |
for (int i = 0; i < tables.length; i++) { |
166 |
tablesArity *= tables[i].getRowCount(); |
167 |
} |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @see com.hardcode.gdbms.data.DataSource#close()
|
172 |
*/
|
173 |
public void stop() throws DriverException { |
174 |
for (int i = 0; i < tables.length; i++) { |
175 |
tables[i].stop(); |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* @see com.hardcode.gdbms.data.DataSource#getFieldIndexByName(String)
|
181 |
*/
|
182 |
public int getFieldIndexByName(String fieldName) throws DriverException { |
183 |
return fnaSupport.getFieldIndexByName(fieldName);
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
188 |
*/
|
189 |
public String getName() { |
190 |
return ""; |
191 |
} |
192 |
|
193 |
/**
|
194 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
|
195 |
*/
|
196 |
public String getDBMS() { |
197 |
return null; |
198 |
} |
199 |
|
200 |
/**
|
201 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
|
202 |
*/
|
203 |
public ReadDriver getDriver() {
|
204 |
return null; |
205 |
} |
206 |
} |