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

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

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

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

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

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

            Mut.ReleaseMutex();
            return(success);
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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.");
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 12
0
 public override void Lock()
 {
     Mut.WaitOne();
 }
Ejemplo n.º 13
0
 public override void Each(Action <Document> action)
 {
     Mut.WaitOne();
     ForEach(action);
     Mut.ReleaseMutex();
 }