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
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
{
string [] arrWord=word.Split(new char [] { ‘ ‘ }, StringSplitOptions .RemoveEmptyEntries);
System.Collections.Generic.List
if (arrWord.Length==0) return key_0;
System.Collections.Generic.List
for (int i=0; i < arrWord.Length-1; i++)
{
System.Collections.Generic.List
key_list[i]=key_i;
}
for (int i=key_0.Count-1;i>=0;i–)
{
foreach (System.Collections.Generic.List
{
if (key_i.Contains(key_0[i])==false )
{
key_0.RemoveAt(i);
continue ;
}
}
}
return key_0;
}
///
/// 获取单个词的索引信息
///
///
///
///
static System.Collections.Generic.List
{
System.Collections.Generic.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;
}
};