Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Add LocalCache implementation of the ParserCache.
Browse files Browse the repository at this point in the history
Summary: This is implementation of the local file system parser cache.

Reviewed By: bobyangyf

fbshipit-source-id: 183fc01273
  • Loading branch information
Lubomir Litchev authored and facebook-github-bot committed Oct 26, 2018
1 parent 9789359 commit 77c91ea
Show file tree
Hide file tree
Showing 11 changed files with 871 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/com/facebook/buck/parser/cache/BUCK
@@ -1,6 +1,6 @@
load("//tools/build_rules:java_rules.bzl", "java_immutables_library")

java_library(
java_immutables_library(
name = "cache",
srcs = glob([
"*.java",
Expand All @@ -9,7 +9,12 @@ java_library(
"PUBLIC",
],
deps = [
"//src/com/facebook/buck/core/config:config",
"//src/com/facebook/buck/core/util/immutables:immutables",
"//src/com/facebook/buck/core/util/log:log",
"//src/com/facebook/buck/counters:counters",
"//src/com/facebook/buck/io/filesystem:filesystem",
"//src/com/facebook/buck/parser/cache/json:json",
"//src/com/facebook/buck/util/config:config",
"//src/com/facebook/buck/util/environment:platform",
"//third-party/java/guava:guava",
Expand Down
50 changes: 50 additions & 0 deletions src/com/facebook/buck/parser/cache/ParserCache.java
@@ -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 src/com/facebook/buck/parser/cache/ParserCacheException.java
@@ -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 src/com/facebook/buck/parser/cache/impl/AbstractParserCacheConfig.java
@@ -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;
}
}
@@ -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();
}
8 changes: 7 additions & 1 deletion src/com/facebook/buck/parser/cache/impl/BUCK
@@ -1,6 +1,6 @@
load("//tools/build_rules:java_rules.bzl", "java_immutables_library")

java_library(
java_immutables_library(
name = "cache",
srcs = glob([
"*.java",
Expand All @@ -12,7 +12,13 @@ java_library(
"PUBLIC",
],
deps = [
"//src/com/facebook/buck/core/config:config",
"//src/com/facebook/buck/core/util/immutables:immutables",
"//src/com/facebook/buck/core/util/log:log",
"//src/com/facebook/buck/counters:counters",
"//src/com/facebook/buck/io/filesystem:filesystem",
"//src/com/facebook/buck/parser/cache:cache",
"//src/com/facebook/buck/parser/cache/json:json",
"//src/com/facebook/buck/util/config:config",
"//src/com/facebook/buck/util/environment:platform",
"//third-party/java/guava:guava",
Expand Down

0 comments on commit 77c91ea

Please sign in to comment.