/// <summary>
 /// 删除数据结构定义
 /// </summary>
 /// <param name="buffer">数据缓冲区</param>
 /// <returns>被删除数据结构索引标识</returns>
 internal IndexIdentity RemoveDataStructure(Buffer buffer)
 {
     if (CanWrite)
     {
         DataStructureBuffer dataStructureBuffer = new DataStructureBuffer(buffer);
         HashString          name = dataStructureBuffer.CacheName;
         ServerDataStructure dataStructure;
         if (DataStructures.TryGetValue(name, out dataStructure))
         {
             int index = dataStructure.Identity.Index;
             FreeIndexs.Add(index);
             buffer.SetIdentity(new IndexIdentity {
                 Index = index, Identity = Array[index].Free()
             });
             DataStructures.Remove(name);
             onOperation(buffer);
             dataStructure.Node.OnRemoved();
             return(dataStructure.Identity);
         }
         buffer.FreeReference();
         return(new IndexIdentity {
             ReturnType = ReturnType.DataStructureNameNotFound
         });
     }
     buffer.FreeReference();
     return(new IndexIdentity {
         ReturnType = ReturnType.CanNotWrite
     });
 }
 /// <summary>
 /// 添加数据结构定义
 /// </summary>
 /// <param name="buffer">数据缓冲区</param>
 /// <returns>数据结构索引标识</returns>
 internal IndexIdentity GetOrCreateDataStructure(Buffer buffer)
 {
     if (CanWrite)
     {
         DataStructureBuffer dataStructureBuffer = new DataStructureBuffer(buffer);
         HashString          name = dataStructureBuffer.CacheName;
         ServerDataStructure dataStructure;
         if (DataStructures.TryGetValue(name, out dataStructure))
         {
             if (dataStructure.IsNodeData(ref dataStructureBuffer.Data))
             {
                 buffer.FreeReference();
                 return(dataStructure.Identity);
             }
             buffer.FreeReference();
             return(new IndexIdentity {
                 ReturnType = ReturnType.DataStructureNameExists
             });
         }
         OnDataStructureCreated = onDataStructureCreated;
         if ((dataStructure = new ServerDataStructure(this, buffer, ref dataStructureBuffer)).Node != null)
         {
             int index = getFreeIndex();
             DataStructures.Add(name, dataStructure);
             Array[index].Set(index, dataStructure);
             buffer.SetIdentity(ref dataStructure.Identity);
             onOperation(buffer);
             OnDataStructureCreated(dataStructure.Identity);
             OnDataStructureCreated = onDataStructureCreated;
             return(dataStructure.Identity);
         }
         buffer.FreeReference();
         return(new IndexIdentity {
             ReturnType = dataStructure.ReturnType
         });
     }
     buffer.FreeReference();
     return(new IndexIdentity {
         ReturnType = ReturnType.CanNotWrite
     });
 }
        /// <summary>
        /// 添加数据结构定义
        /// </summary>
        /// <param name="buffer">数据缓冲区</param>
        /// <returns></returns>
        private bool loadDataStructure(Buffer buffer)
        {
            DataStructureBuffer dataStructureBuffer = new DataStructureBuffer(buffer);
            ServerDataStructure dataStructure       = new ServerDataStructure(this, buffer, ref dataStructureBuffer);
            int index = dataStructureBuffer.Identity.Index;

            if (loadBuffer == null)
            {
                if (!isRebuild && index != getFreeIndex())
                {
                    return(false);
                }
            }
            else if (index >= Array.Length)
            {
                Array = Array.copyNew(Math.Max(Array.Length << 1, Math.Max(sizeof(int), index + 1)));
            }
            DataStructures.Add(dataStructureBuffer.CacheName, dataStructure);
            Array[index].Load(dataStructure);
            return(true);
        }
        /// <summary>
        /// 服务端数据结构定义信息
        /// </summary>
        /// <param name="cache">缓存管理</param>
        /// <param name="buffer">数据缓冲区</param>
        /// <param name="dataStructureBuffer">服务端数据结构定义数据</param>
        internal unsafe ServerDataStructure(CacheManager cache, Buffer buffer, ref DataStructureBuffer dataStructureBuffer) : base(dataStructureBuffer.CacheName, dataStructureBuffer.Identity, dataStructureBuffer.Data)
        {
            ReturnType = ReturnType.ServerDataStructureCreateError;
            fixed(byte *dataFixed = NodeData.Array)
            {
                byte *start = dataFixed + NodeData.Start;
                DataStructureParser nodeParser = new DataStructureParser(start, start + NodeData.Length);
                Type nodeType = nodeParser.Parse();

                if (nodeType != null)
                {
                    Cache.NodeInfo nodeInfo = (Cache.NodeInfo)nodeType.GetField(Cache.Node.NodeInfoFieldName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(null);
                    if (nodeInfo != null)
                    {
                        if (cache.IsFile || !nodeInfo.IsCacheFile || cache.SlaveServer != null)
                        {
                            OperationParameter.NodeParser nodeParameter = new OperationParameter.NodeParser(cache, buffer, dataFixed);
                            nodeParameter.Read += IndexIdentity.SerializeSize;
                            Node            = nodeInfo.CallConstructor(ref nodeParameter);
                            NodeData.Length = (int)(nodeParser.Read - start);
                            byte[] data = NodeData.GetArray();
                            NodeData.Set(data, 0, data.Length);
                        }
                        else
                        {
                            ReturnType = ReturnType.CacheNodeNeedFile;
                        }
                    }
                    else
                    {
                        ReturnType = ReturnType.NotFoundCacheNodeInfo;
                    }
                }
                else
                {
                    ReturnType = ReturnType.CacheNodeParseError;
                }
            }
        }
        /// <summary>
        /// 删除数据结构定义
        /// </summary>
        /// <param name="buffer">数据缓冲区</param>
        /// <returns></returns>
        private bool loadRemoveDataStructure(Buffer buffer)
        {
            DataStructureBuffer dataStructureBuffer = new DataStructureBuffer(buffer);
            HashString          name = dataStructureBuffer.CacheName;
            ServerDataStructure dataStructure;

            if (DataStructures.TryGetValue(name, out dataStructure))
            {
                int index = dataStructure.Identity.Index;
                if (dataStructureBuffer.Identity.Index == index)
                {
                    if (!isRebuild && loadBuffer == null)
                    {
                        FreeIndexs.Add(index);
                    }
                    Array[index].LoadFree(dataStructureBuffer.Identity.Identity);
                    DataStructures.Remove(name);
                    dataStructure.Node.OnRemoved();
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// 获取数据结构索引标识
        /// </summary>
        /// <param name="buffer">数据缓冲区</param>
        /// <returns>数据结构索引标识</returns>
        internal IndexIdentity GetDataStructure(Buffer buffer)
        {
            DataStructureBuffer dataStructureBuffer = new DataStructureBuffer(buffer);
            HashString          name = dataStructureBuffer.CacheName;
            ServerDataStructure dataStructure;

            if (DataStructures.TryGetValue(name, out dataStructure))
            {
                if (dataStructure.IsNodeData(ref dataStructureBuffer.Data))
                {
                    buffer.FreeReference();
                    return(dataStructure.Identity);
                }
                buffer.FreeReference();
                return(new IndexIdentity {
                    ReturnType = ReturnType.DataStructureNameExists
                });
            }
            buffer.FreeReference();
            return(new IndexIdentity {
                ReturnType = ReturnType.DataStructureNameNotFound
            });
        }