Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / indexes / VariableDiskIndexSet.java @ 1956

History | View | Annotate | Download (2.73 KB)

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

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

    
8

    
9
/**
10
 * Implementaci?n de VariableIndexSet que escribe los ?ndices en un fichero
11
 *
12
 * @author Fernando Gonz?lez Cort?s
13
 */
14
public class VariableDiskIndexSet extends DiskIndexSet
15
        implements VariableIndexSet {
16
        private long count = 0;
17

    
18
        /**
19
         * A?ade todos los ?ndices que se pasan como par?metro
20
         *
21
         * @param initialSet Conjunto de ?ndices que se quieren a?adir
22
         *
23
         * @throws IOException Si se produce un fallo al a?adir los ?ndices
24
         */
25
        public void addAll(VariableIndexSet initialSet) throws IOException {
26
                for (long i = 0; i < initialSet.getIndexCount(); i++) {
27
                        addIndex(initialSet.getIndex(i));
28
                }
29

    
30
                count = initialSet.getIndexCount();
31
        }
32

    
33
        /**
34
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#addIndex(long)
35
         */
36
        public void addIndex(long index) throws IOException {
37
                buffer.put((byte) (index >>> 56));
38
                buffer.put((byte) (index >>> 48));
39
                buffer.put((byte) (index >>> 40));
40
                buffer.put((byte) (index >>> 32));
41
                buffer.put((byte) (index >>> 24));
42
                buffer.put((byte) (index >>> 16));
43
                buffer.put((byte) (index >>> 8));
44
                buffer.put((byte) (index >>> 0));
45

    
46
                buffer.flip();
47

    
48
                outputChannel.write(buffer);
49

    
50
                buffer.clear();
51

    
52
                count++;
53
        }
54

    
55
        /**
56
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#getIndex(long)
57
         */
58
        public long getIndex(long nth) throws IOException {
59
                buffer.clear();
60
                inputChannel.read(buffer, nth * 8);
61
                buffer.flip();
62

    
63
                return buffer.getLong();
64
        }
65

    
66
        /**
67
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#getIndexCount()
68
         */
69
        public long getIndexCount() {
70
                return count;
71
        }
72

    
73
        /**
74
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#open(java.io.File)
75
         */
76
        public void open() throws IOException {
77
                file = File.createTempFile("index", "idx");
78
                file.deleteOnExit();
79
                fos = new FileOutputStream(file);
80
                outputChannel = fos.getChannel();
81
        }
82

    
83
        /**
84
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#close()
85
         */
86
        public void close() throws IOException {
87
                inputChannel.close();
88
                fis.close();
89
        }
90

    
91
        /**
92
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#indexSetComplete()
93
         */
94
        public void indexSetComplete() throws IOException {
95
                outputChannel.close();
96
                fos.close();
97
                fis = new FileInputStream(file);
98
                inputChannel = fis.getChannel();
99
        }
100

    
101
        /**
102
         * DOCUMENT ME!
103
         *
104
         * @return DOCUMENT ME!
105
         *
106
         * @throws IOException
107
         *
108
         * @see com.hardcode.gdbms.engine.data.indexes.VariableIndexSet#getIndexes()
109
         */
110
        public long[] getIndexes() throws IOException {
111
                long[] ret = new long[(int) getIndexCount()];
112

    
113
                for (int i = 0; i < ret.length; i++) {
114
                        ret[i] = getIndex(i);
115
                }
116

    
117
                return ret;
118
        }
119
}