Skip to content

Commit

Permalink
Merge pull request #2094 from nextcloud/pulsejet/fix-rgcs-folder
Browse files Browse the repository at this point in the history
perf: make geoNameFolder lazy
  • Loading branch information
artonge authored Oct 17, 2023
2 parents 3502b1d + 783888e commit 0048469
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions lib/Service/ReverseGeoCoderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@
use OCP\Http\Client\IClientService;

class ReverseGeoCoderService {
private ISimpleFolder $geoNameFolder;
private ?ISimpleFolder $geoNameFolderCache = null;
private ?NearestSearch $fsSearcher = null;
/** @var array<int, string> */
private ?array $citiesMapping = null;

public function __construct(
IAppData $appData,
private IAppData $appData,
private IClientService $clientService,
) {
try {
$this->geoNameFolder = $appData->getFolder("geonames");
} catch (NotFoundException $ex) {
$this->geoNameFolder = $appData->newFolder("geonames");
}
}

public function getPlaceForCoordinates(float $latitude, float $longitude): string {
Expand All @@ -60,6 +55,18 @@ public function getPlaceForCoordinates(float $latitude, float $longitude): strin
return $this->getPlaceNameForPlaceId($result[0]->getId());
}

private function geoNameFolder(): ISimpleFolder {
if ($this->geoNameFolderCache === null) {
try {
$this->geoNameFolderCache = $this->appData->getFolder("geonames");
} catch (NotFoundException $ex) {
$this->geoNameFolderCache = $this->appData->newFolder("geonames");
}
}

return $this->geoNameFolderCache;
}

private function getPlaceNameForPlaceId(int $placeId): string {
if ($this->citiesMapping === null) {
$this->downloadCities1000();
Expand All @@ -74,7 +81,7 @@ private function getPlaceNameForPlaceId(int $placeId): string {
}

private function downloadCities1000(bool $force = false): void {
if ($this->geoNameFolder->fileExists('cities1000.csv') && !$force) {
if ($this->geoNameFolder()->fileExists('cities1000.csv') && !$force) {
return;
}

Expand All @@ -94,7 +101,7 @@ private function downloadCities1000(bool $force = false): void {
$cities1000TxtSteam = $zip->getStream('cities1000.txt');

// Dump the txt file info into a smaller csv file.
$destinationStream = $this->geoNameFolder->newFile('cities1000.csv')->write();
$destinationStream = $this->geoNameFolder()->newFile('cities1000.csv')->write();

while (($fields = fgetcsv($cities1000TxtSteam, 0, " ")) !== false) {
$result = fputcsv(
Expand All @@ -116,7 +123,7 @@ private function downloadCities1000(bool $force = false): void {
}

private function loadCities1000(): array {
$csvStream = $this->geoNameFolder->getFile('cities1000.csv')->read();
$csvStream = $this->geoNameFolder()->getFile('cities1000.csv')->read();
$cities = [];

while (($fields = fgetcsv($csvStream)) !== false) {
Expand All @@ -132,7 +139,7 @@ private function loadCities1000(): array {
}

public function buildKDTree($force = false): void {
if ($this->geoNameFolder->fileExists('cities1000.bin') && !$force) {
if ($this->geoNameFolder()->fileExists('cities1000.bin') && !$force) {
return;
}

Expand All @@ -150,7 +157,7 @@ public function buildKDTree($force = false): void {
$kdTreeTmpFileName = tempnam(sys_get_temp_dir(), "nextcloud_photos_");
$persister->convert($tree, $kdTreeTmpFileName);
$kdTreeString = file_get_contents($kdTreeTmpFileName);
$this->geoNameFolder->newFile('cities1000.bin', $kdTreeString);
$this->geoNameFolder()->newFile('cities1000.bin', $kdTreeString);
}

private function loadKdTree(): void {
Expand All @@ -159,7 +166,7 @@ private function loadKdTree(): void {
}

$this->buildKDTree();
$kdTreeFileContent = $this->geoNameFolder->getFile("cities1000.bin")->getContent();
$kdTreeFileContent = $this->geoNameFolder()->getFile("cities1000.bin")->getContent();
$kdTreeTmpFileName = tempnam(sys_get_temp_dir(), "nextcloud_photos_");
file_put_contents($kdTreeTmpFileName, $kdTreeFileContent);
$fsTree = new FSKDTree($kdTreeTmpFileName, new ItemFactory());
Expand Down

0 comments on commit 0048469

Please sign in to comment.