Permalink
Browse files

Merge pull request #24 from iperez/master

Added delete to embedded API
  • Loading branch information...
2 parents e71afba + a8590e8 commit bd5a23777934c45f158f6615e2952ec5bec1d65b @iladriano iladriano committed Jan 27, 2012
@@ -17,6 +17,7 @@
package com.flaptor.indextank.api;
import com.flaptor.indextank.api.resources.Autocomplete;
+import com.flaptor.indextank.api.resources.DeleteDocs;
import com.flaptor.indextank.api.resources.Docs;
import com.flaptor.indextank.api.resources.Search;
import com.ghosthack.turismo.action.Action;
@@ -32,6 +33,8 @@ protected void map() {
put("/indexes/:name/docs", new Docs());
+ delete("/indexes/:name/docs", new DeleteDocs());
+
get("/indexes", new Action() {
public void run() {
// dummy response for embedded server
@@ -132,9 +132,13 @@ public void putDocument(String id, JSONObject fields, JSONObject variables, JSON
BoostingIndexer indexer = engine.getIndexer();
indexer.add(id, new Document(prepareProperties(fields)), Timestamp.inSeconds(), prepareBoosts(variables));
indexer.updateCategories(id, prepareProperties(categories));
- System.out.println(engine.getIndexer().getStats());
}
-
+
+ public void deleteDocument(String id) {
+ BoostingIndexer indexer = engine.getIndexer();
+ indexer.del(id);
+ }
+
private Map<String, String> prepareProperties(JSONObject jo) {
Map<String, String> properties = Maps.newHashMap();
if(jo == null) {
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2011 LinkedIn, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.flaptor.indextank.api.resources;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.json.simple.parser.ParseException;
+
+import com.flaptor.indextank.api.IndexEngineApi;
+import com.ghosthack.turismo.action.Action;
+
+public class DeleteDocs extends Action {
+
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ public void run() {
+ IndexEngineApi api = (IndexEngineApi) ctx().getAttribute("api");
+ try {
+ Object parse = JSONValue.parseWithException(req().getReader());
+ if(parse instanceof JSONObject) { // 200, 400, 404, 409, 503
+ JSONObject jo = (JSONObject) parse;
+ try {
+ deleteDocument(api, jo);
+ res().setStatus(200);
+ return;
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ if(LOG_ENABLED) LOG.severe(e.getMessage());
+ res().setStatus(400);
+ print("Invalid or missing argument"); // TODO: descriptive error msg
+ return;
+ }
+ } else if(parse instanceof JSONArray) {
+ JSONArray statuses = new JSONArray();
+ JSONArray ja = (JSONArray) parse;
+ if(!validateDocuments(ja)) {
+ res().setStatus(400);
+ print("Invalid or missing argument"); // TODO: descriptive error msg
+ return;
+ }
+ boolean hasError = false;
+ for(Object o: ja) {
+ JSONObject jo = (JSONObject) o;
+ JSONObject status = new JSONObject();
+ try {
+ deleteDocument(api, jo);
+ status.put("added", true);
+ } catch(Exception e) {
+ status.put("added", false);
+ status.put("error", "Invalid or missing argument"); // TODO: descriptive error msg
+ hasError = true;
+ }
+ statuses.add(status);
+ }
+ print(statuses.toJSONString());
+ return;
+ }
+ } catch (IOException e) {
+ if(LOG_ENABLED) LOG.severe("DELETE doc, parse input " + e.getMessage());
+ } catch (ParseException e) {
+ if(LOG_ENABLED) LOG.severe("DELETE doc, parse input " + e.getMessage());
+ } catch (Exception e) {
+ if(LOG_ENABLED) LOG.severe("DELETE doc " + e.getMessage());
+ }
+ res().setStatus(503);
+ print("Service unavailable"); // TODO: descriptive error msg
+ }
+
+ private void deleteDocument(IndexEngineApi api, JSONObject jo) {
+ String docid = String.valueOf(jo.get("docid"));
+ api.deleteDocument(docid);
+ }
+
+ private boolean validateDocument(JSONObject jo) {
+ return true; // TODO: validate the document
+ }
+
+ private boolean validateDocuments(JSONArray ja) {
+ for(Object o: ja) {
+ JSONObject jo = (JSONObject) o;
+ if(!validateDocument(jo)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static final Logger LOG = Logger.getLogger(DeleteDocs.class.getName());
+ private static final boolean LOG_ENABLED = true;
+
+}

0 comments on commit bd5a237

Please sign in to comment.