1
2
3
4 package edu.virginia.lib.ole.akubra;
5
6 import java.io.IOException;
7 import java.net.URI;
8 import java.util.Iterator;
9 import java.util.Map;
10
11 import org.akubraproject.Blob;
12 import org.akubraproject.BlobStore;
13 import org.akubraproject.BlobStoreConnection;
14 import org.akubraproject.UnsupportedIdException;
15 import org.akubraproject.impl.AbstractBlobStoreConnection;
16 import org.akubraproject.impl.StreamManager;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.collect.Iterators;
21
22
23
24
25
26 public class TwoStoreConnection extends AbstractBlobStoreConnection {
27
28 private static final Logger log = LoggerFactory
29 .getLogger(TwoStoreConnection.class);
30
31
32
33
34 private TwoStore myowner;
35
36
37
38
39 private BlobStoreConnection right, left;
40
41
42
43
44
45
46
47
48 public TwoStoreConnection(BlobStore owner, Map<String, String> hints)
49 throws UnsupportedOperationException, IOException {
50 super(owner);
51 this.myowner = (TwoStore) owner;
52 this.left = myowner.getLeft().openConnection(null, hints);
53 this.right = myowner.getRight().openConnection(null, hints);
54 }
55
56
57
58
59
60
61
62 public TwoStoreConnection(BlobStore owner, StreamManager streamManager)
63 throws UnsupportedOperationException, IOException {
64 super(owner, streamManager);
65 this.myowner = (TwoStore) owner;
66 this.left = myowner.getLeft().openConnection(null, null);
67 this.right = myowner.getRight().openConnection(null, null);
68 }
69
70
71
72
73 public Blob getBlob(URI blobId, Map<String, String> hints)
74 throws IOException, UnsupportedIdException,
75 UnsupportedOperationException {
76 log.debug("Entering TwoStoreConnection.getBlob()");
77 String blobIdString = blobId.toString();
78 log.debug("Using blobIdString: " +blobIdString);
79 String newBlobIdString = blobIdString.substring(Constants.CHUNK+1);
80 log.debug("Using newBlobIdString: " +newBlobIdString);
81 Short front = Short.parseShort((blobIdString.substring(0, Constants.CHUNK)),
82 2);
83 log.debug("Using front: " +front);
84 URI newBlobId = URI.create(newBlobIdString);
85
86 if (front % 2 == 0) {
87 log.debug("Exiting TwoStoreConnection.getBlob() to the left");
88 return left.getBlob(newBlobId, hints);
89 } else {
90 log.debug("Exiting TwoStoreConnection.getBlob() to the right");
91 return right.getBlob(newBlobId, hints);
92 }
93
94 }
95
96
97
98
99 public Iterator<URI> listBlobIds(String filterPrefix) throws IOException {
100 Iterator<URI> leftiter = left.listBlobIds(filterPrefix);
101 Iterator<URI> rightiter = right.listBlobIds(filterPrefix);
102 return Iterators.concat(leftiter, rightiter);
103 }
104
105
106
107
108 public void sync() throws IOException, UnsupportedOperationException {
109 left.sync();
110 right.sync();
111 }
112
113 @Override
114 public void close() {
115 left.close();
116 right.close();
117 closed = true;
118 }
119
120 }