Skip to content

Instantly share code, notes, and snippets.

@jschoch
Last active December 19, 2015 16:39
Show Gist options
  • Save jschoch/5985267 to your computer and use it in GitHub Desktop.
Save jschoch/5985267 to your computer and use it in GitHub Desktop.
use Amnesia
require Exquisite
defdatabase PCnt do
@how_many 10000
deftable Lots, [:id, :oink],type: :ordered_set do
def run_next(list, nil) do
List.flatten list
end
def run_next(list,selection) do
next = selection.next
values = selection.values
#IO.puts "fetching more stuff, already found #{Enum.count list}\nList: #{inspect list}\n"
#IO.puts "Selection: #{inspect next}\nValues: #{inspect values}"
pid = spawn_link(PCntDBA,:werk,[self,values])
run_next([pid|list],next)
end
def gt(val) do
s = Exquisite.match Lots,
where: id > val
{:atomic, pids} = Amnesia.transaction do
selection = Lots.select( 10, s)
#IO.puts inspect selection
run_next([],selection)
end
Enum.reduce pids,0,fn(pid,acc) -> get_msg(pid) + acc end
end
def get_msg(pid) do
receive do
#{:ok, value} where is_number(value) -> value
{:ok, value} -> value
{:EXIT,pid,status} ->
IO.puts "#{inspect pid} #{status}"
0
doh ->
IO.puts "Poof #{inspect doh}"
0
after 1000 -> IO.puts "out of luck"
end
end
def put_new do
c = Amnesia.Counter.get(:auto_increment_plots)
Amnesia.transaction do
if (c.value == 0) do
IO.puts "Found zero counter"
count = Lots.last
if count != nil do
count = (count.id + 1)
c.increase count
end
end
r = Lots[id: c.value]
c.increase
r.write
r
end
end
end
end
defmodule PCntDBA do
def werk(pid,list) do
#sum = Enum.reduce(list,0,fn(x,acc) -> x.id + acc end)
count = Enum.count list
pid <- {:ok,count}
end
def install do
Amnesia.Schema.create
Amnesia.start
PCnt.create(disk: [node])
PCnt.wait
end
def uninstall do
Amnesia.start
PCnt.destroy
Amnesia.stop
Amnesia.Schema.destroy
end
def create_lots do
lc x inlist Enum.to_list 1..100000 do
PCnt.Lots.put_new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment