Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added delete to embedded API #24

Merged
merged 3 commits into from
Jan 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions embedded-api/com/flaptor/indextank/api/EmbeddedApiV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions embedded-api/com/flaptor/indextank/api/IndexEngineApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,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) {
Expand Down
111 changes: 111 additions & 0 deletions embedded-api/com/flaptor/indextank/api/resources/DeleteDocs.java
Original file line number Diff line number Diff line change
@@ -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;

}