@@ -126,21 +126,22 @@ public CassandraBlobContainer(BlobPath path, String keyspace, CassandraClientFac
126
126
@ Override public boolean blobExists (String blobName ) {
127
127
String blobKey = blobKey (blobName );
128
128
logger .debug ("TODO blobExists {}" , blobKey );
129
+ Cassandra .Client client = null ;
129
130
try {
130
- Cassandra .Client client =
131
- cassandraClientFactory .getCassandraClient ();
132
- try {
133
- return client .get_count (
134
- keyspace ,
135
- blobKey ,
136
- new ColumnParent ("Blobs" ),
137
- ConsistencyLevel .QUORUM ) != 0 ;
138
- }
139
- finally {
131
+ client = cassandraClientFactory .getCassandraClient ();
132
+ return client .get_count (
133
+ keyspace ,
134
+ blobKey ,
135
+ new ColumnParent ("Blobs" ),
136
+ ConsistencyLevel .QUORUM ) != 0 ;
137
+ }
138
+ catch (Exception e ) {
139
+ return false ;
140
+ }
141
+ finally {
142
+ if (client != null ) {
140
143
cassandraClientFactory .closeCassandraClient (client );
141
144
}
142
- } catch (Exception e ) {
143
- return false ;
144
145
}
145
146
}
146
147
@@ -209,7 +210,10 @@ private boolean deleteBlobs(String... blobNames)
209
210
Cassandra .Client client = null ;
210
211
try {
211
212
client = cassandraClientFactory .getCassandraClient ();
212
- readBlob (client , blobKey , listener );
213
+ byte [] blobData = readBlob (client , blobKey );
214
+ logger .debug ("Read {} bytes: {}" , blobKey , blobData .length );
215
+ listener .onPartial (blobData , 0 , blobData .length );
216
+ listener .onCompleted ();
213
217
}
214
218
catch (Exception ex ) {
215
219
listener .onFailure (ex );
@@ -223,19 +227,15 @@ private boolean deleteBlobs(String... blobNames)
223
227
});
224
228
}
225
229
226
- private void readBlob (Cassandra .Client client , String blobKey , ReadBlobListener listener )
230
+ private byte [] readBlob (Cassandra .Client client , String blobKey )
227
231
throws Exception
228
232
{
229
233
ColumnOrSuperColumn columnOrSuperColumn = client .get (
230
234
keyspace ,
231
235
blobKey ,
232
236
new ColumnPath ("Blobs" ).setColumn (utf8 .encode ("data" )),
233
237
ConsistencyLevel .QUORUM );
234
- Column column = columnOrSuperColumn .getColumn ();
235
- byte [] blobData = column .getValue ();
236
- logger .debug ("Read {} bytes: {}" , blobKey , blobData .length );
237
- listener .onPartial (blobData , 0 , blobData .length );
238
- listener .onCompleted ();
238
+ return columnOrSuperColumn .getColumn ().getValue ();
239
239
}
240
240
241
241
@ Override public ImmutableMap <String , BlobMetaData > listBlobsByPrefix (@ Nullable String blobNamePrefix ) throws IOException {
@@ -317,18 +317,19 @@ private Mutation createDelete(String name, long timestamp) {
317
317
logger .debug ("writeBlob {} sizeInBytes: {}" , blobKey (blobName ), sizeInBytes );
318
318
executor .execute (new Runnable () {
319
319
@ Override public void run () {
320
+ Cassandra .Client client = null ;
320
321
try {
321
- Cassandra .Client client =
322
- cassandraClientFactory .getCassandraClient ();
323
- try {
324
- writeBlob (client , blobName , is , sizeInBytes );
325
- listener .onCompleted ();
326
- }
327
- finally {
322
+ client = cassandraClientFactory .getCassandraClient ();
323
+ writeBlob (client , blobName , is , sizeInBytes );
324
+ listener .onCompleted ();
325
+ }
326
+ catch (Exception e ) {
327
+ listener .onFailure (e );
328
+ }
329
+ finally {
330
+ if (client != null ) {
328
331
cassandraClientFactory .closeCassandraClient (client );
329
332
}
330
- } catch (Exception e ) {
331
- listener .onFailure (e );
332
333
}
333
334
}
334
335
});
0 commit comments