From 2f9746c32b2de922c18f9e3837d840e2782f747f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 12 Sep 2014 07:06:12 -0700 Subject: [PATCH] Relax City and Country database checks The strict database checks broke the per-continent City databases, and adding all of those to an array is a bit much (and slow). Closes #23. --- bin/benchmark.php | 26 +++++++++++++++++++++ src/GeoIp2/Database/Reader.php | 42 +++++++++++----------------------- 2 files changed, 39 insertions(+), 29 deletions(-) create mode 100755 bin/benchmark.php diff --git a/bin/benchmark.php b/bin/benchmark.php new file mode 100755 index 0000000..1458ab6 --- /dev/null +++ b/bin/benchmark.php @@ -0,0 +1,26 @@ +#!/usr/bin/env php + +city($ip); + } catch (AddressNotFoundException $e) { + } + if ($i % 1000 == 0) { + print($i . ' ' . $ip . "\n"); + } +} +$endTime = microtime(true); + +$duration = $endTime - $startTime; +print('Requests per second: ' . $count / $duration . "\n"); diff --git a/src/GeoIp2/Database/Reader.php b/src/GeoIp2/Database/Reader.php index 621194a..8201e1e 100644 --- a/src/GeoIp2/Database/Reader.php +++ b/src/GeoIp2/Database/Reader.php @@ -71,11 +71,7 @@ class Reader implements ProviderInterface */ public function city($ipAddress) { - return $this->modelFor( - 'City', - array('GeoLite2-City', 'GeoIP2-City'), - $ipAddress - ); + return $this->modelFor('City', 'City', $ipAddress); } /** @@ -92,11 +88,7 @@ class Reader implements ProviderInterface */ public function country($ipAddress) { - return $this->modelFor( - 'Country', - array('GeoLite2-Country', 'GeoIP2-Country'), - $ipAddress - ); + return $this->modelFor('Country', 'Country', $ipAddress); } public function connectionType($ipAddress) @@ -126,16 +118,9 @@ class Reader implements ProviderInterface ); } - private function modelFor($class, $types, $ipAddress) + private function modelFor($class, $type, $ipAddress) { - if (!in_array($this->metadata()->databaseType, $types)) { - $method = lcfirst($class); - throw new \BadMethodCallException( - "The $method method cannot be used to open a " - . $this->metadata()->databaseType . " database" - ); - } - $record = $this->getRecord($ipAddress); + $record = $this->getRecord($class, $type, $ipAddress); $record['traits']['ip_address'] = $ipAddress; $class = "GeoIp2\\Model\\" . $class; @@ -145,15 +130,7 @@ class Reader implements ProviderInterface private function flatModelFor($class, $type, $ipAddress) { - if ($this->metadata()->databaseType !== $type) { - $method = lcfirst($class); - throw new \BadMethodCallException( - "The $method method cannot be used to open a " - . $this->metadata()->databaseType . " database" - ); - } - - $record = $this->getRecord($ipAddress); + $record = $this->getRecord($class, $type, $ipAddress); $record['ip_address'] = $ipAddress; $class = "GeoIp2\\Model\\" . $class; @@ -161,8 +138,15 @@ class Reader implements ProviderInterface return new $class($record); } - private function getRecord($ipAddress) + private function getRecord($class, $type, $ipAddress) { + if (strpos($this->metadata()->databaseType, $type) === false) { + $method = lcfirst($class); + throw new \BadMethodCallException( + "The $method method cannot be used to open a " + . $this->metadata()->databaseType . " database" + ); + } $record = $this->dbReader->get($ipAddress); if ($record === null) { throw new AddressNotFoundException(