Search in sources :

Example 26 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class FakeProjectFilesystem method getMtimeSortedMatchingDirectoryContents.

@Override
public ImmutableSortedSet<Path> getMtimeSortedMatchingDirectoryContents(final Path pathRelativeToProjectRoot, String globPattern) throws IOException {
    Preconditions.checkState(isDirectory(pathRelativeToProjectRoot));
    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
    return fileContents.keySet().stream().filter(i -> i.getParent().equals(pathRelativeToProjectRoot) && pathMatcher.matches(i.getFileName())).sorted((f0, f1) -> {
        try {
            return getLastModifiedTimeFetcher().getLastModifiedTime(f1).compareTo(getLastModifiedTimeFetcher().getLastModifiedTime(f0));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }).collect(MoreCollectors.toImmutableSortedSet());
}
Also used : Manifest(java.util.jar.Manifest) NoSuchFileException(java.nio.file.NoSuchFileException) ImmutableCollection(com.google.common.collect.ImmutableCollection) FileTime(java.nio.file.attribute.FileTime) Random(java.util.Random) JarFile(java.util.jar.JarFile) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ByteArrayInputStream(java.io.ByteArrayInputStream) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) Clock(com.facebook.buck.timing.Clock) PathMatcher(java.nio.file.PathMatcher) BigInteger(java.math.BigInteger) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) FileVisitor(java.nio.file.FileVisitor) Platform(com.facebook.buck.util.environment.Platform) NotLinkException(java.nio.file.NotLinkException) Set(java.util.Set) FileAttribute(java.nio.file.attribute.FileAttribute) FileSystem(java.nio.file.FileSystem) DefaultProjectFilesystemDelegate(com.facebook.buck.io.DefaultProjectFilesystemDelegate) FileVisitResult(java.nio.file.FileVisitResult) FakeClock(com.facebook.buck.timing.FakeClock) List(java.util.List) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) CopyOption(java.nio.file.CopyOption) Iterables(com.google.common.collect.Iterables) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Hashing(com.google.common.hash.Hashing) LinkedHashMap(java.util.LinkedHashMap) LinkOption(java.nio.file.LinkOption) JarEntry(java.util.jar.JarEntry) ImmutableList(com.google.common.collect.ImmutableList) Jimfs(com.google.common.jimfs.Jimfs) JarInputStream(java.util.jar.JarInputStream) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) OutputStream(java.io.OutputStream) Charsets(com.google.common.base.Charsets) Configuration(com.google.common.jimfs.Configuration) Iterator(java.util.Iterator) Files(java.nio.file.Files) HashCode(com.google.common.hash.HashCode) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MorePaths(com.facebook.buck.io.MorePaths) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) MoreFiles(com.facebook.buck.io.MoreFiles) Comparator(java.util.Comparator) FileSystems(java.nio.file.FileSystems) InputStream(java.io.InputStream) PathMatcher(java.nio.file.PathMatcher) IOException(java.io.IOException)

Example 27 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project bazel by bazelbuild.

the class AndroidSdkRepositoryFunction method getAndroidDeviceSystemImageDirs.

/**
   * Gets PathFragments for /sdk/system-images/*&#47;*&#47;*, which are the directories in the
   * SDK that contain system images needed for android_device.
   *
   * If the sdk/system-images directory does not exist, an empty set is returned.
   */
private static ImmutableSortedSet<PathFragment> getAndroidDeviceSystemImageDirs(Path androidSdkPath, Environment env) throws RepositoryFunctionException, InterruptedException {
    if (!androidSdkPath.getRelative(SYSTEM_IMAGES_DIR).exists()) {
        return ImmutableSortedSet.of();
    }
    DirectoryListingValue systemImagesDirectoryValue = AndroidRepositoryUtils.getDirectoryListing(androidSdkPath, SYSTEM_IMAGES_DIR, env);
    if (systemImagesDirectoryValue == null) {
        return null;
    }
    ImmutableMap<PathFragment, DirectoryListingValue> apiLevelSystemImageDirs = getSubdirectoryListingValues(androidSdkPath, SYSTEM_IMAGES_DIR, systemImagesDirectoryValue, env);
    if (apiLevelSystemImageDirs == null) {
        return null;
    }
    ImmutableSortedSet.Builder<PathFragment> pathFragments = ImmutableSortedSet.naturalOrder();
    for (PathFragment apiLevelDir : apiLevelSystemImageDirs.keySet()) {
        ImmutableMap<PathFragment, DirectoryListingValue> apiTypeSystemImageDirs = getSubdirectoryListingValues(androidSdkPath, apiLevelDir, apiLevelSystemImageDirs.get(apiLevelDir), env);
        if (apiTypeSystemImageDirs == null) {
            return null;
        }
        for (PathFragment apiTypeDir : apiTypeSystemImageDirs.keySet()) {
            for (Dirent architectureSystemImageDir : apiTypeSystemImageDirs.get(apiTypeDir).getDirents()) {
                pathFragments.add(apiTypeDir.getRelative(architectureSystemImageDir.getName()));
            }
        }
    }
    return pathFragments.build();
}
Also used : DirectoryListingValue(com.google.devtools.build.lib.skyframe.DirectoryListingValue) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Dirent(com.google.devtools.build.lib.vfs.Dirent)

Example 28 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class OfflineScribeLoggerTest method unsentLinesStoredForOffline.

@Test
public void unsentLinesStoredForOffline() throws Exception {
    final String whitelistedCategory = "whitelisted_category";
    final String whitelistedCategory2 = "whitelisted_category_2";
    final String blacklistedCategory = "blacklisted_category";
    final ImmutableList<String> blacklistCategories = ImmutableList.of(blacklistedCategory);
    final int maxScribeOfflineLogsKB = 7;
    final ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    final Path logDir = filesystem.getBuckPaths().getOfflineLogDir();
    final ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
    String[] ids = { "test1", "test2", "test3", "test4" };
    char[] longLineBytes = new char[1000];
    Arrays.fill(longLineBytes, 'A');
    String longLine = new String(longLineBytes);
    char[] tooLongLineBytes = new char[6000];
    Arrays.fill(longLineBytes, 'A');
    String tooLongLine = new String(tooLongLineBytes);
    // As we set max space for logs to 7KB, then we expect storing data with 2 'longLine' (which,
    // given UTF-8 is used, will be ~ 2 * 1KB) to succeed. We then expect subsequent attempt to
    // store data with 'tooLongLine' (~6KB) to fail. We also expect that logfile created by first
    // fakeLogger ('test1' id) will be removed as otherwise data from last logger would not fit.
    //
    // Note that code sending already stored offline logs will be triggered by the first log() as
    // it succeeds, but further failing log() (and initiating storing) will stop sending. Hence, no
    // logs will be deleted due to the sending routine.
    FakeFailingOfflineScribeLogger fakeLogger = null;
    for (String id : ids) {
        fakeLogger = new FakeFailingOfflineScribeLogger(blacklistCategories, maxScribeOfflineLogsKB, filesystem, logDir, objectMapper, new BuildId(id));
        // Simulate network issues occurring for some of sending attempts (all after first one).
        // Logging succeeds.
        fakeLogger.log(whitelistedCategory, ImmutableList.of("hello world 1", "hello world 2"));
        // Logging fails.
        fakeLogger.log(whitelistedCategory, ImmutableList.of("hello world 3", "hello world 4"));
        // Event with blacklisted category for offline logging.
        fakeLogger.log(blacklistedCategory, ImmutableList.of("hello world 5", "hello world 6"));
        // Logging fails.
        fakeLogger.log(whitelistedCategory2, ImmutableList.of(longLine, longLine));
        // Logging fails, but offline logging rejects data as well - too big.
        fakeLogger.log(whitelistedCategory2, ImmutableList.of(tooLongLine));
        fakeLogger.close();
    }
    // Check correct logs are in the directory (1st log removed).
    Path[] expectedLogPaths = FluentIterable.from(ImmutableList.copyOf(ids)).transform(id -> filesystem.resolve(logDir.resolve(LOGFILE_PREFIX + id + LOGFILE_SUFFIX))).toArray(Path.class);
    ImmutableSortedSet<Path> logs = filesystem.getMtimeSortedMatchingDirectoryContents(logDir, LOGFILE_PATTERN);
    assertThat(logs, Matchers.allOf(hasItem(expectedLogPaths[1]), hasItem(expectedLogPaths[2]), hasItem(expectedLogPaths[3]), IsIterableWithSize.<Path>iterableWithSize(3)));
    // Check that last logger logged correct data.
    assertEquals(3, fakeLogger.getAttemptStoringCategoriesWithLinesCount());
    InputStream logFile = fakeLogger.getStoredLog();
    String[] whitelistedCategories = { whitelistedCategory, whitelistedCategory2 };
    String[][] whitelistedLines = { { "hello world 3", "hello world 4" }, { longLine, longLine } };
    Iterator<ScribeData> it = null;
    try {
        it = new ObjectMapper().readValues(new JsonFactory().createParser(logFile), ScribeData.class);
    } catch (Exception e) {
        fail("Obtaining iterator for reading the log failed.");
    }
    int dataNum = 0;
    try {
        while (it.hasNext()) {
            assertTrue(dataNum < 2);
            ScribeData data = it.next();
            assertThat(data.getCategory(), is(whitelistedCategories[dataNum]));
            assertThat(data.getLines(), Matchers.allOf(hasItem(whitelistedLines[dataNum][0]), hasItem(whitelistedLines[dataNum][1]), IsIterableWithSize.<String>iterableWithSize(2)));
            dataNum++;
        }
    } catch (Exception e) {
        fail("Reading stored offline log failed.");
    }
    logFile.close();
    assertEquals(2, dataNum);
}
Also used : Path(java.nio.file.Path) LOGFILE_PREFIX(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_PREFIX) Arrays(java.util.Arrays) BufferedInputStream(java.io.BufferedInputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ObjectMappers(com.facebook.buck.util.ObjectMappers) LOGFILE_SUFFIX(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_SUFFIX) TemporaryPaths(com.facebook.buck.testutil.integration.TemporaryPaths) BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Matchers.everyItem(org.hamcrest.Matchers.everyItem) ImmutableList(com.google.common.collect.ImmutableList) BuildId(com.facebook.buck.model.BuildId) FluentIterable(com.google.common.collect.FluentIterable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) ScribeLogger(com.facebook.buck.util.network.ScribeLogger) Assert.fail(org.junit.Assert.fail) LOGFILE_PATTERN(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_PATTERN) Pair(com.facebook.buck.model.Pair) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Charsets(com.google.common.base.Charsets) ObjectArrays(com.google.common.collect.ObjectArrays) Iterator(java.util.Iterator) FakeFailingScribeLogger(com.facebook.buck.util.network.FakeFailingScribeLogger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Rule(org.junit.Rule) IsIterableWithSize(org.hamcrest.collection.IsIterableWithSize) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BuildId(com.facebook.buck.model.BuildId) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 29 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project rhino by PLOS.

the class ArticleOverview method build.

public static ArticleOverview build(ArticleIdentifier articleId, Collection<ArticleIngestion> ingestions, Collection<ArticleRevision> revisions) {
    // Initialize every ingestion number with an empty list of revisions, then fill in revisions.
    Map<Integer, Collection<Integer>> ingestionTable = ingestions.stream().collect(Collectors.toMap(ArticleIngestion::getIngestionNumber, ingestion -> new ArrayList<>(1)));
    for (ArticleRevision revision : revisions) {
        int ingestionKey = revision.getIngestion().getIngestionNumber();
        ingestionTable.get(ingestionKey).add(revision.getRevisionNumber());
    }
    Map<Integer, Integer> revisionTable = revisions.stream().collect(Collectors.toMap(ArticleRevision::getRevisionNumber, revision -> revision.getIngestion().getIngestionNumber()));
    return new ArticleOverview(articleId, Maps.transformValues(ingestionTable, ImmutableSortedSet::copyOf), revisionTable);
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) Collection(java.util.Collection) Map(java.util.Map) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) ArrayList(java.util.ArrayList) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 30 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project gerrit by GerritCodeReview.

the class PostHashtags method applyImpl.

@Override
protected Response<ImmutableSortedSet<String>> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource req, HashtagsInput input) throws RestApiException, UpdateException, PermissionBackendException {
    req.permissions().check(ChangePermission.EDIT_HASHTAGS);
    try (BatchUpdate bu = updateFactory.create(db.get(), req.getChange().getProject(), req.getControl().getUser(), TimeUtil.nowTs())) {
        SetHashtagsOp op = hashtagsFactory.create(input);
        bu.addOp(req.getId(), op);
        bu.execute();
        return Response.<ImmutableSortedSet<String>>ok(op.getUpdatedHashtags());
    }
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Aggregations

ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)51 BuildTarget (com.facebook.buck.model.BuildTarget)26 BuildRule (com.facebook.buck.rules.BuildRule)26 Path (java.nio.file.Path)25 SourcePath (com.facebook.buck.rules.SourcePath)22 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)21 ImmutableList (com.google.common.collect.ImmutableList)21 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)19 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 Optional (java.util.Optional)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)15 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)14 Map (java.util.Map)13 Flavor (com.facebook.buck.model.Flavor)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 Preconditions (com.google.common.base.Preconditions)11 Suppliers (com.google.common.base.Suppliers)10 TargetGraph (com.facebook.buck.rules.TargetGraph)9