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