public XmlStorageIndex(IStorageDriver driver)
        {
            driver.ThrowIfNull(nameof(driver));

            _driver = driver;
            _items  = GetIndex(_driver);
        }
        private async Task SetupImpl()
        {
            Console.WriteLine("> setup");
            var baseStringArray = Enumerable.Repeat(LargeEvt.BaseString, 1000).ToList();
            var driver          = new MemoryStorageDriver();
            var stream          = new EventStream <LargeEvt>(driver);

            int  seqNum = 0;
            long pos;

            while ((pos = driver.GetPosition()) <= 4 * 1024 * 1024)
            {
                seqNum++;
                var toWrite = new LargeEvt(seqNum, baseStringArray);
                await stream.WriteAsync(new[] { toWrite });
            }

            firstNotFitting = checked ((uint)seqNum);

            // add five additional events, just to be safe
            // and also to test DiscardUpTo(some events in the second 4Mb block)
            for (int i = 0; i < 5; ++i)
            {
                seqNum++;
                await stream.WriteAsync(new[] { new LargeEvt(seqNum, baseStringArray) });
            }

            lastEvent = checked ((uint)seqNum);
            Console.WriteLine("< setup");
            storeWithLargeEvents = driver;
        }
Example #3
0
        public DataEngine(ITracer tracer, string instanceName, int maxConcurrency, IStorageDriver storageDriver, DataContainerDescriptor containerDescriptor)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException("tracer");
            }

            if (maxConcurrency <= 0 || maxConcurrency > 10000)
            {
                throw new ArgumentOutOfRangeException("maxConcurrency");
            }

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

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

            m_tracer = tracer;
            m_containerDescriptor = containerDescriptor;
            m_maxConcurrency      = maxConcurrency;
            m_parsedRequestCache  = new ParsedRequestCache(instanceName);
            m_storageDriver       = storageDriver;
            m_parser           = new QueryParser(containerDescriptor, maxConcurrency);
            m_activeProcessors = new ConcurrentDictionary <RequestExecutionContext, Task>(m_maxConcurrency, m_maxConcurrency);
            m_utcLastUsedAt    = DateTime.UtcNow;
        }
        private static void CompileInsertUpdateClauses(
            IStorageDriver storageDriver, DataContainerDescriptor containerDescriptor, RequestExecutionContextCacheInfo cacheInfo, DriverChangeType changeType)
        {
            var updates = cacheInfo.ParsedRequest.Modify.UpdateAssignments;
            var clauses = cacheInfo.ParsedRequest.Modify.InsertUpdateSetClauses;
            var fields  = cacheInfo.ParsedRequest.Modify.ModifiedFields;

            if (clauses.Count != fields.Count)
            {
                throw new Exception(string.Format("Internal error: insert/update clauses count ({0}) does not match count of modified fields ({1})",
                                                  clauses.Count, fields.Count));
            }

            // compile field assignment clauses (SET clauses or value inserts)
            for (var ordinal = 0; ordinal < clauses.Count; ordinal++)
            {
                var clause = clauses[ordinal];
                var field  = fields[ordinal];

                // for bulk requests, primary key is there but it is only used to lookup the record
                // for non-bulk requests, primary key should not be in the list of UPDATE clauses
                if (changeType == DriverChangeType.Update && !cacheInfo.ParsedRequest.IsBulk)
                {
                    if (!storageDriver.CanUpdateField(field.FieldId))
                    {
                        throw new Exception(string.Format("Cannot update field {0}/{1} on entity {2}", field.FieldId, field.Name, cacheInfo.ParsedRequest.TargetEntity.Name));
                    }
                }

                // prepare Action compilation context
                var compilerState = QueryParser.PrepareCompilerState(containerDescriptor, cacheInfo, null);
                compilerState.CompileToAction = true;

                // extractor has signature like Func<ClauseEvaluationContext, T>
                var extractor = clause == null
                                    ? QueryParser.CompileFieldValueExtractorClause(compilerState, field, containerDescriptor, cacheInfo, MakeNullableType(field.DbType))
                                    : QueryParser.CompileClause(compilerState, clause, containerDescriptor, cacheInfo, MakeNullableType(field.DbType));

                // get the value into local variable, to prevent multiple invokations when row writer checks for null
                var extractedValue = Expression.Variable(extractor.Type);

                // now take the extractor and create another method, that will take the value and then put it into the changebuffer's data
                var changeBufferData = Expression.Field(Expression.Field(compilerState.Context, "ChangeBuffer"), "Data");
                var blockBody        = Expression.Block(
                    new[] { extractedValue },
                    Expression.Assign(extractedValue, extractor),
                    DriverRowData.CreateWriteAccessor(extractedValue, changeBufferData, field.DbType, ordinal));

                updates.Add(
                    new ParsedRequest.FieldAssignment
                {
                    Field = field,
                    CompiledExpression = (Action <ClauseEvaluationContext>)QueryParser.CompileClause(blockBody, compilerState)
                });
            }
        }
Example #5
0
        private static DataContainerDescriptor RequireDescriptor(IStorageDriver driver)
        {
            var descriptor = driver.GetDescriptor();

            if (descriptor == null)
            {
                throw new InvalidOperationException("Driver reports that container descriptor is not initialized");
            }
            return(descriptor);
        }
Example #6
0
        public XmlStorage(IStorageDriver storageDriver, IStorageIndex index)
        {
            storageDriver.ThrowIfNull(nameof(storageDriver));
            index.ThrowIfNull(nameof(index));

            _driver = storageDriver;
            _index  = index;

            ClearFilesAndIndex();
        }
        private static void CompileInsertUpdateClauses(
            IStorageDriver storageDriver, DataContainerDescriptor containerDescriptor, RequestExecutionContextCacheInfo cacheInfo, DriverChangeType changeType)
        {
            var updates = cacheInfo.ParsedRequest.Modify.UpdateAssignments;
            var clauses = cacheInfo.ParsedRequest.Modify.InsertUpdateSetClauses;
            var fields = cacheInfo.ParsedRequest.Modify.ModifiedFields;

            if (clauses.Count != fields.Count)
            {
                throw new Exception(string.Format("Internal error: insert/update clauses count ({0}) does not match count of modified fields ({1})",
                    clauses.Count, fields.Count));
            }

            // compile field assignment clauses (SET clauses or value inserts)
            for (var ordinal = 0; ordinal < clauses.Count; ordinal++)
            {
                var clause = clauses[ordinal];
                var field = fields[ordinal];

                // for bulk requests, primary key is there but it is only used to lookup the record
                // for non-bulk requests, primary key should not be in the list of UPDATE clauses
                if (changeType == DriverChangeType.Update && !cacheInfo.ParsedRequest.IsBulk)
                {
                    if (!storageDriver.CanUpdateField(field.FieldId))
                    {
                        throw new Exception(string.Format("Cannot update field {0}/{1} on entity {2}", field.FieldId, field.Name, cacheInfo.ParsedRequest.TargetEntity.Name));
                    }
                }

                // prepare Action compilation context
                var compilerState = QueryParser.PrepareCompilerState(containerDescriptor, cacheInfo, null);
                compilerState.CompileToAction = true;

                // extractor has signature like Func<ClauseEvaluationContext, T>
                var extractor = clause == null
                                    ? QueryParser.CompileFieldValueExtractorClause(compilerState, field, containerDescriptor, cacheInfo, MakeNullableType(field.DbType))
                                    : QueryParser.CompileClause(compilerState, clause, containerDescriptor, cacheInfo, MakeNullableType(field.DbType));
                // get the value into local variable, to prevent multiple invokations when row writer checks for null
                var extractedValue = Expression.Variable(extractor.Type);

                // now take the extractor and create another method, that will take the value and then put it into the changebuffer's data
                var changeBufferData = Expression.Field(Expression.Field(compilerState.Context, "ChangeBuffer"), "Data");
                var blockBody = Expression.Block(
                    new[] {extractedValue},
                    Expression.Assign(extractedValue, extractor),
                    DriverRowData.CreateWriteAccessor(extractedValue, changeBufferData, field.DbType, ordinal));

                updates.Add(
                    new ParsedRequest.FieldAssignment
                    {
                        Field = field,
                        CompiledExpression = (Action<ClauseEvaluationContext>)QueryParser.CompileClause(blockBody, compilerState)
                    });
            }
        }
        public StorageDriverFacade(string extention, IStorageDriver driver)
        {
            driver.ThrowIfNull(nameof(driver));
            if (extention.Length > 3)
            {
                throw new ArgumentException("extention is too long", nameof(extention));
            }
            PathValidator.ValidateExtention(extention);

            _extention = extention.IsEmpty() ? "" : "." + extention;
            _driver    = driver;
        }
Example #9
0
        /// <summary> Disposes a storage driver when no longer used. </summary>
        private static IDisposable Release(IStorageDriver sd)
        {
            while (true)
            {
                var release = sd as IDisposable;
                if (release != null)
                {
                    return(release);
                }

                var asWrap = sd as ReadOnlyDriverWrapper;
                if (asWrap == null)
                {
                    return(new Disposable());
                }

                sd = asWrap.Wrapped;
            }
        }
        private static StorageIndex GetIndex(IStorageDriver driver)
        {
            StorageIndex result = null;

            if (driver.Exists(IndexName))
            {
                try
                {
                    result = XmlClassSerializer.Load <StorageIndex>(driver.Read(IndexName));
                }
                catch (Exception)
                {
                    //TODO: log exception
                }
            }
            return(result ?? new StorageIndex {
                Items = new List <StorageItem>()
            });
        }
 private static DataContainerDescriptor RequireDescriptor(IStorageDriver driver)
 {
     var descriptor = driver.GetDescriptor();
     if (descriptor == null)
     {
         throw new InvalidOperationException("Driver reports that container descriptor is not initialized");
     }
     return descriptor;
 }
Example #12
0
 public storage_driver()
 {
     _driver = GetFreshStorageDriver();
 }
Example #13
0
 internal EventStreamWrapper(IStorageDriver storage, IEnumerable <IProjection <TEvent> > projections, IProjectionCacheProvider projectionCache, ILogAdapter log = null)
 {
     _log        = log;
     Stream      = new EventStream <TEvent>(storage, log);
     _projection = new ReifiedProjectionGroup <TEvent, TState>(projections, projectionCache, log);
 }
 private void EnumTesting(IStorageDriver driver, IEnumerable <string> resultArray)
 {
     _baseDriver.Enum().Returns(_enumTestArray);
     Assert.IsTrue(driver.Enum().ScrambledEquals(resultArray));
 }
Example #15
0
 public StatsDriverWrapper(IStorageDriver inner)
 {
     Inner = inner;
 }
 public void TestSetup()
 {
     _baseDriver     = Substitute.For <IStorageDriver>();
     _driver         = new StorageDriverFacade(Ext, _baseDriver);
     _driverEmptyExt = new StorageDriverFacade("", _baseDriver);
 }
 /// <summary> Open an event stream on the provided store. </summary>
 internal EventStream(IStorageDriver storage, ILogAdapter log = null)
 {
     Storage = storage;
     _log    = log;
 }
Example #18
0
 public void TestSetup()
 {
     _driver = Substitute.For <IStorageDriver>();
     _index  = Substitute.For <IStorageIndex>();
     _info   = new DataInfo("test data");
 }
 public CacheStorageDriver(IStorageDriver source, string path)
 {
     _source = source;
     _cache  = new FileStorageDriver(path);
 }
Example #20
0
 internal MigrationStream(IStorageDriver driver)
 {
     _driver = driver;
 }
Example #21
0
 public void SetUp()
 {
     _driver = GetFreshStorageDriver();
 }
 /// <summary> Return the provided storage driver, for testing purposes. </summary>
 internal StorageConfiguration(IStorageDriver driver)
 {
     _storageDriver = driver;
 }
Example #23
0
 public Repository(IStorageDriver driver, ICryptoService crypto, IMemzConfigProvider configProvider)
 {
     this.driver         = driver;
     this.crypto         = crypto;
     this.configProvider = configProvider;
 }
 public ReadOnlyDriverWrapper(IStorageDriver wrapped)
 {
     Wrapped = wrapped;
 }
 public void TestSetup()
 {
     _driver = Substitute.For <IStorageDriver>();
     _driver.Write(Arg.Is(XmlStorageIndex.IndexName)).Returns(ux => File.Create(XmlStorageIndex.IndexName));
 }
Example #26
-1
        public DataEngine(ITracer tracer, string instanceName, int maxConcurrency, IStorageDriver storageDriver, DataContainerDescriptor containerDescriptor)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException("tracer");
            }

            if (maxConcurrency <= 0 || maxConcurrency > 10000)
            {
                throw new ArgumentOutOfRangeException("maxConcurrency");
            }

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

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

            m_tracer = tracer;
            m_containerDescriptor = containerDescriptor;
            m_maxConcurrency = maxConcurrency;
            m_parsedRequestCache = new ParsedRequestCache(instanceName);
            m_storageDriver = storageDriver;
            m_parser = new QueryParser(containerDescriptor, maxConcurrency);
            m_activeProcessors = new ConcurrentDictionary<RequestExecutionContext, Task>(m_maxConcurrency, m_maxConcurrency);
            m_utcLastUsedAt = DateTime.UtcNow;
        }