From 2d857daea2c1298baa9649d5ba7abf0ace787555 Mon Sep 17 00:00:00 2001 From: Jason Tedor <jason@tedor.me> Date: Wed, 20 Apr 2016 07:56:29 -0400 Subject: [PATCH] [elasticsearch] Return not found instead of error This commit modifies the Elasticsearch client to return Status.NOT_FOUND when an operation fails because the document requested does not exist. This modifies the previous behavior which would return Status.ERROR in this case. This commit also moves the statements that would return Status.ERROR into the corresponding catch blocks so that it is clear that Status.ERROR is only being returned on error paths. --- .../yahoo/ycsb/db/ElasticsearchClient.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java b/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java index 3c0762cd..6a95d9ce 100644 --- a/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java +++ b/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java @@ -30,6 +30,7 @@ import com.yahoo.ycsb.StringByteIterator; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; @@ -185,8 +186,7 @@ public class ElasticsearchClient extends DB { * description for a discussion of error codes. */ @Override - public Status insert(String table, String key, - HashMap<String, ByteIterator> values) { + public Status insert(String table, String key, HashMap<String, ByteIterator> values) { try { final XContentBuilder doc = jsonBuilder().startObject(); @@ -201,8 +201,8 @@ public class ElasticsearchClient extends DB { return Status.OK; } catch (Exception e) { e.printStackTrace(); + return Status.ERROR; } - return Status.ERROR; } /** @@ -218,12 +218,16 @@ public class ElasticsearchClient extends DB { @Override public Status delete(String table, String key) { try { - client.prepareDelete(indexKey, table, key).execute().actionGet(); - return Status.OK; + DeleteResponse response = client.prepareDelete(indexKey, table, key).execute().actionGet(); + if (response.isFound()) { + return Status.OK; + } else { + return Status.NOT_FOUND; + } } catch (Exception e) { e.printStackTrace(); + return Status.ERROR; } - return Status.ERROR; } /** @@ -241,8 +245,7 @@ public class ElasticsearchClient extends DB { * @return Zero on success, a non-zero error code on error or "not found". */ @Override - public Status read(String table, String key, Set<String> fields, - HashMap<String, ByteIterator> result) { + public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) { try { final GetResponse response = client.prepareGet(indexKey, table, key).execute().actionGet(); @@ -259,11 +262,13 @@ public class ElasticsearchClient extends DB { } } return Status.OK; + } else { + return Status.NOT_FOUND; } } catch (Exception e) { e.printStackTrace(); + return Status.ERROR; } - return Status.ERROR; } /** @@ -281,8 +286,7 @@ public class ElasticsearchClient extends DB { * description for a discussion of error codes. */ @Override - public Status update(String table, String key, - HashMap<String, ByteIterator> values) { + public Status update(String table, String key, HashMap<String, ByteIterator> values) { try { final GetResponse response = client.prepareGet(indexKey, table, key).execute().actionGet(); @@ -294,12 +298,13 @@ public class ElasticsearchClient extends DB { client.prepareIndex(indexKey, table, key).setSource(response.getSource()).execute().actionGet(); return Status.OK; + } else { + return Status.NOT_FOUND; } - } catch (Exception e) { e.printStackTrace(); + return Status.ERROR; } - return Status.ERROR; } /** @@ -321,8 +326,12 @@ public class ElasticsearchClient extends DB { * description for a discussion of error codes. */ @Override - public Status scan(String table, String startkey, int recordcount, - Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { + public Status scan( + String table, + String startkey, + int recordcount, + Set<String> fields, + Vector<HashMap<String, ByteIterator>> result) { try { final RangeQueryBuilder rangeQuery = rangeQuery("_id").gte(startkey); final SearchResponse response = client.prepareSearch(indexKey) @@ -336,18 +345,16 @@ public class ElasticsearchClient extends DB { for (SearchHit hit : response.getHits()) { entry = new HashMap<>(fields.size()); - for (String field : fields) { entry.put(field, new StringByteIterator((String) hit.getSource().get(field))); } - result.add(entry); } return Status.OK; } catch (Exception e) { e.printStackTrace(); + return Status.ERROR; } - return Status.ERROR; } } -- GitLab