public static void Connect(string collectionName) { if (Collection != null) { throw new TraceStateException("Connection already established."); } client = new LiteDB.LiteDatabase(@"MyData.db"); //just to update the description state var databases = client.GetCollectionNames(); if (client == null) { throw new TraceStateException("Local db is unreachable."); } Collection = client.GetCollection <Log>(collectionName); Buffers = new LogBuffer[NumberOfBuffers]; for (int i = 0; i < NumberOfBuffers; i++) { Buffers[i] = new LogBuffer(); } Arbiter = new Arbiter2(Buffers); //I create a new delegate in order to call a method with a Conditional Attribute Arbiter.OnAllBuffersFilled += delegate { Flush(); }; timer = new Timer(2000); timer.AutoReset = true; timer.Elapsed += delegate { Timer_Elapsed(null, null); }; timer.Start(); }
public List <string> GetCollectionNames() { LiteDB.LiteDatabase liteDatabase = new LiteDB.LiteDatabase(this.DbLite); List <string> lst = liteDatabase.GetCollectionNames().ToList(); liteDatabase.Dispose(); return(lst); }
private void btnOpen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(openFileDialog.FileName); LiteDB.LiteDatabase liteDatabase = new LiteDB.LiteDatabase(fi.FullName); DataSet dataSet = new DataSet(fi.Name); liteDatabase.GetCollectionNames().ToList().ForEach(name => { var dt = dataSet.Tables.Add(name); List <string> keys = new List <string>(); liteDatabase.GetCollection(name).FindAll().ToList().ForEach(item => keys = item.Keys.Union(keys).ToList()); keys.ForEach(str => dt.Columns.Add(str, typeof(string))); liteDatabase.GetCollection(name).FindAll().ToList().ForEach(item => dt.Rows.Add(item.Values.ToArray().Select(v => v.AsString).ToArray()) ); }); gcData.DataSource = dataSet; } }
private static void AddCommands() { Commands.AdditionalCommands.Add(new KeyValuePair <string, CommandHandler>("dnsdb-dump", cmd => { var db = new LiteDB.LiteDatabase(DnsDbFile); int cols = 0; foreach (var collName in db.GetCollectionNames()) { cmd.Write("===== COLLECTION START (" + collName + ") =====\n"); int docs = 0; int idxs = 0; var coll = db.GetCollection(collName); foreach (var item in coll.FindAll()) { cmd.Write("Doc: " + item.ToString() + "\n"); docs++; } foreach (var idx in coll.GetIndexes()) { cmd.Write($"Idx [{idx.Field}] experssion [{idx.Expression}] unique [{idx.Unique}] slot [{idx.Slot}] maxLevel [{idx.MaxLevel}]\n"); idxs++; } cmd.Write("===== COLLECTION END (" + collName + ") " + docs + " documents " + idxs + " indexes =====\n"); cols++; } cmd.Write("(" + cols + " collections)\n"); })); Commands.AdditionalCommands.Add(new KeyValuePair <string, CommandHandler>("dnsdb-drop", cmd => { cmd.Write("deleting: " + DnsDbFile + "\n"); File.Delete(DnsDbFile); })); Commands.AdditionalCommands.Add(new KeyValuePair <string, CommandHandler>("dnsdb", cmd => { var bg = BgService.Instance; if (bg == null || !bg.TryGetTarget(out var t)) { cmd.WriteLine("Failed to get BgService."); cmd.statusCode = 1; return; } var db = t.DnsDb; if (db == null) { cmd.WriteLine("Failed to get DnsDb."); cmd.statusCode = 1; return; } var subcmd = cmd.ArgOrNull(0); if (subcmd == "stat") { var fileSize = "error: "; try { fileSize = (new FileInfo(db.FilePath).Length / 1024).ToString("N0") + " KB"; } catch (Exception e) { fileSize += e.Message; } cmd.WriteLine($"FileSize: {fileSize}"); cmd.WriteLine($"Count: {db.RecordCount():N0}"); cmd.WriteLine($"Inserts: {db.inserts:N0} times in {db.insertTotalTime:N0} ms"); cmd.WriteLine($"QueryByIp: {db.queryByIps:N0} times in {db.queryByIpTotalTime:N0} ms"); cmd.WriteLine($"QueryByDomain: {db.queryByDomains:N0} times in {db.queryByDomainTotalTime:N0} ms"); } else if (subcmd == "ip") { var r = db.QueryByIp((uint)IPAddress.Parse(cmd.ArgOrNull(1)).Address); cmd.WriteLine("Result: " + (r ?? "(null)")); if (r != null && cmd.ArgOrNull(2) == "del") { cmd.WriteLine($"Deleted {(db.Delete(r) ? "1" : "0")} records"); } } else if (subcmd == "name") { var name = cmd.ArgOrNull(1); if (db.QueryByName(name, out var r)) { cmd.WriteLine("Result: " + r); if (cmd.ArgOrNull(2) == "del") { cmd.WriteLine($"Deleted {(db.Delete(name) ? "1" : "0")} records"); } } else { cmd.WriteLine("No result."); } } else if (subcmd == "clean") { var time = DateTime.Now - NaiveUtils.ParseDuration(cmd.ArgOrNull(1) ?? "3d"); cmd.WriteLine($"Deleting records before {time}..."); var r = db.Clean(time); cmd.WriteLine($"Deleted {r:N0}."); } else if (subcmd == "shrink") { cmd.WriteLine("Shrinking..."); var delta = -db.Shrink(); cmd.WriteLine($"Shrinked, delta: {delta:N0}."); } else { cmd.WriteLine("Unknown sub-command.\n" + "Usage: dnsdb (stat|ip IP [del]|name NAME [del]|shrink|clean [EXPIRED_BEFORE])\n" + "(EXPIRED_BEFORE default: 3d)"); cmd.statusCode = 1; } }));