Beispiel #1
0
        public QueryResult <T> Invoke <T>(RecordSetReader recordSetReader, Session session, ParameterContext parameterContext)
        {
            var reader = (IMaterializingReader <T>)
                         materializeMethod.Invoke(recordSetReader, session, parameterContext);

            return(new QueryResult <T>(reader, session.GetLifetimeToken()));
        }
Beispiel #2
0
        /// <summary>
        /// Gets <see cref="RecordSetReader"/> bound to the specified <paramref name="session"/>.
        /// </summary>
        /// <param name="session">Session to bind.</param>
        /// <param name="parameterContext"><see cref="ParameterContext"/> instance with
        /// the values of query parameters.</param>
        /// <returns>New <see cref="RecordSetReader"/> bound to specified <paramref name="session"/>.</returns>
        public RecordSetReader GetRecordSetReader(Session session, ParameterContext parameterContext)
        {
            ArgumentValidator.EnsureArgumentNotNull(session, nameof(session));
            var enumerationContext = session.CreateEnumerationContext(parameterContext);

            return(RecordSetReader.Create(enumerationContext, this));
        }
Beispiel #3
0
        public void RecordSetReader()
        {
            var testRS = generateRecordSet();

            var rdr = new RecordSetReader(testRS);

            Assert.True(rdr.HasRows);
            Assert.Throws <InvalidOperationException>(() => { var o = rdr[0]; });

            Assert.True(rdr.Read());
            Assert.Equal(4, rdr.FieldCount);
            Assert.Equal("id", rdr.GetName(0));
            Assert.Equal(2, rdr.GetOrdinal("amount"));
            Assert.Equal("added_date", rdr.GetName(3));
            Assert.Throws <IndexOutOfRangeException>(() => { var o = rdr.GetName(4); });

            Assert.Equal(0, rdr.GetInt32(0));
            Assert.Equal(0, rdr[0]);
            Assert.Equal("Name0", rdr[1]);
            Assert.Equal(0, rdr[2]);
            Assert.Equal(1, rdr.GetDateTime(3).Month);

            int cnt = 1;

            while (rdr.Read())
            {
                Assert.Equal(cnt, rdr[0]);
                cnt++;
            }
            Assert.Throws <InvalidOperationException>(() => { var o = rdr[0]; });

            rdr.Dispose();
            Assert.Throws <InvalidOperationException>(() => { var o = rdr.FieldCount; });
            Assert.Throws <InvalidOperationException>(() => { var o = rdr.GetOrdinal("id"); });

            Assert.Equal(100, cnt);

            // read RS from RecordSetReader
            var testRSCopy = RecordSet.FromReader(new RecordSetReader(testRS));

            Assert.Equal(testRS.Count, testRSCopy.Count);
            Assert.Equal(testRS.Columns.Count, testRSCopy.Columns.Count);

            var testRSCopyOneRec = RecordSet.FromReader(new RecordSetReader(testRS), 1);

            Assert.Equal(1, testRSCopyOneRec.Count);

            // read into initialized RecordSet
            var newRS = new RecordSet(
                new[] {
                new RecordSet.Column("id", typeof(int)),
                new RecordSet.Column("name", typeof(string))
            }
                );

            newRS.Load(new RecordSetReader(testRS));
            Assert.Equal(testRS.Count, newRS.Count);
            Assert.Equal("Name99", newRS[99].Field <string>("name"));
        }
Beispiel #4
0
 /// <summary>
 /// Transforms current <see cref="ExecutableProvider"/> instance to <see cref="IEnumerable{T}"/>
 /// sequence of <see cref="Tuple"/>s.
 /// </summary>
 /// <param name="context">The <see cref="EnumerationContext"/> instance where to perform enumeration.</param>
 /// <returns>Sequence of <see cref="Tuple"/>s.</returns>
 public IEnumerable <Tuple> ToEnumerable(EnumerationContext context)
 {
     using var tupleReader = RecordSetReader.Create(context, this);
     while (tupleReader.MoveNext())
     {
         yield return(tupleReader.Current);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Asynchronously gets <see cref="RecordSetReader"/> bound to the specified <paramref name="session"/>.
        /// </summary>
        /// <remarks> Multiple active operations are not supported. Use <see langword="await"/>
        /// to ensure that all asynchronous operations have completed.</remarks>
        /// <param name="session">Session to bind.</param>
        /// <param name="parameterContext"><see cref="ParameterContext"/> instance with
        /// the values of query parameters.</param>
        /// <param name="token">Token to cancel operation.</param>
        /// <returns>Task performing this operation.</returns>
        public async Task <RecordSetReader> GetRecordSetReaderAsync(
            Session session, ParameterContext parameterContext, CancellationToken token)
        {
            ArgumentValidator.EnsureArgumentNotNull(session, nameof(session));
            var enumerationContext =
                await session.CreateEnumerationContextAsync(parameterContext, token).ConfigureAwait(false);

            return(await RecordSetReader.CreateAsync(enumerationContext, this, token).ConfigureAwait(false));
        }
        /// <summary>
        /// Wraps <see cref="RecordSetReader"/> by adding <see cref="Tuple"/> to <typeparamref name="TResult"/>
        /// conversion for individual records.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="recordSetReader">The reader over raw data source.</param>
        /// <param name="context">The context.</param>
        /// <param name="parameterContext">The parameter context.</param>
        /// <param name="itemMaterializer">The materialization delegate performing
        /// <see cref="Tuple"/> instance to instance of <typeparamref name="TResult"/> type conversion.</param>
        public static object Materialize <TResult>(
            RecordSetReader recordSetReader,
            MaterializationContext context,
            ParameterContext parameterContext,
            IItemMaterializer <TResult> itemMaterializer)
        {
            var enumerationContext = (EnumerationContext)recordSetReader.Context;

            if (enumerationContext != null)
            {
                enumerationContext.MaterializationContext = context;
            }

            return(new MaterializingReader <TResult>(recordSetReader, context, parameterContext, itemMaterializer));
        }
        public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair <int>[] entityColumns, Tuple tuple)
        {
            var result = entities[entityIndex];

            if (result != null)
            {
                return(result);
            }

            TypeReferenceAccuracy accuracy;
            int typeId = RecordSetReader.ExtractTypeId(type, typeIdRegistry, tuple, typeIdIndex, out accuracy);

            if (typeId == TypeInfo.NoTypeId)
            {
                return(null);
            }

            bool canCache            = accuracy == TypeReferenceAccuracy.ExactType;
            var  materializationInfo = MaterializationContext.GetTypeMapping(entityIndex, type, typeId, entityColumns);
            Key  key;
            var  keyIndexes = materializationInfo.KeyIndexes;

            if (!KeyFactory.IsValidKeyTuple(tuple, keyIndexes))
            {
                return(null);
            }
            if (keyIndexes.Length <= WellKnown.MaxGenericKeyLength)
            {
                key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, tuple, accuracy, canCache, keyIndexes);
            }
            else
            {
                var keyTuple = materializationInfo.KeyTransform.Apply(TupleTransformType.TransformedTuple, tuple);
                key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, keyTuple, accuracy, canCache, null);
            }
            if (accuracy == TypeReferenceAccuracy.ExactType)
            {
                var entityTuple = materializationInfo.Transform.Apply(TupleTransformType.Tuple, tuple);
                var entityState = Session.Handler.UpdateState(key, entityTuple);
                result = entityState.Entity;
            }
            else
            {
                result = Session.Query.SingleOrDefault(key);
            }
            entities[entityIndex] = result;
            return(result);
        }
Beispiel #8
0
        // Constructors

        internal Domain(DomainConfiguration configuration, object upgradeContextCookie, SqlConnection singleConnection, DefaultSchemaInfo defaultSchemaInfo)
        {
            Configuration                = configuration;
            Handlers                     = new HandlerAccessor(this);
            GenericKeyFactories          = new ConcurrentDictionary <TypeInfo, GenericKeyFactory>();
            RecordSetReader              = new RecordSetReader(this);
            KeyGenerators                = new KeyGeneratorRegistry();
            PrefetchFieldDescriptorCache = new ConcurrentDictionary <TypeInfo, ReadOnlyList <PrefetchFieldDescriptor> >();
            KeyCache                     = new LruCache <Key, Key>(Configuration.KeyCacheSize, k => k);
            QueryCache                   = new LruCache <object, Pair <object, TranslatedQuery> >(Configuration.QueryCacheSize, k => k.First);
            PrefetchActionMap            = new Dictionary <TypeInfo, Action <SessionHandler, IEnumerable <Key> > >();
            Extensions                   = new ExtensionCollection();
            UpgradeContextCookie         = upgradeContextCookie;
            SingleConnection             = singleConnection;
            StorageNodeManager           = new StorageNodeManager(Handlers);
        }
Beispiel #9
0
 internal MaterializingReader(RecordSetReader recordSetReader, MaterializationContext context,
                              ParameterContext parameterContext, IItemMaterializer <TItem> itemMaterializer)
 {
     this.recordSetReader  = recordSetReader;
     this.context          = context;
     this.parameterContext = parameterContext;
     this.itemMaterializer = itemMaterializer;
     if (context.MaterializationQueue == null)
     {
         context.MaterializationQueue = materializationQueue = new Queue <Action>();
     }
     else
     {
         materializationQueue = null;
     }
 }
        private void PutLoadedStatesInCache(IEnumerable <Tuple> queryResult, RecordSetReader reader,
                                            HashSet <Key> foundedKeys)
        {
            var records = reader.Read(queryResult, Provider.Header, manager.Owner.Session);

            foreach (var record in records)
            {
                if (record != null)
                {
                    var fetchedKey = record.GetKey();
                    var tuple      = record.GetTuple();
                    if (tuple != null)
                    {
                        manager.SaveStrongReference(manager.Owner.UpdateState(fetchedKey, tuple));
                        foundedKeys.Add(fetchedKey);
                    }
                }
            }
        }
Beispiel #11
0
        private void UpdateCache(Session session, RecordSetReader source)
        {
            var reader = Domain.EntityDataReader;

            foreach (var record in reader.Read(source.ToEnumerable(), source.Header, session))
            {
                for (int i = 0; i < record.Count; i++)
                {
                    var key = record.GetKey(i);
                    if (key == null)
                    {
                        continue;
                    }
                    var tuple = record.GetTuple(i);
                    if (tuple == null)
                    {
                        continue;
                    }
                    session.UpdateStateInCache(key, tuple);
                }
            }
        }
Beispiel #12
0
        public void RecordSetReader()
        {
            var testRS = new RecordSet(new [] {
                new RecordSet.Column("id", typeof(int)),
                new RecordSet.Column("name", typeof(string)),
                new RecordSet.Column("amount", typeof(decimal)),
                new RecordSet.Column("added_date", typeof(DateTime))
            });

            for (int i = 0; i < 100; i++)
            {
                testRS.Add(new object[] {
                    i, "Name" + i.ToString(), i % 20, new DateTime(2000, 1, 1).AddMonths(i)
                });
            }

            var rdr = new RecordSetReader(testRS);

            Assert.True(rdr.HasRows);
            Assert.Throws <InvalidOperationException>(() => { var o = rdr[0]; });

            Assert.True(rdr.Read());
            Assert.Equal(4, rdr.FieldCount);
            Assert.Equal("id", rdr.GetName(0));
            Assert.Equal(2, rdr.GetOrdinal("amount"));
            Assert.Equal("added_date", rdr.GetName(3));
            Assert.Throws <IndexOutOfRangeException>(() => { var o = rdr.GetName(4); });

            Assert.Equal(0, rdr.GetInt32(0));
            Assert.Equal(0, rdr[0]);
            Assert.Equal("Name0", rdr[1]);
            Assert.Equal(0, rdr[2]);
            Assert.Equal(1, rdr.GetDateTime(3).Month);

            int cnt = 1;

            while (rdr.Read())
            {
                Assert.Equal(cnt, rdr[0]);
                cnt++;
            }
            Assert.Throws <InvalidOperationException>(() => { var o = rdr[0]; });

            rdr.Dispose();
            Assert.Throws <InvalidOperationException>(() => { var o = rdr.FieldCount; });
            Assert.Throws <InvalidOperationException>(() => { var o = rdr.GetOrdinal("id"); });

            Assert.Equal(100, cnt);

            // read RS from RecordSetReader
            var testRSCopy = RecordSet.FromReader(new RecordSetReader(testRS));

            Assert.Equal(testRS.Count, testRSCopy.Count);
            Assert.Equal(testRS.Columns.Count, testRSCopy.Columns.Count);

            // read into initialized RecordSet
            var newRS = new RecordSet(
                new[] {
                new RecordSet.Column("id", typeof(int)),
                new RecordSet.Column("name", typeof(string))
            }
                );

            newRS.Load(new RecordSetReader(testRS));
            Assert.Equal(testRS.Count, newRS.Count);
            Assert.Equal("Name99", newRS[99].Field <string>("name"));
        }