SQL2005CLR函数扩展 – 关于山寨索引(sqlsugar 官网)没想到

随心笔谈2年前发布 编辑
165 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

using System;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.Collections;

using System.Collections.Generic;

public partial class UserDefinedFunctions

{

    ///

    /// 设置索引目录

    ///

    ///

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlBoolean SetRoot(SqlString value)

    {

        if (value.IsNull) return false ;

        if (System.IO.Directory .Exists(value.Value))

        {

            root=value.Value;

            return true ;

        }

        else

        {

            return false ;

        }

    }

    ///

    /// 获取索引目录

    ///

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlString GetRoot()

    {

        return new SqlString (root);

    }

    ///

    /// 建立索引

    ///

    /// 主键

    /// 索引名称

    /// 索引内容

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlInt32 SetIndex(SqlString key,SqlString indexName,SqlString content)

    {

        if (key.IsNull || content.IsNull||indexName.IsNull) return 0;

        return _setIndex(key.Value,indexName.Value, content.Value);

    }

    ///

    /// 查询索引

    ///

    /// 关键字(空格区分)

    /// 索引名称

    ///

    [SqlFunction (TableDefinition=”pk nvarchar(900)” , Name=”GetIndex” , FillRowMethodName=”FillRow” )]

    public static IEnumerable GetIndex(SqlString word,SqlString indexName)

    {

        System.Collections.Generic.List ret=new List ();

        if (word.IsNull || indexName.IsNull) return ret;

        return _getIndex2(word.Value, indexName.Value);

    }

    public static void FillRow(Object obj, out SqlString pk)

    {

        string key=obj.ToString();

        pk=key;

    }

    static string root=@”d:/index” ;

    ///

    /// 获取有空格分隔的索引信息

    ///

    ///

    ///

    ///

    static System.Collections.Generic.List _getIndex2(string word, string indexName)

    {

        string [] arrWord=word.Split(new char [] { ‘ ‘ }, StringSplitOptions .RemoveEmptyEntries);

        System.Collections.Generic.List key_0=_getIndex(arrWord[0], indexName);

        if (arrWord.Length==0) return key_0;

        System.Collections.Generic.List [] key_list=new List [arrWord.Length-1];

        for (int i=0; i < arrWord.Length-1; i++)

        {

            System.Collections.Generic.List key_i=_getIndex(arrWord[i+1],indexName);

            key_list[i]=key_i;

        }

        for (int i=key_0.Count-1;i>=0;i–)

        {

            foreach (System.Collections.Generic.List key_i in key_list)

            {

                if (key_i.Contains(key_0[i])==false )

                {

                    key_0.RemoveAt(i);

                    continue ;

                }

            }

        }

        return key_0;

    }

    ///

    /// 获取单个词的索引信息

    ///

    ///

    ///

    ///

    static System.Collections.Generic.List _getIndex(string word, string indexName)

    {

        System.Collections.Generic.List ret=new List ();

        byte [] bWord=System.Text.Encoding .Unicode.GetBytes(word);

        if (bWord.Length < 4) return ret;

        string path=string .Format(@”{0}/{1}/{2}/{3}/{4}/{5}/” , root,indexName, bWord[0], bWord[1], bWord[2], bWord[3]);

        if (System.IO.Directory .Exists(path)==false )

        {

            return ret;

        }

        string [] arrFiles=System.IO.Directory .GetFiles(path);

        foreach (string file in arrFiles)

        {

            string key=System.IO.Path .GetFileNameWithoutExtension(file);

            string index=System.IO.Path .GetExtension(file).TrimStart(new char [] { ‘.’ });

            int cIndex=int .Parse(index);

            bool bHas=true ;

            for (int i=2; i < bWord.Length – 3; i=i + 2)

            {

                string nextFile=string .Format(@”{0}/{1}/{2}/{3}/{4}/{5}/{6}.{7}” ,

                    root, indexName, bWord[i + 0], bWord[i + 1], bWord[i + 2], bWord[i + 3], key, ++cIndex);

                if (System.IO.File .Exists(nextFile)==false )

                {

                    bHas=false ;

                    break ;

                }

            }

            if (bHas==true &&ret.Contains(key)==false )

                ret.Add(key);

        }

        return ret;

    }

    ///

    /// 建立索引文件

    ///

    ///

    ///

    ///

    ///

    static int _setIndex(string key,string indexName, string content)

    {

        byte [] bContent=System.Text.Encoding .Unicode.GetBytes(content);

        if (bContent.Length <=4) return 0;

        for (int i=0; i < bContent.Length – 3; i=i + 2)

        {

            string path=string .Format(@”{0}/{1}/{2}/{3}/{4}/{5}/” , root,indexName, bContent[i + 0], bContent[i + 1], bContent[i + 2], bContent[i + 3]);

            if (System.IO.Directory .Exists(path)==false )

            {

                System.IO.Directory .CreateDirectory(path);

            }

            string file=string .Format(@”{0}/{1}.{2}” , path, key, i / 2);

            if (System.IO.File .Exists(file)==false )

            {

                System.IO.File .Create(file).Close();

            }

        }

        return content.Length;

    }

};

© 版权声明

相关文章