Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        protected static string SetTable(Type type)
        {
            #region 初始化缓存
            if (!tableNames.ContainsKey(type))
            {
                foreach (var attr in type.CustomAttributes)
                {
                    if (attr.AttributeType == typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute))
                    {
                        tableNames.TryAdd(type, attr.ConstructorArguments[0].Value.ToString());
                        break;
                    }
                }
            }
            #endregion

            var name = tableNames.ContainsKey(type) ? tableNames[type] : type.Name.ToLower();

            #region 分析表结构
            var pkSchema = new PrimaryKeySchema();
            foreach (PropertyInfo info in rft.GetPropertyList(type))
            {
                if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.KeyAttribute)) == null)
                {
                    continue;
                }
                else if (info.PropertyType == typeof(int))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Integer);
                }
                else if (info.PropertyType == typeof(string))
                {
                    pkSchema.Add(info.Name, ColumnValueType.String);
                }
                else if (info.PropertyType == typeof(byte[]))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Binary);
                }
                else
                {
                    throw new Exception("不支持当前类型的主键");
                }
            }
            ;
            #endregion

            #region 生成表结构
            if (tableKey.ContainsKey(name))
            {
                #region 对比已有结构
                tableKey[name].ForEach(key =>
                {
                    pkSchema.ForEach(tmp =>
                    {
                        if (key.Item1 == tmp.Item1 && (key.Item2 != tmp.Item2 || key.Item3 != tmp.Item3))
                        {
                            log.Fatal("Table structure is inconsistent, table name:" + name);
                            return;
                        }
                    });
                });
                #endregion
            }
            else
            {
                #region 创建新表
                //通过表名和主键列的schema创建一个tableMeta
                var client    = new OTSClient(OtsConfig);
                var tableMeta = new TableMeta(name, pkSchema);
                // 设定预留读吞吐量为0,预留写吞吐量为0
                var reservedThroughput = new CapacityUnit(0, 0);

                try
                {
                    // 构造CreateTableRequest对象
                    var reqCreateTable = new CreateTableRequest(tableMeta, reservedThroughput);
                    // 调用client的CreateTable接口,如果没有抛出异常,则说明成功,否则失败
                    client.CreateTable(reqCreateTable);

                    // 生成查询索引
                    var reqCreateSearchIndex = new CreateSearchIndexRequest(name, name);
                    var fieldSchemas         = new List <FieldSchema>();
                    foreach (PropertyInfo info in rft.GetPropertyList(type))
                    {
                        if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.CompareAttribute)) == null)
                        {
                            continue;
                        }
                        else if (info.PropertyType == typeof(int) || info.PropertyType == typeof(long))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.LONG)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(string))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.TEXT)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(double))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.DOUBLE)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(bool))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.BOOLEAN)
                            {
                                index = true
                            });
                        }
                    }
                    ;
                    if (fieldSchemas.Count > 0)
                    {
                        reqCreateSearchIndex.IndexSchame = new IndexSchema()
                        {
                            FieldSchemas = fieldSchemas
                        };
                        client.CreateSearchIndex(reqCreateSearchIndex);
                        log.Info("Create table succeeded, table name:" + name);
                    }
                }
                // 处理异常
                catch (Exception ex)
                {
                    Console.WriteLine("Create table failed, exception:{0}", ex.Message);
                }
                #endregion
            }
            #endregion

            return(name);
        }