Skip to content

Commit 42f0f7c

Browse files
SurferJeffAtGooglebshaffer
authored andcommitted
Read stale data sample for spanner. (GoogleCloudPlatform#434)
1 parent 76c900c commit 42f0f7c

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

spanner/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"src/read_data_with_storing_index.php",
2222
"src/create_storing_index.php",
2323
"src/query_data_with_new_column.php",
24-
"src/read_only_transaction.php"
24+
"src/read_only_transaction.php",
25+
"src/read_stale_data.php"
2526
]
2627
}
2728
}

spanner/spanner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@
8181
})
8282
);
8383

84+
// Read stale data command
85+
$application->add((new Command('read-stale-data'))
86+
->setDefinition($inputDefinition)
87+
->setDescription('Reads sample data from the database, with a maximum staleness of 3 seconds.')
88+
->setCode(function ($input, $output) {
89+
read_stale_data(
90+
$input->getArgument('instance_id'),
91+
$input->getArgument('database_id')
92+
);
93+
})
94+
);
8495
// Add column command
8596
$application->add((new Command('add-column'))
8697
->setDefinition($inputDefinition)

spanner/src/read_stale_data.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START read_stale_data]
27+
use Google\Cloud\Spanner\Duration;
28+
use Google\Cloud\Spanner\SpannerClient;
29+
30+
/**
31+
* Reads sample data from the database. The data is exactly 10 seconds stale.
32+
* Example:
33+
* ```
34+
* read_stale_data
35+
*($instanceId, $databaseId);
36+
* ```
37+
*
38+
* @param string $instanceId The Spanner instance ID.
39+
* @param string $databaseId The Spanner database ID.
40+
*/
41+
function read_stale_data($instanceId, $databaseId)
42+
{
43+
$spanner = new SpannerClient();
44+
$instance = $spanner->instance($instanceId);
45+
$database = $instance->database($databaseId);
46+
// maxStaleness and minReadTimestamp are only available in single-use transactions.
47+
$snapshot = $database->snapshot();
48+
$keySet = $spanner->keySet(['all' => true]);
49+
$results = $snapshot->read(
50+
'Albums',
51+
$keySet,
52+
['SingerId', 'AlbumId', 'AlbumTitle'],
53+
['exactStaleness' => new Duration(10)]
54+
);
55+
56+
foreach ($results->rows() as $row) {
57+
printf('SingerId: %s, AlbumId: %s, AlbumTitle: %s' . PHP_EOL,
58+
$row['SingerId'], $row['AlbumId'], $row['AlbumTitle']);
59+
}
60+
}
61+
// [END read_stale_data]

spanner/test/spannerTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424

2525
class spannerTest extends \PHPUnit_Framework_TestCase
2626
{
27-
/* @var string instanceId */
27+
/** @var string instanceId */
2828
protected static $instanceId;
2929

30-
/* @var string databaseId */
30+
/** @var string databaseId */
3131
protected static $databaseId;
3232

33-
/* @var $instance Instance */
33+
/** @var $instance Instance */
3434
protected static $instance;
3535

36+
/** @var $lastUpdateData int */
37+
protected static $lastUpdateDataTimestamp;
38+
3639
public static function setUpBeforeClass()
3740
{
3841
if (!getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
@@ -116,6 +119,7 @@ public function testAddColumn()
116119
public function testUpdateData()
117120
{
118121
$output = $this->runCommand('update-data');
122+
self::$lastUpdateDataTimestamp = time();
119123
$this->assertEquals('Updated data.' . PHP_EOL, $output);
120124
}
121125

@@ -212,6 +216,25 @@ public function testReadOnlyTransaction()
212216
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
213217
}
214218

219+
/**
220+
* @depends testUpdateData
221+
*/
222+
public function testReadStaleData()
223+
{
224+
// read-stale-data reads data that is exactly 10 seconds old. So, make sure 10 seconds
225+
// have elapsed since testUpdateData().
226+
$elapsed = time() - self::$lastUpdateDataTimestamp;
227+
if ($elapsed < 11) {
228+
sleep(11 - $elapsed);
229+
}
230+
$output = $this->runCommand('read-stale-data');
231+
$this->assertContains('SingerId: 1, AlbumId: 1, AlbumTitle: Go, Go, Go', $output);
232+
$this->assertContains('SingerId: 1, AlbumId: 2, AlbumTitle: Total Junk', $output);
233+
$this->assertContains('SingerId: 2, AlbumId: 1, AlbumTitle: Green', $output);
234+
$this->assertContains('SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
235+
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
236+
}
237+
215238
private function runCommand($commandName)
216239
{
217240
$application = require __DIR__ . '/../spanner.php';

0 commit comments

Comments
 (0)