Skip to content

Instantly share code, notes, and snippets.

@PaulMilla
Created April 26, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulMilla/e46a690d06700da8aa2e317f0c7568b3 to your computer and use it in GitHub Desktop.
Save PaulMilla/e46a690d06700da8aa2e317f0c7568b3 to your computer and use it in GitHub Desktop.
Couchbase Lite implementation that connects to another Couchbase Lite app and replicates data
internal class Program
{
private const string DB_NAME = "app2";
private const string OTHER_DB_NAME = "app1";
private const string OTHER_DB_IP = "127.0.0.1";
private const ushort OTHER_DB_PORT = 59840;
private static readonly Uri OtherUrl = new Uri($"http://{OTHER_DB_IP}:{OTHER_DB_PORT}/{OTHER_DB_NAME}");
private static Manager manager;
private static Database db;
private static void Main()
{
SetupLogger();
var directoryPath = $"D:\\{Process.GetCurrentProcess().ProcessName}";
manager = new Manager(
Directory.CreateDirectory(directoryPath),
ManagerOptions.Default);
db = manager.GetDatabase(DB_NAME);
//Peer-to-peer replication doesn't support web sockets
var pull = db.CreatePullReplication(OtherUrl);
pull.ReplicationOptions.UseWebSocket = false;
pull.Continuous = true;
pull.Start();
var push = db.CreatePushReplication(OtherUrl);
push.ReplicationOptions.UseWebSocket = false;
push.Continuous = true;
push.Start();
Console.WriteLine("Press ESC to stop");
var shutdownTokenSource = new CancellationTokenSource();
HandleCommands(shutdownTokenSource);
}
private static string Tag => "MAIN (Main)";
private static void SetupLogger()
{
log4net.Config.XmlConfigurator.Configure();
Log.SetLogger(new Log4NetLogger());
//Log.Level = Log.LogLevel.Debug;
//Log.Domains.All.Level = Log.LogLevel.Debug;
Log.Domains.ChangeTracker.Level = Log.LogLevel.Debug;
}
private static void HandleCommands(CancellationTokenSource shutdownTokenSource)
{
while (!shutdownTokenSource.IsCancellationRequested)
{
var key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.Spacebar:
PrintDocuments();
break;
case ConsoleKey.Enter:
CreateDocument();
break;
case ConsoleKey.NumPad0:
DeleteDocument();
break;
case ConsoleKey.Escape:
shutdownTokenSource.Cancel();
return;
}
}
}
private static void PrintDocuments()
{
var sb = new StringBuilder("Documents:" + Environment.NewLine);
var allDocumentsQuery = db.CreateAllDocumentsQuery();
var rows = allDocumentsQuery.Run();
var count = 0;
foreach (var row in rows)
{
sb.AppendLine($" {++count}) {JsonConvert.SerializeObject(row.Document.Properties)}");
}
Log.I(Tag, sb.ToString());
}
private static void CreateDocument()
{
var doc = db.CreateDocument();
doc.PutProperties(
new Dictionary<string, object>
{
{"time", DateTime.Now.ToString("G")}
});
Log.I(Tag, $"Created {JsonConvert.SerializeObject(doc.Properties)}");
}
private static void DeleteDocument()
{
var allDocumentsQuery = db.CreateAllDocumentsQuery();
var startKeyDocId = allDocumentsQuery.Run().FirstOrDefault()?.DocumentId;
if (startKeyDocId == null)
return;
var document = db.GetDocument(startKeyDocId);
Log.I(Tag, $"Deleted {JsonConvert.SerializeObject(document.Properties)}");
document.Delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment