View Javadoc

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   * @author ajs6f
24   * @version 1.0 
25   */
26  public class TwoStoreConnection extends AbstractBlobStoreConnection {
27  
28  	private static final Logger log = LoggerFactory
29  			.getLogger(TwoStoreConnection.class);
30  
31  	/**
32  	 * {@link org.akubraproject.BlobStore} to which this connection belongs
33  	 */
34  	private TwoStore myowner;
35  	
36  	/**
37  	 * a {@link org.akubraproject.BlobStoreConnection} for each side of the {@link TwoStore}
38  	 */
39  	private BlobStoreConnection right, left;
40  
41  	/**
42  	 * @param owner {@link org.akubraproject.BlobStore} to which this connection belongs
43  	 * @param hints {@link Map} of hints for this connection which will be passed on to each side
44  	 * @throws IOException
45  	 * @throws UnsupportedOperationException
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  	 * @param owner {@link org.akubraproject.BlobStore} to which this connection belongs
58  	 * @param streamManager
59  	 * @throws IOException
60  	 * @throws UnsupportedOperationException
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  	 * @see org.akubraproject.BlobStoreConnection#getBlob(java.net.URI, java.util.Map)
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  	 *  @see org.akubraproject.BlobStoreConnection#listBlobIds(java.lang.String)
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 	 * @see org.akubraproject.BlobStoreConnection#sync()
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 }