protected void deleteSearch(Group group, GoogleSearch search) {
   synchronized (searchLock) {
     googleDB.search.deleteFromGroup(search, group.getId());
     googleDB.rank.deleteBySearch(group.getId(), search.getId());
     if (!googleDB.search.hasGroup(search)) {
       googleDB.serp.deleteBySearch(search.getId());
       googleDB.search.delete(search);
     }
   }
 }
  public Result jsonSearches(Context context) {
    List<GoogleSearch> searches = context.getAttribute("searches", List.class);
    if (searches.isEmpty()) {
      return Results.json().renderRaw("[]");
    }

    return Results.ok()
        .json()
        .render(
            (Context context0, Result result) -> {
              PrintWriter writer = null;
              OutputStream os = null;
              try {

                String acceptEncoding = context0.getHeader("Accept-Encoding");
                if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
                  result.addHeader("Content-Encoding", "gzip");
                }

                ResponseStreams response = context0.finalizeHeaders(result);
                os = response.getOutputStream();
                if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
                  os = new GZIPOutputStream(os);
                }

                writer = new PrintWriter(os);
                writer.append("[");
                for (int i = 0; i < searches.size(); i++) {
                  GoogleSearch search = searches.get(i);
                  writer.append("{");
                  writer.append("\"id\":").append(Integer.toString(search.getId())).append(",");
                  writer
                      .append("\"keyword\":\"")
                      .append(StringEscapeUtils.escapeJson(search.getKeyword()))
                      .append("\",");
                  writer
                      .append("\"tld\":\"")
                      .append(
                          search.getTld() == null
                              ? ""
                              : StringEscapeUtils.escapeJson(search.getTld()))
                      .append("\",");
                  writer
                      .append("\"device\":\"")
                      .append(SMARTPHONE.equals(search.getDevice()) ? 'M' : 'D')
                      .append("\",");
                  writer
                      .append("\"local\":\"")
                      .append(
                          search.getLocal() == null
                              ? ""
                              : StringEscapeUtils.escapeJson(search.getLocal()))
                      .append("\",");
                  writer
                      .append("\"datacenter\":\"")
                      .append(
                          search.getDatacenter() == null
                              ? ""
                              : StringEscapeUtils.escapeJson(search.getDatacenter()))
                      .append("\",");
                  writer
                      .append("\"custom\":\"")
                      .append(
                          search.getCustomParameters() == null
                              ? ""
                              : StringEscapeUtils.escapeJson(search.getCustomParameters()))
                      .append("\"");
                  writer.append("}");
                  if (i != searches.size() - 1) {
                    writer.append(",");
                  }
                }
                writer.append("]");

              } catch (Exception ex) {
                LOG.warn("HTTP error", ex);
              } finally {
                if (os != null) {
                  try {
                    writer.close();
                    os.close();
                  } catch (Exception ex) {
                  }
                }
              }
            });
  }