1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.ifs2;
20
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.Iterator;
25
26 import org.mycore.common.MCRException;
27
28 import com.google.common.cache.CacheBuilder;
29 import com.google.common.cache.CacheLoader;
30 import com.google.common.cache.LoadingCache;
31
32
33
34
35
36
37
38
39
40
41
42 public class MCRFileStore extends MCRStore {
43
44 private final MCRFileStore thisInstance;
45
46
47 LoadingCache<Integer, MCRFileCollection> collectionLoadingCache = CacheBuilder.newBuilder()
48 .weakValues()
49 .build(new CacheLoader<Integer, MCRFileCollection>() {
50 @Override
51 public MCRFileCollection load(Integer key) throws Exception {
52 return new MCRFileCollection(thisInstance, key);
53 }
54 });
55
56 public MCRFileStore() {
57 super();
58 thisInstance = this;
59 }
60
61
62
63
64
65
66
67 public MCRFileCollection create() throws IOException {
68 int id = getNextFreeID();
69 return create(id);
70 }
71
72
73
74
75
76
77
78
79
80
81 public MCRFileCollection create(int id) throws IOException {
82 Path path = getSlot(id);
83 if (Files.exists(path)) {
84 String msg = "FileCollection with ID " + id + " already exists";
85 throw new MCRException(msg);
86 }
87 return collectionLoadingCache.getUnchecked(id);
88 }
89
90 @Override
91 public void delete(int id) throws IOException {
92 Path path = getSlot(id);
93 if (Files.exists(path)) {
94 super.delete(path);
95 }
96 collectionLoadingCache.invalidate(id);
97 }
98
99
100
101
102
103
104
105
106
107 public MCRFileCollection retrieve(int id) throws IOException {
108 Path path = getSlot(id);
109 if (!Files.exists(path)) {
110 return null;
111 } else {
112 return collectionLoadingCache.getUnchecked(id);
113 }
114 }
115
116
117
118
119
120 public void repairAllMetadata() throws IOException {
121 for (Iterator<Integer> e = listIDs(MCRStore.ASCENDING); e.hasNext();) {
122 retrieve(e.next()).repairMetadata();
123 }
124 }
125 }