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());
}
use of com.google.common.collect.ImmutableSortedSet in project bazel by bazelbuild.
the class AndroidSdkRepositoryFunction method getAndroidDeviceSystemImageDirs.
/**
* Gets PathFragments for /sdk/system-images/*/*/*, 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();
}
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);
}
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);
}
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());
}
}
Aggregations