-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathclient_side_timestamps.feature
161 lines (142 loc) · 5.59 KB
/
client_side_timestamps.feature
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
@cassandra-version-2.1
Feature: Client-side timestamps
Cassandra uses timestamps to serialize write operations. That is, values with
more current timestamps are considered to be the most up-to-date version of
that information. By default, timestamps are assigned by Cassandra on the
server-side (`Cassandra\TimestampGenerator\ServerSide()`). The PHP Driver also
allows for timestamps to be assigned client-side in two ways:
* By providing a client-side timestamp generator such as
`Cassandra\TimestampGenerator\Monotonic()`.
* Explicitly assigning a 'timestamp' when executing a statement or batch
using execution options.
Background:
Given a running Cassandra cluster
And the following schema:
"""cql
CREATE KEYSPACE simplex WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 1
};
USE simplex;
CREATE TABLE playlists (
id uuid,
title text,
album text,
artist text,
song_id uuid,
PRIMARY KEY (id, title, album, artist)
);
"""
Scenario: Create a simple statement with a client-side timestamp
Given the following example:
"""php
<?php
$cluster = Cassandra::cluster()
->withContactPoints('127.0.0.1')
->build();
$session = $cluster->connect("simplex");
$arguments = array(
'song_id' => new Cassandra\Uuid('756716f7-2e54-4715-9f00-91dcbea6cf50'),
'title' => 'La Petite Tonkinoise',
'album' => 'Bye Bye Blackbird',
'artist' => 'Joséphine Baker'
);
$options = array(
'arguments' => $arguments,
'timestamp' => 1234
);
$session->execute(
"INSERT INTO playlists (id, song_id, artist, title, album) " .
"VALUES (62c36092-82a1-3a00-93d1-46196ee77204, ?, ?, ?, ?)",
$options
);
$result = $session->execute("SELECT artist, title, album, WRITETIME(song_id) FROM simplex.playlists");
foreach ($result as $row) {
echo $row['artist'] . ": " . $row['title'] . " / " . $row['album'] . " (". $row['writetime(song_id)'] . ")\n";
}
"""
When it is executed
Then its output should contain:
"""
Joséphine Baker: La Petite Tonkinoise / Bye Bye Blackbird (1234)
"""
Scenario: Create a batch with a client-side timestamp
Given the following example:
"""php
<?php
$cluster = Cassandra::cluster()->build();
$session = $cluster->connect("simplex");
$insertQuery = "INSERT INTO playlists (id, song_id, artist, title, album) " .
"VALUES (62c36092-82a1-3a00-93d1-46196ee77204, ?, ?, ?, ?)";
$prepared = $session->prepare($insertQuery);
$batch = new Cassandra\BatchStatement(Cassandra::BATCH_LOGGED);
$batch->add($prepared, array(
'song_id' => new Cassandra\Uuid('756716f7-2e54-4715-9f00-91dcbea6cf50'),
'title' => 'La Petite Tonkinoise',
'album' => 'Bye Bye Blackbird',
'artist' => 'Joséphine Baker'
));
$batch->add($insertQuery, array(
new Cassandra\Uuid('f6071e72-48ec-4fcb-bf3e-379c8a696488'),
'Willi Ostermann', 'Die Mösch', 'In Gold',
));
$batch->add($prepared, array(
new Cassandra\Uuid('fbdf82ed-0063-4796-9c7c-a3d4f47b4b25'),
'Mick Jager', 'Memo From Turner', 'Performance'
));
$options = array("timestamp" => 5678);
$session->execute($batch, $options);
$result = $session->execute("SELECT artist, title, album, WRITETIME(song_id) FROM simplex.playlists");
foreach ($result as $row) {
echo $row['artist'] . ": " . $row['title'] . " / " . $row['album'] . " (". $row['writetime(song_id)'] . ")\n";
}
"""
When it is executed
Then its output should contain:
"""
Joséphine Baker: La Petite Tonkinoise / Bye Bye Blackbird (5678)
"""
And its output should contain:
"""
Willi Ostermann: Die Mösch / In Gold (5678)
"""
And its output should contain:
"""
Mick Jager: Memo From Turner / Performance (5678)
"""
Scenario: Create a new session using a timestamp generator
Given the following example:
"""php
<?php
$cluster = Cassandra::cluster()
->withContactPoints('127.0.0.1')
->withTimestampGenerator(new Cassandra\TimestampGenerator\Monotonic())
->build();
$session = $cluster->connect("simplex");
for ($i = 0; $i < 10; $i++) {
$arguments = array(
'song_id' => new Cassandra\Uuid("756716f7-2e54-4715-9f00-91dcbea6cf5$i"),
'title' => 'La Petite Tonkinoise',
'album' => 'Bye Bye Blackbird',
'artist' => 'Joséphine Baker'
);
$options = array(
'arguments' => $arguments,
'timestamp' => 1234
);
$session->execute(
"INSERT INTO playlists (id, song_id, artist, title, album) " .
"VALUES (62c36092-82a1-3a00-93d1-46196ee77204, ?, ?, ?, ?)",
$options
);
}
$result = $session->execute("SELECT artist, title, album, song_id FROM simplex.playlists");
foreach ($result as $row) {
echo $row['artist'] . ": " . $row['title'] . " / " . $row['album'] . " (". $row['song_id'] . ")\n";
}
"""
When it is executed
Then its output should contain:
"""
Joséphine Baker: La Petite Tonkinoise / Bye Bye Blackbird (756716f7-2e54-4715-9f00-91dcbea6cf59)
"""