Skip to content
This repository
Browse code

重写FillConnectionToJSON。

内容采集还是使用老的方式,采集为TreeView,然后通过解析TreeView的内容,递归生成JSON。
保证无需双重管理代码。效果非常好,利于扩展。代码也相当小巧了。。。赞~\(≧▽≦)/~
  • Loading branch information...
commit dfaff6224d709ef5f923716abb8d2070cca6d189 1 parent 3214083
MagicDict authored
1  .gitignore
@@ -20,3 +20,4 @@
20 20 !app.config
21 21 !Release Note.txt
22 22 !Bug.txt
  23 +Thumbs.db
363 MagicMongoDBTool/Module/MongoDBHelper/MongoDBHelper_Connection_Present.cs
@@ -32,362 +32,45 @@ public static String GetCurrentSvrInfo()
32 32
33 33 #region"展示数据库结构 WebForm"
34 34 /// <summary>
35   - /// FillConnectionToJSON
  35 + ///
36 36 /// </summary>
37   - /// <param name="mongoConnKey"></param>
  37 + /// <param name="ConnectionName"></param>
38 38 /// <returns></returns>
39   - public static String FillConnectionToJSON(String mongoConnKey)
  39 + public static String FillConnectionToJSON(String ConnectionName)
40 40 {
41   - MongoServer mongoConn = _mongoConnSvrLst[mongoConnKey];
42   - BsonDocument ConnectionNode = new BsonDocument();
43   - try
44   - {
45   - //ReplSetName只能使用在虚拟的Replset服务器,Sharding体系等无效。虽然一个Sharding可以看做一个ReplSet
46   - ConfigHelper.MongoConnectionConfig config = SystemManager.ConfigHelperInstance.ConnectionList[mongoConnKey];
47   - //ReplSet服务器需要Connect才能连接。可能因为这个是虚拟的服务器,没有Mongod实体。
48   - //不过现在改为全部显示的打开连接
49   - mongoConn.Connect();
50   - ///mongoSvr.ReplicaSetName只有在连接后才有效,但是也可以使用Config.ReplsetName
51   - ConnectionNode.Add("children", new BsonArray() { GetInstanceDocument(mongoConnKey, config, mongoConn, null, mongoConn) });
52   -
53   - if (mongoConn.ReplicaSetName != null)
54   - {
55   -
56   - TreeNode ServerListNode = new TreeNode("Servers");
57   - ServerListNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
58   - ServerListNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
59   - foreach (MongoServerInstance ServerInstace in mongoConn.Instances)
60   - {
61   - ServerListNode.Nodes.Add(GetInstanceNode(mongoConnKey, config, mongoConn, ServerInstace, null));
62   - }
63   - //ConnectionNode.Add(ServerListNode);
64   - config.ServerRole = ConfigHelper.SvrRoleType.ReplsetSvr;
65   - }
66   - else
67   - {
68   - BsonDocument ServerStatusDoc = ExecuteMongoSvrCommand(serverStatus_Command, mongoConn).Response;
69   - if (ServerStatusDoc.GetElement("process").Value == "mongos")
70   - {
71   - config.ServerRole = ConfigHelper.SvrRoleType.ShardSvr;
72   -
73   - TreeNode ShardListNode = new TreeNode("Shards");
74   - ShardListNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
75   - ShardListNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
76   - foreach (var lst in GetShardInfo(mongoConn, "host"))
77   - {
78   - TreeNode ShardNode = new TreeNode();
79   - ShardNode.Text = lst.Key;
80   - ShardNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
81   - ShardNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Servers;
82   - String strHostList = lst.Value.ToString();
83   - String[] strAddress = strHostList.Split("/".ToCharArray());
84   - String strAddresslst;
85   - if (strAddress.Length == 2)
86   - {
87   - //#1 replset/host:port,host:port
88   - ShardNode.Text += "[Replset:" + strAddress[0] + "]";
89   - strAddresslst = strAddress[1];
90   - }
91   - else
92   - {
93   - //#2 host:port,host:port
94   - strAddresslst = strHostList;
95   - }
96   - foreach (String item in strAddresslst.Split(",".ToCharArray()))
97   - {
98   - MongoServerSettings tinySetting = new MongoServerSettings();
99   - tinySetting.ConnectionMode = ConnectionMode.Direct;
100   - tinySetting.ReplicaSetName = strAddress[0];
101   - MongoServerAddress tinyAddr;
102   - if (item.Split(":".ToCharArray()).Length == 2)
103   - {
104   - tinyAddr = new MongoServerAddress(item.Split(":".ToCharArray())[0], Convert.ToInt32(item.Split(":".ToCharArray())[1]));
105   - }
106   - else
107   - {
108   - tinyAddr = new MongoServerAddress(item.Split(":".ToCharArray())[0]);
109   - }
110   - tinySetting.Server = tinyAddr;
111   - MongoServer tiny = MongoServer.Create(tinySetting);
112   - ShardNode.Nodes.Add(GetInstanceNode(mongoConnKey, config, mongoConn, tiny.Instance, null));
113   - }
114   - ShardListNode.Nodes.Add(ShardNode);
115   - }
116   - //ConnectionNode.Nodes.Add(ShardListNode);
117   - }
118   - else
119   - {
120   - ///Master - Slave 的判断
121   - BsonElement replElement;
122   - ServerStatusDoc.TryGetElement("repl", out replElement);
123   - if (replElement == null)
124   - {
125   - config.ServerRole = ConfigHelper.SvrRoleType.DataSvr;
126   - }
127   - else
128   - {
129   - if (replElement.Value.AsBsonDocument.GetElement("ismaster").Value == BsonBoolean.True)
130   - {
131   - config.ServerRole = ConfigHelper.SvrRoleType.MasterSvr;
132   - }
133   - else
134   - {
135   - //ismaster 的值不一定是True和False...
136   - config.ServerRole = ConfigHelper.SvrRoleType.SlaveSvr;
137   - }
138   - }
139   - }
140   - }
141   - config.Health = true;
142   - SystemManager.ConfigHelperInstance.ConnectionList[mongoConnKey] = config;
143   - switch (config.ServerRole)
144   - {
145   - case ConfigHelper.SvrRoleType.DataSvr:
146   - ConnectionNode.Add("name", "[Data] " + mongoConnKey);
147   - break;
148   - case ConfigHelper.SvrRoleType.ShardSvr:
149   - ConnectionNode.Add("name", "[Cluster] " + mongoConnKey);
150   - break;
151   - case ConfigHelper.SvrRoleType.ReplsetSvr:
152   - ConnectionNode.Add("name", "[Replset] " + mongoConnKey);
153   - break;
154   - case ConfigHelper.SvrRoleType.MasterSvr:
155   - ConnectionNode.Add("name", "[Master] " + mongoConnKey);
156   -
157   - break;
158   - case ConfigHelper.SvrRoleType.SlaveSvr:
159   - ConnectionNode.Add("name", "[Slave] " + mongoConnKey);
160   - break;
161   - default:
162   - break;
163   - }
164   - }
165   - catch (MongoAuthenticationException)
166   - {
167   - //需要验证的数据服务器,没有Admin权限无法获得数据库列表
168   - if (!SystemManager.IsUseDefaultLanguage())
169   - {
170   - ConnectionNode.Add("name", "[" + SystemManager.mStringResource.GetText(StringResource.TextType.Exception_AuthenticationException) + "]");
171   - }
172   - else
173   - {
174   - ConnectionNode.Add("name", "[MongoAuthenticationException]");
175   - }
176   - }
177   - catch (Exception)
178   - {
179   - //暂时不处理任何异常,简单跳过
180   - //无法连接的理由:
181   - //1.服务器没有启动
182   - //2.认证模式不正确
183   - if (!SystemManager.IsUseDefaultLanguage())
184   - {
185   - ConnectionNode.Add("name", "[" + SystemManager.mStringResource.GetText(StringResource.TextType.Exception_NotConnected) + "]");
186   - }
187   - else
188   - {
189   - ConnectionNode.Add("name", "[Exception]");
190   - }
191   - }
192   - return ConnectionNode.ToJson();
  41 + String strJSON = String.Empty;
  42 + TreeView tree = new TreeView();
  43 + FillConnectionToTreeView(tree);
  44 + //Transform Treeview To JSON
  45 + //必须这样做,防止二重管理的问题。如果这里的逻辑有两套的话,维护起来比较麻烦。
  46 + //一套逻辑,来控制树的内容。然后将TreeView的内容转换为JSON。
  47 + //递归GetSubTreeNode
  48 + strJSON = GetSubTreeNode(tree.Nodes[0]).ToJson();
  49 + return strJSON;
193 50 }
194 51 /// <summary>
195 52 ///
196 53 /// </summary>
197   - /// <param name="mongoConnKey"></param>
198   - /// <param name="config"></param>
199   - /// <param name="mongoConn"></param>
200   - /// <param name="mServerInstace"></param>
201   - /// <param name="mServer"></param>
  54 + /// <param name="SubNode"></param>
202 55 /// <returns></returns>
203   - private static BsonDocument GetInstanceDocument(String mongoConnKey, ConfigHelper.MongoConnectionConfig config,
204   - MongoServer mongoConn, MongoServerInstance mServerInstace, MongoServer mServer)
  56 + private static BsonDocument GetSubTreeNode(TreeNode SubNode)
205 57 {
206   - Boolean isServer = false;
207   - if (mServerInstace == null)
208   - {
209   - isServer = true;
210   - }
211   - BsonDocument SvrInstanceNode = new BsonDocument();
212   - String ConnSvrKey;
213   - if (isServer)
214   - {
215   - ConnSvrKey = mongoConnKey + "/" + mongoConnKey;
216   - }
217   - else
218   - {
219   - ConnSvrKey = mongoConnKey + "/" + mServerInstace.Address.ToString().Replace(":", "@");
220   - }
221   - if (isServer)
222   - {
223   - SvrInstanceNode.Add("name", "Connection");
224   - }
225   - else
226   - {
227   - SvrInstanceNode.Add("name", "Server[" + mServerInstace.Address.ToString() + "]");
228   - }
229   - if ((!String.IsNullOrEmpty(config.UserName)) & (!String.IsNullOrEmpty(config.Password)))
230   - {
231   - config.AuthMode = true;
232   - }
233   - //获取ReadOnly
234   - config.IsReadOnly = false;
235   - List<String> databaseNameList = new List<String>();
236   - BsonArray mongoDBArray = new BsonArray();
237   - if (!String.IsNullOrEmpty(config.DataBaseName))
  58 + if (SubNode.Nodes.Count == 0)
238 59 {
239   - //单数据库模式
240   - BsonDocument mongoDBInfoNode = new BsonDocument();
241   - mongoDBInfoNode.Add("name", config.DataBaseName);
242   - if (isServer)
243   - {
244   - mongoDBInfoNode.Add("children", GetDataBaseInfoDocument(config.DataBaseName, mServer, mongoConnKey + "/" + mongoConnKey));
245   - }
246   - else
247   - {
248   - mongoDBInfoNode.Add("children", GetDataBaseInfoDocument(config.DataBaseName, mServerInstace.Server, mongoConnKey + "/" + mServerInstace.Address.ToString()));
249   - }
250   - mongoDBArray.Add(mongoDBInfoNode);
251   - if (config.AuthMode)
252   - {
253   - config.IsReadOnly = mongoConn.GetDatabase(config.DataBaseName).FindUser(config.UserName).IsReadOnly;
254   - }
255   - SvrInstanceNode.Add("children", mongoDBArray);
  60 + return new BsonDocument("name", SubNode.Text);
256 61 }
257 62 else
258 63 {
259   - MongoServer InstantSrv;
260   - if (isServer)
261   - {
262   - InstantSrv = mServer;
263   - databaseNameList = mServer.GetDatabaseNames().ToList<String>();
264   - }
265   - else
266   - {
267   - MongoServerSettings setting = mongoConn.Settings.Clone();
268   - setting.ConnectionMode = ConnectionMode.Direct;
269   - setting.SlaveOk = true;
270   - setting.Server = mServerInstace.Address;
271   - InstantSrv = new MongoServer(setting);
272   - databaseNameList = InstantSrv.GetDatabaseNames().ToList<String>();
273   - }
274   -
275   - foreach (String strDBName in databaseNameList)
276   - {
277   - try
278   - {
279   - BsonDocument mongoDBInfoNode = new BsonDocument();
280   - String strShowDBName = strDBName;
281   - if (!SystemManager.IsUseDefaultLanguage())
282   - {
283   - if (SystemManager.mStringResource.LanguageType == "Chinese")
284   - {
285   - switch (strDBName)
286   - {
287   - case "admin":
288   - strShowDBName = "管理员权限(admin)";
289   - break;
290   - case "local":
291   - strShowDBName = "本地(local)";
292   - break;
293   - case "config":
294   - strShowDBName = "配置(config)";
295   - break;
296   - default:
297   - break;
298   - }
299   - }
300   - }
301   - mongoDBInfoNode.Add("name", strShowDBName);
302   - mongoDBInfoNode.Add("children", GetDataBaseInfoDocument(strDBName, InstantSrv, ConnSvrKey));
303   - if (strDBName == MongoDBHelper.DATABASE_NAME_ADMIN)
304   - {
305   - if (config.AuthMode)
306   - {
307   - config.IsReadOnly = mongoConn.GetDatabase(strDBName).FindUser(config.UserName).IsReadOnly;
308   - }
309   - }
310   - mongoDBArray.Add(mongoDBInfoNode);
311   - }
312   - catch (Exception)
313   - {
314   - mongoDBArray.Add(new BsonDocument().Add(new BsonElement("name", strDBName + "Exception")));
315   - }
316   - }
317   - SvrInstanceNode.Add("children", mongoDBArray);
318   - }
319   - if (_mongoInstanceLst.ContainsKey(ConnSvrKey))
320   - {
321   - _mongoInstanceLst.Remove(ConnSvrKey);
322   - }
323   - if (!isServer)
324   - {
325   - _mongoInstanceLst.Add(ConnSvrKey, mServerInstace);
326   - }
327   - return SvrInstanceNode;
328   - }
329   - /// <summary>
330   - /// 获得一个表示数据库结构的节点
331   - /// </summary>
332   - /// <param name="strDBName"></param>
333   - /// <param name="mongoSvr"></param>
334   - /// <param name="mongoSvrKey"></param>
335   - /// <returns></returns>
336   - private static BsonArray GetDataBaseInfoDocument(String strDBName, MongoServer mongoSvr, String mongoSvrKey)
337   - {
338   - BsonArray mongoDBNode = new BsonArray();
339   - MongoDatabase mongoDB = mongoSvr.GetDatabase(strDBName);
340   -
341   - BsonArray JavaDocument = new BsonArray();
342   - BsonArray SystemCollection = new BsonArray();
343   - BsonArray GeneralCollection = new BsonArray();
344   - BsonArray GFSCollection = new BsonArray();
345   -
346   - List<String> colNameList = mongoDB.GetCollectionNames().ToList<String>();
347   - foreach (String strColName in colNameList)
348   - {
349   - switch (strColName)
  64 + BsonDocument MultiNode = new BsonDocument();
  65 + MultiNode.Add("name", SubNode.Text);
  66 + BsonArray ChildrenList = new BsonArray();
  67 + foreach (TreeNode item in SubNode.Nodes)
350 68 {
351   - case COLLECTION_NAME_USER:
352   - //system.users,fs,system.js这几个系统级别的Collection不需要放入
353   - break;
354   - case COLLECTION_NAME_JAVASCRIPT:
355   - foreach (BsonDocument t in mongoDB.GetCollection(COLLECTION_NAME_JAVASCRIPT).FindAll())
356   - {
357   - BsonDocument js = new BsonDocument();
358   - js.Add("name", t.GetValue(KEY_ID).ToString());
359   - JavaDocument.Add(js);
360   - }
361   - break;
362   - default:
363   - if (IsSystemCollection(mongoDB.Name, strColName))
364   - {
365   - switch (strColName)
366   - {
367   - case COLLECTION_NAME_GFS_CHUNKS:
368   - case COLLECTION_NAME_GFS_FILES:
369   - GFSCollection.Add(new BsonDocument().Add("name", strColName));
370   - break;
371   - default:
372   - SystemCollection.Add(new BsonDocument().Add("name", strColName));
373   - break;
374   - }
375   - }
376   - else
377   - {
378   - GeneralCollection.Add(new BsonDocument().Add("name", strColName));
379   - }
380   - break;
  69 + ChildrenList.Add(GetSubTreeNode(item));
381 70 }
  71 + MultiNode.Add("children", ChildrenList);
  72 + return MultiNode;
382 73 }
383   -
384   - mongoDBNode.Add(new BsonDocument().Add(new BsonElement("name", "User")));
385   - mongoDBNode.Add(new BsonDocument().Add(new BsonElement("name", "JavaScript")).Add("children", JavaDocument));
386   - mongoDBNode.Add(new BsonDocument().Add(new BsonElement("name", "Grid File System")).Add("children", GFSCollection));
387   - mongoDBNode.Add(new BsonDocument().Add(new BsonElement("name", "Collections(System)")).Add("children", SystemCollection));
388   - mongoDBNode.Add(new BsonDocument().Add(new BsonElement("name", "Collections(General)")).Add("children", GeneralCollection));
389   -
390   - return mongoDBNode;
391 74 }
392 75 #endregion
393 76
BIN  MagicMongoDBTool/Resources/Thumbs.db
Binary file not shown
BIN  MagicMongoDBTool/UserGuide/images/Thumbs.db
Binary file not shown

0 comments on commit dfaff62

Please sign in to comment.
Something went wrong with that request. Please try again.