|
29 | 29 |
|
30 | 30 | import org.apache.cassandra.thrift.Column;
|
31 | 31 | import org.apache.cassandra.thrift.ColumnParent;
|
| 32 | +import org.apache.cassandra.thrift.ColumnPath; |
32 | 33 | import org.apache.cassandra.thrift.ColumnOrSuperColumn;
|
33 | 34 | import org.apache.cassandra.thrift.ConsistencyLevel;
|
34 | 35 | import org.apache.cassandra.thrift.Cassandra;
|
@@ -84,36 +85,40 @@ public AbstractCassandraBlobContainer(BlobPath path, CassandraBlobStore blobStor
|
84 | 85 | }
|
85 | 86 |
|
86 | 87 | @Override public void readBlob(final String blobName, final ReadBlobListener listener) {
|
87 |
| - logger.debug("TODO readBlob blobName={}", blobName); |
88 |
| - /* XXX |
| 88 | + logger.debug("readBlob blobName={}", blobName); |
89 | 89 | blobStore.executor().execute(new Runnable() {
|
90 | 90 | @Override public void run() {
|
91 |
| - InputStream is; |
| 91 | + Cassandra.Client client = null; |
92 | 92 | try {
|
93 |
| - S3Object object = blobStore.client().getObject(blobStore.bucket(), buildKey(blobName)); |
94 |
| - is = object.getObjectContent(); |
95 |
| - } catch (Exception e) { |
96 |
| - listener.onFailure(e); |
97 |
| - return; |
| 93 | + client = CassandraClientFactory.getCassandraClient(); |
| 94 | + readBlob(client, blobName, listener); |
98 | 95 | }
|
99 |
| - byte[] buffer = new byte[blobStore.bufferSizeInBytes()]; |
100 |
| - try { |
101 |
| - int bytesRead; |
102 |
| - while ((bytesRead = is.read(buffer)) != -1) { |
103 |
| - listener.onPartial(buffer, 0, bytesRead); |
104 |
| - } |
105 |
| - listener.onCompleted(); |
106 |
| - } catch (Exception e) { |
107 |
| - try { |
108 |
| - is.close(); |
109 |
| - } catch (IOException e1) { |
110 |
| - // ignore |
| 96 | + catch (Exception ex) { |
| 97 | + listener.onFailure(ex); |
| 98 | + } |
| 99 | + finally { |
| 100 | + if (client != null) { |
| 101 | + CassandraClientFactory.closeCassandraClient(client); |
111 | 102 | }
|
112 |
| - listener.onFailure(e); |
113 | 103 | }
|
114 | 104 | }
|
115 | 105 | });
|
116 |
| - */ |
| 106 | + } |
| 107 | + |
| 108 | + private void readBlob(Cassandra.Client client, String blobName, ReadBlobListener listener) |
| 109 | + throws Exception |
| 110 | + { |
| 111 | + ColumnOrSuperColumn columnOrSuperColumn = client.get( |
| 112 | + keySpace, |
| 113 | + blobPath + '/' + blobName, |
| 114 | + new ColumnPath("Blobs").setColumn(utf8.encode("data")), |
| 115 | + ConsistencyLevel.QUORUM); |
| 116 | + Column column = columnOrSuperColumn.getColumn(); |
| 117 | + byte[] blobData = column.getValue(); |
| 118 | + logger.debug("Read {} ({} bytes): {}", |
| 119 | + blobName, blobData.length, new String(blobData)); |
| 120 | + listener.onPartial(blobData, 0, blobData.length); |
| 121 | + listener.onCompleted(); |
117 | 122 | }
|
118 | 123 |
|
119 | 124 | @Override public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(@Nullable String blobNamePrefix) throws IOException {
|
|
0 commit comments