internal async Task <T> HeadAsync(bool single, bool orDefault)
        {
            // Optimized code path for First/Last/Single variants where we can be smart and only ask for 1 or 2 results from the db

            // we can use the EXACT streaming mode with Limit = 1|2, and it will work if TargetBytes is 0
            if ((this.TargetBytes ?? 0) != 0 || (this.Mode != FdbStreamingMode.Iterator && this.Mode != FdbStreamingMode.Exact))
            {             // fallback to the default implementation
                return(await FdbAsyncEnumerable.Head(this, single, orDefault, this.Transaction.Cancellation).ConfigureAwait(false));
            }

            var options = new FdbRangeOptions()
            {
                Limit       = single ? 2 : 1,
                TargetBytes = 0,
                Mode        = FdbStreamingMode.Exact,
                Reverse     = this.Reversed
            };

            //BUGBUG: do we need special handling if OriginalRange != Range ? (weird combinations of Take/Skip and Reverse)

            var tr      = this.Snapshot ? this.Transaction.Snapshot : this.Transaction;
            var results = await tr.GetRangeAsync(this.Begin, this.End, options, 0).ConfigureAwait(false);

            if (results.IsEmpty)
            {             // no result
                if (!orDefault)
                {
                    throw new InvalidOperationException("The range was empty");
                }
                return(default(T));
            }

            if (single && results.Count > 1)
            {             // there was more than one result
                throw new InvalidOperationException("The range contained more than one element");
            }

            // we have a result
            return(this.Transform(results.First));
        }