/// <inheritdoc /> public IEnumerable <Task <Possible <StrongFingerprint, Failure> > > EnumerateStrongFingerprints( WeakFingerprintHash weak, UrgencyHint urgencyHint, Guid activityId) { // TODO: Extend IAsyncEnumerable up through EnumerateStrongFingerprints var tcs = TaskSourceSlim.Create <IEnumerable <GetSelectorResult> >(); yield return(Task.Run( async() => { try { var results = await ReadOnlyCacheSession.GetSelectors(new Context(Logger), weak.ToMemoization(), CancellationToken.None).ToList(); tcs.SetResult(results); return results.Any() ? results.First().FromMemoization(weak, CacheId) : StrongFingerprintSentinel.Instance; } catch (Exception ex) { tcs.SetException(ex); throw; } })); // For now, callers should always await the first task before enumerating the rest Contract.Assert(tcs.Task.IsCompleted); IEnumerable <GetSelectorResult> otherResults = tcs.Task.GetAwaiter().GetResult(); foreach (var otherResult in otherResults.Skip(1)) { yield return(Task.FromResult(otherResult.FromMemoization(weak, CacheId))); } }
public async Task <Possible <string, Failure> > ProduceFileAsync( CasHash hash, string filename, FileState fileState, UrgencyHint urgencyHint, Guid activityId, CancellationToken cancellationToken) { var result = await ReadOnlyCacheSession.PlaceFileAsync( new Context(Logger), hash.ToMemoization(), new BuildXL.Cache.ContentStore.Interfaces.FileSystem.AbsolutePath(filename), fileState == FileState.ReadOnly?FileAccessMode.ReadOnly : FileAccessMode.Write, ReplaceExistingOnPlaceFile?FileReplacementMode.ReplaceExisting : FileReplacementMode.FailIfExists, fileState.ToMemoization(), cancellationToken); switch (result.Code) { case PlaceFileResult.ResultCode.PlacedWithHardLink: case PlaceFileResult.ResultCode.PlacedWithCopy: return(filename); case PlaceFileResult.ResultCode.NotPlacedAlreadyExists: return(new FileAlreadyExistsFailure(CacheId, hash, filename)); case PlaceFileResult.ResultCode.NotPlacedContentNotFound: return(new NoCasEntryFailure(CacheId, hash)); case PlaceFileResult.ResultCode.Error: case PlaceFileResult.ResultCode.Unknown: return(new CacheFailure(result.ErrorMessage)); default: return(new CacheFailure("Unrecognized PlaceFileAsync result code: " + result.Code + ", error message: " + (result.ErrorMessage ?? string.Empty))); } }
public async Task <Possible <Stream, Failure> > GetStreamAsync(CasHash hash, UrgencyHint urgencyHint, Guid activityId) { var result = await ReadOnlyCacheSession.OpenStreamAsync(new Context(Logger), hash.ToMemoization(), CancellationToken.None); switch (result.Code) { case OpenStreamResult.ResultCode.Success: if (result.Stream != null) { return(result.Stream); } return(new NoCasEntryFailure(CacheId, hash)); case OpenStreamResult.ResultCode.Error: return(new CacheFailure(result.ErrorMessage)); case OpenStreamResult.ResultCode.ContentNotFound: return(new NoCasEntryFailure(CacheId, hash)); default: return(new CacheFailure("Unrecognized OpenStreamAsync result code: " + result.Code + ", error message: " + (result.ErrorMessage ?? string.Empty))); } }
/// <inheritdoc /> public Task <Possible <ValidateContentStatus, Failure> > ValidateContentAsync(CasHash hash, UrgencyHint urgencyHint, Guid activityId) { // TODO: Implement content validation/remediation return(Task.FromResult(new Possible <ValidateContentStatus, Failure>(ValidateContentStatus.NotSupported))); }
/// <inheritdoc /> public async Task <Possible <string, Failure>[]> PinToCasAsync(CasEntries hashes, UrgencyHint urgencyHint, Guid activityId) { List <ContentHash> contentHashes = hashes.Select(hash => hash.ToContentHash()).ToList(); IEnumerable <Task <Indexed <PinResult> > > resultSet = await ReadOnlyCacheSession.PinAsync(new Context(Logger), contentHashes, CancellationToken.None); var results = new Possible <string, Failure> [contentHashes.Count]; foreach (Task <Indexed <PinResult> > resultTask in resultSet) { Indexed <PinResult> individualResult = await resultTask; results[individualResult.Index] = individualResult.Item.FromMemoization(hashes[individualResult.Index], CacheId); } return(results); }
/// <inheritdoc /> public async Task <Possible <string, Failure> > PinToCasAsync(CasHash hash, UrgencyHint urgencyHint, Guid activityId) { var result = await ReadOnlyCacheSession.PinAsync(new Context(Logger), hash.ToMemoization(), CancellationToken.None); return(result.FromMemoization(hash, CacheId)); }
/// <inheritdoc /> public async Task <Possible <CasEntries, Failure> > GetCacheEntryAsync(StrongFingerprint strong, UrgencyHint urgencyHint, Guid activityId) { var hashListResult = await ReadOnlyCacheSession.GetContentHashListAsync( new Context(Logger), new BuildXL.Cache.MemoizationStore.Interfaces.Sessions.StrongFingerprint( strong.WeakFingerprint.ToMemoization(), new Selector(strong.CasElement.ToMemoization(), strong.HashElement.RawHash.ToByteArray())), CancellationToken.None); if (hashListResult.Succeeded) { if (hashListResult.ContentHashListWithDeterminism.ContentHashList == null) { return(new NoMatchingFingerprintFailure(strong)); } SessionEntries?.TryAdd(strong, 1); return(hashListResult.ContentHashListWithDeterminism.FromMemoization()); } else { return(new CacheFailure(hashListResult.ErrorMessage)); } }