]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/ClusterManager.java
Removed contact application support prints
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / ClusterManager.java
1 package org.simantics.acorn;
2
3 import java.io.IOException;
4 import java.math.BigInteger;
5 import java.nio.file.DirectoryStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.StandardCopyOption;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.concurrent.atomic.AtomicBoolean;
14
15 import org.simantics.acorn.cluster.ClusterImpl;
16 import org.simantics.acorn.exception.AcornAccessVerificationException;
17 import org.simantics.acorn.exception.IllegalAcornStateException;
18 import org.simantics.acorn.exception.InvalidHeadStateException;
19 import org.simantics.acorn.internal.ClusterSupport2;
20 import org.simantics.acorn.lru.ChangeSetInfo;
21 import org.simantics.acorn.lru.ClusterInfo;
22 import org.simantics.acorn.lru.ClusterLRU;
23 import org.simantics.acorn.lru.ClusterStreamChunk;
24 import org.simantics.acorn.lru.FileInfo;
25 import org.simantics.acorn.lru.LRU;
26 import org.simantics.db.ClusterCreator;
27 import org.simantics.db.Database.Session.ClusterIds;
28 import org.simantics.db.Database.Session.ResourceSegment;
29 import org.simantics.db.ServiceLocator;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.impl.ClusterBase;
32 import org.simantics.db.impl.ClusterI;
33 import org.simantics.db.impl.ClusterSupport;
34 import org.simantics.db.procore.cluster.ClusterTraits;
35 import org.simantics.db.service.ClusterSetsSupport;
36 import org.simantics.db.service.ClusterUID;
37 import org.simantics.utils.FileUtils;
38 import org.simantics.utils.threads.logger.ITask;
39 import org.simantics.utils.threads.logger.ThreadLogger;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ClusterManager {
44     
45     final static Logger LOGGER = LoggerFactory.getLogger(ClusterManager.class); 
46
47         private ArrayList<String> currentChanges = new ArrayList<String>();
48
49         public final Path dbFolder;
50         private FileCache fileCache;
51         Path lastSessionDirectory;
52         Path workingDirectory;
53
54         public LRU<String, ClusterStreamChunk> streamLRU;
55         public LRU<Long, ChangeSetInfo> csLRU;
56         public ClusterLRU clusterLRU;
57         public LRU<String, FileInfo> fileLRU;
58
59         public MainState mainState;
60         public HeadState state;
61
62         private long lastSnapshot = System.nanoTime();
63
64         final public ClusterSupport2 support = new ClusterSupport2(this);
65
66         /*
67          * Public interface
68          * 
69          */
70
71         public ClusterManager(Path dbFolder, FileCache fileCache) {
72                 this.dbFolder = dbFolder;
73                 this.fileCache = fileCache;
74         }
75
76         public ArrayList<String> getChanges(long changeSetId) throws AcornAccessVerificationException, IllegalAcornStateException {
77                 ChangeSetInfo info = csLRU.getWithoutMutex(changeSetId);
78                 info.acquireMutex();
79                 try {
80                         info.makeResident();
81                         return info.getCCSIds();
82                 } finally {
83                         info.releaseMutex();
84                 }
85         }
86
87         public ClusterBase getClusterByClusterKey(int clusterKey) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
88                 return clusterLRU.getClusterByClusterKey(clusterKey);
89         }
90         
91         public ClusterBase getClusterByClusterUIDOrMake(ClusterUID clusterUID) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
92                 return clusterLRU.getClusterByClusterUIDOrMake(clusterUID);
93         }
94
95         public ClusterImpl getClusterByClusterUIDOrMakeProxy(ClusterUID clusterUID) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
96                 return clusterLRU.getClusterByClusterUIDOrMakeProxy(clusterUID);
97         }
98
99         public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) throws AcornAccessVerificationException {
100                 return clusterLRU.getClusterKeyByClusterUIDOrMake(clusterUID);
101         }
102
103         public int getClusterKeyByClusterUIDOrMakeWithoutMutex(ClusterUID clusterUID) throws IllegalAcornStateException, AcornAccessVerificationException {
104                 return clusterLRU.getClusterKeyByClusterUIDOrMakeWithoutMutex(clusterUID);
105         }
106
107         public int getClusterKeyByUID(long id1, long id2) throws DatabaseException, IllegalAcornStateException {
108                 return clusterLRU.getClusterKeyByUIDWithoutMutex(id1, id2);
109         }
110         
111         public <T extends ClusterI> T getClusterProxyByResourceKey(int resourceKey) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
112                 return clusterLRU.getClusterProxyByResourceKey(resourceKey);
113         }
114
115         public ClusterUID getClusterUIDByResourceKey(int resourceKey) throws DatabaseException, AcornAccessVerificationException {
116                 return clusterLRU.getClusterUIDByResourceKey(resourceKey);
117         }
118
119         public ClusterUID getClusterUIDByResourceKeyWithoutMutex(int resourceKey) throws DatabaseException, IllegalAcornStateException, AcornAccessVerificationException {
120                 return clusterLRU.getClusterUIDByResourceKeyWithoutMutex(resourceKey);
121         }
122
123         /*
124          * Private implementation
125          * 
126          */
127
128         private static long countFiles(Path directory) throws IOException {
129                 try (DirectoryStream<Path> ds = Files.newDirectoryStream(directory)) {
130                         int count = 0;
131                         for (@SuppressWarnings("unused") Path p : ds)
132                                 ++count;
133                         return count;
134                 }
135         }
136
137         // Add check to make sure if it safe to make snapshot (used with cancel which is not yet supported and may cause corrupted head.state writing)
138         private AtomicBoolean safeToMakeSnapshot = new AtomicBoolean(true);
139         private IllegalAcornStateException cause;
140         
141         public synchronized void purge(ServiceLocator locator) throws IllegalAcornStateException {
142                 
143             try {
144
145                 // Schedule writing of all data to disk
146                 refreshHeadState();
147                 // Wait for files to be written
148                 synchronizeWorkingDirectory();
149                 
150                 String currentDir = workingDirectory.getFileName().toString();
151                 Path baseline = workingDirectory.resolveSibling(currentDir + "_baseline");
152                 
153                 Files.createDirectories(baseline);
154                 
155                 for(String clusterKey : state.clusters) {
156                         String[] parts1 = clusterKey.split("#");
157                         String[] parts = parts1[0].split("\\.");
158                         String readDirName = parts1[1];
159                         if(!readDirName.equals(currentDir)) {
160                                 String fileName = parts[0] + "." + parts[1] + ".cluster";
161                                 Path from = dbFolder.resolve(readDirName).resolve(fileName);
162                                 Path to = baseline.resolve(fileName);
163                                 LOGGER.info("purge copies " + from + "  => " + to);
164                                 Files.copy(from, to, StandardCopyOption.COPY_ATTRIBUTES);
165                                 long first = new BigInteger(parts[0], 16).longValue();
166                                 long second = new BigInteger(parts[1], 16).longValue();
167                                 ClusterUID uuid = ClusterUID.make(first, second);
168                                 ClusterInfo info = clusterLRU.getWithoutMutex(uuid);
169                                 info.moveTo(baseline);
170                         }
171                 }
172                 
173                 for (String fileKey : state.files) {
174                         String[] parts = fileKey.split("#");
175                         String readDirName = parts[1];
176                         if(!readDirName.equals(currentDir)) {
177                                 String fileName = parts[0] + ".extFile";
178                                 Path from = dbFolder.resolve(readDirName).resolve(fileName);
179                                 Path to = baseline.resolve(fileName);
180                                 LOGGER.info("purge copies " + from + "  => " + to);
181                                 Files.copy(from, to, StandardCopyOption.COPY_ATTRIBUTES);
182                                 FileInfo info = fileLRU.getWithoutMutex(parts[0]);
183                                 info.moveTo(baseline);
184                         }
185                 }
186                 
187                 for (String fileKey : state.stream) {
188                         String[] parts = fileKey.split("#");
189                         String readDirName = parts[1];
190                         if(!readDirName.equals(currentDir)) {
191                                 ClusterStreamChunk chunk = streamLRU.purge(parts[0]);
192                                 LOGGER.info("purge removes " + chunk);
193                         }
194                 }
195                 
196                 // Change sets
197                 for (String fileKey : state.cs) {
198                         String[] parts = fileKey.split("#");
199                         String readDirName = parts[1];
200                         if(!readDirName.equals(currentDir)) {
201                                 Long revisionId = Long.parseLong(parts[0]);
202                                 ChangeSetInfo info = csLRU.purge(revisionId);
203                                 LOGGER.info("purge removes " + info);
204                         }
205 //                      Path readDir = dbFolder.resolve(parts[1]);
206 //                      Long revisionId = Long.parseLong(parts[0]);
207 //                      int offset = Integer.parseInt(parts[2]);
208 //                      int length = Integer.parseInt(parts[3]);
209 //                      ChangeSetInfo info = new ChangeSetInfo(csLRU, readDir, revisionId, offset, length);
210 //                      csLRU.map(info);
211                 }               
212                 
213                 state.tailChangeSetId = state.headChangeSetId;
214                 
215                 makeSnapshot(locator, true);
216                 
217                 Files.walk(dbFolder, 1).filter(Files::isDirectory).forEach(f -> tryPurgeDirectory(f));
218
219             } catch (IllegalAcornStateException e) {
220                 notSafeToMakeSnapshot(e);
221                 throw e;
222             } catch (IOException e) {
223                 IllegalAcornStateException e1 = new IllegalAcornStateException(e);
224                 notSafeToMakeSnapshot(e1);
225                 throw e1;
226             } catch (AcornAccessVerificationException e) {
227                 IllegalAcornStateException e1 = new IllegalAcornStateException(e);
228                 notSafeToMakeSnapshot(e1);
229                 throw e1;
230                 }
231         
232         }
233         
234         void tryPurgeDirectory(Path f) {
235                 
236                 
237                 LOGGER.info("purge deletes " + f);
238
239                 String currentDir = f.getFileName().toString();
240                 if(currentDir.endsWith("db"))
241                         return;
242
243                 if(currentDir.endsWith("_baseline"))
244                         currentDir = currentDir.replace("_baseline", "");
245
246                 int ordinal = Integer.parseInt(currentDir);
247                 if(ordinal < mainState.headDir - 1) {
248                         LOGGER.info("purge deletes " + f);
249                         FileUtils.deleteDir(f.toFile());
250                 }
251                 
252         }
253
254         public synchronized boolean makeSnapshot(ServiceLocator locator, boolean fullSave) throws IllegalAcornStateException {
255             try {
256             if (!safeToMakeSnapshot.get())
257                 throw cause;
258                 // Maximum autosave frequency is per 60s
259                 if(!fullSave && System.nanoTime() - lastSnapshot < 10*1000000000L) {
260     //              LOGGER.info("lastSnapshot too early");
261                     return false;
262                 }
263     
264                 // Cluster files are always there 
265                 // Nothing has been written => no need to do anything
266                 long amountOfFiles = countFiles(workingDirectory);
267                 if(!fullSave && amountOfFiles == 0) {
268                         //LOGGER.info("makeSnapshot: " + amountOfFiles + " files, skipping snapshot");
269                     return false;
270                 }
271     
272                 LOGGER.info("makeSnapshot: start with " + amountOfFiles + " files");
273     
274                 // Schedule writing of all data to disk
275             refreshHeadState();
276     
277                 // Wait for all files to be written
278                 clusterLRU.shutdown();
279                 fileLRU.shutdown();
280                 streamLRU.shutdown();
281                 csLRU.shutdown();
282                 
283                 // Lets check if it is still safe to make a snapshot
284                 if (!safeToMakeSnapshot.get())
285                     throw cause;
286                 
287                 ClusterSetsSupport cssi = locator.getService(ClusterSetsSupport.class); 
288                 cssi.save();
289
290                 persistHeadState();
291
292                 if (LOGGER.isInfoEnabled()) {
293                         amountOfFiles = countFiles(workingDirectory);
294                         LOGGER.info(" -finished: amount of files is {}", amountOfFiles);
295                 }
296
297                 workingDirectory = dbFolder.resolve(Integer.toString(mainState.headDir));
298                 Files.createDirectories(workingDirectory);
299     
300                 cssi.updateWriteDirectory(workingDirectory);
301     
302                 clusterLRU.setWriteDir(workingDirectory);
303                 fileLRU.setWriteDir(workingDirectory);
304                 streamLRU.setWriteDir(workingDirectory);
305                 csLRU.setWriteDir(workingDirectory);
306     
307                 clusterLRU.resume();
308                 fileLRU.resume();
309                 streamLRU.resume();
310                 csLRU.resume();
311     
312                 lastSnapshot = System.nanoTime();
313                 
314                 return true;
315             } catch (IllegalAcornStateException e) {
316                 notSafeToMakeSnapshot(e);
317                 throw e;
318             } catch (IOException e) {
319                 IllegalAcornStateException e1 = new IllegalAcornStateException(e);
320                 notSafeToMakeSnapshot(e1);
321                 throw e1;
322             }
323         }
324         
325         private void refreshHeadState() throws IOException, IllegalAcornStateException {
326                 state.clusters.clear();
327                 state.files.clear();
328                 state.stream.clear();
329                 state.cs.clear();
330
331                 clusterLRU.persist(state.clusters);
332                 fileLRU.persist(state.files);
333                 streamLRU.persist(state.stream);
334                 csLRU.persist(state.cs);
335         }
336         
337         private void synchronizeWorkingDirectory() throws IOException {
338                 // Sync current working directory
339                 Files.walk(workingDirectory, 1).filter(Files::isRegularFile).forEach(FileIO::uncheckedSyncPath);
340         }
341         
342         private void persistHeadState() throws IOException {
343                 synchronizeWorkingDirectory();
344                 state.save(workingDirectory);
345                 mainState.headDir++;
346         }
347
348         
349 //      public void save() throws IOException {
350 //
351 //              refreshHeadState();
352 //              
353 //              clusterLRU.shutdown();
354 //              fileLRU.shutdown();
355 //              streamLRU.shutdown();
356 //              csLRU.shutdown();
357 //
358 //              persistHeadState();
359 //
360 //              mainState.save(getBaseDirectory());
361
362 //              try {
363 //                      ThreadLogVisualizer visualizer = new ThreadLogVisualizer();
364 //                      visualizer.read(new DataInputStream(new FileInputStream(
365 //                                      ThreadLogger.LOG_FILE)));
366 //                      visualizer.visualize3(new PrintStream(ThreadLogger.LOG_FILE
367 //                                      + ".svg"));
368 //              } catch (FileNotFoundException e) {
369 //                      // TODO Auto-generated catch block
370 //                      e.printStackTrace();
371 //              }
372
373                 // LOGGER.info("-- load statistics --");
374                 // for(Pair<ClusterUID, Integer> entry :
375                 // CollectionUtils.valueSortedEntries(histogram)) {
376                 // LOGGER.info(" " + entry.second + " " + entry.first);
377                 // }
378
379 //      }
380         
381         private void acquireAll() throws IllegalAcornStateException {
382                 clusterLRU.acquireMutex();
383                 fileLRU.acquireMutex();
384                 streamLRU.acquireMutex();
385                 csLRU.acquireMutex();
386         }
387         
388         private void releaseAll() {
389                 csLRU.releaseMutex();
390                 streamLRU.releaseMutex();
391                 fileLRU.releaseMutex();
392                 clusterLRU.releaseMutex();
393         }
394
395         private AtomicBoolean rollback = new AtomicBoolean(false);
396         
397         boolean rolledback() {
398             return rollback.get();
399         }
400         
401         public void load() throws IOException {
402
403                 // Main state
404                 mainState = MainState.load(dbFolder, () -> rollback.set(true));
405
406                 lastSessionDirectory = dbFolder.resolve(Integer.toString(mainState.headDir - 1));
407                 
408                 // Head State
409                 if (mainState.isInitial()) {
410                         state = new HeadState();
411                 } else {
412                         try {
413                     state = HeadState.load(lastSessionDirectory);
414                 } catch (InvalidHeadStateException e) {
415                     // For backwards compatibility only!
416                     Throwable cause = e.getCause();
417                     if (cause instanceof Throwable) {
418                         try {
419                             org.simantics.db.javacore.HeadState oldState = org.simantics.db.javacore.HeadState.load(lastSessionDirectory);
420                             
421                             HeadState newState = new HeadState();
422                             newState.clusters = oldState.clusters;
423                             newState.cs = oldState.cs;
424                             newState.files = oldState.files;
425                             newState.stream = oldState.stream;
426                             newState.headChangeSetId = oldState.headChangeSetId;
427                             newState.reservedIds = oldState.reservedIds;
428                             newState.transactionId = oldState.transactionId;
429                             state = newState;
430                         } catch (InvalidHeadStateException e1) {
431                             throw new IOException("Could not load HeadState due to corruption", e1);
432                         }
433                     } else {
434                         // This should never happen as MainState.load() checks the integrity
435                         // of head.state files and rolls back in cases of corruption until a
436                         // consistent state is found (could be case 0 - initial db state)
437                         // IF this does happen something is completely wrong
438                         throw new IOException("Could not load HeadState due to corruption", e);
439                     }
440                 }
441                 }
442                 try {
443                 workingDirectory = dbFolder.resolve(Integer.toString(mainState.headDir));
444                 Files.createDirectories(workingDirectory);
445     
446                 csLRU = new LRU<Long, ChangeSetInfo>(this, "Change Set", workingDirectory);
447                 streamLRU = new LRU<String, ClusterStreamChunk>(this, "Cluster Stream", workingDirectory);
448                 clusterLRU = new ClusterLRU(this, "Cluster", workingDirectory);
449                 fileLRU = new LRU<String, FileInfo>(this, "External Value", workingDirectory);
450     
451                 acquireAll();
452                 
453                 // Clusters
454                 for (String clusterKey : state.clusters) {
455                         String[] parts1 = clusterKey.split("#");
456                         String[] parts = parts1[0].split("\\.");
457                         long first = new BigInteger(parts[0], 16).longValue();
458                         long second = new BigInteger(parts[1], 16).longValue();
459                         ClusterUID uuid = ClusterUID.make(first, second);
460                         Path readDir = dbFolder.resolve(parts1[1]);
461                         int offset = Integer.parseInt(parts1[2]);
462                         int length = Integer.parseInt(parts1[3]);
463                         clusterLRU.map(new ClusterInfo(this, clusterLRU, readDir, uuid, offset, length));
464                 }
465                 // Files
466                 for (String fileKey : state.files) {
467     //                  LOGGER.info("loadFile: " + fileKey);
468                         String[] parts = fileKey.split("#");
469                         Path readDir = dbFolder.resolve(parts[1]);
470                         int offset = Integer.parseInt(parts[2]);
471                         int length = Integer.parseInt(parts[3]);
472                         FileInfo info = new FileInfo(fileLRU, fileCache, readDir, parts[0], offset, length);
473                         fileLRU.map(info);
474                 }
475                 // Update chunks
476                 for (String fileKey : state.stream) {
477     //                  LOGGER.info("loadStream: " + fileKey);
478                         String[] parts = fileKey.split("#");
479                         Path readDir = dbFolder.resolve(parts[1]);
480                         int offset = Integer.parseInt(parts[2]);
481                         int length = Integer.parseInt(parts[3]);
482                         ClusterStreamChunk info = new ClusterStreamChunk(this,
483                                         streamLRU, readDir, parts[0], offset, length);
484                         streamLRU.map(info);
485                 }
486                 // Change sets
487                 for (String fileKey : state.cs) {
488                         String[] parts = fileKey.split("#");
489                         Path readDir = dbFolder.resolve(parts[1]);
490                         Long revisionId = Long.parseLong(parts[0]);
491                         int offset = Integer.parseInt(parts[2]);
492                         int length = Integer.parseInt(parts[3]);
493                         ChangeSetInfo info = new ChangeSetInfo(csLRU, fileCache, readDir, revisionId, offset, length);
494                         csLRU.map(info);
495                 }
496                 
497                 releaseAll();
498                 } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
499                     // ROLLBACK ONE DIR UNTIL WE ARE FINE!
500                     throw new IOException(e);
501                 }
502         }
503
504         public <T> T clone(ClusterUID uid, ClusterCreator creator) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException, IOException {
505                 
506                 clusterLRU.ensureUpdates(uid);
507                 
508                 ClusterInfo info = clusterLRU.getWithoutMutex(uid);
509                 return info.clone(uid, creator);
510         }
511
512         //private int loadCounter = 0;
513
514         public static void startLog(String msg) {
515                 tasks.put(msg, ThreadLogger.getInstance().begin(msg));
516         }
517
518         public static void endLog(String msg) {
519                 ITask task = tasks.get(msg);
520                 if (task != null)
521                         task.finish();
522         }
523
524         static Map<String, ITask> tasks = new HashMap<String, ITask>();
525
526         public void update(ClusterUID uid, ClusterImpl clu) throws AcornAccessVerificationException, IllegalAcornStateException {
527                 ClusterInfo info = clusterLRU.getWithoutMutex(uid);
528                 info.acquireMutex();
529                 try {
530                         info.update(clu);
531                 } finally {
532                         info.releaseMutex();
533                 }
534         }
535
536         public long getClusterIdOrCreate(ClusterUID clusterUID) {
537                 return 1;
538         }
539
540         public int getResourceKey(ClusterUID uid, int index) throws AcornAccessVerificationException {
541                 return clusterLRU.getResourceKey(uid, index);
542         }
543
544         public int getResourceKeyWitoutMutex(ClusterUID uid, int index) throws IllegalAcornStateException {
545                 return clusterLRU.getResourceKeyWithoutMutex(uid, index);
546         }
547
548         public ClusterIds getClusterIds() throws IllegalAcornStateException {
549                 clusterLRU.acquireMutex();
550
551                 try {
552                         Collection<ClusterInfo> infos = clusterLRU.values();
553                         final int status = infos.size();
554                         final long[] firsts = new long[status];
555                         final long[] seconds = new long[status];
556
557                         int index = 0;
558                         for (ClusterInfo info : infos) {
559                                 firsts[index] = 0;
560                                 seconds[index] = info.getKey().second;
561                                 index++;
562                         }
563
564                         return new ClusterIds() {
565
566                                 @Override
567                                 public int getStatus() {
568                                         return status;
569                                 }
570
571                                 @Override
572                                 public long[] getFirst() {
573                                         return firsts;
574                                 }
575
576                                 @Override
577                                 public long[] getSecond() {
578                                         return seconds;
579                                 }
580
581                         };
582
583                 } catch (Throwable t) {
584                         throw new IllegalAcornStateException(t);
585                 } finally {
586                         clusterLRU.releaseMutex();
587                 }
588         }
589
590         public void addIntoCurrentChangeSet(String ccs) throws IllegalAcornStateException {
591                 csLRU.acquireMutex();
592
593                 try {
594                         currentChanges.add(ccs);
595                 } catch (Throwable t) {
596                         throw new IllegalAcornStateException(t);
597                 } finally {
598                         csLRU.releaseMutex();
599                 }
600         }
601
602         public void commitChangeSet(long changeSetId, byte[] data) throws IllegalAcornStateException {
603                 csLRU.acquireMutex();
604                 try {
605                         ArrayList<String> csids = new ArrayList<String>(currentChanges);
606                         currentChanges = new ArrayList<String>();
607                         new ChangeSetInfo(csLRU, fileCache, changeSetId, data, csids);
608                 } catch (Throwable t) {
609                         throw new IllegalAcornStateException(t);
610                 } finally {
611                         csLRU.releaseMutex();
612                 }
613         }
614
615         public byte[] getMetadata(long changeSetId) throws AcornAccessVerificationException, IllegalAcornStateException {
616                 
617                 ChangeSetInfo info = csLRU.getWithoutMutex(changeSetId);
618                 if (info == null) return null;
619         info.acquireMutex();
620         try {
621             return info.getMetadataBytes();
622         } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
623             throw e;
624         } catch (Throwable t) {
625                         throw new IllegalAcornStateException(t);
626                 } finally {
627                         info.releaseMutex();
628                 }
629         }
630
631         public byte[] getResourceFile(final byte[] clusterUID, final int resourceIndex) throws AcornAccessVerificationException, IllegalAcornStateException {
632
633                 ClusterUID uid = ClusterUID.make(clusterUID, 0);
634                 String key = uid.toString() + "_" + resourceIndex;
635                 FileInfo info = fileLRU.getWithoutMutex(key);
636                 if(info == null) return null;
637                 info.acquireMutex();
638                 try {
639                         return info.getResourceFile();
640                 } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
641                     throw e;
642                 } catch (Throwable t) {
643                         throw new IllegalAcornStateException(t);
644                 } finally {
645                         info.releaseMutex();
646                 }
647         }
648
649         public ResourceSegment getResourceSegment(final byte[] clusterUID, final int resourceIndex, final long segmentOffset, short segmentSize) throws AcornAccessVerificationException, IllegalAcornStateException {
650                 ClusterUID uid = ClusterUID.make(clusterUID, 0);
651
652                 String key = uid.toString() + "_" + resourceIndex;
653                 FileInfo info = fileLRU.getWithoutMutex(key);
654                 if(info == null) return null;
655                 info.acquireMutex();
656                 try {
657                         return info.getResourceSegment(clusterUID, resourceIndex, segmentOffset, segmentSize);
658                 } catch (Throwable t) {
659                         throw new IllegalAcornStateException(t);
660                 } finally {
661                         info.releaseMutex();
662                 }
663         }
664
665         public void modiFileEx(ClusterUID uid, int resourceKey, long offset, long size, byte[] bytes, long pos, ClusterSupport support) throws IllegalAcornStateException {
666                 try {
667                         String key = uid.toString() + "_" + ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
668
669                         FileInfo info = null;
670                         fileLRU.acquireMutex();
671                         try {
672                                 info = fileLRU.get(key);
673                                 if (info == null) {
674                                         info = new FileInfo(fileLRU, fileCache, key, (int) (offset + size));
675                                 }
676                         } catch (Throwable t) {
677                                 throw new IllegalAcornStateException(t);
678                         } finally {
679                                 fileLRU.releaseMutex();
680                         }
681                         
682                         info.acquireMutex();
683                         try {
684                                 info.updateData(bytes, offset, pos, size);
685                         } catch (Throwable t) {
686                                 throw new IllegalAcornStateException(t);
687                         } finally {
688                                 info.releaseMutex();
689                         }
690                 } catch (DatabaseException e) {
691                         e.printStackTrace();
692                 }
693         }
694
695     public void shutdown() {
696         clusterLRU.shutdown();
697         fileLRU.shutdown();
698         streamLRU.shutdown();
699         csLRU.shutdown();
700     }
701
702     public void notSafeToMakeSnapshot(IllegalAcornStateException t) {
703         this.safeToMakeSnapshot.compareAndSet(true, false);
704         this.cause = t;
705     }
706
707     public long getTailChangeSetId() {
708         return state.tailChangeSetId;
709     }
710
711     public FileCache getFileCache() {
712         return fileCache;
713     }
714
715 }