public void RemoveChild(GUTag c) { if (Children.Contains(c.Id)) { Children.Remove(c.Id); } }
public List <GUTag> QueryTagParent(GUTag tag) { //AssertValid(tag); 由于有两个视图,可能会用一个已经失效的GUTag进行查询。 tag = QueryTag(tag.Id); if (null == tag) { return(new List <GUTag>()); } if (tag.Id == StaticCfg.Ins.DefaultTagID) { return(new List <GUTag>()); } List <GUTag> ret = new List <GUTag>(); //如果有ParentID,直接返回 if (tag.PId != null) { GUTag ptag = QueryTag(tag.PId); ret.Add(ptag); } return(ret); }
////////////////////////////////////////////////////////// #region 修改Tag:父子关系 public int SetParentWithPreBrother(GUTag parent, GUTag child, GUTag preBrother) { lock (this) { //添加的tag必须是有效节点 AssertValid(parent); AssertValid(child); AssertValid(preBrother); GUTag pTag = QueryTag(parent.Id); GUTag cTag = QueryTag(child.Id); GUTag bTag = QueryTag(preBrother.Id); //保护性检查,防止调用无效 if (pTag != null && cTag != null && bTag != null) { pTag.AddChildWithPrebrother(cTag, bTag); System.Diagnostics.Debug.Assert(cTag.PId == pTag.Id); AddUptSqlDB(pTag); AddUptSqlDB(cTag); ChangeNotify(); //Save(child); parent保存实际上已经保存所有了,这儿就不需要保存了。 } return(ITagDBConst.R_OK); } }
private List <GUTag> QueryWildSql(string title) { lock (this) { if (qWildCmd == null) { qWildCmd = new SQLiteCommand(@"SELECT * FROM Tags where (Title like @Title or Alias like @Title)", Conn); qWildCmd.Parameters.AddRange(new[] { new SQLiteParameter("@Title", DbType.String), }); } List <GUTag> ret = new List <GUTag>(); qWildCmd.Parameters[0].Value = '%' + title + '%'; using (SQLiteDataReader r = qWildCmd.ExecuteReader()) { while (r.Read()) { GUTag tag = ReadGUTagFromR(r); ret.Add(tag); } } System.Diagnostics.Debug.WriteLine("query:" + title + "==>" + ret.Count); return(ret); } }
public bool IsSame(GUTag other) { if (other.Id != Id) { return(false); } if (other.PId != PId) { return(false); } if (other.Alias.Count != Alias.Count) { return(false); } if (other.Children.Count != Children.Count) { return(false); } for (int i = 0; i < Alias.Count; i++) { if (other.Alias[i] != Alias[i]) { return(false); } } for (int i = 0; i < Children.Count; i++) { if (other.Children[i] != Children[i]) { return(false); } } return(true); }
private GUTag ReadGUTagFromR(SQLiteDataReader r) { GUTag tag = new GUTag(); //0. ID tag.Id = r.GetGuid((int)FIELD_IDX.ID); //1. Title tag.AddAlias(r.GetString((int)FIELD_IDX.Title)); //2. Alias string alias = r.GetString((int)FIELD_IDX.Alias); string[] aliasList = alias.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string a in aliasList) { tag.AddAlias(a); } //3. PID tag.PId = r.GetGuid((int)FIELD_IDX.PID); //4. Children string chilrend = r.GetString((int)FIELD_IDX.Children); string[] childList = chilrend.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string c in childList) { tag.AddChild(Guid.Parse(c)); } tag.Path = r.GetString((int)FIELD_IDX.Path); return(tag); }
private GUTag QuerySqlDB(Guid id) { //lock (this) //这儿导致死锁 //定时器线程:Import,进入lock,在Import时,会Notify,从而更新UI,Dispatch(UI)线程,等待UI返回。 //UI线程:更新UI时又会Query,再次进入lock,被阻塞。 { if (queryCmd == null) { queryCmd = new SQLiteCommand(@"SELECT * FROM Tags where (ID=@ID)", Conn); queryCmd.Parameters.AddRange(new[] { new SQLiteParameter("@ID", DbType.Guid), }); } queryCmd.Parameters[0].Value = id; using (SQLiteDataReader r = queryCmd.ExecuteReader()) { if (r.Read()) { GUTag tag = ReadGUTagFromR(r); return(tag); } else { return(null); } } } }
public int Export(string exportFile) { using (StreamWriter w = new StreamWriter(exportFile)) { List <GUTag> all = new List <GUTag>(); SQLiteCommand q = new SQLiteCommand(@"SELECT * FROM Tags", Conn); using (SQLiteDataReader r = q.ExecuteReader()) { while (r.Read()) { GUTag tag = ReadGUTagFromR(r); if (tag != null) { all.Add(tag); } } } all.Sort((x, y) => x.Id.CompareTo(y.Id)); foreach (GUTag j in all) { w.WriteLine(JsonConvert.SerializeObject(j)); } } return(0); }
private GUTag NewTag(GUTag t) { GUTag tag = new GUTag(t.Title, t.Id); SaveAndUpdateCache(tag); //ChangeNotify();//这个地方可以不用notify,在设置父子关系的时候再notify return(tag); }
public int QueryChildrenCount(GUTag tag) { lock (this) { //AssertValid(tag); GUTag tmp = QueryTag(tag.Id); return(tmp == null ? 0 : tmp.Children.Count); } }
private void RemoveFromHash(GUTag j) { AssertValid(j); id2TagCache?.Remove(j.Id); DelSqlDB(j); //AllTagSet.Remove(j); }
private void SaveAndUpdateCache(GUTag j) { //Debug.Assert(id2Gutag[j.Id] == null); if (id2TagCache != null) { id2TagCache[j.Id] = j; } AddUptSqlDB(j); }
public GUTag NewTag(string title) { lock (this) { GUTag tag = new GUTag(title); SaveAndUpdateCache(tag); //ChangeNotify();//这个地方可以不用notify,在设置父子关系的时候再notify return(tag); } }
//修改Child节点的位置(direct=-1:下移一个,1:上移一个) public void ChangePos(GUTag child, int direct) { int idx = Children.IndexOf(child.Id); int newIdx = idx + direct; if (newIdx >= 0 && newIdx < Children.Count) { Children.RemoveAt(idx); Children.Insert(newIdx, child.Id); } }
public void Merge(GUTag other) { foreach (string a in other.Alias) { AddAlias(a); } foreach (Guid c in other.Children) { AddChild(c); } //foreach (string p in tag.Parents) AddParent(p); }
//解除child的父节点 与本child的联系 private void RemoveChild(GUTag child) { AssertValid(child); GUTag pTag = QueryTag(child.PId); if (pTag != null) { pTag.RemoveChild(child); AddUptSqlDB(pTag); } child.PId = Guid.Empty; }
public bool Get(GUTag tag) { object o = switchs[tag]; if (o == null) { return(true); } else { return((bool)o); } }
public List <string> QueryTagAlias(GUTag tag) { //AssertValid(tag); tag = QueryTag(tag.Id); if (tag == null) { return(new List <string>()); } else { return(tag.Alias); } }
public void InitDir(GUTag parent) { if (string.IsNullOrEmpty(path)) { if (Id == StaticCfg.Ins.DefaultTagID) { path = CfgPath.DefaultTagDir; } else { string pdir = parent.GetDir(false); path = System.IO.Path.Combine(pdir, Title); } } }
public int MergeAlias(GUTag mainTag, GUTag aliasTag) { lock (this) { AssertValid(mainTag); AssertValid(aliasTag); mainTag = QueryTag(mainTag.Id); aliasTag = QueryTag(aliasTag.Id); RemoveFromHash(aliasTag); mainTag.Merge(aliasTag); SaveAndUpdateCache(mainTag); //allTag.Add(tag2, tmp1);//别名也需要快速索引 ChangeNotify(); return(ITagDBConst.R_OK); } }
public GUTag ChangeTitle(GUTag tag, string newTitle) { lock (this) { tag = QueryTag(tag.Id); if (tag == null) { return(null); } AssertValid(tag); tag.ChangeTitle(newTitle); AddUptSqlDB(tag); ChangeNotify(); return(tag); } }
public int SetPath(GUTag tag, string path) { lock (this) { tag = QueryTag(tag.Id); if (tag == null) { return(0); } AssertValid(tag); tag.ChangePath(path); AddUptSqlDB(tag); ChangeNotify(); return(0); } }
private void DelSqlDB(GUTag tag) { if (delCmd == null) { delCmd = new SQLiteCommand(@"DELETE FROM Tags where (ID = @ID)", Conn); delCmd.Parameters.AddRange(new[] { new SQLiteParameter("@ID", DbType.Guid), }); } //根节点不允许删除 if (tag.Id != StaticCfg.Ins.DefaultTagID) { delCmd.Parameters[0].Value = tag.Id; delCmd.ExecuteNonQuery(); } }
public void Swtich(GUTag tag) { if (switchs[tag] == null) { switchs[tag] = false; } else { bool s = (bool)switchs[tag]; switchs.Remove(tag); switchs[tag] = !s; } if (null != SwitchChanged) { SwitchChanged(); } }
public int RemoveTag(GUTag tag) { lock (this) { tag = QueryTag(tag.Id); if (tag == null || tag.Id == StaticCfg.Ins.DefaultTagID) { return(ITagDBConst.R_OK); } AssertValid(tag); RemoveChild(tag); id2TagCache?.Remove(tag.Id); DelSqlDB(tag); ChangeNotify(); return(ITagDBConst.R_OK); } }
//解除原来child所有parent,并与新的parent建立关系 public int ResetParent(GUTag parent, GUTag child) { lock (this) { parent = QueryTag(parent.Id); child = QueryTag(child.Id); if (parent == null || child == null) { return(ITagDBConst.R_OK); } AssertValid(parent); AssertValid(child); RemoveChild(child); SetParent(parent, child); //AddUptSqlDB(parent); ChangeNotify(); return(ITagDBConst.R_OK); } }
public void AddChildWithPrebrother(GUTag child, GUTag brother) { if (Children.Contains(child.Id)) { return; } int idx = Children.FindIndex(id => id == brother.Id); if (idx >= 0) { Children.Insert(idx + 1, child.Id); } else { Children.Add(child.Id); } child.PId = Id; }
private GUTag QueryTag(Guid id) { if (id2TagCache != null) { GUTag tmp = id2TagCache[id] as GUTag; if (tmp == null) { tmp = QuerySqlDB(id); if (tmp != null) { id2TagCache[id] = tmp; } } return(tmp); } else { return(QuerySqlDB(id)); } }
private string ParentHistory(GUTag a) { a = QueryTag(a.Id); string ret = a.Title; while (a != null) { var parents = QueryTagParent(a); if (parents.Count == 0) { break; } else { a = parents[0]; ret = ret + ">" + a.Title; } } return(ret); }
public List <GUTag> QueryTagChildren(GUTag tag) { //AssertValid(tag); tag = QueryTag(tag.Id); if (tag == null) { return(new List <GUTag>()); } List <GUTag> gutagChildren = new List <GUTag>(); foreach (Guid id in tag.Children) { GUTag c = QueryTag(id); if (c != null) { gutagChildren.Add(c); } } return(gutagChildren); }