Esempio n. 1
0
 private static FileInfo GetFile(AlphaDecimal key)
 {
     return(FileRepositoryConfiguration
            .Directory()
            .GetFiles("{0}.record".FormatWith(key), SearchOption.AllDirectories)
            .FirstOrDefault());
 }
Esempio n. 2
0
        IRecord <T> IRepository <T> .Update(IRecord <T> record)
        {
            if (null == record)
            {
                throw new ArgumentNullException("record");
            }

            if (null == record.Cacheability)
            {
                throw new RepositoryException();
            }

            if (null == record.Expiration)
            {
                throw new RepositoryException();
            }

            if (null == record.Status)
            {
                throw new RepositoryException();
            }

            if (null == record.Urn)
            {
                throw new RepositoryException();
            }

            if (!record.Key.HasValue)
            {
                throw new RepositoryException();
            }

            var keySelection = Repository.Select(record.Key.Value);

            if (null == keySelection)
            {
                throw new RepositoryException();
            }

            var urnSelection = Repository.Select(record.Urn);

            if (null != urnSelection &&
                keySelection.Key != urnSelection.Key)
            {
                throw new RepositoryException();
            }

            if (!Repository.Exists(record.Key.Value))
            {
                return(null);
            }

            record.Modified = DateTime.UtcNow;

            new RecordFile(record).Save(FileRepositoryConfiguration.Directory());

            return(record);
        }
Esempio n. 3
0
        private static FileInfo GetFile(AbsoluteUri urn)
        {
            if (null == urn)
            {
                throw new ArgumentNullException("urn");
            }

            var dir = new DirectoryInfo(urn.ToPath(FileRepositoryConfiguration.Directory()).FullName);

            return(dir.Exists
                       ? dir.GetFiles("*.record", SearchOption.TopDirectoryOnly).FirstOrDefault()
                       : null);
        }
Esempio n. 4
0
        IRecord <T> IRepository <T> .Insert(IRecord <T> record)
        {
            if (null == record)
            {
                throw new ArgumentNullException("record");
            }

            if (null == record.Cacheability)
            {
                throw new RepositoryException();
            }

            if (null == record.Expiration)
            {
                throw new RepositoryException();
            }

            if (null == record.Status)
            {
                throw new RepositoryException();
            }

            if (null == record.Urn)
            {
                throw new RepositoryException();
            }

            if (record.Key.HasValue)
            {
                throw new RepositoryException();
            }

            if (Repository.Exists(record.Urn))
            {
                throw new RepositoryException();
            }

            record.Created  = DateTime.UtcNow;
            record.Key      = AlphaDecimal.Random();
            record.Modified = DateTime.UtcNow;

            new RecordFile(record).Save(FileRepositoryConfiguration.Directory());

            return(record);
        }
Esempio n. 5
0
        IEnumerable <IRecord <T> > IRepository <T> .Query(XPathExpression expression)
        {
            if (null == expression)
            {
                throw new ArgumentNullException("expression");
            }

            if (null == expression.Expression)
            {
                throw new ArgumentOutOfRangeException("expression");
            }

            var files = FileRepositoryConfiguration
                        .Directory()
                        .GetFiles("*.record", SearchOption.AllDirectories);

#if NET20
            foreach (var file in files)
            {
                var obj = RecordFile.Load(file);
                if (string.IsNullOrEmpty(obj.Body))
                {
                    continue;
                }

                var selection = obj.ToXml().CreateNavigator().Select(expression.Expression);
                if (0 != selection.Count)
                {
                    result.Add(obj.ToRecord <T>());
                }
            }

            return(result);
#else
            return((from file in files
                    select RecordFile.Load(file)
                    into obj
                    where !string.IsNullOrEmpty(obj.Body)
                    let navigator = obj.ToXml().CreateNavigator()
                                    let count = navigator.Select(expression.Expression).Count
                                                where 0 != count
                                                select obj.ToRecord <T>()).ToList());
#endif
        }
Esempio n. 6
0
        public FileRepository(FileRepositoryConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (string.IsNullOrEmpty(configuration.BasePath))
            {
                throw new ArgumentNullException(nameof(configuration.BasePath));
            }

            if (string.IsNullOrEmpty(configuration.InputPath))
            {
                throw new ArgumentNullException(nameof(configuration.InputPath));
            }

            inputPath = Path.Combine(configuration.BasePath, configuration.InputPath);
        }
        /// <summary>
        /// Creates a file repository with the given name and token provider.
        /// </summary>
        /// <param name="name">The name of the file repository.</param>
        /// <param name="tokenProvider">The token provider to use.</param>
        /// <param name="defaultPath">The default path for the repository.</param>
        /// <param name="retentionPeriod">The retention period.</param>
        /// <returns>The file repository.</returns>
        internal static FileRepository CreateFileRepository(string name, IFileTokenProvider tokenProvider, string defaultPath, TimeSpan retentionPeriod)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            if (tokenProvider == null)
            {
                throw new ArgumentNullException("tokenProvider");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("defaultPath");
            }

            FileRepositoryConfiguration fileRepoConfig = ConfigurationSettings.GetFileRepositoryConfigurationSection();
            FileRepositoryConfigElement configElement  = null;

            if (fileRepoConfig != null &&
                fileRepoConfig.FileRepositories != null)
            {
                configElement = fileRepoConfig.FileRepositories.Cast <FileRepositoryConfigElement>().FirstOrDefault(f => f.Name == name);
            }

            string path = string.Empty;

            if (configElement != null)
            {
                path = configElement.Path;
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                path = defaultPath;
            }

            return(new FileRepository(name, path, tokenProvider, EventLog.Application, retentionPeriod));
        }
Esempio n. 8
0
        public static IServiceCollection AddCanOpenFileEds(this IServiceCollection serviceCollection, FileRepositoryConfiguration config)
        {
            if (serviceCollection is null)
            {
                throw new ArgumentException(nameof(IServiceCollection));
            }

            serviceCollection.AddSingleton <ICanOpenObjectRepository, MiControlCanObjectFileRepository>(provider =>
            {
                var logger      = provider.GetRequiredService <ILogger <CanObjectFileRepository> >();
                var memoryCache = provider.GetRequiredService <IMemoryCache>();

                return(new MiControlCanObjectFileRepository(logger, memoryCache, config.MiControlJsonEdsPath));
            });

            serviceCollection.AddSingleton <IScorpioCanOpenObjectRepository, ScorpioCanObjectFileRepository>(provider =>
            {
                var logger      = provider.GetRequiredService <ILogger <CanObjectFileRepository> >();
                var memoryCache = provider.GetRequiredService <IMemoryCache>();

                return(new ScorpioCanObjectFileRepository(logger, memoryCache, config.ScorpioCanJsonEdsPath));
            });


            return(serviceCollection);
        }