-
Notifications
You must be signed in to change notification settings - Fork 126
/
ReplicatorConfiguration.cs
339 lines (292 loc) · 11.1 KB
/
ReplicatorConfiguration.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// ReplicatorConfiguration.cs
//
// Copyright (c) 2018 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Couchbase.Lite.Internal.Logging;
using Couchbase.Lite.Support;
using Couchbase.Lite.Util;
using JetBrains.Annotations;
using LiteCore.Interop;
namespace Couchbase.Lite.Sync
{
/// <summary>
/// An enum representing the direction of a <see cref="Replicator"/>
/// </summary>
[Flags]
public enum ReplicatorType
{
/// <summary>
/// The replication will push data from local to remote
/// </summary>
Push = 1 << 0,
/// <summary>
/// The replication will pull data from remote to local
/// </summary>
Pull = 1 << 1,
/// <summary>
/// The replication will operate in both directions
/// </summary>
PushAndPull = Push | Pull
}
/// <summary>
/// A set of flags describing the properties of a replicated
/// document.
/// </summary>
[Flags]
public enum DocumentFlags
{
/// <summary>
/// The replication action represents a deletion of the
/// document in question
/// </summary>
Deleted = 1 << 0,
/// <summary>
/// The replication action represents a loss of access from
/// the server for the document in question (i.e. no more access
/// granted from the sync function)
/// </summary>
AccessRemoved = 1 << 1
}
/// <summary>
/// An enum representing level of opt in on progress of replication
/// </summary>
[Flags]
internal enum ReplicatorProgressLevel : int
{
/// <summary>
/// No additional replication progress callback
/// </summary>
Overall,
/// <summary>
/// Every document replication ended callback
/// </summary>
PerDocument, // >=1
/// <summary>
/// Every blob replication progress callback
/// </summary>
PerAttachment // >=2
}
/// <summary>
/// A class representing configuration options for a <see cref="Replicator"/>
/// </summary>
public sealed partial class ReplicatorConfiguration
{
#region Constants
private const string Tag = nameof(ReplicatorConfiguration);
#endregion
#region Variables
[NotNull]private readonly Freezer _freezer = new Freezer();
private Authenticator _authenticator;
private bool _continuous;
private Func<Document, DocumentFlags, bool> _pushFilter;
private Func<Document, DocumentFlags, bool> _pullValidator;
private Database _otherDb;
private Uri _remoteUrl;
private ReplicatorType _replicatorType = ReplicatorType.PushAndPull;
private C4SocketFactory _socketFactory;
private IConflictResolver _resolver;
#endregion
#region Properties
/// <summary>
/// Gets or sets the class which will authenticate the replication
/// </summary>
[CanBeNull]
public Authenticator Authenticator
{
get => _authenticator;
set => _freezer.SetValue(ref _authenticator, value);
}
/// <summary>
/// A set of Sync Gateway channel names to pull from. Ignored for push replicatoin.
/// The default value is null, meaning that all accessible channels will be pulled.
/// Note: channels that are not accessible to the user will be ignored by Sync Gateway.
/// </summary>
[CanBeNull]
public IList<string> Channels
{
get => Options.Channels;
set => _freezer.PerformAction(() => Options.Channels = value);
}
/// <summary>
/// Gets or sets whether or not the <see cref="Replicator"/> should stay
/// active indefinitely. The default is <c>false</c>
/// </summary>
public bool Continuous
{
get => _continuous;
set => _freezer.SetValue(ref _continuous, value);
}
/// <summary>
/// Gets the local database participating in the replication.
/// </summary>
[NotNull]
public Database Database { get; }
/// <summary>
/// A set of document IDs to filter by. If not null, only documents with these IDs will be pushed
/// and/or pulled
/// </summary>
[CanBeNull]
public IList<string> DocumentIDs
{
get => Options.DocIDs;
set => _freezer.PerformAction(() => Options.DocIDs = value);
}
/// <summary>
/// Extra HTTP headers to send in all requests to the remote target
/// </summary>
[NotNull]
public IDictionary<string, string> Headers
{
get => Options.Headers;
set => _freezer.PerformAction(() => Options.Headers = CBDebug.MustNotBeNull(WriteLog.To.Sync, Tag, nameof(Headers), value));
}
/// <summary>
/// Gets or sets a certificate to trust. All other certificates received
/// by a <see cref="Replicator"/> with this configuration will be rejected.
/// </summary>
[CanBeNull]
public X509Certificate2 PinnedServerCertificate
{
get => Options.PinnedServerCertificate;
set => _freezer.PerformAction(() => Options.PinnedServerCertificate = value);
}
/// <summary>
/// Func delegate that takes Document input parameter and bool output parameter
/// Document pull will be allowed if output is true, othewise, Document pull
/// will not be allowed
/// </summary>
public Func<Document, DocumentFlags, bool> PullFilter
{
get => _pullValidator;
set => _freezer.PerformAction(() => _pullValidator = value);
}
/// <summary>
/// Func delegate that takes Document input parameter and bool output parameter
/// Document push will be allowed if output is true, othewise, Document push
/// will not be allowed
/// </summary>
public Func<Document, DocumentFlags, bool> PushFilter
{
get => _pushFilter;
set => _freezer.PerformAction(() => _pushFilter = value);
}
/// <summary>
/// A value indicating the direction of the replication. The default is
/// <see cref="ReplicatorType.PushAndPull"/> which is bidirectional
/// </summary>
public ReplicatorType ReplicatorType
{
get => _replicatorType;
set => _freezer.SetValue(ref _replicatorType, value);
}
/// <summary>
/// Gets or sets the replicator heartbeat keep-alive interval.
/// </summary>
public TimeSpan Heartbeat
{
get => Options.Heartbeat;
set => _freezer.PerformAction(() => Options.Heartbeat = value);
}
/// <summary>
/// Gets the target to replicate with (either <see cref="Database"/>
/// or <see cref="Uri"/>
/// </summary>
[NotNull]
public IEndpoint Target { get; }
/// <summary>
/// The implemented custom conflict resolver object can be registered to the replicator
/// at ConflictResolver property. The default value of the conflictResolver is null.
/// When the value is null, the default conflict resolution will be applied.
/// </summary>
[CanBeNull]
public IConflictResolver ConflictResolver
{
get => _resolver;
set => _freezer.PerformAction(() => _resolver = value);
}
internal TimeSpan CheckpointInterval
{
get => Options.CheckpointInterval;
set => _freezer.PerformAction(() => Options.CheckpointInterval = value);
}
[NotNull]
internal ReplicatorOptionsDictionary Options { get; set; } = new ReplicatorOptionsDictionary();
[CanBeNull]
internal Database OtherDB
{
get => _otherDb;
set => _freezer.SetValue(ref _otherDb, value);
}
internal ReplicatorProgressLevel ProgressLevel
{
get => Options.ProgressLevel;
set => _freezer.PerformAction(() => Options.ProgressLevel = value);
}
[CanBeNull]
internal Uri RemoteUrl
{
get => _remoteUrl;
set => _freezer.SetValue(ref _remoteUrl, value);
}
internal C4SocketFactory SocketFactory
{
get => _socketFactory.open != IntPtr.Zero ? _socketFactory : LiteCore.Interop.SocketFactory.InternalFactory;
set => _freezer.SetValue(ref _socketFactory, value);
}
#endregion
#region Constructors
/// <summary>
/// Constructs a new builder object with the required properties
/// </summary>
/// <param name="database">The database that will serve as the local side of the replication</param>
/// <param name="target">The endpoint to replicate to, either local or remote</param>
/// <exception cref="ArgumentException">Thrown if an unsupported <see cref="IEndpoint"/> implementation
/// is provided as <paramref name="target"/></exception>
public ReplicatorConfiguration([NotNull] Database database, [NotNull] IEndpoint target)
{
Database = CBDebug.MustNotBeNull(WriteLog.To.Sync, Tag, nameof(database), database);
Target = CBDebug.MustNotBeNull(WriteLog.To.Sync, Tag, nameof(target), target);
var castTarget = Misc.TryCast<IEndpoint, IEndpointInternal>(target);
castTarget.Visit(this);
}
#endregion
#region Internal Methods
[NotNull]
internal ReplicatorConfiguration Freeze()
{
var retVal = new ReplicatorConfiguration(Database, Target)
{
Authenticator = Authenticator,
#if COUCHBASE_ENTERPRISE
AcceptOnlySelfSignedServerCertificate = AcceptOnlySelfSignedServerCertificate,
#endif
Continuous = Continuous,
PushFilter = PushFilter,
PullFilter = PullFilter,
ReplicatorType = ReplicatorType,
ProgressLevel = ProgressLevel,
ConflictResolver = ConflictResolver,
Options = Options
};
retVal._freezer.Freeze("Cannot modify a ReplicatorConfiguration that is in use");
return retVal;
}
#endregion
}
}