-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Move Polylines to files when accessed (#2949)
- Loading branch information
Showing
8 changed files
with
191 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\PolyLine; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class PolylinesToFiles extends Command | ||
{ | ||
protected $signature = 'app:polylines-to-files'; | ||
protected $description = 'Convert polylines to files'; | ||
|
||
public function handle(): int { | ||
$start = microtime(true); | ||
$rows = DB::table('poly_lines') | ||
->where('polyline', '!=', '{}') | ||
->get(); | ||
$this->info('Found ' . $rows->count() . ' polylines.'); | ||
$affectedRows = 0; | ||
|
||
// get 100 rows at a time | ||
foreach ($rows->chunk(100) as $chunk) { | ||
$ids = $chunk->pluck('id')->toArray(); | ||
$affectedRows += PolyLine::whereIn('id', $ids)->get()->map(function($polyline) { | ||
$polyline->polyline; // trigger the __get method | ||
return $polyline; | ||
})->count(); | ||
$this->output->write('.'); | ||
} | ||
$this->output->newLine(); | ||
|
||
$time_elapsed_secs = microtime(true) - $start; | ||
Log::debug($affectedRows . ' polylines converted in ' . $time_elapsed_secs . ' seconds.'); | ||
$this->info($affectedRows . ' polylines converted in ' . $time_elapsed_secs . ' seconds.'); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\PolyLine; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class PolylineStorageService | ||
{ | ||
private Filesystem $disk; | ||
private ?string $content = null; | ||
|
||
public function __construct() { | ||
$this->disk = Storage::build([ | ||
'driver' => config('trwl.polyline_storage_driver'), | ||
'root' => storage_path(config('trwl.polyline_storage_path')), | ||
]); | ||
} | ||
|
||
private function store(string $content, string $hash = null): bool { | ||
$hash = $hash ?? md5($content); | ||
|
||
if ($this->disk->exists($this->storageName($hash))) { | ||
return true; | ||
} | ||
return $this->disk->put($this->storageName($hash), $content); | ||
} | ||
|
||
public function get(string $hash): string { | ||
if ($this->content !== null) { | ||
return $this->content; | ||
} | ||
if (!$this->disk->exists($this->storageName($hash))) { | ||
return ''; | ||
} | ||
|
||
return $this->disk->get($this->storageName($hash)); | ||
} | ||
|
||
public function delete(string $hash): void { | ||
$this->disk->delete($this->storageName($hash)); | ||
} | ||
|
||
public function getOrCreate(PolyLine $polyLine): string { | ||
$content = $polyLine->getAttribute('polyline'); | ||
$hash = $polyLine->getAttribute('hash'); | ||
|
||
if (!$this->empty($content)) { | ||
$success = $this->store($content, $hash); | ||
|
||
if ($success && config('trwl.polyline_clear_after_copy')) { | ||
$polyLine->update(['polyline' => '{}']); | ||
} | ||
} | ||
|
||
return $this->get($hash); | ||
} | ||
|
||
/** | ||
* Get the storage name for a given hash. | ||
* This breaks the hash into 4 characters and uses them as subdirectories | ||
* to avoid having too many files in one directory. | ||
*/ | ||
private function storageName(string $hash): string { | ||
return substr($hash, 0, 2) . '/' . substr($hash, 2, 2) . '/' . $hash; | ||
} | ||
|
||
private function empty(string $content): bool { | ||
$content = trim($content); | ||
return empty($content) || $content === '{}' || $content === '[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters