Skip to content

Instantly share code, notes, and snippets.

@ctford
Forked from mneedham/neo_loading.rb
Created June 10, 2012 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctford/2906469 to your computer and use it in GitHub Desktop.
Save ctford/2906469 to your computer and use it in GitHub Desktop.
Loading stuff into neo via the batch API
# So the problem is inserting data into neo using the batch API. So we have a bunch of people and we want to put them into the graph and also
# add to to the index so that we can search for them.
# The way the batch API works is that you can refer to previous commands by referencing their index in the list of commands (zero indexed)
# e.g. if I want to reference the person added in the first command I would reference that node as {0}
# You should be able to see how that works in the code below.
neo_people_to_load =
[{ :name => "Mark Needham", :id => 1 },
{ :name => "Jenn Smith", :id => 2 },
{ :name => "Chris Ford", :id => 3 }]
insert_commands = neo_people_to_load.map do |person|
[:create_node, {:id => person[:id], :name => person[:name]}]
end
index_commands = neo_people_to_load.each_with_index.map do |person, index|
[:add_node_to_index, "people", "name", person[:name], "{#{index}}"]
end
people_commands = insert_commands + index_commands
# So what we want to get is the following:
# [
# [:create_node, {:id=>"1", :name=>"Mark Needham"}], [:add_node_to_index, "people", "name", "Mark Needham", "{0}"],
# [:create_node, {:id=>"2", :name=>"Jenn Smith"}], [:add_node_to_index, "people", "name", "Jenn Smith", "{2}"],
# [:create_node, {:id=>"3", :name=>"Chris Ford"}, [:add_node_to_index, "people", "name", "Chris Ford", "{4}"]
# ]
@mneedham
Copy link

You could change the last line to be:

people_commands = insert_commands + index_commands

But otherwise it's cool. I like the idea of splitting up the concerns. I guess the functional way encourages you to do that whereas imperative you tend to do it all in one go.

@ctford
Copy link
Author

ctford commented Jun 10, 2012

Yep, + is nicer.

I think the functional style does encourage separation, because output is a value and not a side effect, so you can interleave etc later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment