Skip to content

Commit 7eee1c6

Browse files
author
Tom May
committed
Implement CassandraBlobStore.delete(BlobPath).
1 parent cf2f00b commit 7eee1c6

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

plugins/cassandra/src/main/java/org/elasticsearch/cassandra/blobstore/CassandraBlobStore.java

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,19 @@ public int bufferSizeInBytes() {
142142
}
143143

144144
@Override public void delete(BlobPath path) {
145-
logger.debug("TODO delete {}", path);
146-
/* XXX TODO
147-
ObjectListing prevListing = null;
148-
while (true) {
149-
ObjectListing list;
150-
if (prevListing != null) {
151-
list = client.listNextBatchOfObjects(prevListing);
152-
} else {
153-
list = client.listObjects(bucket, path.buildAsString("/"));
154-
}
155-
for (S3ObjectSummary summary : list.getObjectSummaries()) {
156-
client.deleteObject(summary.getBucketName(), summary.getKey());
157-
}
158-
if (list.isTruncated()) {
159-
prevListing = list;
160-
} else {
161-
break;
162-
}
145+
String blobPath = path.buildAsString("/");
146+
logger.debug("TODO delete {}", blobPath);
147+
try {
148+
ImmutableMap<String, BlobMetaData> blobs =
149+
listBlobsByPrefix(blobPath, null);
150+
String[] blobNames =
151+
blobs.keySet().toArray(new String[blobs.size()]);
152+
deleteBlobs(blobPath, blobNames);
153+
}
154+
catch (IOException ex) {
155+
// Oh well, nothing we can do but log.
156+
logger.warn("delete {} failed", ex, blobPath);
163157
}
164-
*/
165158
}
166159

167160
@Override public void close() {
@@ -189,19 +182,26 @@ boolean blobExists(String blobPath, String blobName) {
189182
}
190183

191184
boolean deleteBlob(String blobPath, String blobName) throws IOException {
192-
String blobKey = blobKey(blobPath, blobName);
193-
logger.debug("deleteBlob {}", blobKey);
194-
Cassandra.Client client =
195-
CassandraClientFactory.getCassandraClient();
196-
try {
197-
long timestamp = System.currentTimeMillis();
185+
logger.debug("deleteBlob {}", blobKey(blobPath, blobName));
186+
return deleteBlobs(blobPath, blobName);
187+
}
198188

199-
Map<String, Map<String, List<Mutation>>> mutationMap =
200-
new HashMap<String, Map<String, List<Mutation>>>();
189+
private boolean deleteBlobs(String blobPath, String... blobNames)
190+
throws IOException
191+
{
192+
long timestamp = System.currentTimeMillis();
201193

202-
// Delete the blob data from Blobs.
194+
Map<String, Map<String, List<Mutation>>> mutationMap =
195+
new HashMap<String, Map<String, List<Mutation>>>();
196+
197+
List<Mutation> blobNamesMutations = new ArrayList<Mutation>();
203198

204-
List<Mutation> blobsMutations = new ArrayList<Mutation>();
199+
for (String blobName : blobNames) {
200+
String blobKey = blobKey(blobPath, blobName);
201+
202+
// Delete the blob data from Blobs.
203+
204+
List<Mutation> blobsMutations = new ArrayList<Mutation>(1);
205205
blobsMutations.add(createDelete(null, timestamp));
206206

207207
Map<String, List<Mutation>> blobsMutationMap =
@@ -212,18 +212,20 @@ boolean deleteBlob(String blobPath, String blobName) throws IOException {
212212

213213
// Delete the blobName from BlobNames.
214214

215-
List<Mutation> blobNamesMutations = new ArrayList<Mutation>();
216215
blobNamesMutations.add(createDelete(blobName, timestamp));
216+
}
217217

218-
Map<String, List<Mutation>> blobNamesMutationMap =
219-
new HashMap<String, List<Mutation>>();
220-
blobNamesMutationMap.put("BlobNames", blobNamesMutations);
218+
Map<String, List<Mutation>> blobNamesMutationMap =
219+
new HashMap<String, List<Mutation>>();
220+
blobNamesMutationMap.put("BlobNames", blobNamesMutations);
221221

222-
mutationMap.put(blobPath, blobNamesMutationMap);
222+
mutationMap.put(blobPath, blobNamesMutationMap);
223223

224+
Cassandra.Client client = null;
225+
try {
226+
client = CassandraClientFactory.getCassandraClient();
224227
client.batch_mutate(
225228
keySpace, mutationMap, ConsistencyLevel.QUORUM);
226-
227229
return true;
228230
}
229231
catch (Exception e) {
@@ -232,7 +234,9 @@ boolean deleteBlob(String blobPath, String blobName) throws IOException {
232234
return false;
233235
}
234236
finally {
235-
CassandraClientFactory.closeCassandraClient(client);
237+
if (client != null) {
238+
CassandraClientFactory.closeCassandraClient(client);
239+
}
236240
}
237241
}
238242

0 commit comments

Comments
 (0)