From 8e912ba25248fc086f7982f48491b14904dd9e2f Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 1 Mar 2021 16:33:33 +0100 Subject: [PATCH] improve time-space complexity of identifying receivers --- app/realtime.php | 2 +- src/Appwrite/Realtime/Realtime.php | 28 ++++++++------------ tests/unit/Realtime/RealtimeChannelsTest.php | 5 ---- tests/unit/Realtime/RealtimeGuestTest.php | 14 ---------- tests/unit/Realtime/RealtimeTest.php | 13 --------- 5 files changed, 12 insertions(+), 50 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index eea09e065..7648cd62a 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -116,7 +116,7 @@ $server->on('workerStart', function ($server, $workerId) use (&$subscriptions, & */ $event = json_decode($payload, true); - $receivers = Realtime::identifyReceivers($event, $connections, $subscriptions); + $receivers = Realtime::identifyReceivers($event, $subscriptions); foreach ($receivers as $receiver) { if ($server->exist($receiver) && $server->isEstablished($receiver)) { diff --git a/src/Appwrite/Realtime/Realtime.php b/src/Appwrite/Realtime/Realtime.php index ba60a3332..256ad64b3 100644 --- a/src/Appwrite/Realtime/Realtime.php +++ b/src/Appwrite/Realtime/Realtime.php @@ -80,26 +80,20 @@ class Realtime * @param array $connections * @param array $subscriptions */ - static function identifyReceivers(array &$event, array &$connections, array &$subscriptions) + static function identifyReceivers(array &$event, array &$subscriptions) { $receivers = []; - foreach ($connections as $connection) { - if ($connection['projectId'] !== $event['project']) { - continue; - } - - foreach ($connection['roles'] as $role) { - if (\array_key_exists($role, $subscriptions[$event['project']])) { - foreach ($event['data']['channels'] as $channel) { - if ( - \array_key_exists($channel, $subscriptions[$event['project']][$role]) - && (\in_array($role, $event['permissions']) || \in_array('*', $event['permissions'])) - ) { - foreach (array_keys($subscriptions[$event['project']][$role][$channel]) as $ids) { - $receivers[] = $ids; - } - break; + if ($subscriptions[$event['project']]) { + foreach ($subscriptions[$event['project']] as $role => $subscription) { + foreach ($event['data']['channels'] as $channel) { + if ( + \array_key_exists($channel, $subscriptions[$event['project']][$role]) + && (\in_array($role, $event['permissions']) || \in_array('*', $event['permissions'])) + ) { + foreach (array_keys($subscriptions[$event['project']][$role][$channel]) as $ids) { + $receivers[] = $ids; } + break; } } } diff --git a/tests/unit/Realtime/RealtimeChannelsTest.php b/tests/unit/Realtime/RealtimeChannelsTest.php index afe52373b..d5b090395 100644 --- a/tests/unit/Realtime/RealtimeChannelsTest.php +++ b/tests/unit/Realtime/RealtimeChannelsTest.php @@ -160,7 +160,6 @@ class RealtimeChannelsTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -197,7 +196,6 @@ class RealtimeChannelsTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -235,7 +233,6 @@ class RealtimeChannelsTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -273,7 +270,6 @@ class RealtimeChannelsTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -303,7 +299,6 @@ class RealtimeChannelsTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); diff --git a/tests/unit/Realtime/RealtimeGuestTest.php b/tests/unit/Realtime/RealtimeGuestTest.php index d8d19c2d0..4eff23b1c 100644 --- a/tests/unit/Realtime/RealtimeGuestTest.php +++ b/tests/unit/Realtime/RealtimeGuestTest.php @@ -61,7 +61,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -72,7 +71,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -83,7 +81,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -93,7 +90,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -103,7 +99,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -113,7 +108,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -123,7 +117,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -133,7 +126,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -143,7 +135,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -153,7 +144,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -163,7 +153,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -174,7 +163,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -184,7 +172,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -195,7 +182,6 @@ class RealtimeGuestTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); diff --git a/tests/unit/Realtime/RealtimeTest.php b/tests/unit/Realtime/RealtimeTest.php index 29af833ee..a819f4cb7 100644 --- a/tests/unit/Realtime/RealtimeTest.php +++ b/tests/unit/Realtime/RealtimeTest.php @@ -83,7 +83,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -94,7 +93,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -105,7 +103,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -116,7 +113,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -127,7 +123,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -138,7 +133,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -149,7 +143,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -160,7 +153,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -171,7 +163,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -181,7 +172,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -192,7 +182,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -202,7 +191,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions ); @@ -213,7 +201,6 @@ class RealtimeTest extends TestCase $receivers = Realtime::identifyReceivers( $event, - $this->connections, $this->subscriptions );