Collaborative Map Reduze with Javascript (this is more interesting than it might sound)

Angelegt von suung Tue, 23 Nov 2010 16:51:00 GMT

This a like one year old article about Using Javascript to share computing time amongst nodes (that is computers on the web).

Well, it's even coming with code.I
I think, we all wait for the time, where it's realistic to use one users's power without additional installation and security holes for his data privacy.

Javascript inside the browser could be exactly that.


Yeah, here is the code

 

<html>
  <head>
    <script type="text/javascript">
 
      function map() {
        /* count the number of words in the body of document */
        var words = document.body.innerHTML.split(/\\n|\\s/).length;
        emit('reduce', {'count': words});
      }
 
      function reduce() {
        /* sum up all the word counts */
        var sum = 0;
        var docs = document.body.innerHTML.split(/\\n/);
        for each (num in docs) { sum+= parseInt(num) > 0 ? parseInt(num) : 0 }
        emit('finalize', {'sum': sum});
      }
 
      function emit(phase, data) { ... }
    </script>
  </head>
 
  <body onload="map();">
    ... DATA ...
  </body>
</html>
 

require "rubygems"
require "sinatra"
 
configure do
  set :map_jobs, Dir.glob("data/*.txt")
  set :reduce_jobs, []
  set :result, nil
end
 
get "/" do
  redirect "/map/#{options.map_jobs.pop}" unless options.map_jobs.empty?
  redirect "/reduce"                      unless options.reduce_jobs.empty?
  redirect "/done"
end
 
get "/map/*"  do erb :map,    :file => params[:splat].first; end
get "/reduce" do erb :reduce, :data => options.reduce_jobs;  end
get "/done"   do erb :done,   :answer => options.result;     end
 
post "/emit/:phase" do
  case params[:phase]
  when "reduce" then
    options.reduce_jobs.push params['count']
    redirect "/"
 


  when "finalize" then
    options.result = params['sum']
    redirect "/done"
  end
end

 

 

 

Trackbacks

Verwenden Sie den folgenden Link zur Rückverlinkung von Ihrer eigenen Seite:
http://praktikanten.brueckenschlaeger.org/trackbacks?article_id=371

Leave a comment

Comments