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

perf: make geoNameFolder lazy #2094

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all 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
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
Loading