public override bool Load() { Log.WriteLine($"Loading database..."); if (Directory.Exists(Path)) { Mut.WaitOne(); Collections.Clear(); string[] files = Directory.GetFiles(Path); foreach (string file in files) { // file = Full Path string name = System.IO.Path.GetFileNameWithoutExtension(file); Collection c = JsonConvert.DeserializeObject <Collection>( Encoding.UTF8.GetString(File.Sync.ReadFile(file)), SerializerSettings); Collections.Add(name, c); } Mut.ReleaseMutex(); Log.WriteLine($"Loaded {files.Length} collections."); return(true); } else { Mut.WaitOne(); Directory.Create(Path); Collections.Clear(); Mut.ReleaseMutex(); Log.WriteLine("No database found. Created directory."); return(false); } }
public override object this[string index] { get { Mut.WaitOne(); bool success = TryGetValue(index, out object v); Mut.ReleaseMutex(); if (success) { return(v); } else { return(null); } } set { Mut.WaitOne(); if (ContainsKey(index)) { if (Remove(index)) { Add(index, value); } } else { Add(index, value); } Mut.ReleaseMutex(); } }
public override List <Document> FindMatching(Predicate <Document> predicate) { Mut.WaitOne(); List <Document> l = FindAll(predicate); Mut.ReleaseMutex(); return(l); }
public override Document FindOne(Predicate <Document> predicate) { Mut.WaitOne(); Document t = Find(predicate); Mut.ReleaseMutex(); return(t); }
public bool HasCollection(string name) { Mut.WaitOne(); bool success = Collections.TryGetValue(name, out Collection val); Mut.ReleaseMutex(); return(success); }
public override bool RemoveCollection(string name) { Mut.WaitOne(); bool success = Collections.Remove(name); Mut.ReleaseMutex(); return(success); }
public override int RemoveMatching(Predicate <Document> predicate) { Mut.WaitOne(); int count = RemoveAll(predicate); Mut.ReleaseMutex(); return(count); }
public override bool RemoveOne(Predicate <Document> predicate) { Mut.WaitOne(); bool success = Remove(Find(predicate)); Mut.ReleaseMutex(); return(success); }
public override Collection GetCollection(string name) { Mut.WaitOne(); bool success = Collections.TryGetValue(name, out Collection val); Mut.ReleaseMutex(); if (!success) { return(null); } else { return(val); } }
public override void Save() { Log.WriteLine("Saving database..."); Mut.WaitOne(); Dictionary <string, Collection> .Enumerator en = Collections.GetEnumerator(); while (en.MoveNext()) { string path = System.IO.Path.Join(Path, $"{en.Current.Key}.json"); en.Current.Value.Lock(); byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(en.Current.Value, Database.SerializerSettings)); File.Sync.OverwriteFile(data, path); en.Current.Value.Release(); } Mut.ReleaseMutex(); Log.WriteLine("Done."); }
public override bool AddCollection(string name, out Collection collection) { Mut.WaitOne(); Collection t = new Collection(); bool success = Collections.TryAdd(name, t); if (success) { collection = t; } else { collection = null; } Mut.ReleaseMutex(); return(success); }
public override void Lock() { Mut.WaitOne(); }
public override void Each(Action <Document> action) { Mut.WaitOne(); ForEach(action); Mut.ReleaseMutex(); }