Skip to content

Instantly share code, notes, and snippets.

@PaulMilla
Created February 20, 2018 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulMilla/f167380af0ce1f7775f37d3e6cf18154 to your computer and use it in GitHub Desktop.
Save PaulMilla/f167380af0ce1f7775f37d3e6cf18154 to your computer and use it in GitHub Desktop.
Test case demonstrating WAL file size growth. Tested on Couchbase.Lite 1.4.0 & 1.4.1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Couchbase.Lite;
using Couchbase.Lite.Util;
namespace CouchbaseWalIssue
{
public class Program
{
private const string DIRECTORY_PATH = "D:\\CouchbaseWalIssue";
private const string DB_NAME = "waltest";
private static Manager manager;
private static Database db;
private static void SetupLogger()
{
// Basic logger that prints to the Console
Log.SetLogger(new ConsoleLogger());
Log.Domains.Database.Level = Log.LogLevel.None;
}
private static void Main()
{
SetupLogger();
const string tag = "MAIN (Main)";
manager = new Manager(Directory.CreateDirectory(DIRECTORY_PATH), ManagerOptions.Default);
db = manager.GetDatabase(DB_NAME);
Log.I(tag, $"WAL size: {GetWalFileSize() / 1024.0} KB");
const int newDocsCount = 1000;
Log.I(tag, $"Adding {newDocsCount} documents..");
AddDocuments(newDocsCount);
Log.I(tag, $"Document count: {db.GetDocumentCount()}");
Log.I(tag, $"WAL size: {GetWalFileSize() / (1024.0*1024.0)} MB");
const int purgeDocsCount = 500;
Log.I(tag, $"Purging {purgeDocsCount} documents..");
PurgeDocuments(purgeDocsCount);
Log.I(tag, $"Document count: {db.GetDocumentCount()}");
Log.I(tag, $"WAL size: {GetWalFileSize() / (1024.0*1024.0)} MB");
Log.I(tag, "Compacting..");
db.Compact();
Log.I(tag, $"WAL size: {GetWalFileSize() / (1024.0*1024.0)} MB");
Log.I(tag, "Closing db..");
db.Close();
Log.I(tag, $"WAL size: {GetWalFileSize() / (1024.0*1024.0)} MB");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static long GetWalFileSize()
{
var walFile = new FileInfo($"{DIRECTORY_PATH}\\{DB_NAME}.cblite2\\db.sqlite3-wal");
return walFile.Length;
}
private static void AddDocuments(int newDocsCount)
{
db.RunInTransaction(
() =>
{
for (var i = 0; i < newDocsCount; i++)
{
var doc = db.CreateDocument();
doc.PutProperties(
new Dictionary<string, object>
{
{"SentTimeStamp", DateTime.Now.ToString("G")},
{"SenderId", new string('*', 50)},
{"MessageType", new string('*', 50)},
{"Payload", new string('*', 1000)}
});
}
return true;
});
}
private static void PurgeDocuments(int purgeDocsCount)
{
var docIds = db.CreateAllDocumentsQuery()
.Run()
.Take(purgeDocsCount)
.Select(x => x?.DocumentId);
foreach (var docId in docIds)
{
if (docId == null)
return;
var document = db.GetDocument(docId);
document.Purge();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment