Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / FilteredDataSource.java @ 466

History | View | Annotate | Download (4.55 KB)

1
package com.hardcode.gdbms.engine;
2

    
3
import java.io.IOException;
4

    
5
import com.hardcode.gdbms.engine.data.DataSource;
6
import com.hardcode.gdbms.engine.data.DriverException;
7
import com.hardcode.gdbms.engine.data.ReadDriver;
8
import com.hardcode.gdbms.engine.data.indexes.IndexFactory;
9
import com.hardcode.gdbms.engine.data.indexes.VariableIndexSet;
10
import com.hardcode.gdbms.engine.instruction.Expression;
11
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
12
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
13
import com.hardcode.gdbms.engine.instruction.SemanticException;
14
import com.hardcode.gdbms.engine.values.BooleanValue;
15
import com.hardcode.gdbms.engine.values.Value;
16

    
17

    
18
/**
19
 * Representa una fuente de datos que contiene una cl?usula where mediante la
20
 * cual se filtran los campos
21
 *
22
 * @author Fernando Gonz?lez Cort?s
23
 */
24
public class FilteredDataSource implements DataSource {
25
    private DataSource source;
26
    private Expression whereExpression;
27
    private VariableIndexSet indexes;
28

    
29
    /**
30
     * Creates a new FilteredDataSource object.
31
     *
32
     * @param source DataSource que se va a filtrar
33
     * @param whereExpression Expresi?n de la cl?usula where
34
     */
35
    public FilteredDataSource(DataSource source,
36
        Expression whereExpression) {
37
        this.source = source;
38
        this.whereExpression = whereExpression;
39
    }
40

    
41
    /**
42
     * M?todo que construye el array de ?ndices de las posiciones que las filas
43
     * filtradas ocupan en el DataSource origen
44
     *
45
     * @throws DriverException Si se produce un fallo en el driver al acceder a
46
     *         los datos
47
     * @throws IOException Si se produce un error accediendo al DataSource
48
     *         subyacente
49
     * @throws SemanticException Si se produce alg?n error sem?ntico al evaluar
50
     *         la expresi?n
51
     * @throws IncompatibleTypesException Si la expresi?n where no evalua a
52
     *         booleano
53
     */
54
    public void filtrar()
55
        throws DriverException, IOException, SemanticException {
56
        indexes = IndexFactory.createVariableIndex();
57
        indexes.open();
58

    
59
        for (long i = 0; i < source.getRowCount(); i++) {
60
            try {
61
                if (((BooleanValue) whereExpression.evaluateExpression(i)).getValue()) {
62
                    indexes.addIndex(i);
63
                }
64
            } catch (ClassCastException e) {
65
                throw new IncompatibleTypesException(
66
                    "where expression is not boolean");
67
            }
68
        }
69

    
70
        indexes.indexSetComplete();
71
    }
72

    
73
    /**
74
     * @see com.hardcode.gdbms.engine.data.DataSource#open()
75
     */
76
    public void start() throws DriverException {
77
        source.start();
78
    }
79

    
80
    /**
81
     * @see com.hardcode.gdbms.engine.data.DataSource#close()
82
     */
83
    public void stop() throws DriverException {
84
        source.stop();
85

    
86
        try {
87
            indexes.close();
88
        } catch (IOException e) {
89
            throw new DriverException(e);
90
        }
91
    }
92

    
93
    /**
94
     * @see com.hardcode.gdbms.engine.data.DataSource#getName()
95
     */
96
    public String getName() {
97
        return source.getName();
98
    }
99

    
100
    /**
101
     * @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
102
     */
103
    public int getFieldIndexByName(String fieldName)
104
        throws DriverException, FieldNotFoundException {
105
        return source.getFieldIndexByName(fieldName);
106
    }
107

    
108
    /**
109
     * @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldValue(long, int)
110
     */
111
    public Value getFieldValue(long rowIndex, int fieldId)
112
        throws DriverException {
113
        try {
114
            return source.getFieldValue(indexes.getIndex(rowIndex), fieldId);
115
        } catch (IOException e) {
116
            throw new DriverException(e);
117
        }
118
    }
119

    
120
    /**
121
     * @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldCount()
122
     */
123
    public int getFieldCount() throws DriverException {
124
        return source.getFieldCount();
125
    }
126

    
127
    /**
128
     * @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldName(int)
129
     */
130
    public String getFieldName(int fieldId) throws DriverException {
131
        return source.getFieldName(fieldId);
132
    }
133

    
134
    /**
135
     * @see com.hardcode.gdbms.engine.data.ReadAccess#getRowCount()
136
     */
137
    public long getRowCount() throws DriverException {
138
        return indexes.getIndexCount();
139
    }
140

    
141
        /**
142
         * @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
143
         */
144
        public String getDBMS() {
145
                return source.getDBMS();
146
        }
147

    
148
        /**
149
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
150
         */
151
        public ReadDriver getDriver() {
152
                return null;
153
        }
154
}