summaryrefslogtreecommitdiffstats
path: root/glusterfs-hadoop/0.20.2/src/main/java/org/apache/hadoop/fs/glusterfs/GlusterFUSEInputStream.java
blob: e92237aeea20dfe2b44b1be0e8cbb664816e2a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 *
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of GlusterFS.
 *
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

package org.apache.hadoop.fs.glusterfs;

import java.io.*;
import java.util.TreeMap;

import org.apache.hadoop.fs.FSInputStream;
import org.apache.hadoop.fs.FileSystem;


public class GlusterFUSEInputStream extends FSInputStream {
        File                                  f;
        boolean                               lastActive;
        long                                  pos;
        boolean                               closed;
        String                                thisHost;
        RandomAccessFile                      fuseInputStream;
        RandomAccessFile                      fsInputStream;
        GlusterFSBrickClass                   thisBrick;
        int                                   nodeLocation;
        TreeMap<Integer, GlusterFSBrickClass> hnts;

        public GlusterFUSEInputStream (File f, TreeMap<Integer, GlusterFSBrickClass> hnts,
                                       String hostname) throws IOException {
                this.f = f;
                this.pos = 0;
                this.closed = false;
                this.hnts = hnts;
                this.thisHost = hostname;
                this.fsInputStream = null;
                this.fuseInputStream = new RandomAccessFile(f.getPath(), "r");

                this.lastActive = true; // true == FUSE, false == backed file

                String directFilePath = null;
                if (this.hnts != null) {
                        directFilePath = findLocalFile(f.getPath(), this.hnts);
                        if (directFilePath != null) {
                                this.fsInputStream = new RandomAccessFile(directFilePath, "r");
                                this.lastActive = !this.lastActive;
                        }
                }
        }

        public String findLocalFile (String path, TreeMap<Integer, GlusterFSBrickClass> hnts) {
                int i = 0;
                String actFilePath = null;
                GlusterFSBrickClass gfsBrick = null;

                gfsBrick = hnts.get(0);

                /* do a linear search for the matching host not worrying
                   about file stripes */
                for (i = 0; i < hnts.size(); i++) {
                        gfsBrick = hnts.get(i);
                        actFilePath = gfsBrick.brickIsLocal(this.thisHost);
                        if (actFilePath != null) {
                                this.thisBrick = gfsBrick;
                                this.nodeLocation = i;
                                break;
                        }
                }

                return actFilePath;
        }

        public long getPos () throws IOException {
                return pos;
        }

        public synchronized int available () throws IOException {
                return (int) ((f.length()) - getPos());
        }

        public void seek (long pos) throws IOException {
                fuseInputStream.seek(pos);
                if (fsInputStream != null)
                        fsInputStream.seek(pos);
        }

        public boolean seekToNewSource (long pos) throws IOException {
                return false;
        }

        public RandomAccessFile chooseStream (long start, int[] nlen)
                throws IOException {
                GlusterFSBrickClass gfsBrick = null;
                RandomAccessFile in = fuseInputStream;
                boolean oldActiveStream = lastActive;
                lastActive = true;

                if ((hnts != null) && (fsInputStream != null)) {
                        gfsBrick = hnts.get(0);
                        if (!gfsBrick.isChunked()) {
                                in = fsInputStream;
                                lastActive = false;
                        } else {
                                // find the current location in the tree and the amount of data it can serve
                                int[] nodeInTree = thisBrick.getBrickNumberInTree(start, nlen[0]);

                                // does this node hold the byte ranges we have been requested for ?
                                if ((nodeInTree[2] != 0) && thisBrick.brickHasFilePart(nodeInTree[0], nodeLocation)) {
                                        in = fsInputStream;
                                        nlen[0] = nodeInTree[2]; // the amount of data that can be read from the stripe
                                        lastActive = false;
                                }
                        }
                }

                return in;
        }

        public synchronized int read () throws IOException {
                int byteRead = 0;
                RandomAccessFile in = null;

                if (closed)
                        throw new IOException("Stream Closed.");

                int[] nlen = { 1 };

                in = chooseStream(getPos(), nlen);

                byteRead = in.read();
                if (byteRead >= 0) {
                        pos++;
                        syncStreams(1);
                }

                return byteRead;
        }

        public synchronized int read (byte buff[], int off, int len) throws
                IOException {
                int result = 0;
                RandomAccessFile in = null;

                if (closed)
                        throw new IOException("Stream Closed.");

                int[] nlen = {len}; // hack to make len mutable
                in = chooseStream(pos, nlen);

                result = in.read(buff, off, nlen[0]);
                if (result > 0) {
                        pos += result;
                        syncStreams(result);
                }

                return result;
        }

        /**
         * TODO: use seek() insted of skipBytes(); skipBytes does I/O
         */
        public void syncStreams (int bytes) throws IOException {
                if ((hnts != null) && (hnts.get(0).isChunked()) && (fsInputStream != null))
                        if (!this.lastActive)
                                fuseInputStream.skipBytes(bytes);
                        else
                                fsInputStream.skipBytes(bytes);
        }

        public synchronized void close () throws IOException {
                if (closed)
                        throw new IOException("Stream closed.");

                super.close();
                if (fsInputStream != null)
                        fsInputStream.close();
                fuseInputStream.close();

                closed = true;
        }

        // Not supported - mark () and reset ()

        public boolean markSupported () {
                return false;
        }

        public void mark (int readLimit) {}

        public void reset () throws IOException {
                throw new IOException("Mark/Reset not supported.");
        }
}