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