This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LocalCache implementation of the ParserCache.
Summary: This is implementation of the local file system parser cache. Reviewed By: bobyangyf fbshipit-source-id: 183fc01273
- Loading branch information
1 parent
9789359
commit 77c91ea
Showing
11 changed files
with
871 additions
and
18 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,50 @@ | ||
/* | ||
* Copyright 2018-present Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.facebook.buck.parser.cache; | ||
|
||
import com.facebook.buck.parser.api.BuildFileManifest; | ||
import com.google.common.hash.HashCode; | ||
|
||
/** This is the main interface for interacting with the cache. */ | ||
public interface ParserCache { | ||
|
||
/** | ||
* Stores a {@link BuildFileManifest} object to the cache. | ||
* | ||
* @param weakFingerprint the weak fingerprint for the {@code buildFileManifest}. | ||
* @param strongFingerprint the strong fingerprint for the {@code buildFileManifest}. | ||
* @param buildFileManifest the {@link BuildFileManifest} to store in the cache. | ||
* @throws ParserCacheException thrown when there is an error storing the {@link | ||
* BuildFileManifest} | ||
*/ | ||
void storeBuildFileManifest( | ||
HashCode weakFingerprint, HashCode strongFingerprint, BuildFileManifest buildFileManifest) | ||
throws ParserCacheException; | ||
|
||
/** | ||
* Gets a cached {@link BuildFileManifest} if one is available, based on passed in parameters. | ||
* | ||
* @param weakFingerprint the weak fingerprint for the {@code buildFileManifest}. | ||
* @param strongFingerprint the strong fingerprint for the {@code buildFileManifest}. | ||
* @return an {@link BuildFileManifest} object status operation if the operation is successful. In | ||
* case of failure an appropriate exception is thrown. | ||
* @throws ParserCacheException thrown when there is an error constructing the {@link | ||
* BuildFileManifest} from the {@link ParserCache}. | ||
*/ | ||
BuildFileManifest getBuildFileManifest(HashCode weakFingerprint, HashCode strongFingerprint) | ||
throws ParserCacheException; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/com/facebook/buck/parser/cache/ParserCacheException.java
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,61 @@ | ||
/* | ||
* Copyright 2018-present Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.facebook.buck.parser.cache; | ||
|
||
/** This exception is thrown when there are failures while doing {@link ParserCache} operations. */ | ||
public class ParserCacheException extends Exception { | ||
|
||
/** | ||
* Constructs a {@code ParserCacheException} object | ||
* | ||
* @param message the message of the Exception. | ||
*/ | ||
public ParserCacheException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructs a {@code ParserCacheException} object | ||
* | ||
* @param cause the cause for this exception | ||
* @param message the message of the Exception. | ||
*/ | ||
public ParserCacheException(Throwable cause, String message) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Constructs a {@code ParserCacheException} object | ||
* | ||
* @param format a format {@link String} to create the {@link ParserCacheException} message. | ||
* @param parameters the parameters for formatting the message. | ||
*/ | ||
public ParserCacheException(String format, Object... parameters) { | ||
super(String.format(format, parameters)); | ||
} | ||
|
||
/** | ||
* Constructs a {@code ParserCacheException} object | ||
* | ||
* @param cause the cause for this exception | ||
* @param format a format {@link String} to create the {@link ParserCacheException} message. | ||
* @param parameters the parameters for formatting the message. | ||
*/ | ||
public ParserCacheException(Throwable cause, String format, Object... parameters) { | ||
super(String.format(format, parameters), cause); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
src/com/facebook/buck/parser/cache/impl/AbstractParserCacheConfig.java
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,119 @@ | ||
/* | ||
* Copyright 2018-present Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.facebook.buck.parser.cache.impl; | ||
|
||
import com.facebook.buck.core.config.BuckConfig; | ||
import com.facebook.buck.core.config.ConfigView; | ||
import com.facebook.buck.core.util.immutables.BuckStyleImmutable; | ||
import com.facebook.buck.core.util.log.Logger; | ||
import com.facebook.buck.io.filesystem.ProjectFilesystem; | ||
import com.facebook.buck.parser.cache.ParserCacheException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import org.immutables.value.Value; | ||
|
||
/** Class that implements the {@link com.facebook.buck.parser.cache.ParserCache} configuration. */ | ||
@Value.Immutable(builder = false, copy = false) | ||
@BuckStyleImmutable | ||
public abstract class AbstractParserCacheConfig implements ConfigView<BuckConfig> { | ||
private static final Logger LOG = Logger.get(AbstractParserCacheConfig.class); | ||
|
||
public static final String PARSER_CACHE_SECTION_NAME = "parser"; | ||
public static final String PARSER_CACHE_LOCAL_LOCATION_NAME = "dir"; | ||
public static final String PARSER_CACHE_LOCAL_MODE_NAME = "dir_mode"; | ||
public static final String DEFAULT_PARSER_CACHE_MODE_VALUE = "NONE"; | ||
|
||
@Override | ||
@Value.Parameter | ||
public abstract BuckConfig getDelegate(); | ||
|
||
private ParserCacheAccessMode getCacheMode() throws ParserCacheException { | ||
String cacheMode = | ||
getDelegate() | ||
.getValue(PARSER_CACHE_SECTION_NAME, PARSER_CACHE_LOCAL_MODE_NAME) | ||
.orElse(DEFAULT_PARSER_CACHE_MODE_VALUE); | ||
ParserCacheAccessMode result; | ||
try { | ||
result = ParserCacheAccessMode.valueOf(cacheMode.toUpperCase()); | ||
} catch (IllegalArgumentException e) { | ||
throw new ParserCacheException( | ||
"Unusable cache.%s: '%s'", PARSER_CACHE_LOCAL_MODE_NAME, cacheMode); | ||
} | ||
return result; | ||
} | ||
|
||
/** Obtains a {@link AbstractParserDirCacheEntry} from the {@link BuckConfig}. */ | ||
@Value.Lazy | ||
protected AbstractParserDirCacheEntry obtainDirEntry() { | ||
String dirLocation = | ||
getDelegate() | ||
.getValue(PARSER_CACHE_SECTION_NAME, PARSER_CACHE_LOCAL_LOCATION_NAME) | ||
.orElse(null); | ||
|
||
if (dirLocation == null) { | ||
return ParserDirCacheEntry.of( | ||
Optional.empty(), ParserCacheAccessMode.NONE); // Disable local cache. | ||
} | ||
|
||
ParserCacheAccessMode parserCacheAccessMode = ParserCacheAccessMode.NONE; | ||
try { | ||
parserCacheAccessMode = getCacheMode(); | ||
} catch (ParserCacheException t) { | ||
LOG.error(t, "Could not get ParserCacheAccessMode for AbstractCacheConfig."); | ||
} | ||
|
||
if (parserCacheAccessMode == ParserCacheAccessMode.NONE) { | ||
return ParserDirCacheEntry.of( | ||
Optional.empty(), ParserCacheAccessMode.NONE); // Disable local cache. | ||
} | ||
|
||
Path pathToCacheDir; | ||
ProjectFilesystem filesystem = getDelegate().getFilesystem(); | ||
if (dirLocation.isEmpty()) { | ||
pathToCacheDir = filesystem.getBuckPaths().getBuckOut().resolve(dirLocation); | ||
} else { | ||
pathToCacheDir = filesystem.getPath(dirLocation); | ||
if (!pathToCacheDir.isAbsolute()) { | ||
pathToCacheDir = filesystem.getBuckPaths().getBuckOut().resolve(pathToCacheDir); | ||
} | ||
} | ||
return ParserDirCacheEntry.of(Optional.of(pathToCacheDir), parserCacheAccessMode); | ||
} | ||
|
||
/** @returns the location for the local cache. */ | ||
@Value.Lazy | ||
public Optional<Path> getDirCacheLocation() { | ||
AbstractParserDirCacheEntry parserDirCacheEntry = obtainDirEntry(); | ||
if (parserDirCacheEntry != null) { | ||
return parserDirCacheEntry.getDirCacheLocation(); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
/** @returns whether the local cache is enabled. */ | ||
@Value.Lazy | ||
public boolean isDirParserCacheEnabled() { | ||
AbstractParserDirCacheEntry parserDirCacheEntry = obtainDirEntry(); | ||
|
||
if (!parserDirCacheEntry.getDirCacheLocation().isPresent() | ||
|| parserDirCacheEntry.getDirCacheMode() == ParserCacheAccessMode.NONE) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/com/facebook/buck/parser/cache/impl/AbstractParserDirCacheEntry.java
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,35 @@ | ||
/* | ||
* Copyright 2018-present Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.facebook.buck.parser.cache.impl; | ||
|
||
import com.facebook.buck.core.util.immutables.BuckStylePackageVisibleImmutable; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import org.immutables.value.Value; | ||
|
||
/** Class that implements the parser cache configuration. */ | ||
@Value.Immutable(builder = false, copy = false) | ||
@BuckStylePackageVisibleImmutable | ||
abstract class AbstractParserDirCacheEntry { | ||
/** Gets the location for local cache. */ | ||
@Value.Parameter | ||
abstract Optional<Path> getDirCacheLocation(); | ||
|
||
/** Gets the mode set for this cache entry. */ | ||
@Value.Parameter | ||
abstract ParserCacheAccessMode getDirCacheMode(); | ||
} |
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
Oops, something went wrong.