Example #1
0
 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);
     }
 }
Example #2
0
 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();
     }
 }
Example #3
0
        public override List <Document> FindMatching(Predicate <Document> predicate)
        {
            Mut.WaitOne();
            List <Document> l = FindAll(predicate);

            Mut.ReleaseMutex();
            return(l);
        }
Example #4
0
        public override Document FindOne(Predicate <Document> predicate)
        {
            Mut.WaitOne();
            Document t = Find(predicate);

            Mut.ReleaseMutex();
            return(t);
        }
Example #5
0
        public bool HasCollection(string name)
        {
            Mut.WaitOne();
            bool success = Collections.TryGetValue(name, out Collection val);

            Mut.ReleaseMutex();
            return(success);
        }
Example #6
0
        public override bool RemoveCollection(string name)
        {
            Mut.WaitOne();
            bool success = Collections.Remove(name);

            Mut.ReleaseMutex();
            return(success);
        }
Example #7
0
        public override int RemoveMatching(Predicate <Document> predicate)
        {
            Mut.WaitOne();
            int count = RemoveAll(predicate);

            Mut.ReleaseMutex();
            return(count);
        }
Example #8
0
        public override bool RemoveOne(Predicate <Document> predicate)
        {
            Mut.WaitOne();
            bool success = Remove(Find(predicate));

            Mut.ReleaseMutex();
            return(success);
        }
Example #9
0
        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);
            }
        }
Example #10
0
 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.");
 }
Example #11
0
        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);
        }
Example #12
0
 public override void Release()
 {
     Mut.ReleaseMutex();
 }
Example #13
0
 public override void Each(Action <Document> action)
 {
     Mut.WaitOne();
     ForEach(action);
     Mut.ReleaseMutex();
 }