/// <summary>
        /// Initializes a new instance of the <see cref="Database" /> class.
        /// </summary>
        /// <param name="dataPath">The database directory path.</param>
        public Database(string dataPath = null)
        {
            if (String.IsNullOrWhiteSpace(dataPath))
            {
                var appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                dataPath = Path.Combine(appPath, DATA_DIRECTORY_NAME);
            }

            DataPath = dataPath;            
            EnsureDataDirectoryExists(DataPath);
            _log.Info($"Data Path: {DataPath}");

            IndexBasePath = Path.Combine(DataPath, INDEX_DIRECTORY_NAME);
            EnsureIndexDirectoryExists(IndexBasePath);
            _log.Info($"Index Base Path: {IndexBasePath}");
           
            DocumentStorage = new LightningDocumentStorage(DataPath);
            _collections = new ConcurrentDictionary<string, Collection>();

            var persistedSchemas = DocumentStorage.GetAllAsync(Schema.COLLECTION_NAME).Result.Select(d => d.ToSchema());
            foreach (var schema in persistedSchemas)
            {
                var collection = new Collection(schema, this);
                _collections.TryAdd(schema.Name, collection);
            }

            var schemaPersistenceIntervalSeconds = Double.Parse(ConfigurationManager.AppSettings["Schema.PersistenceIntervalSeconds"] ?? "1");
            _schemaPersistenceTimer = new Timer(_ => Task.Run(async () => await PersistSchemas().ConfigureAwait(false)), null, TimeSpan.FromSeconds(schemaPersistenceIntervalSeconds), TimeSpan.FromSeconds(schemaPersistenceIntervalSeconds));
        }       
        public SubClassDocumentStorage(IDocumentStorage <TRoot, TId> parent, SubClassMapping mapping)
        {
            _parent  = parent;
            _mapping = mapping;

            FromObject = _mapping.TableName.QualifiedName;

            _defaultWhere = determineWhereFragment();
            _fields       = _parent.SelectFields();
        }
Exemple #3
0
        public DeleteById(string sql, IDocumentStorage storage, object id, object document = null)
        {
            Sql = sql;
            _storage = storage;
            if (id == null) throw new ArgumentNullException(nameof(id));

            Id = id;
            Document = document;

        }
Exemple #4
0
        public Session(IDocumentStorage storage, Store store)
        {
            _storage        = storage;
            _store          = store;
            _isolationLevel = store.Configuration.IsolationLevel;

            _maps = new Dictionary <IndexDescriptor, IList <MapState> >();

            _connection = _store.Configuration.ConnectionFactory.CreateConnection();
            _dialect    = SqlDialectFactory.For(_connection);
        }
Exemple #5
0
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            _schema = theStore.Schema;

            theMapping = _schema.MappingFor(typeof(User)).As<DocumentMapping>();
            theMapping.DuplicateField("UserName");

            _storage = _schema.StorageFor(typeof(User));

            theDerivedTable = _schema.DbObjects.TableSchema(theMapping);
        }
Exemple #6
0
 public void Configure(MartenExpressionParser parser, IDocumentStorage storage, IQueryableDocument mapping, UpdateBatch batch)
 {
     if (Where == null)
     {
         batch.Delete(mapping.Table, Id, storage.IdType);
     }
     else
     {
         batch.DeleteWhere(mapping.Table, Where);
     }
 }
Exemple #7
0
 public SentimentTrainingController(
     ILogger <SentimentTrainingController> logger,
     IDocumentStorage storage,
     ILexiconLoader lexiconLoader,
     IServiceProvider provider)
 {
     this.logger        = logger ?? throw new ArgumentNullException(nameof(logger));
     this.storage       = storage ?? throw new ArgumentNullException(nameof(storage));
     this.lexiconLoader = lexiconLoader ?? throw new ArgumentNullException(nameof(lexiconLoader));
     this.provider      = provider ?? throw new ArgumentNullException(nameof(provider));
 }
Exemple #8
0
 public void Configure(MartenExpressionParser parser, IDocumentStorage storage, IDocumentMapping mapping, UpdateBatch batch)
 {
     if (Where == null)
     {
         batch.Delete(mapping.Table, Id, storage.IdType);
     }
     else
     {
         batch.DeleteWhere(mapping.Table, Where);
     }
 }
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            _schema = theStore.Schema;

            theMapping = _schema.MappingFor(typeof(User)).As <DocumentMapping>();
            theMapping.DuplicateField("UserName");


            _storage = _schema.StorageFor(typeof(User));

            theDerivedTable = _schema.DbObjects.TableSchema(theMapping);
        }
Exemple #10
0
        public DeleteById(string sql, IDocumentStorage storage, object id, object document = null)
        {
            Sql      = sql;
            _storage = storage;
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            Id       = id;
            Document = document;
        }
Exemple #11
0
        static void Main(string[] args)
        {
            var document = new Document();
            // Save a document as txt file using "Save" dialog
            DocumentManager docManager = new TxtDocumentManager();

            docManager.Save(document);
            // Or use the IDocStorage interface to save a document
            IDocumentStorage storage = docManager.CreateStorage();

            storage.Save("path", document);
        }
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            _schema = theStore.Schema;

            theMapping = theStore.Tenancy.Default.MappingFor(typeof(User)).As <DocumentMapping>();
            theMapping.DuplicateField("UserName");


            _storage = theStore.Tenancy.Default.StorageFor(typeof(User));

            theDerivedTable = new DocumentTable(theMapping);
        }
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            ConnectionSource.CleanBasicDocuments();
            _schema = _container.GetInstance <DocumentSchema>();

            theMapping = _schema.MappingFor(typeof(User)).As <DocumentMapping>();
            theMapping.DuplicateField("UserName");


            _storage = _schema.StorageFor(typeof(User));

            theDerivedTable = _schema.TableSchema(theMapping.TableName);
        }
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            ConnectionSource.CleanBasicDocuments();
            _schema = _container.GetInstance<DocumentSchema>();

            theMapping = _schema.MappingFor(typeof(User));
            theMapping.DuplicateField("UserName");


            _storage = _schema.StorageFor(typeof(User));

            theDerivedTable = _schema.TableSchema(theMapping.TableName);
        }
Exemple #15
0
 public void Configure(MartenExpressionParser parser, IDocumentStorage storage, IDocumentMapping mapping, UpdateBatch batch)
 {
     if (Query == null)
     {
         batch.Delete(mapping.QualifiedTableName, Id, storage.IdType);
     }
     else
     {
         var where = Query.BuildWhereClause();
         batch.DeleteWhere(mapping.QualifiedTableName, where);
     }
     
 }
Exemple #16
0
        public bool Save(Document document)
        {
            if (!this.SaveDialog())
            {
                return(false);
            }
            // using Factory method to create a new document storage
            IDocumentStorage storage = this.CreateStorage();

            storage.Save(this._name, document);

            return(true);
        }
        private void storeEntity <T>(T entity, IDocumentStorage <T> storage) where T : notnull
        {
            if (entity is IVersioned versioned)
            {
                if (versioned.Version != Guid.Empty)
                {
                    storage.Store(this, entity, versioned.Version);
                    return;
                }
            }

            // Put it in the identity map -- if necessary
            storage.Store(this, entity);
        }
Exemple #18
0
        public static void EventHandlers(IEventBus bus, IDocumentStorage readRepo, IRepository writeRepo)
        {
            var channelHistoryService = new ChannelHistoryHandlers(readRepo);
            var user        = new UsersHandlers(writeRepo);
            var userService = new UserRegistrationHandlers(writeRepo);

            bus.Register <ChannelCreated>(channelHistoryService.When);
            bus.Register <ChannelJoined>(channelHistoryService.When);
            bus.Register <ChannelLeft>(channelHistoryService.When);
            bus.Register <MessageSubmitted>(channelHistoryService.When);
            bus.Register <MessageEdited>(channelHistoryService.When);
            bus.Register <RegistrationStarted>(user.When);
            bus.Register <RegistrationSucceeded>(userService.When);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Collection" /> class.
        /// </summary>
        /// <param name="name">The name of the Document Collection.</param>
        /// <param name="database">The database.</param>
        /// <param name="schema">The schema.</param>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        internal Collection(string name, Database database, Schema schema = null)
        {
            if (String.IsNullOrWhiteSpace(name))
                throw new ArgumentException($"{nameof(name)} cannot be null or blank");
            if (database == null)
                throw new ArgumentNullException(nameof(database));

            Name = name;
            Schema = schema ?? Schema.CreateDefault(name);

            _documentStorage = database.DocumentStorage;            

            var indexPath = Path.Combine(database.IndexBasePath, name);
            _luceneIndex = new LuceneIndex(indexPath, Schema);            
        }        
Exemple #20
0
        internal static IWhereFragment BuildWhereFragment(this IDocumentStorage storage, QueryModel query, MartenExpressionParser parser)
        {
            var wheres = query.AllBodyClauses().OfType <WhereClause>().ToArray();

            if (wheres.Length == 0)
            {
                return(storage.DefaultWhereFragment());
            }

            var @where = wheres.Length == 1
                ? parser.ParseWhereFragment(storage.Fields, wheres.Single().Predicate)
                : new CompoundWhereFragment(parser, storage.Fields, "and", wheres);

            return(storage.FilterDocuments(query, @where));
        }
        public T?Load <T>(int id) where T : notnull
        {
            assertNotDisposed();

            var storage = StorageFor <T>();

            var document = storage switch
            {
                IDocumentStorage <T, int> i => i.Load(id, this),
                IDocumentStorage <T, long> l => l.Load(id, this),
                _ => throw new DocumentIdTypeMismatchException(
                          $"The identity type for document type {typeof(T).FullNameInCode()} is not numeric")
            };

            return(document);
        }
        public EntityMetadataQueryHandler(object id, IDocumentStorage storage)
        {
            _id      = id;
            _storage = storage;

            SourceType = storage.DocumentType;

            if (storage.Fields is DocumentMapping m)
            {
                _columns = m.Schema.Table.Columns.OfType <MetadataColumn>().ToArray();
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(storage));
            }
        }
        public async Task <T?> LoadAsync <T>(int id, CancellationToken token = default) where T : notnull
        {
            assertNotDisposed();

            var storage = StorageFor <T>();

            var document = storage switch
            {
                IDocumentStorage <T, int> i => await i.LoadAsync(id, this, token).ConfigureAwait(false),
                IDocumentStorage <T, long> l => await l.LoadAsync(id, this, token).ConfigureAwait(false),
                _ => throw new DocumentIdTypeMismatchException(
                          $"The identity type for document type {typeof(T).FullNameInCode()} is not numeric")
            };

            return(document);
        }
        private void readQueryModel(QueryModel queryModel, IDocumentStorage storage, bool considerSelectors,
                                    IFieldMapping fields)
        {
            readBodyClauses(queryModel, storage);


            if (considerSelectors && !(Model.SelectClause.Selector is QuerySourceReferenceExpression))
            {
                var visitor = new SelectorVisitor(this);
                visitor.Visit(Model.SelectClause.Selector);
            }

            foreach (var resultOperator in queryModel.ResultOperators)
            {
                AddResultOperator(resultOperator, fields);
            }
        }
Exemple #25
0
        /// <summary>
        /// Attempts to find a storage for the specified document
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        private IDocumentStorage FindStorage(IDocument document)
        {
            IDocumentStorage storage = null;

            foreach (var s in Storages)
            {
                if (s.CanWriteDocument(document.GetType()))
                {
                    storage = s;
                    break;
                }
            }

            if (storage == null)
            {
                throw new ArgumentException("Failed to find storage object.");
            }

            return(storage);
        }
Exemple #26
0
        public IDocumentStorage StorageFor(Type documentType)
        {
            return(_documentTypes.GetOrAdd(documentType, type =>
            {
                var mapping = MappingFor(documentType);
                if (mapping is IDocumentStorage)
                {
                    buildSchemaObjectsIfNecessary(mapping);
                    return mapping.As <IDocumentStorage>();
                }


                assertNoDuplicateDocumentAliases();

                IDocumentStorage storage = mapping.BuildStorage(this);

                buildSchemaObjectsIfNecessary(mapping);

                return storage;
            }));
        }
Exemple #27
0
        private IList <IIncludePlan> readBodyClauses(QueryModel queryModel, IDocumentStorage storage)
        {
            var includes = new List <IIncludePlan>();

            if (!(Model.SelectClause.Selector is QuerySourceReferenceExpression))
            {
                var visitor = new IncludeVisitor(includes);
                visitor.Visit(Model.SelectClause.Selector);
            }

            for (var i = 0; i < queryModel.BodyClauses.Count; i++)
            {
                var clause = queryModel.BodyClauses[i];
                switch (clause)
                {
                case WhereClause where:
                    CurrentStatement.WhereClauses.Add(@where);
                    break;

                case OrderByClause orderBy:
                    CurrentStatement.Orderings.AddRange(orderBy.Orderings);
                    break;

                case AdditionalFromClause additional:
                    var isComplex       = queryModel.BodyClauses.Count > i + 1 || queryModel.ResultOperators.Any() || includes.Any();
                    var elementType     = additional.ItemType;
                    var collectionField = storage.Fields.FieldFor(additional.FromExpression);

                    CurrentStatement = CurrentStatement.ToSelectMany(collectionField, _session, isComplex, elementType);


                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(includes);
        }
 public static async Task <object> GetAsync(this IDocumentStorage storage, int id, Type type)
 {
     return((await storage.GetAsync(new IdentityDocument(id, type)))?.FirstOrDefault());
 }
 public SubClassDocumentStorage(IDocumentStorage parent, SubClassMapping mapping)
 {
     _parent  = parent;
     _mapping = mapping;
 }
Exemple #30
0
		public StorageService(IDocumentStorage documentStorage)
		{
			_documentStorage = documentStorage;
		}
Exemple #31
0
 public BulkLoader(IDocumentStorage <T, TId> storage)
 {
     _storage = storage;
 }
 /// <summary>
 /// Deletes a document from the store.
 /// </summary>
 public static Task DeleteAsync(this IDocumentStorage storage, int id, Type type)
 {
     return(storage.DeleteAsync(new IdentityDocument(id, type)));
 }
Exemple #33
0
 public void Load(bool forceReadOnly)
 {
     if (this.IsLoaded)
     {
         throw new InvalidOperationException();
     }
     Stream stream = null;
     try
     {
         stream = this._projectItem.GetStream(ProjectItemStreamMode.Read);
         this._storage = this.CreateStorage();
         this.LoadStreamIntoStorage(stream);
         this._readOnly = forceReadOnly;
         if (!forceReadOnly)
         {
             ProjectItemAttributes attributes = this._projectItem.Attributes;
             this._readOnly = (attributes & ProjectItemAttributes.ReadOnly) != ProjectItemAttributes.Normal;
         }
         this.OnAfterLoad(EventArgs.Empty);
         this._dirty = false;
     }
     catch
     {
         this.DisposeStorage();
         throw;
     }
     finally
     {
         if (stream != null)
         {
             stream.Close();
             stream = null;
         }
     }
 }
Exemple #34
0
 private void DisposeStorage()
 {
     if (this._storage != null)
     {
         this._storage.Dispose();
         this._storage = null;
     }
 }
 public static async Task <T> GetAsync <T>(this IDocumentStorage storage, int id) where T : class
 {
     return((await storage.GetAsync <T>(id))?.FirstOrDefault());
 }
 public DocumentService(IRepository repo, IDocumentStorage storage)
 {
     this.storage = storage;
     this.repository = repo;
 }
 /// <summary>
 /// Updates a document in to the store.
 /// </summary>
 public static Task UpdateAsync(this IDocumentStorage storage, int id, object item)
 {
     return(storage.UpdateAsync(new IdentityDocument(id, item)));
 }
 public SearchEngine(IVocabulary vocabulary, IDocumentStorage documentStorage, ITokinizer tokinizer)
 {
     _vocabulary      = vocabulary;
     _documentStorage = documentStorage;
     _tokinizer       = tokinizer;
 }
Exemple #39
0
 public SoftDelete(IDocumentStorage storage)
 {
     _sql =
         $"update {storage.TableName.QualifiedName} as d set {SchemaConstants.DeletedColumn} = True, {SchemaConstants.DeletedAtColumn} = now()";
 }
 public DocumentsInteractor(IDocumentRepository documentRepository, IDocumentStorage documentStorage)
 {
     this.documentRepository = documentRepository;
     this.documentStorage    = documentStorage;
 }
Exemple #41
0
 public DocumentStatement(IDocumentStorage storage) : base(storage, storage.Fields)
 {
     _storage = storage;
 }
        public void Initialize()
        {
            _appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            _dataPath = Path.Combine(_appPath, "data");
            if (!Directory.Exists(_dataPath))
                Directory.CreateDirectory(_dataPath);

            _documentStorage = new LightningDocumentStorage(_dataPath);
        }