/// <summary> /// 获取类型信息 /// </summary> /// <param name="type"></param> /// <returns></returns> private Xml.Node get(Type type) { if (type != null) { Xml.Node node; if (typeLock.TryGetValue(type, out node)) { return(node); } HashString typeName; AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { using (typeNameStream) { typeNameStream.Reset(ref buffer); AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder nameBuilder = new AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder { NameStream = typeNameStream, IsXml = true }; nameBuilder.Xml(type); typeName = typeNameStream.ToString(); } } finally { UnmanagedPool.Default.PushOnly(ref buffer); } if (types.TryGetValue(typeName, out node)) { types.Remove(typeName); } typeLock.Set(type, node); return(node); } return(default(Xml.Node)); }
/// <summary> /// 获取属性信息 /// </summary> /// <param name="property"></param> /// <returns></returns> private Xml.Node get(PropertyInfo property) { if (property != null) { HashString propertyName; AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { using (propertyNameStream) { propertyNameStream.Reset(ref buffer); AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder nameBuilder = new AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder { NameStream = propertyNameStream, IsXml = true }; nameBuilder.Xml(property.DeclaringType); propertyNameStream.Write('.'); propertyNameStream.SimpleWrite(property.Name); propertyName = propertyNameStream.ToString(); } } finally { UnmanagedPool.Default.PushOnly(ref buffer); } Xml.Node node; properties.TryGetValue(propertyName, out node); return(node); } return(default(Xml.Node)); }
/// <summary> /// 获取字段信息 /// </summary> /// <param name="field"></param> /// <returns></returns> private Xml.Node get(FieldInfo field) { if (field != null) { HashString fieldName; AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { using (fieldNameStream) { fieldNameStream.Reset(ref buffer); AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder nameBuilder = new AutoCSer.Extensions.TypeExtensionCodeGenerator.NameBuilder { NameStream = fieldNameStream, IsXml = true }; nameBuilder.Xml(field.DeclaringType); fieldNameStream.Write('.'); fieldNameStream.SimpleWrite(field.Name); fieldName = fieldNameStream.ToString(); } } finally { UnmanagedPool.Default.PushOnly(ref buffer); } Xml.Node node; fields.TryGetValue(fieldName, out node); return(node); } return(default(Xml.Node)); }
/// <summary> /// 获取类型名称 /// </summary> /// <param name="type">类型</param> /// <returns>类型名称</returns> public unsafe string GetName(Type type) { if (type.IsArray) { AutoCSer.Memory.Pointer buffer = UnmanagedPool.Tiny.GetPointer(); try { using (NameStream = new CharStream(ref buffer)) { new AutoCSer.Reflection.TypeNameBuilder { NameStream = NameStream }.Array(type, false); return(NameStream.ToString()); } } finally { UnmanagedPool.Tiny.PushOnly(ref buffer); } } if (type.IsGenericType) { AutoCSer.Memory.Pointer buffer = UnmanagedPool.Tiny.GetPointer(); try { using (NameStream = new CharStream(ref buffer)) { new AutoCSer.Reflection.TypeNameBuilder { NameStream = NameStream }.GenericName(type); return(NameStream.ToString()); } } finally { UnmanagedPool.Tiny.PushOnly(ref buffer); } } return(type.Name); }
/// <summary> /// ASCII字节搜索器 /// </summary> /// <param name="data">数据起始位置</param> internal ByteSearcher(ref AutoCSer.Memory.Pointer data) { if (data.Data == null) { State = bytes = null; tableType = 0; } else { int stateCount = *data.Int; State = data.Byte + sizeof(int); bytes = State + stateCount * 3 * sizeof(int); if (stateCount < 256) { tableType = 0; } else if (stateCount < 65536) { tableType = 1; } else { tableType = 2; } } }
internal bool ResponsePage(ref AutoCSer.Memory.Pointer buffer, ref AutoCSer.Memory.Pointer encodeBuffer) { if (ResponseStream == null) { ResponseStream = new UnmanagedStream(default(AutoCSer.Memory.Pointer)); } buffer = UnmanagedPool.Default.GetPointer(); ResponseStream.Reset(ref buffer); using (ResponseStream) { if (EncodeStream == null) { EncodeStream = new UnmanagedStream(default(AutoCSer.Memory.Pointer)); } encodeBuffer = UnmanagedPool.Default.GetPointer(); EncodeStream.Reset(ref encodeBuffer); using (EncodeStream) { Response bodyResponse = new Response { Stream = ResponseStream, EncodeStream = EncodeStream, Encoding = DomainServer.ResponseEncoding }; if (page(ref bodyResponse)) { HttpResponse.SetBody(ResponseStream); return(true); } } } return(false); }
/// <summary> /// 重建数据 /// </summary> protected void create() { int heapIndex = Array.Length, newCount = heapIndex << 1, newHeapSize = newCount * sizeof(int); KeyValue <keyType, valueType>[] newArray = new KeyValue <keyType, valueType> [newCount]; AutoCSer.Memory.Pointer newHeap = isStaticUnmanaged ? Unmanaged.GetStaticPointer(newHeapSize, false) : Unmanaged.GetPointer(newHeapSize, false), oldHeap = Heap; int *newHeapFixed = newHeap.Int; Array.CopyTo(newArray, 0); AutoCSer.Memory.Common.CopyNotNull(Heap.Byte, newHeapFixed, newHeapSize >> 1); do { --newCount; newHeapFixed[newCount] = newCount; }while (newCount != heapIndex); Array = newArray; Heap = newHeap; if (isStaticUnmanaged) { Unmanaged.FreeStatic(ref oldHeap); } else { Unmanaged.Free(ref oldHeap); } }
/// <summary> /// 获取解压数据 /// </summary> /// <returns>解压数据</returns> internal byte[] Get() { AutoCSer.SubBuffer.PoolBufferFull buffer = default(AutoCSer.SubBuffer.PoolBufferFull); AutoCSer.Memory.Pointer data = UnmanagedPool.Default.GetPointer(); try { AutoCSer.SubBuffer.Pool.GetBuffer(ref buffer, bufferSize); using (dataStream = new UnmanagedStream(ref data)) { fixed(byte *bufferFixed = buffer.GetFixedBuffer()) { byte *start = bufferFixed + buffer.StartIndex; do { int length = CompressStream.Read(buffer.Buffer, buffer.StartIndex, bufferSize); if (length == 0) { break; } dataStream.WriteNotEmpty(start, length); }while (true); } return(dataStream.Data.GetArray()); } } finally { UnmanagedPool.Default.PushOnly(ref data); buffer.Free(); } }
/// <summary> /// 创建客户端命令数据 /// </summary> /// <param name="commandData"></param> /// <param name="commands"></param> /// <returns></returns> internal bool CreateCommandData(ref AutoCSer.Memory.Pointer commandData, ref MemoryMap commands) { if (this.commandData.Data != null) { commandData = Unmanaged.GetPointer8(this.commandData.ByteSize, true); commands = new MemoryMap(commandData.Data); commands.Set(TcpServer.Server.CheckCommandIndex); //if (isCustomData) commands.Set(TcpServer.Server.CustomDataCommandIndex); if (isKeepCallback) { commands.Set(TcpServer.Server.CancelKeepCommandIndex); } if (isMergeCommand) { commands.Set(TcpServer.Server.MergeCommandIndex); } //if (Attribute.IsRemoteExpression) //{ // commands.Set(TcpServer.Server.RemoteExpressionNodeIdCommandIndex); // commands.Set(TcpServer.Server.RemoteExpressionCommandIndex); //} return(true); } return(false); }
/// <summary> /// 清除数据 /// </summary> internal void Clear() { if (Array.Length == defaultArrayLength) { int *heapFixed = Heap.Int; if (*heapFixed != 0) { reset(heapFixed); System.Array.Clear(Array, 0, Array.Length); } } else { KeyValue <keyType, valueType>[] newArray = new KeyValue <keyType, valueType> [defaultArrayLength]; AutoCSer.Memory.Pointer newHeap = isStaticUnmanaged ? Unmanaged.GetStaticPointer(defaultArrayLength * sizeof(int), false) : Unmanaged.GetPointer(defaultArrayLength * sizeof(int), false), oldHeap = Heap; reset(newHeap.Int); Array = newArray; Heap = newHeap; if (isStaticUnmanaged) { Unmanaged.FreeStatic(ref oldHeap); } else { Unmanaged.Free(ref oldHeap); } } }
/// <summary> /// 解析多值集合 /// </summary> private void getValues() { HashSet <SubString> hashSet = HashSetCreator.CreateSubString(); foreach (SubString value in this.value.split(',')) { if (value.Length != 0) { hashSet.Add(value); } } this.value = null; if (hashSet.Count > 1) { valueData = AutoCSer.StateSearcher.CharBuilder.Create(hashSet.getArray(value => (string)value), false); values = new StateSearcher.CharSearcher(ref valueData); } else { foreach (string value in hashSet) { this.value = value; } } }
/// <summary> /// 获取数据库连接信息 /// </summary> /// <param name="isPool">是否启用数据库连接池</param> /// <returns>数据库连接信息</returns> public unsafe Sql.Connection Get(bool isPool = false) { ProviderAttribute provider = EnumAttribute <Provider, ProviderAttribute> .Array((byte)Provider); AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { using (CharStream connectionStream = new CharStream(ref buffer)) { connectionStream.SimpleWrite("Provider="); connectionStream.Write(provider.Name); connectionStream.SimpleWrite(";Data Source="); connectionStream.Write(DataSource); if (Password != null) { connectionStream.Write(";Database Password="******";Extended Properties='"); connectionStream.Write(provider.Excel); connectionStream.Write(IsTitleColumn ? ";HDR=YES;IMEX=" : ";HDR=NO;IMEX="); AutoCSer.Extensions.NumberExtension.ToString((byte)Intermixed, connectionStream); connectionStream.Write('\''); return(new Sql.Connection { Type = ClientKind.Excel, ConnectionString = connectionStream.ToString(), IsPool = isPool }); } } finally { UnmanagedPool.Default.Push(ref buffer); } }
/// <summary> /// 数组模拟最小堆 /// </summary> /// <param name="isStaticUnmanaged">是否固定内存申请</param> internal ArrayHeap(bool isStaticUnmanaged = false) { Array = new KeyValue <keyType, valueType> [defaultArrayLength]; this.isStaticUnmanaged = isStaticUnmanaged; Heap = isStaticUnmanaged ? Unmanaged.GetStaticPointer(defaultArrayLength * sizeof(int), false) : Unmanaged.GetPointer(defaultArrayLength * sizeof(int), false); reset(Heap.Int); }
/// <summary> /// 连接字符串集合 /// </summary> /// <param name="array"></param> /// <param name="join"></param> /// <param name="nullString">null 值替换字符串</param> /// <returns></returns> public unsafe static string JoinString(ref SubArray <valueType> array, char join, string nullString = null) { if (nullString == null) { nullString = string.Empty; } if (array.Length == 0) { return(nullString); } if (NumberJoinChar == null) { return(subArrayJoinChar(array, join, nullString)); } AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { using (CharStream stream = new CharStream(ref buffer)) { NumberJoinChar(stream, array.Array, array.Start, array.Length, join, nullString); return(new string(stream.Char, 0, stream.Length)); } } finally { UnmanagedPool.Default.PushOnly(ref buffer); } }
/// <summary> /// 域名搜索 /// </summary> /// <param name="domains">域名信息集合</param> /// <param name="servers">域名服务信息集合</param> private DomainSearchData(byte[][] domains, DomainServer[] servers) { this.domains = domains; this.servers = servers; data = AutoCSer.StateSearcher.ByteBuilder.Create(domains, false); searcher = new DomainSearcher(ref data); }
internal void ResponseLocationPathAjax(ref AutoCSer.Memory.Pointer buffer) { buffer = UnmanagedPool.Default.GetPointer(); AjaxStream.Reset(ref buffer); using (AjaxStream) { SetJsContentType(); bool isCallBack = ResponseAjaxCallBack(AjaxStream); if (LocationPath.ErrorPath == null) { AjaxStream.Write(@"{""LocationPath"":"); AjaxStream.WriteJson(LocationPath.LocationPath ?? notFound404); } else { AjaxStream.Write(@"{""ErrorPath"":"); AjaxStream.WriteJson(LocationPath.ErrorPath); if (LocationPath.ReturnPath != null) { AjaxStream.Write(@",""ReturnPath"":"); AjaxStream.WriteJson(LocationPath.ReturnPath); } } AjaxStream.Write('}'); if (isCallBack) { AjaxStream.Write(')'); } PageResponse.SetBody(AjaxStream, ref DomainServer.ResponseEncoding); } }
public bool CreateResponse(ref AutoCSer.Memory.Pointer buffer, ref CallResponse response) { if (CreateResponse(ref buffer)) { response = CallResponse; return(true); } return(false); }
/// <summary> /// 增加数据 /// </summary> /// <param name="value">新增的数据</param> private void onInserted(valueType value) { int *intCounts = counts.Int; int identity = GetKey(value); if (identity >= size) { int newLength = int.MaxValue - 1, oldLength = size; if ((identity & 0x40000000) == 0 && oldLength != 0x40000000) { for (newLength = oldLength << 1; newLength <= identity; newLength <<= 1) { ; } } Array.ToSize(newLength); AutoCSer.Memory.Pointer newCounts = Unmanaged.GetPointer8(newLength * sizeof(int), true); try { AutoCSer.Memory.Common.CopyNotNull(intCounts, newCounts.Int, size * sizeof(int)); Unmanaged.Free(ref counts); counts = newCounts; size = newLength; newCounts.SetNull(); int index = oldLength, count = (intCounts = counts.Int)[--index]; for (int step = 1; (index -= step) != 0; step <<= 1) { count += intCounts[index]; } intCounts[oldLength] = count; } catch (Exception error) { SqlTable.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer); } finally { Unmanaged.Free(ref newCounts); } } valueType newValue = AutoCSer.Metadata.DefaultConstructor <valueType> .Constructor(); AutoCSer.MemberCopy.Copyer <modelType> .Copy(newValue, value, MemberMap); setMemberCacheAndValue(newValue); Array[identity] = newValue; for (uint index = (uint)identity, countStep = 1, length = (uint)size; index <= length; countStep <<= 1) { ++intCounts[index]; while ((index & countStep) == 0) { countStep <<= 1; } index += countStep; } ++Count; callOnInserted(newValue); }
/// <summary> /// 释放资源 /// </summary> public void Dispose() { if (CharTypeData.Data != DefaultCharTypeData.Data) { Unmanaged.Free(ref CharTypeData); CharTypeData = new AutoCSer.Memory.Pointer { Data = DefaultCharTypeData.Data }; } }
/// <summary> /// AJAX 响应输出 /// </summary> /// <typeparam name="valueType">输出数据类型</typeparam> /// <param name="value">输出数据</param> internal unsafe void Response <valueType>(ref valueType value) where valueType : struct { AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { Response(ref value, ref buffer); SocketResponse(ref PageResponse); } finally { UnmanagedPool.Default.PushOnly(ref buffer); } }
/// <summary> /// 释放资源 /// </summary> public override void Dispose() { base.Dispose(); if (CharTypeData.Data != StringTrieGraph.DefaultCharTypeData.Data) { Unmanaged.Free(ref CharTypeData); CharTypeData = new AutoCSer.Memory.Pointer { Data = StringTrieGraph.DefaultCharTypeData.Data }; } }
protected void checkMatchMap() { int matchMapSize = (formatLength + 7) >> 3, matchMapDataSize = Math.Max((int)((uint)matchMapSize).UpToPower2(), 8); if (matchMapData.ByteSize < matchMapDataSize) { Unmanaged.Free(ref matchMapData); matchMapData = Unmanaged.GetPointer(matchMapDataSize, false); } matchMap.Set(matchMapData.ULong, (matchMapSize + 7) >> 3); }
/// <summary> /// 输出缓冲区池测试 /// </summary> /// <param name="left"></param> /// <param name="right"></param> public void AddBuffer(int left, int right) { AutoCSer.Memory.Pointer buffer = UnmanagedPool.Tiny.GetPointer(); try { if (CreateResponse(ref buffer)) { Response(left + right); RepsonseEnd(); } } finally { UnmanagedPool.Tiny.Push(ref buffer); } }
/// <summary> /// 获取文本的匹配索引位置 /// </summary> /// <param name="key">数据关键字</param> /// <param name="text">数据文本</param> /// <param name="maxLength">最大长度</param> /// <returns>匹配索引位置集合</returns> public unsafe KeyValue <int, int>[] FormatTextIndexs(ref keyType key, ref SubString text, int maxLength) { int size = ((text.Length + 63) >> 3) & (int.MaxValue - 7); if (getResultIndexsMapBuffer.ByteSize < size) { Unmanaged.Free(ref getResultIndexsMapBuffer); getResultIndexsMapBuffer = Unmanaged.GetPointer(size, true); } int count = searcher.FormatTextIndexs(ref key, ref text, ref queryResult, maxLength, ref resultIndexs, ref getResultIndexsMapBuffer); return(new LeftArray <KeyValue <int, int> >(count, resultIndexs).GetArray()); }
internal void Response(ref AutoCSer.Memory.Pointer buffer) { AjaxStream.Reset(ref buffer); using (AjaxStream) { SetJsContentType(); if (ResponseAjaxCallBack(AjaxStream)) { AjaxStream.Write(')'); } PageResponse.SetBody(AjaxStream, ref DomainServer.ResponseEncoding); } }
/// <summary> /// 获取查询信息 /// </summary> /// <typeparam name="valueType">对象类型</typeparam> /// <typeparam name="modelType">模型类型</typeparam> /// <param name="sqlTool">SQL操作工具</param> /// <param name="createQuery"></param> /// <param name="query">查询信息</param> /// <returns>对象集合</returns> internal override void GetSelectQuery <valueType, modelType> (Sql.Table <valueType, modelType> sqlTool, ref CreateSelectQuery <modelType> createQuery, ref SelectQuery <modelType> query) { CharStream sqlStream = Interlocked.Exchange(ref this.sqlStream, null); if (sqlStream == null) { sqlStream = new CharStream(default(AutoCSer.Memory.Pointer)); } AutoCSer.Memory.Pointer buffer = default(AutoCSer.Memory.Pointer); try { buffer = UnmanagedPool.Default.GetPointer(); sqlStream.Reset(ref buffer); if ((createQuery.GetCount | query.SkipCount) != 0) { sqlStream.Write("select * from("); } sqlStream.Write("select "); if (query.MemberMap != null) { DataModel.Model <modelType> .GetNames(sqlStream, query.MemberMap, constantConverter); } else { sqlStream.Write('*'); } sqlStream.Write(" from "); sqlStream.Write(sqlTool.TableName); sqlStream.Write(' '); createQuery.WriteWhere(sqlTool, sqlStream, ref query); createQuery.WriteOrder(sqlTool, sqlStream, constantConverter, ref query); if ((createQuery.GetCount | query.SkipCount) != 0) { sqlStream.Write(")where rownum between "); AutoCSer.Extensions.NumberExtension.ToString(query.SkipCount, sqlStream); sqlStream.Write(" and "); AutoCSer.Extensions.NumberExtension.ToString(createQuery.GetCount, sqlStream); sqlStream.Write(" order by rownum asc"); } query.Sql = sqlStream.ToString(); } finally { UnmanagedPool.Default.Push(ref buffer); sqlStream.Dispose(); Interlocked.Exchange(ref this.sqlStream, sqlStream); } }
/// <summary> /// 重新加载数据 /// </summary> /// <param name="connection"></param> /// <param name="query">查询信息</param> internal override void Reset(ref DbConnection connection, ref SelectQuery <modelType> query) { ReturnValue <LeftArray <valueType> > valueArray = SqlTable.SelectQueue(ref connection, ref query); if (valueArray.ReturnType == ReturnType.Success) { int maxIdentity = valueArray.Value.maxKey(value => GetKey(value), 0); if (memberGroup == 0) { SqlTable.Identity64 = maxIdentity + baseIdentity; } int length = maxIdentity >= IdentityArray.ArraySize ? 1 << ((uint)maxIdentity).bits() : IdentityArray.ArraySize; IdentityArray <valueType> newValues = new IdentityArray <valueType>(length); AutoCSer.Memory.Pointer newCounts = Unmanaged.GetPointer8(length * sizeof(int), true); try { int *intCounts = newCounts.Int; foreach (valueType value in valueArray.Value) { setMemberCacheAndValue(value); int identity = GetKey(value); newValues[identity] = value; intCounts[identity] = 1; } for (int step = 2; step != length; step <<= 1) { for (int index = step, countStep = step >> 1; index != length; index += step) { intCounts[index] += intCounts[index - countStep]; } } Unmanaged.Free(ref counts); this.Array = newValues; counts = newCounts; size = length; Count = valueArray.Value.Length; newCounts.SetNull(); } catch (Exception error) { SqlTable.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer); } finally { Unmanaged.Free(ref newCounts); } } else { SqlTable.Log.Fatal(typeof(valueType).fullName() + " 数据加载失败 " + valueArray.ReturnType.ToString(), LogLevel.Fatal | LogLevel.AutoCSer); } }
internal void Response <valueType>(ref valueType value, ref AutoCSer.Memory.Pointer buffer) where valueType : struct { AjaxStream.Reset(ref buffer); using (AjaxStream) { SetJsContentType(); bool isCallBack = ResponseAjaxCallBack(AjaxStream); AutoCSer.JsonSerializer.Serialize(ref value, AjaxStream, JsonSerializeConfig); if (isCallBack) { AjaxStream.Write(')'); } PageResponse.SetBody(AjaxStream, ref DomainServer.ResponseEncoding); } }
/// <summary> /// 对象转换JSON字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="config">配置参数</param> /// <returns>Json字符串</returns> private string serialize <valueType>(ref valueType value, SerializeConfig config) { Config = config ?? DefaultConfig; AutoCSer.Memory.Pointer buffer = UnmanagedPool.Default.GetPointer(); try { CharStream.Reset(ref buffer); using (CharStream) { serialize(ref value); return(CharStream.ToString()); } } finally { UnmanagedPool.Default.PushOnly(ref buffer); } }
/// <summary> /// 获取页面输出数据流 /// </summary> /// <param name="buffer"></param> /// <returns></returns> internal UnmanagedStream GetResponseStream(ref AutoCSer.Memory.Pointer buffer) { UnmanagedStream responseStream = Interlocked.Exchange(ref ResponseStream, null); if (responseStream == null) { responseStream = new UnmanagedStream(ref buffer); } else { responseStream.Reset(ref buffer); } CallResponse.Set(responseStream, ref DomainServer.ResponseEncoding); return(responseStream); }