statesystem: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HT_IO.java
CommitLineData
a52fde77 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson
a52fde77
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
3de60236 5 *
a52fde77
AM
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
3de60236 10 *
a52fde77
AM
11 *******************************************************************************/
12
e894a508 13package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;
a52fde77
AM
14
15import java.io.File;
16import java.io.FileInputStream;
17import java.io.FileOutputStream;
18import java.io.IOException;
3b7f5abe 19import java.nio.channels.ClosedChannelException;
a52fde77
AM
20import java.nio.channels.FileChannel;
21
49a817d5
MK
22import org.eclipse.tracecompass.internal.statesystem.core.Activator;
23
a52fde77 24/**
360f899e
EB
25 * This class abstracts inputs/outputs of the HistoryTree nodes.
26 *
27 * It contains all the methods and descriptors to handle reading/writing nodes
28 * to the tree-file on disk and all the caching mechanisms.
29 *
49a817d5
MK
30 * This abstraction is mainly for code isolation/clarification purposes. Every
31 * HistoryTree must contain 1 and only 1 HT_IO element.
3de60236 32 *
ffd0aa67 33 * @author Alexandre Montplaisir
3de60236 34 *
a52fde77
AM
35 */
36class HT_IO {
360f899e
EB
37 /* Configuration of the History Tree */
38 private final HTConfig fConfig;
a52fde77
AM
39
40 /* Fields related to the file I/O */
3de60236
AM
41 private final FileInputStream fis;
42 private final FileOutputStream fos;
43 private final FileChannel fcIn;
44 private final FileChannel fcOut;
a52fde77 45
0c1d4577 46 // TODO test/benchmark optimal cache size
49a817d5 47 private static final int CACHE_SIZE = 256;
6993c1ca
EB
48 private final HTNode fNodeCache[] = new HTNode[CACHE_SIZE];
49
a52fde77
AM
50 /**
51 * Standard constructor
3de60236 52 *
360f899e 53 * @param config
49a817d5 54 * The configuration object for the StateHistoryTree
ab604305 55 * @param newFile
360f899e 56 * Flag indicating that the file must be created from scratch
49a817d5 57 *
a52fde77 58 * @throws IOException
360f899e 59 * An exception can be thrown when file cannot be accessed
a52fde77 60 */
8d47cc34 61 public HT_IO(HTConfig config, boolean newFile) throws IOException {
360f899e 62 fConfig = config;
a52fde77 63
360f899e 64 File historyTreeFile = config.getStateFile();
a52fde77 65 if (newFile) {
360f899e 66 boolean success1 = true;
a52fde77
AM
67 /* Create a new empty History Tree file */
68 if (historyTreeFile.exists()) {
ab604305
AM
69 success1 = historyTreeFile.delete();
70 }
cb42195c 71 boolean success2 = historyTreeFile.createNewFile();
ab604305
AM
72 if (!(success1 && success2)) {
73 /* It seems we do not have permission to create the new file */
74 throw new IOException("Cannot create new file at " + //$NON-NLS-1$
75 historyTreeFile.getName());
a52fde77 76 }
a52fde77
AM
77 fis = new FileInputStream(historyTreeFile);
78 fos = new FileOutputStream(historyTreeFile, false);
79 } else {
80 /*
81 * We want to open an existing file, make sure we don't squash the
82 * existing content when opening the fos!
83 */
84 this.fis = new FileInputStream(historyTreeFile);
85 this.fos = new FileOutputStream(historyTreeFile, true);
86 }
87 this.fcIn = fis.getChannel();
88 this.fcOut = fos.getChannel();
89 }
90
a52fde77 91 /**
8d47cc34 92 * Read a node from the file on disk.
3b7f5abe 93 *
8d47cc34
AM
94 * @param seqNumber
95 * The sequence number of the node to read.
96 * @return The object representing the node
3b7f5abe
AM
97 * @throws ClosedChannelException
98 * Usually happens because the file was closed while we were
99 * reading. Instead of using a big reader-writer lock, we'll
100 * just catch this exception.
a52fde77 101 */
8d47cc34 102 public synchronized HTNode readNode(int seqNumber) throws ClosedChannelException {
6993c1ca
EB
103 /* Do a cache lookup */
104 int offset = seqNumber & (CACHE_SIZE - 1);
105 HTNode readNode = fNodeCache[offset];
106 if (readNode != null && readNode.getSequenceNumber() == seqNumber) {
49a817d5 107 return readNode;
6993c1ca
EB
108 }
109
110 /* Lookup on disk */
a52fde77
AM
111 try {
112 seekFCToNodePos(fcIn, seqNumber);
360f899e 113 readNode = HTNode.readNode(fConfig, fcIn);
6993c1ca
EB
114
115 /* Put the node in the cache. */
116 fNodeCache[offset] = readNode;
a52fde77 117 return readNode;
3b7f5abe
AM
118 } catch (ClosedChannelException e) {
119 throw e;
a52fde77 120 } catch (IOException e) {
3b7f5abe 121 /* Other types of IOExceptions shouldn't happen at this point though */
49a817d5 122 Activator.getDefault().logError(e.getMessage(), e);
a52fde77
AM
123 return null;
124 }
125 }
126
e8b7cc14 127 public synchronized void writeNode(HTNode node) {
a52fde77 128 try {
6993c1ca
EB
129 /* Insert the node into the cache. */
130 int seqNumber = node.getSequenceNumber();
131 int offset = seqNumber & (CACHE_SIZE - 1);
132 fNodeCache[offset] = node;
133
a52fde77 134 /* Position ourselves at the start of the node and write it */
6993c1ca 135 seekFCToNodePos(fcOut, seqNumber);
a52fde77
AM
136 node.writeSelf(fcOut);
137 } catch (IOException e) {
138 /* If we were able to open the file, we should be fine now... */
49a817d5 139 Activator.getDefault().logError(e.getMessage(), e);
a52fde77
AM
140 }
141 }
142
e8b7cc14 143 public FileChannel getFcOut() {
a52fde77
AM
144 return this.fcOut;
145 }
146
8d47cc34 147 public FileInputStream supplyATReader(int nodeOffset) {
a52fde77
AM
148 try {
149 /*
150 * Position ourselves at the start of the Mapping section in the
151 * file (which is right after the Blocks)
152 */
360f899e 153 seekFCToNodePos(fcIn, nodeOffset);
a52fde77 154 } catch (IOException e) {
49a817d5 155 Activator.getDefault().logError(e.getMessage(), e);
a52fde77
AM
156 }
157 return fis;
158 }
159
8d47cc34 160 public synchronized void closeFile() {
3de60236
AM
161 try {
162 fis.close();
163 fos.close();
164 } catch (IOException e) {
49a817d5 165 Activator.getDefault().logError(e.getMessage(), e);
3de60236 166 }
1a4205d9
AM
167 }
168
8d47cc34 169 public synchronized void deleteFile() {
1a4205d9 170 closeFile();
3de60236 171
360f899e 172 File historyTreeFile = fConfig.getStateFile();
6993c1ca 173 if (!historyTreeFile.delete()) {
36bf82a2 174 /* We didn't succeed in deleting the file */
49a817d5 175 Activator.getDefault().logError("Failed to delete" + historyTreeFile.getName()); //$NON-NLS-1$
36bf82a2
AM
176 }
177 }
178
a52fde77
AM
179 /**
180 * Seek the given FileChannel to the position corresponding to the node that
181 * has seqNumber
3de60236 182 *
49a817d5
MK
183 * @param fc
184 * the channel to seek
185 * @param seqNumber
186 * the node sequence number to seek the channel to
a52fde77 187 * @throws IOException
49a817d5 188 * If some other I/O error occurs
a52fde77
AM
189 */
190 private void seekFCToNodePos(FileChannel fc, int seqNumber)
191 throws IOException {
a52fde77 192 /*
360f899e 193 * Cast to (long) is needed to make sure the result is a long too and
a52fde77
AM
194 * doesn't get truncated
195 */
360f899e 196 fc.position(HistoryTree.TREE_HEADER_SIZE
49a817d5 197 + ((long) seqNumber) * fConfig.getBlockSize());
a52fde77
AM
198 }
199
200}
This page took 0.081854 seconds and 5 git commands to generate.