<?php

$cb = new Couchbase("10.3.3.95",
    "Administrator",
    "password",
    "default");


#$id = uniqid("blahblah");
$id = "observe";
printf("Have ID $id\n");

$casvals = array();
$kvpairs = array();

# Generate our key-value pairs.
# The view will simply emit the row if it contains a 'php_value' field
for ($i = 0; $i < 300; $i++) {
    $k = "";
    $k = $id . "seq_$i";
    $v = json_encode(
        array("php_value" => $k)
    );
    $kvpairs[$k] = $v;
}

# Now, set all the kv pairs
$casvals = $cb->setMulti($kvpairs);

# Ensure that they've all persisted (sometimes not all of them will).
$status = $cb->keyDurabilityMulti($casvals,
    array("persist_to" => 2, "replicate_to" => 1));

$didnt_persist = array();
foreach ($status as $k => $v) {
    # For each of those keys which did not yet persist, store them in
    # a lookup. These are the keys we don't expect to show up in our view
    if (!$v) {
        printf("Ooops.. key %s didn't persist\n", $k);
        $didnt_persist[$k] = true;
    }
}

#Stop Persistence
#$rv = $cb->getServers();
#foreach ($rv as $srv) {
#    list($ip,$port) = explode(":",$srv);
#    $cmd = sprintf('/opt/couchbase/bin/cbepctl %s:11210 stop',$ip);
#    printf("%s\n",$cmd);
#    exec($cmd);
#}  

# Execute the view, this should now contain (at least) all the keys
# that we stored and that were persisted
$res = $cb->view(
    "dummy",
    "dummy",
    array("stale" => "false", "debug" => "true", "startkey" => "\"observe\"")
);

foreach ($res["rows"] as $row) {
    # Delete each row that we find in the view.
    # Sometimes the view might return outdated keys (or rather,
    # it will return keys who have been deleted from the cache, but
    # not removed from the disk/index on all nodes/replicas. We can't
    # really determine this, unfortunately?
    # )
    $b = $cb->delete($row["id"]);

    if (!$b) {
        printf("oops.. couldn't delete %s. %s (%d)\n",
            $row["id"],
            $cb->getResultMessage(),
            $cb->getResultCode()
        );
    }
}


# Finally, try to get all the keys again.
# In other words, all keys which have values are those which did
# not originally appear in the view.
$second = $cb->getMulti(array_keys($kvpairs));
$BAD_COUNT = 0;

foreach ($second as $k => $v) {
    if ($v) {
        if (array_key_exists($k, $didnt_persist)) {
            printf("Found key %s, but we didn't manage to persist it\n", $k);
            continue;
        }
        printf("Hrrm.. value %s still has key %s\n", $k, $v);
        $BAD_COUNT++;
    }
}

printf("Total keys that should have been deleted, but weren't: %d\n", $BAD_COUNT);

# unset($res["rows"]);
printf("Total rows: %d\n", $res["total_rows"]);
$fp = fopen("view_results.out", "w");
#$dumptxt = print_r($res, true);
#fwrite($fp, $dumptxt);
$json = json_encode($res);
fwrite($fp, $json);

#Start Persistence
#$rv = $cb->getServers();
#foreach ($rv as $srv) {
#    list($ip,$port) = explode(":",$srv);
#    $cmd = sprintf('/opt/couchbase/bin/cbepctl %s:11210 start',$ip);
#    printf("%s\n",$cmd);
#    exec($cmd);
#}
