Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
James Thornton
file 103 lines (90 sloc) 3.172 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//
// Copyright 2012 James Thornton (http://jamesthornton.com)
// BSD License (see LICENSE for details)
//

// To understand bundle notation, see http://groovy.codehaus.org/Multiple+Assignment


def save_blog_entry(entry_bundle, author_id, topic_bundles) {

  def create_indexed_vertex = { final List bundle ->
    (data, index_name, keys) = bundle
    vertex = g.addVertex()
    index = g.idx(index_name)
    for (property in data) {
      if (property.value == null) continue;
      vertex.setProperty(property.key, property.value)
      if (keys == null || keys.contains(property.key))
index.put(property.key, String.valueOf(property.value), vertex)
    }
    return vertex
  }

  def update_indexed_vertex = { final Vertex vertex, final List bundle ->
    (data, index_name, keys) = bundle
    index = g.idx(index_name);
    // remove vertex from index
    for (String key in vertex.getPropertyKeys()) {
      if (keys == null || keys.contains(key)) {
value = vertex.getProperty(key);
index.remove(key, String.valueOf(value), vertex);
      }
    }
    // update element properties
    ElementHelper.removeProperties([vertex]);
    ElementHelper.setProperties(vertex, data);
    // add vertex to index
    for (entry in data.entrySet()) {
      if (entry.value == null) continue;
      if (keys == null || keys.contains(entry.key))
index.put(entry.key,String.valueOf(entry.value),vertex);
    }
    return vertex;
  }

  def create_or_update_vertex = { final List bundle, final String index_key ->
    (data, index_name, keys) = bundle
    index_value = data[index_key]
    vertices = g.idx(index_name).get(index_key, index_value).toList()
    if (vertices.size() == 0) {
      vertex = create_indexed_vertex(bundle)
    } else {
      assert vertices.size() == 1
      vertex = update_indexed_vertex(vertices[0], bundle)
    }
    return vertex
  }

  def get_or_create_vertex = { final List bundle, final String index_key ->
    (data, index_name, keys) = bundle
    index_value = data[index_key]
    vertices = g.idx(index_name).get(index_key, index_value).toList()
    if (vertices.size() == 0) {
      vertex = create_indexed_vertex(bundle)
    } else {
      assert vertices.size() == 1
      vertex = vertices[0]
    }
    return vertex
  }

  def transaction = { final Closure closure ->
    g.setMaxBufferSize(0);
    g.startTransaction();
    try {
      results = closure();
      g.stopTransaction(TransactionalGraph.Conclusion.SUCCESS);
      return results;
    } catch (e) {
      g.stopTransaction(TransactionalGraph.Conclusion.FAILURE);
      return e;
    }
  }

  def save_blog_entry = {
    entry = create_or_update_vertex(entry_bundle, "docid");
    author = g.v(author_id)
    found = entry.out("author").filter{it == author}.count()
    if (!found) { g.addEdge(entry, author, "author"); }
    for (topic_bundle in topic_bundles) {
      topic = get_or_create_vertex(topic_bundle, "slug");
      // TODO: remove topics if needed
      found = entry.out("tagged").filter{it == topic}.count()
      if (!found) { g.addEdge(entry, topic, "tagged"); }
    }
    return entry;
  }
  
  return transaction(save_blog_entry);
}


Something went wrong with that request. Please try again.