Exemplo n.º 1
0
        private void commitTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try {
                FileLock.WaitOne();
                T [] entityCache = EntityCache;
                if (entityCache == null)
                {
                    return;
                }

                if (DatabaseCategory != null)
                {
                    System.Diagnostics.Debug.WriteLine("Delayed writing changes in xml entities to database.");
                    CustomConfig.CommitChanges(PrepareToDatabase(entityCache), DatabaseCategory, databaseProfile);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Delayed writing changes in xml entities to file {0}.", EntitiesFile));
                    DataProvider.SaveAllObjectsToXML(EntitiesFile, entityCache);
                }
            } catch (Exception ex) {
                ErrorHandling.LogException(ex);
            } finally {
                FileLock.ReleaseMutex();
            }
        }
Exemplo n.º 2
0
        protected void FlushCache()
        {
            if (!LazyCommit || CommitTimer == null)
            {
                return;
            }

            try {
                FileLock.WaitOne();

                if (CommitTimer.Enabled)
                {
                    CommitTimer.Stop();
                    commitTimer_Elapsed(null, null);
                }
            } finally {
                FileLock.ReleaseMutex();
            }
        }
Exemplo n.º 3
0
        public virtual T CommitChanges()
        {
            if (!IsDirty)
            {
                return((T)this);
            }

            try {
                FileLock.WaitOne();
                List <T>           allObjects = new List <T> (GetAllEntities(false));
                DbColumnManager [] managers   = DataProvider.GetDBManagers(typeof(T));
                T   thisObject = (T)this;
                int index      = DataProvider.FindObjectIndexByField(allObjects, XmlEntityFields.Id, id, managers);

                if (index < 0)
                {
                    if (!DataProvider.CreateNewObjectId(allObjects, managers, thisObject, XmlEntityFields.Id))
                    {
                        return((T)this);
                    }

                    IsDirty = false;
                    allObjects.Add((T)thisObject.Clone());
                }
                else
                {
                    allObjects.RemoveAt(index);
                    IsDirty = false;
                    allObjects.Insert(index, (T)thisObject.Clone());
                }

                DoCommitChanges(allObjects.ToArray());
#if !DEBUG
            } catch (InvalidOperationException) {
#endif
            } finally {
                FileLock.ReleaseMutex();
            }

            return((T)this);
        }
Exemplo n.º 4
0
        protected void DeleteEntity(long entId)
        {
            try {
                FileLock.WaitOne();
                List <T> allObjects = new List <T> (GetAllEntities(false));
                int      index      = DataProvider.FindObjectIndexByField(allObjects, XmlEntityFields.Id, entId);

                if (index < 0)
                {
                    return;
                }

                allObjects.RemoveAt(index);

                DoCommitChanges(allObjects.ToArray());
#if !DEBUG
            } catch (InvalidOperationException) {
#endif
            } finally {
                FileLock.ReleaseMutex();
            }
        }
Exemplo n.º 5
0
        protected virtual T [] GetAllEntities(bool clone)
        {
            T [] cache = EntityCache;
            if (lastDatabaseProfile != databaseProfile)
            {
                lastDatabaseProfile = databaseProfile;
                EntityCache         = cache = null;
            }

            if (cache == null)
            {
                if (DatabaseCategory != null)
                {
                    T [] fromDatabase = CustomConfig.Get <T> (DatabaseCategory, databaseProfile);
                    if (fromDatabase.Length == 0 && !string.IsNullOrEmpty(databaseProfile))
                    {
                        if (OnCustomProfileEmpty())
                        {
                            fromDatabase = CustomConfig.Get <T> (DatabaseCategory, databaseProfile);
                        }
                    }

                    EntityCache = cache = OnEntitiesLoaded(PrepareFromDatabase(fromDatabase));
                }

                T [] fromFile = null;
                if (File.Exists(EntitiesFile))
                {
                    fromFile = OnEntitiesLoaded(DataProvider.GetAllObjectsFromXML <T> (FileLock, EntitiesFile));
                }

                if (fromFile != null && fromFile.Length > 0)
                {
                    T [] toDatabase       = null;
                    T [] toRemoveFromFile = null;
                    if (cache == null || cache.Length == 0)
                    {
                        EntityCache = cache = fromFile;

                        if (DatabaseCategory != null)
                        {
                            toDatabase       = PrepareToDatabase(cache);
                            toRemoveFromFile = toDatabase;
                        }
                    }
                    else
                    {
                        toRemoveFromFile = PrepareToDatabase(fromFile);
                    }

                    if (toDatabase != null && toDatabase.Length > 0)
                    {
                        CustomConfig.CommitChanges(toDatabase, DatabaseCategory, databaseProfile);
                    }

                    if (toRemoveFromFile != null && toRemoveFromFile.Length > 0)
                    {
                        T [] newEntitiesInFile = fromFile.Except(toRemoveFromFile, this).ToArray();
                        if (newEntitiesInFile.Length == 0)
                        {
                            try {
                                File.Delete(EntitiesFile);
                            } catch (Exception) {
                            }
                        }

                        if (File.Exists(EntitiesFile) && toRemoveFromFile.Length > 0)
                        {
                            try {
                                FileLock.WaitOne();
                                System.Diagnostics.Debug.WriteLine(string.Format("Writing changes in xml entities to file {0}.", EntitiesFile));

                                DataProvider.SaveAllObjectsToXML(EntitiesFile, newEntitiesInFile);
                            } catch (UnauthorizedAccessException uaex) {
                                ErrorHandling.LogException(uaex);
                            }
                        }