/** * 检测传入的元素类型 * * @param listTpye * @param o */ private void CheckObjectType(List <string> listTpye, Object o) { if (o == null) { throw new Exception("object is null"); } if (o.GetType().IsArray) { Type elementType = o.GetType().GetElementType(); listTpye.Add("list"); CheckObjectType(listTpye, BasicClassTypeUtil.CreateObject(elementType)); } else if (o is IList) { listTpye.Add("list"); IList list = (IList)o; if (list.Count > 0) { CheckObjectType(listTpye, list[0]); } else { listTpye.Add("?"); } } else if (o is IDictionary) { listTpye.Add("map"); IDictionary map = (IDictionary)o; if (map.Count > 0) { foreach (object key in map.Keys) { listTpye.Add(BasicClassTypeUtil.CS2UniType(key.GetType().ToString())); CheckObjectType(listTpye, map[key]); break; } } else { listTpye.Add("?"); listTpye.Add("?"); //throw new ArgumentException("map can not is empty"); } } else { listTpye.Add(BasicClassTypeUtil.CS2UniType(o.GetType().ToString())); } }
/** * 放入一个元素 * @param <T> * @param name * @param t */ public void Put <T>(string name, T t) { if (name == null) { throw new ArgumentException("put key can not is null"); } if (t == null) { throw new ArgumentException("put value can not is null"); } TarsOutputStream _out = new TarsOutputStream(); _out.setServerEncoding(_encodeName); _out.Write(t, 0); byte[] sBuffer = TarsUtil.getTarsBufArray(_out.getMemoryStream()); if (_iVer == Const.PACKET_TYPE_TUP3) { cachedData.Remove(name); if (_new_data.ContainsKey(name)) { _new_data[name] = sBuffer; } else { _new_data.Add(name, sBuffer); } } else { List <string> listType = new List <string>(); CheckObjectType(listType, t); string className = BasicClassTypeUtil.TransTypeList(listType); Dictionary <string, byte[]> pair = new Dictionary <string, byte[]>(1); pair.Add(className, sBuffer); cachedData.Remove(name); if (_data.ContainsKey(name)) { _data[name] = pair; } else { _data.Add(name, pair); } } }
/** * Put in an element. * @param <T> * @param name * @param value */ public void Put <T>(string name, T value) { if (name == null) { throw new ArgumentException("put key can not is null"); } if (value == null) { throw new ArgumentException("put value can not is null"); } TarsOutputStream _out = new TarsOutputStream(); _out.SetServerEncoding(_encodeName); _out.Write(value, 0); byte[] sBuffer = TarsUtil.GetTarsBufferArray(_out.GetMemoryStream()); if (_iVer == Const.TUP_VERSION_3) { cachedData.Remove(name); if (_new_data.ContainsKey(name)) { _new_data[name] = sBuffer; } else { _new_data.Add(name, sBuffer); } } else { List <string> listType = new List <string>(); CheckObjectType(listType, value); string className = BasicClassTypeUtil.TransTypeList(listType); Dictionary <string, byte[]> map = new Dictionary <string, byte[]>(1); map.Add(className, sBuffer); cachedData.Remove(name); if (_data.ContainsKey(name)) { _data[name] = map; } else { _data.Add(name, map); } } }
private Object GetCacheProxy <T>(string className) { return(BasicClassTypeUtil.CreateObject <T>()); }
/** * 获取一个元素,只能用于tup版本2,如果待获取的数据为tup3,则抛异常 * @param <T> * @param name * @return * @throws ObjectCreateException */ public T Get <T>(string name) { if (_iVer == Const.PACKET_TYPE_TUP3) { throw new Exception("data is encoded by new version, please use getTarsStruct(String name,T proxy)"); } object obj = null; if (!_data.ContainsKey(name)) { return((T)obj); } else if (cachedData.ContainsKey(name)) { if (!cachedData.TryGetValue(name, out obj)) { obj = null; } return((T)obj); } else { Dictionary <string, byte[]> pair; _data.TryGetValue(name, out pair); string strBasicType = ""; string className = null; byte[] data = new byte[0]; // 找到和T类型对应的数据data foreach (KeyValuePair <string, byte[]> e in pair) { className = e.Key; data = e.Value; if (className == null || className == string.Empty) { continue; } // 比较基本类型 strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString()); if (className.Length > 0 && className == strBasicType) { break; } if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map") { break; } if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } } try { object objtmp = GetCacheProxy <T>(className); if (objtmp == null) { return((T)objtmp); } obj = decodeData(data, objtmp); if (obj != null) { SaveDataCache(name, obj); } return((T)obj); } catch (Exception ex) { QTrace.Trace(this + " Get Exception: " + ex.Message); throw new ObjectCreateException(ex); } } }
/** * Get an element, only for tup version 2, if the data to be acquired is version tup3, * throw an exception. * @param <T> * @param name * @return * @throws ObjectCreateException */ public T Get <T>(string name) { if (_iVer == Const.TUP_VERSION_3) { throw new Exception("data is encoded by new version, please use Get(string Name, T DefaultObj)"); } object obj = null; if (!_data.ContainsKey(name)) { return((T)obj); } else if (cachedData.ContainsKey(name)) { if (!cachedData.TryGetValue(name, out obj)) { obj = null; } return((T)obj); } else { Dictionary <string, byte[]> map; _data.TryGetValue(name, out map); string strBasicType = ""; string className = null; byte[] data = new byte[0]; // Find the data corresponding to the T type. foreach (KeyValuePair <string, byte[]> pair in map) { className = pair.Key; data = pair.Value; if (className == null || className == string.Empty) { continue; } // Comparative basic type. strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString()); if (className.Length > 0 && className == strBasicType) { break; } if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map") { break; } if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } } try { object tmpObj = GetCacheProxy <T>(className); if (tmpObj == null) { return((T)tmpObj); } obj = DecodeData(data, tmpObj); if (obj != null) { SaveDataCache(name, obj); } return((T)obj); } catch (Exception ex) { QTrace.Trace(this + " Get Exception: " + ex.Message); throw new ObjectCreateException(ex); } } }