Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / file / FileDataWareImpl.java @ 5568

History | View | Annotate | Download (4.9 KB)

1
package com.hardcode.gdbms.engine.data.file;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7

    
8
import com.hardcode.gdbms.engine.data.db.FakeTransactionSupport;
9
import com.hardcode.gdbms.engine.data.driver.DriverException;
10
import com.hardcode.gdbms.engine.data.driver.FileDriver;
11
import com.hardcode.gdbms.engine.data.edition.EditionInfo;
12
import com.hardcode.gdbms.engine.data.edition.PKTable;
13
import com.hardcode.gdbms.engine.values.Value;
14
import com.hardcode.gdbms.engine.values.ValueCollection;
15
import com.hardcode.gdbms.engine.values.ValueFactory;
16

    
17
/**
18
 * @author Fernando Gonz?lez Cort?s
19
 */
20
public class FileDataWareImpl extends FileDataSourceAdapter implements
21
        FileDataWare {
22

    
23
    private FakeTransactionSupport ftSupport = new FakeTransactionSupport(this);
24

    
25
    public void start() throws DriverException {
26
        throw new RuntimeException("Invoke beginTrans in a DataWare");
27
    }
28

    
29
    public void stop() throws DriverException {
30
        throw new RuntimeException(
31
                "Invoke commitTrans/rollBackTrans in a DataWare");
32
    }
33

    
34
    public void beginTrans() throws DriverException {
35
        super.start();
36
        ftSupport.beginTrans();
37
    }
38

    
39
    public void commitTrans() throws DriverException {
40
        ((FileDriver)getDriver()).writeFile(this);
41
        super.stop();
42
        ftSupport.commitTrans();
43
    }
44

    
45
    public void copy(File in, File out) throws IOException {
46
        FileInputStream fis = new FileInputStream(in);
47
        FileOutputStream fos = new FileOutputStream(out);
48
        byte[] buf = new byte[1024];
49
        int i = 0;
50
        while ((i = fis.read(buf)) != -1) {
51
            fos.write(buf, 0, i);
52
        }
53
        fis.close();
54
        fos.close();
55
    }
56

    
57
    public void deleteRow(long rowId) throws DriverException {
58
        ftSupport.deleteRow(rowId);
59
    }
60

    
61
    public void insertEmptyRow(ValueCollection pk) throws DriverException {
62
        ftSupport.insertEmptyRow(pk);
63
    }
64

    
65
    public void insertFilledRow(Value[] values) throws DriverException {
66
        ftSupport.insertFilledRow(values);
67
    }
68

    
69
    public void rollBackTrans() throws DriverException {
70
        super.stop();
71
        ftSupport.rollBackTrans();
72
    }
73

    
74
    public void setFieldValue(long row, int fieldId, Value value)
75
            throws DriverException {
76
        ValueCollection pkValue = null;
77
        Value[] originalRow = null;
78
        if (row < super.getRowCount()) {
79
            pkValue = getOriginalPKValue(row);
80
            originalRow = getOriginalRow(row);
81
        }
82
        ftSupport.setFieldValue(row, fieldId, value, pkValue, originalRow);
83
    }
84

    
85
    /**
86
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
87
     *      int)
88
     */
89
    public Value getFieldValue(long rowIndex, int fieldId)
90
            throws DriverException {
91
        // If there is no transaction
92
        if (!ftSupport.isTransaction()) {
93
            return super.getFieldValue(rowIndex, fieldId);
94
        }
95

    
96
        // get where's the row
97
        EditionInfo fip = ftSupport.getPKTable().getIndexLocation(
98
                (int) rowIndex);
99
        int flag = fip.getFlag();
100

    
101
        // get the value
102
        if ((flag == PKTable.ADDED) || (flag == PKTable.MODIFIED)) {
103
            return ftSupport.getInternalBuffer().getFieldValue(fip.getIndex(),
104
                    fieldId);
105
        } else {
106
            return super.getFieldValue(fip.getIndex(), fieldId);
107
        }
108
    }
109

    
110
    /**
111
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
112
     */
113
    public long getRowCount() throws DriverException {
114
        if (ftSupport.isTransaction()) {
115
            // If there is a transaction
116
            return ftSupport.getPKTable().getPKCount();
117
        } else {
118
            // If there's not
119
            return super.getRowCount();
120
        }
121
    }
122

    
123
    /**
124
     * Gets the value of the original row
125
     * 
126
     * @param rowIndex
127
     *            index of the row to be retrieved
128
     * 
129
     * @return Row values
130
     * 
131
     * @throws DriverException
132
     *             if the operation fails
133
     */
134
    private Value[] getOriginalRow(long rowIndex) throws DriverException {
135
        Value[] ret = new Value[getFieldCount()];
136

    
137
        for (int i = 0; i < ret.length; i++) {
138
            ret[i] = super.getFieldValue(rowIndex, i);
139
        }
140

    
141
        return ret;
142
    }
143

    
144
    /**
145
     * Gets the primary key value in the original DataSource
146
     * 
147
     * @param rowIndex
148
     *            index of the row to be read
149
     * 
150
     * @return PK
151
     * 
152
     * @throws DriverException
153
     *             If the operation fails
154
     */
155
    private ValueCollection getOriginalPKValue(long rowIndex)
156
            throws DriverException {
157
        int[] fieldsId = getPrimaryKeys();
158
        Value[] pks = new Value[fieldsId.length];
159

    
160
        for (int i = 0; i < pks.length; i++) {
161
            pks[i] = super.getFieldValue(rowIndex, fieldsId[i]);
162
        }
163

    
164
        return ValueFactory.createValue(pks);
165
    }
166
}