V1_0.java [src/java/m/timeseries/delete] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package m.timeseries.delete;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import csip.annotations.*;
import javax.ws.rs.Path;
import csip.ModelDataService;
import csip.SessionLogger;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* Mongodb timeseries data deletion
*
* @author Dave Patterson
*/
@Name("Timeseries delete")
@Description("Delete a timeseries collection or document in mongo.")
@Author(org = "CSU")
@License(License.MIT)
@Path("m/delete/1.0")
public class V1_0 extends ModelDataService {
MongoClient mongo;
MongoDatabase db;
JSONObject results = new JSONObject();
protected void open(String mongoclienturi) {
MongoClientURI u = new MongoClientURI(mongoclienturi);
mongo = new MongoClient(u);
if (u.getDatabase() == null) {
throw new RuntimeException("Missing database in mongo uri: " + mongoclienturi);
}
if (!u.getDatabase().equals("csip_timeseries")) {
throw new RuntimeException("Can only modify csip_timeseries collection");
}
db = mongo.getDatabase(u.getDatabase());
}
protected void close() {
mongo.close();
}
@Override
public void doProcess() throws Exception {
String mongo_uri = getStringParam("mongo_uri");
String mongo_collection = getStringParam("mongo_collection");
JSONArray ids = getJSONArrayParam("ids", null);
if (ids != null) {
deleteIds(mongo_uri, mongo_collection, ids);
} else {
// If no ids specified, then delete the collection.
deleteCollection(mongo_uri, mongo_collection);
}
}
public void deleteCollection(String mongo_uri, String mongo_collection) throws JSONException {
open(mongo_uri);
MongoCollection<Document> collection = db.getCollection(mongo_collection);
collection.drop();
results.put("deleted_collection", mongo_collection);
close();
}
public void deleteIds(String mongo_uri, String mongo_collection, JSONArray ids) throws JSONException {
open(mongo_uri);
MongoCollection<Document> collection = db.getCollection(mongo_collection);
List<String> ids_list = new ArrayList();
for (int i = 0; i < ids.length(); i++) {
ids_list.add(ids.getString(i));
}
List<String> deleted_ids = new ArrayList();
FindIterable<Document> c = collection.find();
for (Document finddoc : c) {
String find_id = (String) finddoc.get("_id");
if (ids_list.contains(find_id)) {
LOG.info("Deleting existing doc");
collection.deleteOne(finddoc);
deleted_ids.add(find_id);
}
}
results.put("deleted_document_ids", new JSONArray(deleted_ids));
close();
}
@Override
protected void postProcess() throws Exception {
putResult("result", results);
}
public static void main(String[] args) throws JSONException {
V1_0 timeseries = new V1_0();
JSONArray delete_ids = new JSONArray();
delete_ids.put("test_ref");
timeseries.deleteIds("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", delete_ids);
timeseries.deleteCollection("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll");
}
public static String[] toStringArray(List<String> data) {
return (String[]) data.stream().map(String::toString).toArray();
}
}