1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.akubraproject.fs;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27
28 import java.net.URI;
29
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import org.akubraproject.Blob;
39 import org.akubraproject.BlobStore;
40 import org.akubraproject.impl.AbstractBlobStoreConnection;
41 import org.akubraproject.impl.StreamManager;
42
43
44
45
46
47
48 class FSBlobStoreConnection extends AbstractBlobStoreConnection {
49 private static final Logger log = LoggerFactory.getLogger(FSBlobStoreConnection.class);
50
51 private final File baseDir;
52 private final Set<File> modified;
53
54 FSBlobStoreConnection(BlobStore blobStore, File baseDir, StreamManager manager, boolean noSync) {
55 super(blobStore, manager);
56 this.baseDir = baseDir;
57 this.modified = noSync ? null : new HashSet<File>();
58 }
59
60 @Override
61 public Blob getBlob(URI blobId, Map<String, String> hints) throws IOException {
62 ensureOpen();
63
64 if (blobId == null)
65 throw new UnsupportedOperationException();
66
67 return new FSBlob(this, baseDir, blobId, streamManager, modified);
68 }
69
70 @Override
71 public Iterator<URI> listBlobIds(String filterPrefix) {
72 ensureOpen();
73 return new FSBlobIdIterator(baseDir, filterPrefix);
74 }
75
76 @Override
77 public void sync() throws IOException {
78 ensureOpen();
79
80 if (modified == null)
81 throw new UnsupportedOperationException("You promised you weren't going to call sync!");
82
83 for (File f : modified) {
84 try {
85 FileInputStream fis = new FileInputStream(f);
86 try {
87 fis.getFD().sync();
88 } finally {
89 fis.close();
90 }
91 } catch (IOException ioe) {
92 log.warn("Error sync'ing file '" + f + "'", ioe);
93 }
94 }
95
96 modified.clear();
97 }
98
99 @Override
100 public void close() {
101 if (modified != null)
102 modified.clear();
103
104 super.close();
105 }
106 }