-
Notifications
You must be signed in to change notification settings - Fork 45
/
query-consistency.php
65 lines (57 loc) · 2.39 KB
/
query-consistency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
$cluster = new \Couchbase\Cluster('couchbase://192.168.1.194');
$bucket = $cluster->openBucket('default');
$RANDOM_NUMBER = rand(0, 10000000);
$bucket->upsert('user:'.$RANDOM_NUMBER, array(
"name" => array("Brass", "Doorknob"),
"email" => "[email protected]",
"random" => $RANDOM_NUMBER)
);
$query = \Couchbase\N1qlQuery::fromString(
'SELECT name, email, random, META(default).id FROM default WHERE $1 IN name'
);
$query->positionalParams(['Brass']);
// If this line is removed, the latest 'random' field might not be present
$query->consistency(\Couchbase\N1qlQuery::REQUEST_PLUS);
printf("Expecting random: %d\n", $RANDOM_NUMBER);
$result = $bucket->query($query);
foreach ($result->rows as $row) {
printf("Name: %s, Email: %s, Random: %d\n", implode(" ", $row->name), $row->email, $row->random);
if ($row->random == $RANDOM_NUMBER) {
echo "!!! Found or newly inserted document !!!\n";
}
if (getenv("REMOVE_DOORKNOBS")) {
echo "Removing " . $row->id . " (Requested via env)\n";
$bucket->remove($row->id);
}
}
// More light-weight way to do this is to use AT_PLUS consistency
// It will require mutation tokens enabled during connection.
$cluster = new \Couchbase\Cluster('couchbase://192.168.1.194?fetch_mutation_tokens=true');
$bucket = $cluster->openBucket('default');
$RANDOM_NUMBER = rand(0, 10000000);
$result = $bucket->upsert('user:'.$RANDOM_NUMBER, array(
"name" => array("Brass", "Doorknob"),
"email" => "[email protected]",
"random" => $RANDOM_NUMBER)
);
// construct mutation state from the list of mutation results
$mutationState = \Couchbase\MutationState::from($result);
$query = \Couchbase\N1qlQuery::fromString(
'SELECT name, email, random, META(default).id FROM default WHERE $1 IN name'
);
$query->positionalParams(['Brass']);
// If this line is removed, the latest 'random' field might not be present
$query->consistentWith($mutationState);
printf("Expecting random: %d\n", $RANDOM_NUMBER);
$result = $bucket->query($query);
foreach ($result->rows as $row) {
printf("Name: %s, Email: %s, Random: %d\n", implode(" ", $row->name), $row->email, $row->random);
if ($row->random == $RANDOM_NUMBER) {
echo "!!! Found or newly inserted document !!!\n";
}
if (getenv("REMOVE_DOORKNOBS")) {
echo "Removing " . $row->id . " (Requested via env)\n";
$bucket->remove($row->id);
}
}