[关闭]
@leviyuan 2017-09-30T07:27:52.000000Z 字数 3709 阅读 472

SuperConfig

config


格式规范

索引的构建过程

  1. // 假定数据结构是这样的
  2. class Config
  3. {
  4. public int id;
  5. public string a;
  6. public string b;
  7. public int c;
  8. public Config(int id, string a, string b, int c)
  9. {
  10. this.id = id;
  11. this.a = a;
  12. this.b = b;
  13. this.c = c;
  14. }
  15. }
  16. static void buildindex()
  17. {
  18. // 所有数据都以id作为唯一主键
  19. Dictionary<int, Config> datas = new Dictionary<int, Config>();
  20. // 列举4条测试数据
  21. Config c1 = new Config(1, "A", "小猪", 1);
  22. Config c2 = new Config(2, "A", "小猫", 2);
  23. Config c3 = new Config(3, "B", "小猪", 2);
  24. Config c4 = new Config(4, "B", "小猫", 1);
  25. // 配置表中的数据默认都会添加在这个datas里面
  26. foreach (var c in new Config[] { c1, c2, c3, c4 })
  27. datas.Add(c.id, c);
  28. // 构建索引
  29. // 索引 a|b 的生成
  30. Dictionary<string, Dictionary<string, List<int>>> index_a_b = new Dictionary<string, Dictionary<string, List<int>>>();
  31. foreach (var c in datas.Values)
  32. {
  33. if (!index_a_b.ContainsKey(c.a))
  34. index_a_b.Add(c.a, new Dictionary<string, List<int>>());
  35. if (!index_a_b[c.a].ContainsKey(c.b))
  36. index_a_b[c.a].Add(c.b, new List<int>());
  37. index_a_b[c.a][c.b].Add(c.id);
  38. }
  39. // 有了 a|b 这个索引,可以快速查询数据
  40. // a的值是“A”的数据,他们的b有哪些值
  41. foreach (var item in index_a_b["A"])
  42. Console.WriteLine("a=A,b=" + item.Key + ", 有这些条配置(id列表)" + item.Value);
  43. // 直接查找a=A,b=小猫的数据条目
  44. Console.WriteLine(index_a_b["A"]["小猫"]);// 这里是个List<int> 满足条件的id列表
  45. }

lua代码示例

go代码示例

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注