-
Notifications
You must be signed in to change notification settings - Fork 247
/
ScanResult.cs
98 lines (83 loc) · 2.77 KB
/
ScanResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Couchbase.Utils;
using System;
using System.Text;
using Couchbase.Core.Exceptions;
using Couchbase.Core.IO.Operations;
using Couchbase.Core.IO.Transcoders;
#nullable enable
namespace Couchbase.KeyValue.RangeScan
{
/// <inheritdoc />
public class ScanResult : IScanResult
{
private readonly SlicedMemoryOwner<byte> _body;
private readonly string _id;
private readonly DateTime? _expiryTime;
private readonly int _seqno;
private readonly ulong _cas;
private readonly bool _idOnly;
private readonly ITypeTranscoder _transcoder;
private readonly Flags? _flags;
private readonly OpCode _opCode;
internal ScanResult(SlicedMemoryOwner<byte> body, string id, bool idOnly, DateTime? expiryTime, int seqno, ulong cas, OpCode opCode, ITypeTranscoder transcoder, Flags? flags = default)
{
_body = body;
_id = id;
_idOnly = idOnly;
_expiryTime = expiryTime;
_seqno = seqno;
_cas = cas;
_flags = flags;
_transcoder = transcoder;
_opCode = opCode;
}
private void RequireBody() {
if (_idOnly) {
throw new UnsupportedException("This result came from a scan configured to return only document IDs.");
}
}
public bool IsEmpty()
{
if (this.Id == String.Empty)
{
return true;
}
return false;
}
public bool IdOnly => _idOnly;
/// <inheritdoc />
public string Id => _id;
internal ReadOnlyMemory<byte> Body => _body.Memory;
/// <inheritdoc />
public DateTime? ExpiryTime => _expiryTime;
/// <inheritdoc />
public ulong Cas => _cas;
public byte[] ContentAsBytes()
{
RequireBody();
return Body.Span.ToArray();
}
public string ContentAsString()
{
return Encoding.UTF8.GetString(Body.Span.ToArray());
}
/// <inheritdoc />
public T? ContentAs<T>()
{
RequireBody();
return _transcoder.Decode<T>(Body, _flags ?? default, _opCode);
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("ScanResult{");
sb.Append("id = ").Append(Id);
if (!_idOnly) sb.Append(", content = ").Append(ContentAsString());
if (_flags.HasValue) sb.Append(", flags = 0x").Append(_flags.Value);
sb.Append(", cas = ").Append(Cas);
if (ExpiryTime.HasValue) sb.Append(", expiry = ").Append(ExpiryTime.Value);
sb.Append('}');
return sb.ToString();
}
}
}