Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f755325a94f4d07020cd22eae30ed34be2c3b50 (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2012, 2013 Ericsson
 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *******************************************************************************/

package org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;

/**
 * This class exists mainly for code isolation/clarification purposes. It
 * contains all the methods and descriptors to handle reading/writing to the
 * tree-file on disk and all the caching mechanisms. Every HistoryTree should
 * contain 1 and only 1 HT_IO element.
 *
 * @author alexmont
 *
 */
class HT_IO {

    /* reference to the tree to which this IO-object belongs */
    private final HistoryTree tree;

    /* Fields related to the file I/O */
    private final File historyTreeFile;
    private final FileInputStream fis;
    private final FileOutputStream fos;
    private final FileChannel fcIn;
    private final FileChannel fcOut;

    private final int CACHE_SIZE = 256;
    private final HTNode fNodeCache[] = new HTNode[CACHE_SIZE];

    /**
     * Standard constructor
     *
     * @param tree
     * @param newFile
     *            Are we creating a new file from scratch?
     * @throws IOException
     */
    HT_IO(HistoryTree tree, boolean newFile) throws IOException {
        this.tree = tree;
        historyTreeFile = tree.getConfig().getStateFile();
        boolean success1 = true;

        if (newFile) {
            /* Create a new empty History Tree file */
            if (historyTreeFile.exists()) {
                success1 = historyTreeFile.delete();
            }
            boolean success2 = historyTreeFile.createNewFile();
            if (!(success1 && success2)) {
                /* It seems we do not have permission to create the new file */
                throw new IOException("Cannot create new file at " + //$NON-NLS-1$
                        historyTreeFile.getName());
            }
            fis = new FileInputStream(historyTreeFile);
            fos = new FileOutputStream(historyTreeFile, false);
        } else {
            /*
             * We want to open an existing file, make sure we don't squash the
             * existing content when opening the fos!
             */
            this.fis = new FileInputStream(historyTreeFile);
            this.fos = new FileOutputStream(historyTreeFile, true);
        }
        this.fcIn = fis.getChannel();
        this.fcOut = fos.getChannel();
    }

    /**
     * Generic "read node" method, which checks if the node is in memory first,
     * and if it's not it goes to disk to retrieve it.
     *
     * @param seqNumber
     *            Sequence number of the node we want
     * @return The wanted node in object form
     * @throws ClosedChannelException
     *             If the channel was closed before we could read
     */
    HTNode readNode(int seqNumber) throws ClosedChannelException {
        HTNode node = readNodeFromMemory(seqNumber);
        if (node == null) {
            return readNodeFromDisk(seqNumber);
        }
        return node;
    }

    private HTNode readNodeFromMemory(int seqNumber) {
        for (HTNode node : tree.getLatestBranch()) {
            if (node.getSequenceNumber() == seqNumber) {
                return node;
            }
        }
        return null;
    }

    /**
     * This method here isn't private, if we know for sure the node cannot be in
     * memory it's a bit faster to use this directly (when opening a file from
     * disk for example)
     *
     * @throws ClosedChannelException
     *             Usually happens because the file was closed while we were
     *             reading. Instead of using a big reader-writer lock, we'll
     *             just catch this exception.
     */
    synchronized HTNode readNodeFromDisk(int seqNumber) throws ClosedChannelException {
        /* Do a cache lookup */
        int offset = seqNumber & (CACHE_SIZE - 1);
        HTNode readNode = fNodeCache[offset];
        if (readNode != null && readNode.getSequenceNumber() == seqNumber) {
          return readNode;
        }

        /* Lookup on disk */
        try {
            seekFCToNodePos(fcIn, seqNumber);
            readNode = HTNode.readNode(tree, fcIn);

            /* Put the node in the cache. */
            fNodeCache[offset] = readNode;
            return readNode;
        } catch (ClosedChannelException e) {
            throw e;
        } catch (IOException e) {
            /* Other types of IOExceptions shouldn't happen at this point though */
            e.printStackTrace();
            return null;
        }
    }

    void writeNode(HTNode node) {
        try {
            /* Insert the node into the cache. */
            int seqNumber = node.getSequenceNumber();
            int offset = seqNumber & (CACHE_SIZE - 1);
            fNodeCache[offset] = node;

            /* Position ourselves at the start of the node and write it */
            seekFCToNodePos(fcOut, seqNumber);
            node.writeSelf(fcOut);
        } catch (IOException e) {
            /* If we were able to open the file, we should be fine now... */
            e.printStackTrace();
        }
    }

    FileChannel getFcOut() {
        return this.fcOut;
    }

    FileInputStream supplyATReader() {
        try {
            /*
             * Position ourselves at the start of the Mapping section in the
             * file (which is right after the Blocks)
             */
            seekFCToNodePos(fcIn, tree.getNodeCount());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fis;
    }

    File supplyATWriterFile() {
        return tree.getConfig().getStateFile();
    }

    long supplyATWriterFilePos() {
        return HistoryTree.TREE_HEADER_SIZE
                + ((long) tree.getNodeCount() * tree.getConfig().getBlockSize());
    }

    synchronized void closeFile() {
        try {
            fis.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    synchronized void deleteFile() {
        closeFile();

        if (!historyTreeFile.delete()) {
            /* We didn't succeed in deleting the file */
            //TODO log it?
        }
    }

    /**
     * Seek the given FileChannel to the position corresponding to the node that
     * has seqNumber
     *
     * @param seqNumber
     * @throws IOException
     */
    private void seekFCToNodePos(FileChannel fc, int seqNumber)
            throws IOException {
        fc.position(HistoryTree.TREE_HEADER_SIZE + (long) seqNumber
                * tree.getConfig().getBlockSize());
        /*
         * cast to (long) is needed to make sure the result is a long too and
         * doesn't get truncated
         */
    }

}

Back to the top