Skip to content

Commit d288c0c

Browse files
committed
Add deploy cache data in chunks
1 parent 96f2759 commit d288c0c

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/DeployCache.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,72 @@ public static function createTable(): void {
4444
);
4545
}
4646

47+
/**
48+
* Adds deploy cache data to PathInfos that don't
49+
* already have it.
50+
*
51+
* @param \Iterator<PathInfo>
52+
* @return \Iterator<PathInfo>
53+
*/
54+
public static function addCacheData(
55+
\Iterator $path_infos,
56+
): \Iterator {
57+
global $wpdb;
58+
59+
$chunks = Utils::chunkIterator( $path_infos, 200 );
60+
foreach ( $chunks as $chunk ) {
61+
$to_lookup = [];
62+
foreach ( $chunk as $pi ) {
63+
if ( isset( $pi->deploy_cache ) ) {
64+
yield $pi;
65+
} else {
66+
$to_lookup[] = $pi;
67+
}
68+
}
69+
70+
if ( empty( $to_lookup ) ) {
71+
continue;
72+
}
73+
74+
$path_hashes = array_map(
75+
function ( PathInfo $pi ): string {
76+
return $pi->getPathHash();
77+
},
78+
$to_lookup,
79+
);
80+
81+
$placeholders = implode( ',', array_fill( 0, count( $path_hashes ), '%s' ) );
82+
$table_name = self::getTableName();
83+
$sql = "SELECT path_hash,data_hash,namespace
84+
FROM $table_name
85+
WHERE path_hash IN ($placeholders)";
86+
$query = $wpdb->prepare( $sql, ...$path_hashes );
87+
$results = $wpdb->get_results( $query );
88+
89+
$results_by_hash = [];
90+
foreach ( $results as $result ) {
91+
$current = $results_by_hash[ $result->path_hash ] ?? [];
92+
$current[ $result->namespace ] = $result->data_hash;
93+
$results_by_hash[ $result->path_hash ] = $current;
94+
}
95+
foreach ( $to_lookup as $pi ) {
96+
$cached = $results_by_hash[ $pi->getPathHash() ] ?? null;
97+
98+
if ( $cached ) {
99+
if ( STATIC_DEPLOY_DEBUG ) {
100+
WsLog::d(
101+
'Adding deploy cache data for '
102+
. $pi->path . ': ' . json_encode( $cached )
103+
);
104+
}
105+
yield $pi->withDeployCache( $cached );
106+
} else {
107+
yield $pi;
108+
}
109+
}
110+
}
111+
}
112+
47113
public static function addFile(
48114
string $path,
49115
string $ns = self::DEFAULT_NAMESPACE,

src/DeployerTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public function uploadFiles( string $processed_site_path ): void {
106106

107107
$redirects = CrawledFiles::listRedirects();
108108

109-
self::uploadFilesIter( $path_infos( $files, $redirects ) );
109+
$pis = $path_infos( $files, $redirects );
110+
$pis = DeployCache::addCacheData( $pis );
111+
self::uploadFilesIter( $pis );
110112
}
111113
}

src/DirectDeployer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public function deployPaths(
106106
$crawled = CrawledFiles::addPathsIter( $crawled );
107107
$crawled = $this->url_discovery->discoverURLs( $crawled );
108108

109-
$processed = $this->processor->processIter( $crawled );
110-
$this->deployer->uploadFilesIter( $processed );
109+
$pis = $this->processor->processIter( $crawled );
110+
$pis = DeployCache::addCacheData( $pis );
111+
$this->deployer->uploadFilesIter( $pis );
111112
}
112113
}

0 commit comments

Comments
 (0)