public async void Test_Case_10() { using (var zedb = await OpenTestDatabaseAsync()) { var db = FoundationDB.Filters.Logging.FdbLoggingExtensions.Logged(zedb, (tr) => Log(tr.Log.GetTimingsReport(true))); { var subspace = db.GlobalSpace; // clear everything and write some values await db.WriteAsync((tr) => { tr.ClearRange(subspace.Keys.Encode("K0000"), subspace.Keys.Encode("K9999Z")); for (int i = 0; i < 100; i++) { tr.Set(subspace.Keys.Encode("K" + i.ToString("D4")), Slice.FromString("V" + i.ToString("D4"))); } }, this.Cancellation); using (var tr = db.BeginTransaction(this.Cancellation)) { tr.ClearRange(subspace.Keys.Encode("K0010"), subspace.Keys.Encode("K0020")); tr.ClearRange(subspace.Keys.Encode("K0050"), subspace.Keys.Encode("K0060")); var chunk = await tr.GetRangeAsync( KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0000")), KeySelector.LastLessOrEqual(subspace.Keys.Encode("K9999")), new FdbRangeOptions { Mode = FdbStreamingMode.WantAll, Reverse = true } ); //no commit } } } }
private async Task <Chunk> GetChunkAtAsync([NotNull] IFdbTransaction trans, long offset) { Contract.Requires(trans != null && offset >= 0); var chunkKey = await trans.GetKeyAsync(KeySelector.LastLessOrEqual(DataKey(offset))).ConfigureAwait(false); if (chunkKey.IsNull) { // nothing before (sparse) return(default(Chunk)); } if (chunkKey < DataKey(0)) { // off beginning return(default(Chunk)); } long chunkOffset = DataKeyOffset(chunkKey); Slice chunkData = await trans.GetAsync(chunkKey).ConfigureAwait(false); if (chunkOffset + chunkData.Count <= offset) { // in sparse region after chunk return(default(Chunk)); } return(new Chunk(chunkKey, chunkData, chunkOffset)); }
public async Task Test_Case_10() { using (var db = await OpenTestPartitionAsync()) { db.SetDefaultLogHandler((log) => Log(log.GetTimingsReport(true))); // clear everything and write some values await db.WriteAsync(async tr => { var subspace = await db.Root.Resolve(tr); tr.ClearRange(subspace.Encode("K0000"), subspace.Encode("K9999Z")); for (int i = 0; i < 100; i++) { tr.Set(subspace.Encode("K" + i.ToString("D4")), Value("V" + i.ToString("D4"))); } }, this.Cancellation); using (var tr = await db.BeginTransactionAsync(this.Cancellation)) { var subspace = await db.Root.Resolve(tr); tr.ClearRange(subspace.Encode("K0010"), subspace.Encode("K0020")); tr.ClearRange(subspace.Encode("K0050"), subspace.Encode("K0060")); _ = await tr.GetRangeAsync( KeySelector.FirstGreaterOrEqual(subspace.Encode("K0000")), KeySelector.LastLessOrEqual(subspace.Encode("K9999")), new FdbRangeOptions { Mode = FdbStreamingMode.WantAll, Reverse = true } ); //no commit } } }
private async Task <long> ComputeSizeAsync(IFdbReadOnlyTransaction tr) { Contract.Requires(tr != null); var keyRange = this.Subspace.Keys.ToRange(); var lastKey = await tr.GetKeyAsync(KeySelector.LastLessOrEqual(keyRange.End)).ConfigureAwait(false); if (lastKey < keyRange.Begin) { return(0); } return(this.Subspace.Keys.DecodeFirst <long>(lastKey) + 1); }
public async void Test_Case_11() { using (var zedb = await OpenTestDatabaseAsync()) { var db = FoundationDB.Filters.Logging.FdbLoggingExtensions.Logged(zedb, (tr) => Log(tr.Log.GetTimingsReport(true))); { var subspace = db.GlobalSpace; // clear everything and write some values await db.WriteAsync((tr) => { tr.ClearRange(subspace.Keys.Encode("K0000"), subspace.Keys.Encode("K9999Z")); for (int i = 0; i < 100; i++) { tr.Set(subspace.Keys.Encode("K" + i.ToString("D4")), Slice.FromString("V" + i.ToString("D4"))); } }, this.Cancellation); using (var tr = db.BeginTransaction(this.Cancellation)) { tr.ClearRange(subspace.Keys.Encode("K0010"), subspace.Keys.Encode("K0020")); tr.ClearRange(subspace.Keys.Encode("K0050"), subspace.Keys.Encode("K0060")); tr.Set(subspace.Keys.Encode("K0021"), Slice.Empty); tr.Set(subspace.Keys.Encode("K0042"), Slice.Empty); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0005"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0010"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0015"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0022"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0049"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0050"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0055"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0061"))); //no commit } using (var tr = db.BeginTransaction(this.Cancellation)) { //tr.SetOption(FdbTransactionOption.ReadYourWritesDisable); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0000"))); // equal=false, offset=1 await tr.GetKeyAsync(KeySelector.FirstGreaterThan(subspace.Keys.Encode("K0011"))); // equal=true, offset=1 await tr.GetKeyAsync(KeySelector.LastLessOrEqual(subspace.Keys.Encode("K0022"))); // equal=true, offset=0 await tr.GetKeyAsync(KeySelector.LastLessThan(subspace.Keys.Encode("K0033"))); // equal=false, offset=0 await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Keys.Encode("K0040")) + 1000); // equal=false, offset=7 ? await tr.GetKeyAsync(KeySelector.LastLessThan(subspace.Keys.Encode("K0050")) + 1000); // equal=false, offset=6 ? } } } }
/// <summary> /// Read from the blob, starting at <paramref name="offset"/>, retrieving up to <paramref name="n"/> bytes (fewer then n bytes are returned when the end of the blob is reached). /// </summary> public async Task <Slice> ReadAsync([NotNull] IFdbReadOnlyTransaction trans, long offset, int n) { if (trans == null) { throw new ArgumentNullException("trans"); } if (offset < 0) { throw new ArgumentNullException("offset", "Offset cannot be less than zero"); } long?size = await GetSizeAsync(trans).ConfigureAwait(false); if (size == null) { return(Slice.Nil); // not found } if (offset >= size.Value) { return(Slice.Empty); } // read all chunks matching the segment we need, and copy them in our buffer var buffer = new byte[Math.Min(n, size.Value - offset)]; await trans .GetRange( KeySelector.LastLessOrEqual(DataKey(offset)), KeySelector.FirstGreaterOrEqual(DataKey(offset + n)) ) .ForEachAsync((chunk) => { // get offset of this chunk long chunkOffset = DataKeyOffset(chunk.Key); Slice chunkData = chunk.Value; checked { // intersect chunk bounds with output int delta = (int)(chunkOffset - offset); int start = delta; int end = delta + chunkData.Count; if (start < 0) { start = 0; } if (end > n) { end = n; } // compute the relative offsets in the chunk int rStart = start - delta; int rEnd = end - delta; var intersect = chunkData[rStart, rEnd]; if (intersect.IsPresent) { // copy the data that fits intersect.CopyTo(buffer, start); } } }) .ConfigureAwait(false); return(new Slice(buffer, 0, buffer.Length)); }
public async Task Test_Case_11() { using (var db = await OpenTestPartitionAsync()) { db.SetDefaultLogHandler((log) => Log(log.GetTimingsReport(true))); var location = db.Root; // clear everything and write some values await db.WriteAsync(async tr => { var subspace = await location.Resolve(tr); tr.ClearRange(subspace.Encode("K0000"), subspace.Encode("K9999Z")); for (int i = 0; i < 100; i++) { tr.Set(subspace.Encode("K" + i.ToString("D4")), Value("V" + i.ToString("D4"))); } }, this.Cancellation); using (var tr = await db.BeginTransactionAsync(this.Cancellation)) { var subspace = await location.Resolve(tr); tr.ClearRange(subspace.Encode("K0010"), subspace.Encode("K0020")); tr.ClearRange(subspace.Encode("K0050"), subspace.Encode("K0060")); tr.Set(subspace.Encode("K0021"), Slice.Empty); tr.Set(subspace.Encode("K0042"), Slice.Empty); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0005"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0010"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0015"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0022"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0049"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0050"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0055"))); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0061"))); //no commit } using (var tr = await db.BeginTransactionAsync(this.Cancellation)) { var subspace = await location.Resolve(tr); //tr.SetOption(FdbTransactionOption.ReadYourWritesDisable); await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0000"))); // equal=false, offset=1 await tr.GetKeyAsync(KeySelector.FirstGreaterThan(subspace.Encode("K0011"))); // equal=true, offset=1 await tr.GetKeyAsync(KeySelector.LastLessOrEqual(subspace.Encode("K0022"))); // equal=true, offset=0 await tr.GetKeyAsync(KeySelector.LastLessThan(subspace.Encode("K0033"))); // equal=false, offset=0 await tr.GetKeyAsync(KeySelector.FirstGreaterOrEqual(subspace.Encode("K0040")) + 1000); // equal=false, offset=7 ? await tr.GetKeyAsync(KeySelector.LastLessThan(subspace.Encode("K0050")) + 1000); // equal=false, offset=6 ? } } }