Example #1
0
        public static List <PropertyCompareResult> Compare <TDocumentWithObject, TObjectOnly>(TDocumentWithObject oldObject, TDocumentWithObject newObject)
        {
            if (TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name == TypeOfWrapper.TypeOf(typeof(History <TObjectOnly>)).Name || TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name == TypeOfWrapper.TypeOf(typeof(SystemDb <TObjectOnly>)).Name)
            {
                return(new List <PropertyCompareResult>());
            }
            PropertyInfo[] properties = typeof(TDocumentWithObject).GetProperties();
            var            result     = new List <PropertyCompareResult>();

            foreach (PropertyInfo pi in properties)
            {
                if (pi.CustomAttributes.Any(ca => ca.AttributeType == typeof(IgnorePropertyCompareAttribute)))
                {
                    continue;
                }

                object oldValue = pi.GetValue(oldObject), newValue = pi.GetValue(newObject);

                if (!Equals(oldValue, newValue))
                {
                    result.Add(new PropertyCompareResult(pi.Name, oldValue?.ToString() ?? "", newValue?.ToString() ?? "", TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name));
                }
            }
            //Console.WriteLine("  Property name: {0} -- old: {1}, new: {2}",resultItem.Name, resultItem.OldValue ?? "<null>", resultItem.NewValue ?? "<null>");
            return(result);
        }
Example #2
0
        public StorageDatabase <TDocumentWithObject, TObjectOnly> Documents <TDocumentWithObject, TObjectOnly>(string directoryName = null)
            where TDocumentWithObject : IStandardDocument <TObjectOnly>
        {
            string key = TypeOfWrapper.TypeOf(typeof(StorageDatabase <TDocumentWithObject, TObjectOnly>)).FullName + "+" + directoryName;

            this.Logger?.Trace(this.GetType().Name, $"Accessing database storage with key {key} for directory name {directoryName} . Documment with object : {typeof(TDocumentWithObject).Name}, Object type only :  {typeof(TObjectOnly).Name}");

            if (!this.cache.ContainsKey(key))
            {
                lock (this.padlock)
                {
                    if (!this.cache.ContainsKey(key))
                    {
                        var val = new StorageDatabase <TDocumentWithObject, TObjectOnly>(
                            this.DataBaseSettings.AkkaCommonStorageMechanism,
                            directoryName,
                            this,
                            this.Logger);

                        this.cache[key] = val;
                    }
                }
            }
            return(this.cache[key] as StorageDatabase <TDocumentWithObject, TObjectOnly>);
        }
Example #3
0
        public static DbStats GetSystemStatistics(DatabaseService DatabaseService, Func <SystemDb <object>, bool> query = null, string documentName = "")
        {
            var dbStats = new DbStats();
            StorageDatabase <SystemDb <object>, object> sys = DatabaseService.Documents <SystemDb <object>, object>(null);

            var docs = new List <SystemDb <object> >();

            foreach (string s in sys.LoadAllIdsIncludingDeletedDocuments())
            {
                docs.Add(sys.Load(s));
            }
            docs = docs.Where(
                x =>
                x.DocumentName.ToLower() != documentName.ToLower() && true ||
                x.DocumentName.ToLower() == documentName.ToLower()
                ).ToList();

            if (query != null)
            {
                docs = docs.Where(query).ToList();
            }

            int HistoryCount = docs.Count(x => x.DocumentName == TypeOfWrapper.TypeOf(typeof(History <>)).Name);

            dbStats.TotalDocumentCount   = docs.Count - HistoryCount;
            dbStats.DeletedDocumentCount = docs.Count(x => x.Deleted);

            dbStats.HistoryDocumentCount = HistoryCount;

            return(dbStats);
        }
Example #4
0
        public static void LogDocument <TDocumentWithObject, TObjectOnly>(string documentId, TDocumentWithObject oldDoc, TDocumentWithObject newDoc, string operation, IStorageMechanism storageMechanism, string databaseName, DatabaseService DatabaseService)
            where TDocumentWithObject : IStandardDocument <TObjectOnly>
        {
            if (documentId == null)
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name != TypeOfWrapper.TypeOf(typeof(History <>)).Name)
            {
                HistoryService.UpdateDocumentHistory <TDocumentWithObject, TObjectOnly>(oldDoc, newDoc, storageMechanism, databaseName, DatabaseService);
            }

            if (TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name == TypeOfWrapper.TypeOf(typeof(SystemDb <>)).Name)
            {
                return;
            }
        }
Example #5
0
        public static string DetermineDatabaseName <TTDocumentWithObject, TTObjectOnly>(string documentName)
        {
            string dname            = "";
            bool   dataIsSomeObject = typeof(TTObjectOnly) == typeof(object);

            if (string.IsNullOrEmpty(documentName))
            {
                if (dataIsSomeObject)
                {
                    dname = TypeOfWrapper.TypeOf(typeof(TTDocumentWithObject)).Name;
                }
                else
                {
                    dname = TypeOfWrapper.TypeOf(typeof(TTObjectOnly)).Name;
                }
            }
            else
            {
                dname = documentName;
            }
            return(dname);
        }
Example #6
0
        public static void UpdateDocumentHistory <TDocumentWithObject, TObjectOnly>(TDocumentWithObject original, TDocumentWithObject dbObject, IStorageMechanism storageMechanism, string databaseName, DatabaseService DatabaseService)
            where TDocumentWithObject : IStandardDocument <TObjectOnly>
        {
            if (TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name == TypeOfWrapper.TypeOf(typeof(History <>)).Name || TypeOfWrapper.TypeOf(typeof(TDocumentWithObject)).Name == TypeOfWrapper.TypeOf(typeof(SystemDb <>)).Name || original?.Id == null)
            {
                return;
            }

            List <PropertyCompareResult> compares = Compare <TDocumentWithObject, TObjectOnly>(original, dbObject);

            using (StorageDatabase <History <object>, object> history = DatabaseService.DatabaseAccessRegardlessOfTransaction <History <object>, object>(storageMechanism, null))
            {
                history.CreateAll(
                    compares.Select(
                        x => new History <object>
                {
                    OldValue     = x.OldValue,
                    NewValue     = x.NewValue,
                    DocumentName = x.DocumentName,
                    DateTime     = x.DateTime,
                    Name         = x.Name
                }).ToList());
            }
        }
Example #7
0
        internal StorageDatabase <TDocumentWithObject, TObjectOnly> DatabaseAccessRegardlessOfTransaction <TDocumentWithObject, TObjectOnly>(IStorageMechanism storageMechanism, string directoryName)
            where TDocumentWithObject : IStandardDocument <TObjectOnly>
        {
            string key = TypeOfWrapper.TypeOf(typeof(StorageDatabase <TDocumentWithObject, TObjectOnly>)).FullName + "+" + directoryName;

            this.Logger?.Trace(this.GetType().Name, $"Accessing database storage outside the usual pipeline with key {key} for directory name {directoryName} with storage mechanism {storageMechanism?.GetType().Name}. Documment with object : {typeof(TDocumentWithObject).Name}, Object type only :  {typeof(TObjectOnly).Name}");

            if (!this.cacheForTransactionScenarios.ContainsKey(key))
            {
                lock (this.padlock2)
                {
                    if (!this.cacheForTransactionScenarios.ContainsKey(key))
                    {
                        var val = new StorageDatabase <TDocumentWithObject, TObjectOnly>(
                            storageMechanism,
                            directoryName,
                            this,
                            this.Logger);
                        this.cacheForTransactionScenarios[key] = val;
                    }
                }
            }
            return(this.cacheForTransactionScenarios[key] as StorageDatabase <TDocumentWithObject, TObjectOnly>);
        }
Example #8
0
        public DataBaseSettings(string databaseName, ISerializationFactory serializationFactory, IStorageIO fileio, string storageName, IPecanLogger logger, string baseDirectory)
        {
            if (databaseName == null)
            {
                throw new ArgumentNullException(nameof(databaseName));
            }

            this.FileIo = fileio ?? new StorageIO(logger);

            if (baseDirectory != null)
            {
                baseDirectory = baseDirectory.TrimEnd('/').TrimEnd('\\').TrimEnd('/').TrimEnd('\\');
                if (!this.FileIo.DirectoryExists(baseDirectory))
                {
                    this.FileIo.CreateDirectory(baseDirectory);
                }
            }

            DbStorageLocation = (string.IsNullOrEmpty(baseDirectory) ? AppDomain.CurrentDomain.BaseDirectory : baseDirectory) + "\\" + (string.IsNullOrEmpty(storageName) ? "pecan" : storageName);

            this.StorageMechanismMech = new JsonStorageMechanism(
                new FileStorageSystem(this, this.FileIo, serializationFactory ?? new JsonSerializationFactory(logger), logger),
                databaseName,
                DbStorageLocation,
                logger
                );
            Type pecanDbLoggerType = typeof(PecanDbLogger);

            PecanDbLogger.DefaultPecanLogger = logger;
            Config akkaConfig =
                $@"akka.loglevel = DEBUG
                    akka.loggers=[""{pecanDbLoggerType.FullName}, {pecanDbLoggerType.GetTypeInfo().Assembly.GetName().Name}""]";

            this.AppActorSystemystem        = ActorSystem.Create("StorageActorSystem-" + Guid.NewGuid().ToString(), akkaConfig);
            this.StorageActor               = this.AppActorSystemystem.ActorOf(Props.Create(() => new StorageActor(logger)), TypeOfWrapper.TypeOf(typeof(StorageActor)).Name);
            this.AkkaCommonStorageMechanism = new AkkaActorSystemMechanism(this.StorageActor, this.StorageMechanismMech, this, logger);
        }
Example #9
0
        public StorageActor(IPecanLogger logger)
        {
            this.Logger = logger;
            logger?.Trace(this.GetType().Name, $"{this.GetType().Name} Creating {nameof(StorageReadActor)} .. ");
            //multi reader
            IActorRef actorReadPool = Context.ActorOf(Props.Create(() => new StorageReadActor(logger)).WithRouter(new RoundRobinPool(1000)), TypeOfWrapper.TypeOf(typeof(StorageReadActor)).Name);

            logger?.Trace(this.GetType().Name, $"{this.GetType().Name} Creating {nameof(StorageWriteActor)} .. ");
            //single writer
            IActorRef actorWriteRef = Context.ActorOf(Props.Create(() => new StorageWriteActor(logger)), TypeOfWrapper.TypeOf(typeof(StorageWriteActor)).Name);

            this.Receive <Func <bool> >(
                message =>
            {
                logger?.Trace(this.GetType().Name, $"{this.GetType().Name} forwarding message {message?.GetType().Name} to {actorWriteRef.Path.ToStringWithAddress()}");
                actorWriteRef.Forward(message);
            });
            this.Receive <Func <object> >(
                message =>
            {
                logger?.Trace(this.GetType().Name, $"{this.GetType().Name} forwarding message {message?.GetType().Name} to {actorReadPool.Path.ToStringWithAddress()}");

                actorReadPool.Forward(message);
            });
            this.Receive <Terminated>(
                t =>
            {
                string message = "Actor just got terminated : " + t.ActorRef.Path;
                logger?.Fatal(this.GetType().Name, message);
            });
        }