Ejemplo n.º 1
0
        /// <summary>
        /// DeleteAsync
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="versions"></param>
        /// <returns></returns>
        /// <exception cref="KVStoreException"></exception>
        public async Task DeleteAsync <T>(IEnumerable <string> keys, IEnumerable <int> versions) where T : KVStoreEntity, new()
        {
            ThrowIf.NullOrEmpty(versions, nameof(versions));

            if (keys.Count() != versions.Count())
            {
                throw Exceptions.VersionsKeysNotEqualError();
            }

            KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>();

            try
            {
                await _engine.EntityDeleteAsync(
                    entityDef.KVStoreName,
                    entityDef.EntityType.FullName !,
                    keys,
                    versions
                    ).ConfigureAwait(false);
            }
            catch (Exception ex) when(!(ex is KVStoreException))
            {
                throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, keys: keys, values: versions, innerException: ex);
            }
        }
Ejemplo n.º 2
0
        public void EntityMapper_ToParameter_Performance_Test(EngineType engineType)
        {
            var entities = Mocker.GetPublishers(1000000);

            var def = EntityDefFactory.GetDef <PublisherEntity>();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Restart();
            foreach (var entity in entities)
            {
                _ = entity.ToParameters(def !, engineType);
            }
            stopwatch.Stop();

            _output.WriteLine($"Emit: {stopwatch.ElapsedMilliseconds}");

            stopwatch.Restart();
            foreach (var entity in entities)
            {
                _ = entity.ToParametersUsingReflection(def !, engineType);
            }
            stopwatch.Stop();

            _output.WriteLine($"Reflection: {stopwatch.ElapsedMilliseconds}");
        }
Ejemplo n.º 3
0
        public void EntityMapper_ToParameter_Test(EngineType engineType)
        {
            PublisherEntity publisherEntity = Mocker.MockOnePublisherEntity();

            var emit_results = publisherEntity.ToParameters(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1);

            var reflect_results = publisherEntity.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1);

            AssertEqual(emit_results, reflect_results, engineType);

            //PublisherEntity2

            PublisherEntity2 publisherEntity2 = new PublisherEntity2();

            var emit_results2 = publisherEntity2.ToParameters(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1);

            var reflect_results2 = publisherEntity2.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1);

            AssertEqual(emit_results2, reflect_results2, engineType);

            //PublisherEntity3

            PublisherEntity3 publisherEntity3 = new PublisherEntity3();

            var emit_results3 = publisherEntity3.ToParameters(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1);

            var reflect_results3 = publisherEntity3.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1);

            AssertEqual(emit_results3, reflect_results3, engineType);
        }
Ejemplo n.º 4
0
        private FromExpression <T> InternalJoin <Target>(string joinType, Expression joinExpr) where Target : DatabaseEntity
        {
            EntityDef targetDef = EntityDefFactory.GetDef <Target>() !;

            _statementBuilder.Append(' ');
            _statementBuilder.Append(joinType);
            _statementBuilder.Append(' ');
            _statementBuilder.Append(targetDef.DbTableReservedName);
            _statementBuilder.Append(" ON ");
            _statementBuilder.Append(joinExpr.ToStatement(_expressionContext));
            _statementBuilder.Append(' ');

            return(this);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// DeleteAllAsync
        /// </summary>
        /// <returns></returns>
        /// <exception cref="KVStoreException"></exception>
        public async Task DeleteAllAsync <T>() where T : KVStoreEntity, new()
        {
            KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>();

            try
            {
                await _engine.EntityDeleteAllAsync(
                    entityDef.KVStoreName,
                    entityDef.EntityType.FullName !
                    ).ConfigureAwait(false);
            }
            catch (Exception ex) when(!(ex is KVStoreException))
            {
                throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, null, ex);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 反应Version变化
        /// </summary>
        /// <exception cref="KVStoreException"></exception>
        public async Task <IEnumerable <T?> > GetAllAsync <T>() where T : KVStoreEntity, new()
        {
            KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>();

            try
            {
                IEnumerable <Tuple <string?, int> > tuples = await _engine.EntityGetAllAsync(
                    entityDef.KVStoreName,
                    entityDef.EntityType.FullName !).ConfigureAwait(false);

                return(MapTupleToEntity <T>(tuples));
            }
            catch (Exception ex) when(!(ex is KVStoreException))
            {
                throw Exceptions.Unkown(type: typeof(T).FullName, storeName: entityDef.KVStoreName, key: null, innerException: ex);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 反应Version变化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="lastUser"></param>
        /// <returns></returns>
        /// <exception cref="KVStoreException"></exception>
        public async Task UpdateAsync <T>(IEnumerable <T> items, string lastUser) where T : KVStoreEntity, new()
        {
            if (!items.Any())
            {
                return;
            }

            ThrowIf.NotValid(items, nameof(items));

            KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>();

            try
            {
                IEnumerable <int> originalVersions = items.Select(t => t.Version).ToArray();

                foreach (var t in items)
                {
                    t.LastUser = lastUser;
                    t.LastTime = TimeUtil.UtcNow;
                }

                await _engine.EntityUpdateAsync(
                    entityDef.KVStoreName,
                    entityDef.EntityType.FullName !,
                    items.Select(t => GetEntityKey(t, entityDef)),
                    items.Select(t => SerializeUtil.ToJson(t)),
                    originalVersions).ConfigureAwait(false);

                //反应Version变化
                foreach (var t in items)
                {
                    t.Version++;
                }
            }

            catch (Exception ex) when(!(ex is KVStoreException))
            {
                throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, items, ex);
            }
        }
        /// <summary>
        /// DeleteAsync
        /// </summary>
        /// <param name="database"></param>
        /// <param name="whereExpr"></param>
        /// <param name="transactionContext"></param>
        /// <returns></returns>
        /// <exception cref="DatabaseException"></exception>
        public static async Task DeleteAsync <T>(this IDatabase database, Expression <Func <T, bool> > whereExpr, TransactionContext?transactionContext = null) where T : DatabaseEntity, new()
        {
            EntityDef entityDef = EntityDefFactory.GetDef <T>() !;

            if (!entityDef.DatabaseWriteable)
            {
                throw Exceptions.NotWriteable(entityDef.EntityFullName, entityDef.DatabaseName);
            }

            try
            {
                WhereExpression <T> where = database.Where(whereExpr).And(t => !t.Deleted);

                var command = DbCommandBuilder.CreateDeleteCommand(database.EngineType, entityDef, where);

                await database.DatabaseEngine.ExecuteCommandNonQueryAsync(transactionContext?.Transaction, entityDef.DatabaseName !, command).ConfigureAwait(false);
            }
            catch (Exception ex) when(!(ex is DatabaseException))
            {
                throw Exceptions.UnKown(entityDef.EntityFullName, whereExpr.ToString(), ex);
            }
        }
        /// <summary>
        /// AddOrUpdateByIdAsync
        /// </summary>
        /// <param name="database"></param>
        /// <param name="item"></param>
        /// <param name="transContext"></param>
        /// <returns></returns>
        /// <exception cref="DatabaseException"></exception>
        public static async Task AddOrUpdateByIdAsync <T>(this IDatabase database, T item, TransactionContext?transContext = null) where T : DatabaseEntity, new()
        {
            ThrowIf.NotValid(item, nameof(item));

            EntityDef entityDef = EntityDefFactory.GetDef <T>() !;

            if (!entityDef.DatabaseWriteable)
            {
                throw Exceptions.NotWriteable(entityDef.EntityFullName, entityDef.DatabaseName);
            }

            try
            {
                item.LastTime = TimeUtil.UtcNow;

                if (item.Version < 0)
                {
                    item.Version = 0;
                }

                var command = DbCommandBuilder.CreateAddOrUpdateCommand(database.EngineType, entityDef, item);

                using var reader = await database.DatabaseEngine.ExecuteCommandReaderAsync(transContext?.Transaction, entityDef.DatabaseName !, command, true).ConfigureAwait(false);

                IList <T> entities = reader.ToEntities <T>(database.EngineType, entityDef);

                T newItem = entities[0];

                item.CreateTime = newItem.CreateTime;
                item.Version    = newItem.Version;
                item.LastUser   = newItem.LastUser;
            }
            catch (Exception ex) when(!(ex is DatabaseException))
            {
                throw Exceptions.UnKown(entityDef.EntityFullName, SerializeUtil.ToJson(item), ex);
            }
        }
Ejemplo n.º 10
0
        public string GetEntityKey <T>(T item) where T : KVStoreEntity, new()
        {
            KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>();

            return(GetEntityKey(item, entityDef));
        }
Ejemplo n.º 11
0
 public DefaultKVStore(IKVStoreEngine kvstoreEngine)
 {
     _engine = kvstoreEngine;
     EntityDefFactory.Initialize(kvstoreEngine);
 }
Ejemplo n.º 12
0
        public async Task Test_EntityMapperPerformanceAsync(int index)
        {
            index++;



            IDatabase database = _sqlite;

            IList <BookEntity_Client>?books = Mocker.GetBooks_Client(500);

            TransactionContext?trans = await _sqlIteTransaction.BeginTransactionAsync <BookEntity_Client>().ConfigureAwait(false);

            IEnumerable <BookEntity_Client> re = await database.RetrieveAsync <BookEntity_Client>(b => b.Deleted, trans).ConfigureAwait(false);

            await database.AddAsync <BookEntity_Client>(Mocker.GetBooks_Client(1)[0], "", trans).ConfigureAwait(false);

            try
            {
                //await database.AddAsync<BookEntity>(books[0], "", trans);

                await database.BatchAddAsync(books, "x", trans).ConfigureAwait(false);

                await _sqlIteTransaction.CommitAsync(trans).ConfigureAwait(false);
            }
            catch
            {
                await _sqlIteTransaction.RollbackAsync(trans).ConfigureAwait(false);
            }


            Stopwatch stopwatch = new Stopwatch();

            using SqliteConnection mySqlConnection = new SqliteConnection("Data Source=sqlite_test2.db");



            //time = 0;
            int loop = 10;

            TimeSpan time0 = TimeSpan.Zero, time1 = TimeSpan.Zero, time2 = TimeSpan.Zero, time3 = TimeSpan.Zero;

            for (int cur = 0; cur < loop; ++cur)
            {
                await mySqlConnection.OpenAsync().ConfigureAwait(false);


                using SqliteCommand command0 = new SqliteCommand("select * from tb_bookentity_client limit 1000", mySqlConnection);

                SqliteDataReader?reader0 = await command0.ExecuteReaderAsync().ConfigureAwait(false);

                List <BookEntity_Client> list1 = new List <BookEntity_Client>();
                List <BookEntity_Client> list2 = new List <BookEntity_Client>();
                List <BookEntity_Client> list3 = new List <BookEntity_Client>();

                int len = reader0.FieldCount;
                EntityPropertyDef[] propertyDefs = new EntityPropertyDef[len];
                MethodInfo[]        setMethods   = new MethodInfo[len];

                EntityDef definition = EntityDefFactory.GetDef <BookEntity_Client>() !;

                for (int i = 0; i < len; ++i)
                {
                    propertyDefs[i] = definition.GetPropertyDef(reader0.GetName(i)) !;
                    setMethods[i]   = propertyDefs[i].SetMethod;
                }


                Func <IDataReader, object> mapper1 = EntityMapperDelegateCreator.CreateToEntityDelegate(definition, reader0, 0, definition.FieldCount, false, Database.Engine.EngineType.SQLite);


                //Warning: 如果用Dapper,小心DateTimeOffset的存储,会丢失offset,然后转回来时候,会加上当地时间的offset
                TypeHandlerHelper.AddTypeHandlerImpl(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler(), false);
                Func <IDataReader, object> mapper2 = DataReaderTypeMapper.GetTypeDeserializerImpl(typeof(BookEntity_Client), reader0);



                Stopwatch stopwatch1 = new Stopwatch();
                Stopwatch stopwatch2 = new Stopwatch();
                Stopwatch stopwatch3 = new Stopwatch();

                while (reader0.Read())
                {
                    stopwatch1.Start();

                    object obj1 = mapper1(reader0);

                    list1.Add((BookEntity_Client)obj1);
                    stopwatch1.Stop();



                    stopwatch2.Start();
                    object obj2 = mapper2(reader0);

                    list2.Add((BookEntity_Client)obj2);
                    stopwatch2.Stop();


                    stopwatch3.Start();

                    BookEntity_Client item = new BookEntity_Client();

                    for (int i = 0; i < len; ++i)
                    {
                        EntityPropertyDef property = propertyDefs[i];

                        object?value = TypeConvert.DbValueToTypeValue(reader0[i], property, Database.Engine.EngineType.SQLite);

                        if (value != null)
                        {
                            setMethods[i].Invoke(item, new object?[] { value });
                        }
                    }

                    list3.Add(item);

                    stopwatch3.Stop();
                }

                time1 += stopwatch1.Elapsed;
                time2 += stopwatch2.Elapsed;
                time3 += stopwatch3.Elapsed;

                await reader0.DisposeAsync().ConfigureAwait(false);

                command0.Dispose();

                await mySqlConnection.CloseAsync().ConfigureAwait(false);
            }

            _output.WriteLine("Emit Coding : " + (time1.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture));
            _output.WriteLine("Dapper : " + (time2.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture));
            _output.WriteLine("Reflection : " + (time3.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture));
        }
Ejemplo n.º 13
0
 public string ToStatement()
 {
     return($" FROM {EntityDefFactory.GetDef<T>()!.DbTableReservedName} {_statementBuilder}");
 }
Ejemplo n.º 14
0
        public Task <TransactionContext> BeginTransactionAsync <T>(IsolationLevel?isolationLevel = null) where T : DatabaseEntity
        {
            EntityDef entityDef = EntityDefFactory.GetDef <T>() !;

            return(BeginTransactionAsync(entityDef.DatabaseName !, isolationLevel));
        }