/// <summary> /// 从左到右最大匹配 /// </summary> /// <param name="start">匹配起始位置</param> /// <param name="end">匹配结束位置</param> /// <param name="matchs">匹配结果集合</param> private void leftRightMatchs(char *start, char *end, ref LeftArray <KeyValue <int, int> > matchs) { Dictionary <char, Node> bootNode = Boot.Nodes; string value; Node node = Boot, linkNode = null; int index = -1; do { char letter = *start; if (node.GetLinkWhereNull(letter, ref node, ref linkNode) == 0) { if (linkNode != null) { goto LINK; } BOOT: ++index; node = Boot; continue; LINK: do { int isGetValue = linkNode.GetNodeOrLink(letter, ref node, ref linkNode, out value); if (value != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } if (isGetValue == 0) { if (linkNode == null) { goto BOOT; } } else { break; } }while (true); } ++index; if ((value = node.Value) != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } }while (++start != end); node = node.Link; while (node != null) { if ((value = node.GetLink(ref node)) != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } } }
private bool checkAddWord(int start, int length) { SubString word = new SubString(start, length, formatText); HashString wordKey = word; if (searcher.results.GetResult(ref wordKey, ref queryResult)) { result.PrepLength(1); result.Array[result.Length++].Set(wordKey, queryResult); return(true); } return(false); }
/// <summary> /// 复制数据 /// </summary> /// <param name="array"></param> public void CopyTo(ref LeftArray <KeyValuePair <keyType, ResultIndexArray> > array) { array.PrepLength(Dictionary.Count); foreach (KeyValuePair <keyType, ResultIndexArray> value in Dictionary) { array.UnsafeAdd(value); } }
private void addWord(int start, int length) { HashString word = new SubString(start, length, formatText); if (searcher.results.GetResult(ref word, ref queryResult)) { result.PrepLength(1); result.Array[result.Length++].Set(word, queryResult); } }
/// <summary> /// 加载数据结构索引标识 /// </summary> /// <param name="parser"></param> /// <returns></returns> private bool loadIndexIdentity(ref OperationParameter.NodeParser parser) { if (parser.ReadInt() == Array.Length) { FreeIndexs.PrepLength(Array.Length); ServerDataStructure dataStructure; for (int index = Array.Length; index != 0;) { if (!Array[--index].Load((ulong)parser.ReadLong(), out dataStructure)) { return(false); } if (dataStructure == null) { FreeIndexs.UnsafeAdd(index); } } isRebuild = false; return(true); } return(false); }
/// <summary> /// TCP 服务信息集合 /// </summary> /// <param name="cache"></param> /// <param name="registerServer"></param> internal ServerSet(ServerSetCache cache, Server registerServer) { Server = new Log { Type = LogType.RegisterServer, Server = cache.Server }; registerServer.SetIpPort(cache.Server); if (cache.Servers != null) { Servers.PrepLength(cache.Servers.Length); foreach (ServerInfo server in cache.Servers) { Servers.UnsafeAdd(new Log { Type = LogType.RegisterServer, Server = server }); registerServer.SetIpPort(server); } } }
/// <summary> /// 填充数据块集合 /// </summary> /// <param name="datas">填充数据块集合</param> /// <returns></returns> private bool getBlockList(ref LeftArray <SubArray <byte> > datas) { int startIndex = (int)(currentData - dataPoint); for (byte count = *currentData; count != 0; count = *currentData) { currentData += count; if (++currentData >= dataEnd) { return(false); } datas.PrepLength(1); datas.Array[datas.Length++].Set(data, ++startIndex, count); startIndex += count; } ++currentData; return(true); }
/// <summary> /// 新建文件处理 /// </summary> /// <param name="e"></param> private void onCreated(FileSystemEventArgs e) { if (isDisposed == 0) { FileInfo file = new FileInfo(e.FullPath); if (file.Exists) { Monitor.Enter(fileLock); long seconds = currentSeconds + timeoutSeconds; try { files.PrepLength(1); files.Array[files.Length++].Set(file, seconds); if (onTimeSeconds != long.MaxValue) { onTimeSeconds = seconds; } } finally { Monitor.Exit(fileLock); } } } }
/// <summary> /// GIF文件 /// </summary> /// <param name="data">GIF文件数据</param> private unsafe File(byte[] data) { fixed(byte *dataFixed = data) { if ((*(int *)dataFixed & 0xffffff) == ('G' | ('I' << 8) | ('F' << 16))) { Width = *(short *)(dataFixed + 6); Height = *(short *)(dataFixed + 8); byte globalFlag = *(dataFixed + 10); backgroundColorIndex = *(dataFixed + 11); pixelAspectRadio = *(dataFixed + 12); colorResoluTion = (byte)(((globalFlag >> 4) & 7) + 1); sortFlag = (byte)(globalFlag & 8); byte *currentData = dataFixed + 6 + 7; if ((globalFlag & 0x80) != 0) { int colorCount = 1 << ((globalFlag & 7) + 1); if (data.Length < 14 + (colorCount << 1) + colorCount) { return; } currentData = Decoder.FillColor(GlobalColors = new Color[colorCount], currentData); } Decoder decoder = new Decoder(data, dataFixed, currentData); while (!decoder.IsFileEnd) { blocks.PrepLength(1); if (!decoder.Next(ref blocks.Array[blocks.Length++])) { return; } } IsCompleted = true; } } }
public void CallParse(ref Node value) { space(); if (State != ParseState.Success) { return; } if (*current == '<') { char code = *(current + 1); if (((bits[code & 0xff] & targetStartCheckBit) | (code & 0xff00)) == 0) { if (code == '/') { value.SetString(string.Empty); return; } if (code == '!') { searchCData2(); if (State == ParseState.Success) { value.SetString(xml, (int)(valueStart - xmlFixed), valueSize); } return; } State = ParseState.NotFoundTagStart; return; } char *nameStart; LeftArray <KeyValue <SubString, Node> > nodes = default(LeftArray <KeyValue <SubString, Node> >); KeyValue <Range, Range>[] attributes; int nameSize = 0; do { nameStart = getName(ref nameSize); if (State != ParseState.Success) { return; } if (nameStart == null) { value.SetNode(ref nodes); return; } nodes.PrepLength(1); nodes.Array[nodes.Length].Key.Set(xml, (int)(nameStart - xmlFixed), nameSize); attributes = Config.IsAttribute && this.attributes.Length != 0 ? this.attributes.GetArray() : null; if (isTagEnd == 0) { CallParse(ref nodes.Array[nodes.Length].Value); if (State != ParseState.Success || CheckNameEnd(nameStart, nameSize) == 0) { return; } } if (attributes != null) { nodes.Array[nodes.Length].Value.SetAttribute(xml, attributes); } ++nodes.Length; }while (true); } else { valueStart = current; value.Type = NodeType.String; do { if (*current == '<') { value.String.Set(xml, (int)(valueStart - xmlFixed), (int)(endSpace() - valueStart)); if (Config.IsTempString && value.Type == NodeType.EncodeString) { value.Type = NodeType.TempString; } return; } if (*current == '&') { value.Type = NodeType.EncodeString; while (*++current != ';') { if (*current == '<') { State = ParseState.DecodeError; return; } } } ++current; }while (true); } }
/// <summary> /// 写入字符串 /// </summary> /// <param name="value"></param> /// <returns></returns> internal long WriteNotEmpty(string value) { int dataLength = encoding.GetByteCountNotNull(value); Monitor.Enter(bufferLock); if (isDisposed || isError) { Monitor.Exit(bufferLock); return(-1); } long fileBufferLength = this.fileBufferLength; bool isWritting = this.isWritting; if (buffer.IsBuffer) { if (buffer.CanWrite(dataLength)) { buffer.Write(value, ref encoding); this.fileBufferLength += dataLength; this.isWritting = true; Monitor.Exit(bufferLock); } else { try { buffers.PrepLength(2); buffers.Array[buffers.Length++] = buffer; buffer.Clear(); buffers.Array[buffers.Length++].WriteNotPool(value, new byte[dataLength], ref encoding); this.fileBufferLength += dataLength; this.isWritting = true; } finally { Monitor.Exit(bufferLock); } } } else { try { if (dataLength < bufferPool.Size) { buffer.Get(bufferPool); buffer.Write(value, ref encoding); } else { buffers.PrepLength(1); buffers.Array[buffers.Length++].WriteNotPool(value, new byte[dataLength], ref encoding); } this.fileBufferLength += dataLength; this.isWritting = true; } finally { Monitor.Exit(bufferLock); } } if (!isWritting) { AutoCSer.Threading.ThreadPool.Tiny.FastStart(this, AutoCSer.Threading.Thread.CallType.FileStreamWriteFile); } return(fileBufferLength); }
internal void AddFile(byte[] name, byte[] fileName, string saveFileName) { Files.PrepLength(1); Files.Array[Files.Length++].SetFile(name, fileName, saveFileName); }
internal void Add(byte[] name, byte[] value) { Values.PrepLength(1); Files.Array[Files.Length++].Set(name, value); }
private void addWord(int start, int length, WordType wordType) { words.PrepLength(1); words.Array[words.Length++].Set(new SubString(start, length, formatText), wordType); }
/// <summary> /// 初始化 /// </summary> internal void Start() { int isDisposed = 1; SubBuffer.PoolBufferFull buffer = default(SubBuffer.PoolBufferFull); try { if (checkStateFile()) { byte[] stateData = new byte[stateBufferSize]; stateFileStream = new FileStream(stateFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, stateBufferSize, FileOptions.None); stateFileStream.Seek(-stateBufferSize, SeekOrigin.End); stateFileStream.Read(stateData, 0, stateBufferSize); fixed(byte *stateDataFixed = stateData) { identity = *(ulong *)stateDataFixed; dataFileLength = *(long *)(stateDataFixed + sizeof(ulong)); } if (((uint)identity & (DataCountPerFile - 1)) == 0) { dataFileLength = 0; } if (dataFileLength == 0) { FileInfo dataFileInfo = new FileInfo(dataFileName); if (dataFileInfo.Exists) { if (dataFileInfo.Length == 0) { dataFileInfo.Delete(); } else { AutoCSer.IO.File.MoveBak(dataFileInfo.FullName); } } dataFileStream = new FileStream(dataFileInfo.FullName, FileMode.CreateNew, FileAccess.Write, FileShare.Read, bufferPool.Size, FileOptions.None); } else { FileInfo dataFileInfo = new FileInfo(dataFileName); if (!dataFileInfo.Exists) { Node.Cache.TcpServer.Log.Add(Log.LogType.Error, "没有找到消息队列数据文件 " + dataFileInfo.FullName); return; } if (dataFileInfo.Length < dataFileLength) { Node.Cache.TcpServer.Log.Add(Log.LogType.Error, "消息队列数据文件 " + dataFileInfo.FullName + " 大小错误 " + dataFileInfo.Length.toString() + " < " + dataFileLength.toString()); return; } dataFileStream = new FileStream(dataFileInfo.FullName, FileMode.Open, FileAccess.Write, FileShare.Read, bufferPool.Size, FileOptions.None); if (dataFileStream.Length > dataFileLength) { dataFileStream.SetLength(dataFileLength); dataFileStream.Flush(true); } dataFileStream.Seek(0, SeekOrigin.End); FileInfo indexFileInfo = new FileInfo(getIndexFileName(identity)); bufferPool.Get(ref buffer); fixed(byte *bufferFixed = buffer.Buffer) { byte *bufferStart = bufferFixed + buffer.StartIndex, end = bufferStart + buffer.Length; ulong baseIdentity = this.baseIdentity; if (indexFileInfo.Exists && indexFileInfo.Length >= sizeof(int) * 2) { #region 初始化数据文件数据包索引信息 int count = (int)(indexFileInfo.Length >> 3), index = 0; long fileIndex = 0; indexs = new LeftArray <PacketIndex>(count); PacketIndex[] indexArray = indexs.Array; using (FileStream indexFileStream = new FileStream(indexFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferPool.Size, FileOptions.None)) { do { indexFileStream.Read(buffer.Buffer, buffer.StartIndex, buffer.Length); byte *read = bufferStart; do { indexArray[index].Set(baseIdentity + *(uint *)read, fileIndex += *(int *)(read + sizeof(int))); if (++index == count) { break; } }while ((read += sizeof(int) * 2) != end); }while (index != count); } while (index != 0) { if ((fileIndex = indexArray[--index].FileIndex) == dataFileLength) { if (indexArray[index].Identity == identity) { indexs.Length = index + 1; } break; } if (fileIndex < dataFileLength) { break; } } #endregion } if (indexs.Length == 0) { #region 重建数据文件数据包索引信息 if (indexs.Array == null) { indexs = new LeftArray <PacketIndex>(1 << 10); } indexs.Array[0].Set(baseIdentity); indexs.Length = 1; int bufferIndex = 0, readBufferSize = Math.Min(buffer.Length, createIndexBufferSize), bufferEndIndex, dataSize; long nextFileSize = dataFileLength, fileIndex = 0; using (FileStream fileStream = new FileStream(dataFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, readBufferSize, FileOptions.None)) { readBufferSize -= sizeof(int); do { bufferEndIndex = fileStream.Read(buffer.Buffer, buffer.StartIndex + bufferIndex, readBufferSize - bufferIndex); nextFileSize -= bufferEndIndex; bufferEndIndex += bufferIndex - sizeof(int) * 3; bufferIndex = 0; do { byte *read = bufferStart + bufferIndex; dataSize = *(int *)read; if (dataSize < 0) { baseIdentity += *(uint *)(read + sizeof(int) * 2); bufferIndex += -dataSize + PacketHeaderSize + sizeof(int); } else { baseIdentity += *(uint *)(read + sizeof(int)); bufferIndex += dataSize + PacketHeaderSize; } indexs.PrepLength(1); indexs.Array[indexs.Length].Set(baseIdentity, fileIndex + bufferIndex); ++indexs.Length; }while (bufferIndex <= bufferEndIndex); fileIndex += bufferIndex; switch (dataSize = bufferIndex - bufferEndIndex) { case 1: case 2: case 3: *(ulong *)bufferStart = *(ulong *)(bufferStart + bufferIndex); *(uint *)(bufferStart + sizeof(ulong)) = *(uint *)(bufferStart + (bufferIndex + sizeof(ulong))); bufferIndex = sizeof(int) * 3 - dataSize; break; case 4: case 5: case 6: case 7: *(ulong *)bufferStart = *(ulong *)(bufferStart + bufferIndex); bufferIndex = sizeof(int) * 3 - dataSize; break; case 8: case 9: case 10: case 11: *(uint *)bufferStart = *(uint *)(bufferStart + bufferIndex); bufferIndex = sizeof(int) * 3 - dataSize; break; case 12: bufferIndex = 0; break; default: fileStream.Seek(dataSize -= sizeof(int) * 3, SeekOrigin.Current); nextFileSize -= dataSize; bufferIndex = 0; break; } }while (nextFileSize > 0); } #endregion } } } } else { stateFileStream = new FileStream(stateFileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read, stateBufferSize, FileOptions.None); FileInfo dataFileInfo = new FileInfo(dataFileName); if (dataFileInfo.Exists) { if (dataFileInfo.Length == 0) { dataFileInfo.Delete(); } else { AutoCSer.IO.File.MoveBak(dataFileInfo.FullName); } } dataFileStream = new FileStream(dataFileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read, bufferPool.Size, FileOptions.None); } if (indexs.Array == null) { indexs = new LeftArray <PacketIndex>(1 << 10); } if (indexs.Length == 0) { indexs.Array[0].Set(baseIdentity); indexs.Length = 1; } writeHandle = write; StatePacketIndex.Set(identity, dataFileLength); AutoCSer.DomainUnload.Unloader.Add(disposeHandle, DomainUnload.Type.Action); isDisposed = 0; } finally { buffer.TryFree(); if (isDisposed == 0) { Interlocked.Exchange(ref isWrite, 0); onStart(); if (!bufferQueue.IsEmpty && Interlocked.CompareExchange(ref isWrite, 1, 0) == 0) { write(); } } else { indexs.Length = 0; Dispose(); } } }
/// <summary> /// 获取搜索数据标识集合(匹配所有分词结果) /// </summary> /// <param name="text">搜索关键字</param> /// <param name="maxSize">关键字最大字符长度</param> /// <param name="isKey">数据标识过滤</param> /// <returns>数据标识集合</returns> public LeftArray <keyType> SearchAll(string text, int maxSize, Func <keyType, bool> isKey = null) { resultArray.Length = 0; Simplified.Set(text, maxSize); Search(true); switch (queryResult.Count) { case 0: break; case 1: foreach (KeyValuePair <keyType, ResultIndexArray> result in queryResult[0].Value.Dictionary) { if (isKey(result.Key)) { resultArray.Add(result.Key); } } break; default: Dictionary <keyType, ResultIndexArray> resultDictionary = null; foreach (KeyValue <HashString, QueryResult> result in queryResult) { if (resultDictionary == null || result.Value.Dictionary.Count < resultDictionary.Count) { resultDictionary = result.Value.Dictionary; } } resultCountDictionary.Empty(); foreach (keyType key in resultDictionary.Keys) { if (isKey(key)) { resultCountDictionary.Set(key, 0); } } if (resultCountDictionary.Count != 0) { int count = 0, keyCount = resultCountDictionary.Count; foreach (KeyValue <HashString, QueryResult> result in queryResult) { if (!object.ReferenceEquals(result.Value.Dictionary, resultDictionary)) { int nextCount = count + 1, resultCount; keyCount = 0; foreach (keyType key in result.Value.Dictionary.Keys) { if (resultCountDictionary.TryGetValue(key, out resultCount) && resultCount == count) { resultCountDictionary.Set(key, nextCount); ++keyCount; } } if (keyCount == 0) { resultCountDictionary.Empty(); break; } count = nextCount; } } if (keyCount > 0) { resultArray.PrepLength(keyCount); foreach (KeyValue <keyType, int> result in resultCountDictionary.KeyValues) { if (result.Value == count) { resultArray.UnsafeAdd(result.Key); } } } } break; } return(resultArray); }
/// <summary> /// 从左到右最大匹配 /// </summary> /// <param name="start">匹配起始位置</param> /// <param name="end">匹配结束位置</param> /// <param name="matchs">匹配结果集合</param> internal void LeftRightMatchs(char *start, char *end, ref LeftArray <KeyValue <int, int> > matchs) { Node[][] pool = NodePool.Pool; if (nodes.Data != null) { string value; int node = boot, linkNode = 0, index = -1; do { char letter = *start; if (node != boot) { goto NEXT; } BOOT: if ((node = nodes.Int[letter]) == 0) { ++index; node = boot; continue; } goto MATCH; NEXT: if (pool[node >> ArrayPool.ArraySizeBit][node & ArrayPool.ArraySizeAnd].GetLinkWhereNull(letter, ref node, ref linkNode) == 0) { if (linkNode == 0) { goto BOOT; } do { int isGetValue = pool[linkNode >> ArrayPool.ArraySizeBit][linkNode & ArrayPool.ArraySizeAnd].GetNodeOrLink(letter, ref node, ref linkNode, out value); if (value != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } if (isGetValue == 0) { if (linkNode == 0) { goto BOOT; } } else { break; } }while (true); } MATCH: ++index; if ((value = pool[node >> ArrayPool.ArraySizeBit][node & ArrayPool.ArraySizeAnd].Value) != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } }while (++start != end); if (node != boot) { node = pool[node >> ArrayPool.ArraySizeBit][node & ArrayPool.ArraySizeAnd].Link; while (node != 0) { if ((value = pool[node >> ArrayPool.ArraySizeBit][node & ArrayPool.ArraySizeAnd].GetLink(ref node)) != null) { matchs.PrepLength(1); matchs.Array[matchs.Length++].Set(index - value.Length + 1, value.Length); } } } } }