Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre release #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions firestore/src/firestore_snippets/Counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Firestore;

use Google\Cloud\Firestore\DocumentReference;

// [START fs_counter_classes]
/** Counter is a collection of documents (shards)
* to realize counter with high frequency
*/
class Counter
{
/**
* @var int
*/
private $numShards;

public function __construct(int $numShards)
{
$this->numShards = $numShards;
}

public function getNumShards()
{
return $this->numShards;
}
}


/**
* Shard is a single counter, which is used in a group
* of other shards within Counter.
* */
class Shard
{
/**
* @var int
*/
private $count;

public function __construct(int $count)
{
$this->count = $count;
}

public function getCount()
{
return $this->count;
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну, вот здесь ещё лишняя пустая строка

// [END fs_counter_classes]


class SolutionCounters {
// [START fs_create_counter]
/**
* InitCounter creates a given number of shards as
* subcollection of specified document.
*
* @param DocumentReference $ref Firestore document
* @param int $numShards The number of counter fragments. (default 10)
*/
public static function initCounter(DocumentReference $ref, int $numShards = 10)
{
$counter = new Counter($numShards);
$colRef = $ref->collection('SHARDS');
for ($i = 0; $i < $counter->getNumShards(); $i++) {
$doc = $colRef->document($i);
$doc->set(['Cnt' => 0]);
}
}
// [END fs_create_counter]

// [START fs_increment_counter]
/**
* incrementCounter increments a randomly picked shard.
*
* @param DocumentReference $ref Firestore document
* @param int $numShards The number of counter fragments. (default 10)
*/
public static function incrementCounter(DocumentReference $ref, int $numShards = 10)
{
$colRef = $ref->collection('SHARDS');
$shardIdx = random_int(0, $numShards-1);
$doc = $colRef->document($shardIdx);
$doc->update([
['path' => 'Cnt', 'value' => FieldValue::increment(1)]
]);
}
// [END fs_increment_counter]

// [START fs_get_count]
/**
* getCount returns a total count across all shards.
*
* @param DocumentReference $ref Firestore document
* @return int
*/
public static function getCount(DocumentReference $ref)
{
$result = 0;
$docCollection = $ref->collection('SHARDS')->documents();
foreach ($docCollection as $doc) {
$result += $doc->data()['Cnt'];
}
return $result;
}
// [END fs_get_count]
}
126 changes: 126 additions & 0 deletions firestore/test/CounterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Firestore;

use Google\Cloud\Firestore\SolutionCounters;
use Google\Cloud\Firestore\Counter;
use Google\Cloud\Firestore\Shard;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;

/**
* Tests for Firebase distributed counters
*/
class SolutionCountersTest extends TestCase
{
use TestTrait;

const COLLECTION_NAME='Shards_collection';
const SHARD_NAME='Distributed_counters';

private static $firestoreProjectId;

private static $ref;

public static function setUpBeforeClass()
{
require_once __DIR__."/../src/firestore_snippets/SolutionCounters.php";

self::$firestoreProjectId = self::requireEnv('FIRESTORE_PROJECT_ID');

$db = new FirestoreClient([
'projectId' => self::$firestoreProjectId
]);

self::$ref=$db->collection(self::COLLECTION_NAME)->document(self::SHARD_NAME);
}

protected function setUp()
{
if (!extension_loaded('grpc')) {
self::markTestSkipped('Must enable grpc extension.');
}
}


/**
* @covers Google\Cloud\Firestore\Counter::getNumShards
*/
public function testCounter()
{
$counter=new Counter(5);
$this->assertEquals(5, $counter->getNumShards());

$counter2=new Counter(1);
$this->assertEquals(1, $counter2->getNumShards());
}

/**
* @covers Google\Cloud\Firestore\Shard::getCount
*/
public function testShard()
{
$shard = new Shard(7);
$this->assertEquals(7, $shard->getCount());

$shard2 = new Shard(2);
$this->assertEquals(2, $shard2->getCount());
}

/**
* @covers Google\Cloud\Firestore\SolutionCounters::initCounter
* @covers Google\Cloud\Firestore\SolutionCounters::incrementCounter
* @covers Google\Cloud\Firestore\SolutionCounters::getCount
*/
public function testSolutionCounters()
{
$cnt=5;
SolutionCounters::initCounter(self::$ref, $cnt);

$collect = self::$ref->collection('SHARDS');
$docCollection = $collect->documents();
$docIdList=[];

foreach ($docCollection as $docSnap) {
$docIdList[] = $docSnap->id();
}
$this->assertEquals($cnt, count($docIdList));

$this->assertEquals(0, SolutionCounters::getCount(self::$ref));

SolutionCounters::incrementCounter(self::$ref, $cnt);
$this->assertEquals(1, SolutionCounters::getCount(self::$ref));

SolutionCounters::incrementCounter(self::$ref, $cnt);
$this->assertEquals(2, SolutionCounters::getCount(self::$ref));

SolutionCounters::incrementCounter(self::$ref, $cnt);
$this->assertEquals(3, SolutionCounters::getCount(self::$ref));

foreach ($docIdList as $docId){
$collect->document($docId)->delete();
}
}

/** Remove SHARD_NAME document
*/
public static function tearDownAfterClass()
{
self::$ref->delete();
}
}