Exemple #1
0
        public int GetVersion(long eventTimestamp)
        {
            var firstTable = AllSplitTableList.FirstOrDefault();

            //如果不需要分表,直接返回
            if (firstTable != null && !sharding)
            {
                return(0);
            }
            var nowUtcTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            var subMinutes = (eventTimestamp - (firstTable != null ? firstTable.CreateTime : nowUtcTime)) / (1000 * 60);

            return(subMinutes > 0 ? (int)(subMinutes / shardingMinutes) : 0);
        }
Exemple #2
0
        public async ValueTask <TableInfo> GetTable(long eventTimestamp)
        {
            var firstTable = AllSplitTableList.FirstOrDefault();

            //如果不需要分表,直接返回
            if (firstTable != null && !sharding)
            {
                return(firstTable);
            }
            var nowUtcTime  = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            var subMinutes  = (eventTimestamp - (firstTable != null ? firstTable.CreateTime : nowUtcTime)) / (60 * 1000);
            var version     = subMinutes > 0 ? (int)(subMinutes / shardingMinutes) : 0;
            var resultTable = AllSplitTableList.FirstOrDefault(t => t.Version == version);

            if (resultTable == default)
            {
                var table = new TableInfo
                {
                    Version    = version,
                    Prefix     = EventTable,
                    CreateTime = nowUtcTime,
                    Name       = EventTable + "_" + version
                };
                try
                {
                    await TableRepository.CreateEventTable(table);

                    AllSplitTableList.Add(table);
                    resultTable = table;
                }
                catch (Exception ex)
                {
                    AllSplitTableList = await TableRepository.GetTableListFromDb();

                    if (ex is Npgsql.PostgresException e && e.SqlState != "42P07" && e.SqlState != "23505")
                    {
                        throw;
                    }
                    resultTable = AllSplitTableList.First(t => t.Version == version);
                }
            }
            return(resultTable);
        }