coux.del(db, function(err, ok) {
     if (err && err.error != "not_found") {
         console.log(err)
     } else {
         coux.put(db, e(function() {
             cb()
         }))
     }
 })
 coux.put(testDb, e(function() {
   var control_ddoc = require('../lib/design/control');
   coux.put(testDb.concat(control_ddoc._id), control_ddoc, function(err, ok) {
     if (err && err.error !== "conflict") {
         e()(err);
       } else {
         cb()
       }
   })
 }));
Ejemplo n.º 3
0
 exports._dependencies.getMeFromFB(pairingUserDoc.pairing_token, function(err, resp) {
     if (err) {
         pairingUserDoc.state = 'error';
         pairingUserDoc.error = err;
         console.error("facebook error logged to session doc", err, pairingUserDoc._id);
         coux.put([config.host, config.users_db, pairingUserDoc._id], pairingUserDoc, e());            
         return; // the client has enough info to retry
     }
     function findOrCreateUserDoc() {
         console.log('findOrCreateUserDoc')
         // find or create a user that has this facebook_id and this app_id
         var userId = "sp_fb_"+resp.id+"_app_"+pairingUserDoc.pairing_app_id,
           userDocId = "org.couchdb.user:"******"user found", fb_user._id);
             } else { // make one
                 fb_user = {
                    _id : userDocId,
                    fb_id : resp.id,
                    app_id : pairingUserDoc.pairing_app_id,
                    name : userId,
                   full_name : resp.name,
                   type : "user",
                   state : "new",
                   roles : []
                 };
                 console.log("making user: ", fb_user._id);
             }
             syncpointHelpers.activatePairingUser(pairingUserDoc, fb_user, config);
         });
     }
     // kick off that long chain, if the 
     if (resp.id && resp.name) {
         findOrCreateUserDoc();
     }
 });
 coux.put(testTemplate, e(function() {
   coux.put(testTemplate.concat('test-doc'), {foobar: "ok"}, e(cb));
 }));
}, function() {
    console.log("setting up replications");

    // setup replication from devices <-> master
    var master = dbs[0]
        , dbs2 = dbs.slice(1)
        ;
    
    
    function triggerRepsAndGo() {
        asyncFold(dbs2, function(db, cb) {
            var rpc = url.parse(db)
                , dbName = rpc.pathname.split('/')[1];
            rpc.pathname = "/_replicate";
            var replicator = url.format(rpc);
            coux.post(replicator, {
                continuous : CONTINUOUS,
                source : dbName,
                target : master
            }, e(function() {
                var repdef = {
                    continuous : CONTINUOUS,
                    source : master,
                    target : dbName
                };
                if (FILTER) {
                    repdef.filter = "filter/random";
                    repdef.query_params = {rand:Math.round(Math.random() * 100).toString()};
                }
                coux.post(replicator, repdef, e(function() {
                    cb()
                }));
            }))
        }, function() {
            console.log("replication is running, start the measurement")
            measure.start(dbs, function(notify) {
                console.log("measure is running, start the load: "+LOAD)
                load.start(notify, dbs2);
            })
        }, 10);
    };
    
    
    if (FILTER) {
        console.log("setting up filter fun");
        var filterFun = 'fun(Doc, Req) -> GetFrom = fun(Key, {Plist}) -> proplists:get_value(Key, Plist, null) end, Rand = GetFrom(<<"rand">>, GetFrom(<<"query">>, Req)), case GetFrom(<<"rand">>, Doc) of Rand -> true; _ -> false end end.';
        var ddoc = {
            language: "erlang",
            filters : {    
                random : filterFun
            }
        }  ;
        // var config = url.parse(master)
        //     , dbName = config.pathname.split('/')[1];
        // config.pathname = "/_config/native_query_servers/erlang";
        // config = url.format(config);
        // coux.put(config,'{couch_native_process, start_link, []}')
        coux.put([master, "_design/filter"], ddoc, e(function(err, ok) {
            triggerRepsAndGo()
        }));
    } else {
        triggerRepsAndGo()
    }
    
}, 5);