View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Stores file collections containing files and directories.
34   * 
35   * For each store, properties must be defined, for example
36   * 
37   * MCR.IFS2.Store.ID.Class=org.mycore.datamodel.ifs2.MCRFileStore
38   * MCR.IFS2.Store.ID.BaseDir=/foo/bar MCR.IFS2.Store.ID.SlotLayout=4-2-2
39   * 
40   * @author Frank Lützenkirchen
41   */
42  public class MCRFileStore extends MCRStore {
43  
44      private final MCRFileStore thisInstance;
45  
46      //MCR-1868: prevents parallel threads to read and write mcrmeta.xml concurrently on instantiation
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       * Creates and stores a new, empty file collection using the next free ID in
63       * the store.
64       * 
65       * @return a newly created file collection
66       */
67      public MCRFileCollection create() throws IOException {
68          int id = getNextFreeID();
69          return create(id);
70      }
71  
72      /**
73       * Creates and stores a new, empty file collection with the given ID
74       * 
75       * @param id
76       *            the ID of the file collection
77       * @return a newly created file collection
78       * @throws IOException
79       *             when a file collection with the given ID already exists
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      * Returns the file collection stored under the given ID, or null when no
101      * collection is stored for the given ID.
102      * 
103      * @param id
104      *            the file collection's ID
105      * @return the file collection with the given ID, or null
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      * Repairs metadata of all file collections stored here
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 }