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