From 31471177555f745db130ec03cd99401f5fb08f20 Mon Sep 17 00:00:00 2001 From: vadym_biliuga Date: Fri, 15 May 2009 12:05:42 +0000 Subject: [PATCH] git-svn-id: https://abrams/svn/LanSpider-repo@22 16f2e333-51b0-4855-8c5a-a66b7be24171 --- LanSpider.sln | 25 ++ WebSearch/App_Code/SearchEngine.cs | 22 + WebSearch/App_Code/SearchIndex.dbml | 38 ++ WebSearch/App_Code/SearchIndex.dbml.layout | 31 ++ WebSearch/App_Code/SearchIndex.designer.cs | 585 ++++++++++++++++++++++++++ WebSearch/Default.aspx | 162 +++++++ WebSearch/Default.aspx.cs | 43 ++ WebSearch/MasterPage.master | 21 + WebSearch/MasterPage.master.cs | 14 + WebSearch/web.config | 127 ++++++ src/LanSpider/App.config | 12 + src/LanSpider/Database/Epam_Index.cs | 6 + src/LanSpider/Database/Epam_Index.dbml | 26 ++ src/LanSpider/Database/Epam_Index.dbml.layout | 32 ++ src/LanSpider/Database/Epam_Index.designer.cs | 542 ++++++++++++++++++++++++ src/LanSpider/Database/Manager.cs | 173 +++++--- src/LanSpider/LanSpider.cs | 155 ++++--- src/LanSpider/LanSpider.csproj | 20 +- src/LanSpider/Multithreading/WorkManager.cs | 3 +- src/LanSpider/Program.cs | 18 +- src/LanSpider/Properties/Settings.Designer.cs | 35 +- src/LanSpider/Properties/Settings.settings | 19 +- src/LanSpider/Resources/create_db.sql | 59 +-- 23 files changed, 2015 insertions(+), 153 deletions(-) create mode 100644 WebSearch/App_Code/SearchEngine.cs create mode 100644 WebSearch/App_Code/SearchIndex.dbml create mode 100644 WebSearch/App_Code/SearchIndex.dbml.layout create mode 100644 WebSearch/App_Code/SearchIndex.designer.cs create mode 100644 WebSearch/Default.aspx create mode 100644 WebSearch/Default.aspx.cs create mode 100644 WebSearch/MasterPage.master create mode 100644 WebSearch/MasterPage.master.cs create mode 100644 WebSearch/web.config create mode 100644 src/LanSpider/App.config create mode 100644 src/LanSpider/Database/Epam_Index.cs create mode 100644 src/LanSpider/Database/Epam_Index.dbml create mode 100644 src/LanSpider/Database/Epam_Index.dbml.layout create mode 100644 src/LanSpider/Database/Epam_Index.designer.cs rewrite src/LanSpider/Resources/create_db.sql (63%) diff --git a/LanSpider.sln b/LanSpider.sln index 5757e42..efd45d6 100644 --- a/LanSpider.sln +++ b/LanSpider.sln @@ -5,6 +5,27 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LanSpider", "src\LanSpider\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extensions", "src\Extensions\Extensions.csproj", "{74132A4C-87E0-47BF-A8E3-7600B65A525E}" EndProject +Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "WebSearch", "WebSearch\", "{57D4D534-C8AF-46AB-9AB1-867E32274985}" + ProjectSection(WebsiteProperties) = preProject + TargetFramework = "3.5" + Debug.AspNetCompiler.VirtualPath = "/WebSearch" + Debug.AspNetCompiler.PhysicalPath = "WebSearch\" + Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\WebSearch\" + Debug.AspNetCompiler.Updateable = "true" + Debug.AspNetCompiler.ForceOverwrite = "true" + Debug.AspNetCompiler.FixedNames = "false" + Debug.AspNetCompiler.Debug = "True" + Release.AspNetCompiler.VirtualPath = "/WebSearch" + Release.AspNetCompiler.PhysicalPath = "WebSearch\" + Release.AspNetCompiler.TargetPath = "PrecompiledWeb\WebSearch\" + Release.AspNetCompiler.Updateable = "true" + Release.AspNetCompiler.ForceOverwrite = "true" + Release.AspNetCompiler.FixedNames = "false" + Release.AspNetCompiler.Debug = "False" + VWDPort = "1623" + DefaultWebSiteLanguage = "Visual C#" + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -19,6 +40,10 @@ Global {74132A4C-87E0-47BF-A8E3-7600B65A525E}.Debug|Any CPU.Build.0 = Debug|Any CPU {74132A4C-87E0-47BF-A8E3-7600B65A525E}.Release|Any CPU.ActiveCfg = Release|Any CPU {74132A4C-87E0-47BF-A8E3-7600B65A525E}.Release|Any CPU.Build.0 = Release|Any CPU + {57D4D534-C8AF-46AB-9AB1-867E32274985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57D4D534-C8AF-46AB-9AB1-867E32274985}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57D4D534-C8AF-46AB-9AB1-867E32274985}.Release|Any CPU.ActiveCfg = Debug|Any CPU + {57D4D534-C8AF-46AB-9AB1-867E32274985}.Release|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WebSearch/App_Code/SearchEngine.cs b/WebSearch/App_Code/SearchEngine.cs new file mode 100644 index 0000000..a8954b6 --- /dev/null +++ b/WebSearch/App_Code/SearchEngine.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Configuration; + +/// +/// Summary description for SearchEngine +/// +public class SearchEngine +{ + private SearchIndexDataContext _dataContext = new SearchIndexDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString); + public SearchEngine() + { + } + + public System.Data.Linq.ISingleResult Search(string phrase) + { + var result = _dataContext.Search(phrase, "Any"); + return result; + } +} diff --git a/WebSearch/App_Code/SearchIndex.dbml b/WebSearch/App_Code/SearchIndex.dbml new file mode 100644 index 0000000..31621eb --- /dev/null +++ b/WebSearch/App_Code/SearchIndex.dbml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + +
+ + + + + + + + +
+ + + + + + + + +
+ + + + + +
\ No newline at end of file diff --git a/WebSearch/App_Code/SearchIndex.dbml.layout b/WebSearch/App_Code/SearchIndex.dbml.layout new file mode 100644 index 0000000..27282b2 --- /dev/null +++ b/WebSearch/App_Code/SearchIndex.dbml.layout @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WebSearch/App_Code/SearchIndex.designer.cs b/WebSearch/App_Code/SearchIndex.designer.cs new file mode 100644 index 0000000..ccbcd64 --- /dev/null +++ b/WebSearch/App_Code/SearchIndex.designer.cs @@ -0,0 +1,585 @@ +#pragma warning disable 1591 +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.3082 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Data.Linq; +using System.Data.Linq.Mapping; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + + + +[System.Data.Linq.Mapping.DatabaseAttribute(Name="EpamSharesIndex")] +public partial class SearchIndexDataContext : System.Data.Linq.DataContext +{ + + private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); + + #region Extensibility Method Definitions + partial void OnCreated(); + partial void InsertFile(File instance); + partial void UpdateFile(File instance); + partial void DeleteFile(File instance); + partial void InsertFolder(Folder instance); + partial void UpdateFolder(Folder instance); + partial void DeleteFolder(Folder instance); + #endregion + + public SearchIndexDataContext() : + base(global::System.Configuration.ConfigurationManager.ConnectionStrings["TestEpamSharesIndexConnectionString"].ConnectionString, mappingSource) + { + OnCreated(); + } + + public SearchIndexDataContext(string connection) : + base(connection, mappingSource) + { + OnCreated(); + } + + public SearchIndexDataContext(System.Data.IDbConnection connection) : + base(connection, mappingSource) + { + OnCreated(); + } + + public SearchIndexDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : + base(connection, mappingSource) + { + OnCreated(); + } + + public SearchIndexDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : + base(connection, mappingSource) + { + OnCreated(); + } + + public System.Data.Linq.Table Files + { + get + { + return this.GetTable(); + } + } + + public System.Data.Linq.Table Folders + { + get + { + return this.GetTable(); + } + } + + public System.Data.Linq.Table FileInfos + { + get + { + return this.GetTable(); + } + } + + [Function(Name="dbo.Search")] + public ISingleResult Search([Parameter(DbType="NVarChar(1000)")] string phrase, [Parameter(DbType="NVarChar(100)")] string type) + { + IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), phrase, type); + return ((ISingleResult)(result.ReturnValue)); + } +} + +[Table(Name="dbo.[File]")] +public partial class File : INotifyPropertyChanging, INotifyPropertyChanged +{ + + private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); + + private int _file_id; + + private int _parent_folder_id; + + private string _name; + + private string _type; + + private long _size; + + private System.Data.Linq.Binary _indexed_on; + + private EntityRef _Folder; + + #region Extensibility Method Definitions + partial void OnLoaded(); + partial void OnValidate(System.Data.Linq.ChangeAction action); + partial void OnCreated(); + partial void Onfile_idChanging(int value); + partial void Onfile_idChanged(); + partial void Onparent_folder_idChanging(int value); + partial void Onparent_folder_idChanged(); + partial void OnnameChanging(string value); + partial void OnnameChanged(); + partial void OntypeChanging(string value); + partial void OntypeChanged(); + partial void OnsizeChanging(long value); + partial void OnsizeChanged(); + partial void Onindexed_onChanging(System.Data.Linq.Binary value); + partial void Onindexed_onChanged(); + #endregion + + public File() + { + this._Folder = default(EntityRef); + OnCreated(); + } + + [Column(Storage="_file_id", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)] + public int file_id + { + get + { + return this._file_id; + } + set + { + if ((this._file_id != value)) + { + this.Onfile_idChanging(value); + this.SendPropertyChanging(); + this._file_id = value; + this.SendPropertyChanged("file_id"); + this.Onfile_idChanged(); + } + } + } + + [Column(Storage="_parent_folder_id", DbType="Int NOT NULL", UpdateCheck=UpdateCheck.Never)] + public int parent_folder_id + { + get + { + return this._parent_folder_id; + } + set + { + if ((this._parent_folder_id != value)) + { + if (this._Folder.HasLoadedOrAssignedValue) + { + throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); + } + this.Onparent_folder_idChanging(value); + this.SendPropertyChanging(); + this._parent_folder_id = value; + this.SendPropertyChanged("parent_folder_id"); + this.Onparent_folder_idChanged(); + } + } + } + + [Column(Storage="_name", DbType="NVarChar(255)", UpdateCheck=UpdateCheck.Never)] + public string name + { + get + { + return this._name; + } + set + { + if ((this._name != value)) + { + this.OnnameChanging(value); + this.SendPropertyChanging(); + this._name = value; + this.SendPropertyChanged("name"); + this.OnnameChanged(); + } + } + } + + [Column(Storage="_type", DbType="NVarChar(50)", UpdateCheck=UpdateCheck.Never)] + public string type + { + get + { + return this._type; + } + set + { + if ((this._type != value)) + { + this.OntypeChanging(value); + this.SendPropertyChanging(); + this._type = value; + this.SendPropertyChanged("type"); + this.OntypeChanged(); + } + } + } + + [Column(Storage="_size", DbType="BigInt NOT NULL", UpdateCheck=UpdateCheck.Never)] + public long size + { + get + { + return this._size; + } + set + { + if ((this._size != value)) + { + this.OnsizeChanging(value); + this.SendPropertyChanging(); + this._size = value; + this.SendPropertyChanged("size"); + this.OnsizeChanged(); + } + } + } + + [Column(Storage="_indexed_on", AutoSync=AutoSync.Always, DbType="rowversion", IsDbGenerated=true, IsVersion=true, UpdateCheck=UpdateCheck.Never)] + public System.Data.Linq.Binary indexed_on + { + get + { + return this._indexed_on; + } + set + { + if ((this._indexed_on != value)) + { + this.Onindexed_onChanging(value); + this.SendPropertyChanging(); + this._indexed_on = value; + this.SendPropertyChanged("indexed_on"); + this.Onindexed_onChanged(); + } + } + } + + [Association(Name="Folder_File", Storage="_Folder", ThisKey="parent_folder_id", OtherKey="folder_id", IsForeignKey=true)] + public Folder Folder + { + get + { + return this._Folder.Entity; + } + set + { + Folder previousValue = this._Folder.Entity; + if (((previousValue != value) + || (this._Folder.HasLoadedOrAssignedValue == false))) + { + this.SendPropertyChanging(); + if ((previousValue != null)) + { + this._Folder.Entity = null; + previousValue.Files.Remove(this); + } + this._Folder.Entity = value; + if ((value != null)) + { + value.Files.Add(this); + this._parent_folder_id = value.folder_id; + } + else + { + this._parent_folder_id = default(int); + } + this.SendPropertyChanged("Folder"); + } + } + } + + public event PropertyChangingEventHandler PropertyChanging; + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void SendPropertyChanging() + { + if ((this.PropertyChanging != null)) + { + this.PropertyChanging(this, emptyChangingEventArgs); + } + } + + protected virtual void SendPropertyChanged(String propertyName) + { + if ((this.PropertyChanged != null)) + { + this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } +} + +[Table(Name="dbo.Folder")] +public partial class Folder : INotifyPropertyChanging, INotifyPropertyChanged +{ + + private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); + + private int _folder_id; + + private int _parent_id; + + private string _path; + + private System.Data.Linq.Binary _indexed_on; + + private EntitySet _Files; + + #region Extensibility Method Definitions + partial void OnLoaded(); + partial void OnValidate(System.Data.Linq.ChangeAction action); + partial void OnCreated(); + partial void Onfolder_idChanging(int value); + partial void Onfolder_idChanged(); + partial void Onparent_idChanging(int value); + partial void Onparent_idChanged(); + partial void OnpathChanging(string value); + partial void OnpathChanged(); + partial void Onindexed_onChanging(System.Data.Linq.Binary value); + partial void Onindexed_onChanged(); + #endregion + + public Folder() + { + this._Files = new EntitySet(new Action(this.attach_Files), new Action(this.detach_Files)); + OnCreated(); + } + + [Column(Storage="_folder_id", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)] + public int folder_id + { + get + { + return this._folder_id; + } + set + { + if ((this._folder_id != value)) + { + this.Onfolder_idChanging(value); + this.SendPropertyChanging(); + this._folder_id = value; + this.SendPropertyChanged("folder_id"); + this.Onfolder_idChanged(); + } + } + } + + [Column(Storage="_parent_id", DbType="Int NOT NULL", UpdateCheck=UpdateCheck.Never)] + public int parent_id + { + get + { + return this._parent_id; + } + set + { + if ((this._parent_id != value)) + { + this.Onparent_idChanging(value); + this.SendPropertyChanging(); + this._parent_id = value; + this.SendPropertyChanged("parent_id"); + this.Onparent_idChanged(); + } + } + } + + [Column(Storage="_path", DbType="NVarChar(4000) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)] + public string path + { + get + { + return this._path; + } + set + { + if ((this._path != value)) + { + this.OnpathChanging(value); + this.SendPropertyChanging(); + this._path = value; + this.SendPropertyChanged("path"); + this.OnpathChanged(); + } + } + } + + [Column(Storage="_indexed_on", AutoSync=AutoSync.Always, DbType="rowversion", IsDbGenerated=true, IsVersion=true, UpdateCheck=UpdateCheck.Never)] + public System.Data.Linq.Binary indexed_on + { + get + { + return this._indexed_on; + } + set + { + if ((this._indexed_on != value)) + { + this.Onindexed_onChanging(value); + this.SendPropertyChanging(); + this._indexed_on = value; + this.SendPropertyChanged("indexed_on"); + this.Onindexed_onChanged(); + } + } + } + + [Association(Name="Folder_File", Storage="_Files", ThisKey="folder_id", OtherKey="parent_folder_id")] + public EntitySet Files + { + get + { + return this._Files; + } + set + { + this._Files.Assign(value); + } + } + + public event PropertyChangingEventHandler PropertyChanging; + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void SendPropertyChanging() + { + if ((this.PropertyChanging != null)) + { + this.PropertyChanging(this, emptyChangingEventArgs); + } + } + + protected virtual void SendPropertyChanged(String propertyName) + { + if ((this.PropertyChanged != null)) + { + this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } + + private void attach_Files(File entity) + { + this.SendPropertyChanging(); + entity.Folder = this; + } + + private void detach_Files(File entity) + { + this.SendPropertyChanging(); + entity.Folder = null; + } +} + +[Table(Name="dbo.vFileInfo")] +public partial class FileInfo +{ + + private string _name; + + private string _path; + + private string _FullName; + + private long _size; + + private string _type; + + public FileInfo() + { + } + + [Column(Storage="_name", DbType="NVarChar(255)")] + public string name + { + get + { + return this._name; + } + set + { + if ((this._name != value)) + { + this._name = value; + } + } + } + + [Column(Storage="_path", DbType="NVarChar(4000) NOT NULL", CanBeNull=false)] + public string path + { + get + { + return this._path; + } + set + { + if ((this._path != value)) + { + this._path = value; + } + } + } + + [Column(Storage="_FullName", DbType="NVarChar(4000)")] + public string FullName + { + get + { + return this._FullName; + } + set + { + if ((this._FullName != value)) + { + this._FullName = value; + } + } + } + + [Column(Storage="_size", DbType="BigInt NOT NULL")] + public long size + { + get + { + return this._size; + } + set + { + if ((this._size != value)) + { + this._size = value; + } + } + } + + [Column(Storage="_type", DbType="NVarChar(50)")] + public string type + { + get + { + return this._type; + } + set + { + if ((this._type != value)) + { + this._type = value; + } + } + } +} +#pragma warning restore 1591 diff --git a/WebSearch/Default.aspx b/WebSearch/Default.aspx new file mode 100644 index 0000000..27937ec --- /dev/null +++ b/WebSearch/Default.aspx @@ -0,0 +1,162 @@ +<%@ Page Title="LanSpider Web Search" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> + + +

LanSpider Web Search

+
+ + + + +
+ + All + Images + Video + Music + + + + + + + + + <%# Eval("path") %> + + + + + + + + + + + + + + + + <%# Eval("path") %> + + + + + + + + + + + + + + +
+ No files was found.
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ File name + Path + Size + Type
+
+ + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+
+ diff --git a/WebSearch/Default.aspx.cs b/WebSearch/Default.aspx.cs new file mode 100644 index 0000000..bf9f0a0 --- /dev/null +++ b/WebSearch/Default.aspx.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +public partial class _Default : System.Web.UI.Page +{ + protected void Page_Load(object sender, EventArgs e) + { + string searchMask = Request.QueryString["q"]; + if (!String.IsNullOrEmpty(searchMask)) + { + Search(searchMask); + } + else + { + ListView1.DataSource = null; + ListView1.DataBind(); + } + } + + protected void Search(string searchMask) + { + SearchEngine se = new SearchEngine(); + ListView1.DataSource = se.Search(searchMask); + ListView1.DataBind(); + } + + protected void btnSearch_Click(object sender, EventArgs e) + { + if (!String.IsNullOrEmpty(this.TextBox1.Text)) + Response.Redirect(String.Format("~/Default.aspx?q={0}", this.TextBox1.Text)); + } + + protected override void OnInit(EventArgs e) + { + base.OnInit(e); + if (this.IsPostBack && (Request.Params["__EVENTTARGET"] == btnSearch.UniqueID)) + Response.Redirect("~/Default.aspx?q=" + this.Server.UrlEncode(Request.Params[TextBox1.UniqueID])); + } +} diff --git a/WebSearch/MasterPage.master b/WebSearch/MasterPage.master new file mode 100644 index 0000000..ec30197 --- /dev/null +++ b/WebSearch/MasterPage.master @@ -0,0 +1,21 @@ +<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> + + + + + + + +

Epam Shares Search

+
+ + +
+
+ + + +
+
+ + diff --git a/WebSearch/MasterPage.master.cs b/WebSearch/MasterPage.master.cs new file mode 100644 index 0000000..3c39f82 --- /dev/null +++ b/WebSearch/MasterPage.master.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +public partial class MasterPage : System.Web.UI.MasterPage +{ + protected void Page_Load(object sender, EventArgs e) + { + + } +} diff --git a/WebSearch/web.config b/WebSearch/web.config new file mode 100644 index 0000000..9b99f5f --- /dev/null +++ b/WebSearch/web.config @@ -0,0 +1,127 @@ + + + + + + +
+ +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/LanSpider/App.config b/src/LanSpider/App.config new file mode 100644 index 0000000..d1556bd --- /dev/null +++ b/src/LanSpider/App.config @@ -0,0 +1,12 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/LanSpider/Database/Epam_Index.cs b/src/LanSpider/Database/Epam_Index.cs new file mode 100644 index 0000000..68b8263 --- /dev/null +++ b/src/LanSpider/Database/Epam_Index.cs @@ -0,0 +1,6 @@ +namespace LanSpider +{ + partial class Epam_IndexDataContext + { + } +} diff --git a/src/LanSpider/Database/Epam_Index.dbml b/src/LanSpider/Database/Epam_Index.dbml new file mode 100644 index 0000000..befc839 --- /dev/null +++ b/src/LanSpider/Database/Epam_Index.dbml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + +
+ + + + + + + + + + +
+
\ No newline at end of file diff --git a/src/LanSpider/Database/Epam_Index.dbml.layout b/src/LanSpider/Database/Epam_Index.dbml.layout new file mode 100644 index 0000000..2e530e8 --- /dev/null +++ b/src/LanSpider/Database/Epam_Index.dbml.layout @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/LanSpider/Database/Epam_Index.designer.cs b/src/LanSpider/Database/Epam_Index.designer.cs new file mode 100644 index 0000000..c5ee0b2 --- /dev/null +++ b/src/LanSpider/Database/Epam_Index.designer.cs @@ -0,0 +1,542 @@ +#pragma warning disable 1591 +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.3082 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace LanSpider.Database +{ + using System.Data.Linq; + using System.Data.Linq.Mapping; + using System.Data; + using System.Collections.Generic; + using System.Reflection; + using System.Linq; + using System.Linq.Expressions; + using System.ComponentModel; + using System; + + + [System.Data.Linq.Mapping.DatabaseAttribute(Name="EpamSharesIndex")] + public partial class Epam_IndexDataContext : System.Data.Linq.DataContext + { + + private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); + + #region Extensibility Method Definitions + partial void OnCreated(); + partial void Insertfile(file instance); + partial void Updatefile(file instance); + partial void Deletefile(file instance); + partial void Insertfolder(folder instance); + partial void Updatefolder(folder instance); + partial void Deletefolder(folder instance); + #endregion + + public Epam_IndexDataContext() : + base(global::LanSpider.Properties.Settings.Default.EpamSharesIndexConnectionString, mappingSource) + { + OnCreated(); + } + + public Epam_IndexDataContext(string connection) : + base(connection, mappingSource) + { + OnCreated(); + } + + public Epam_IndexDataContext(System.Data.IDbConnection connection) : + base(connection, mappingSource) + { + OnCreated(); + } + + public Epam_IndexDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : + base(connection, mappingSource) + { + OnCreated(); + } + + public Epam_IndexDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : + base(connection, mappingSource) + { + OnCreated(); + } + + public System.Data.Linq.Table files + { + get + { + return this.GetTable(); + } + } + + public System.Data.Linq.Table folders + { + get + { + return this.GetTable(); + } + } + } + + [Table(Name="dbo.[file]")] + public partial class file : INotifyPropertyChanging, INotifyPropertyChanged + { + + private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); + + private int _file_id; + + private int _parent_folder_id; + + private string _name; + + private string _type; + + private long _size; + + private System.DateTime _indexed_on; + + private EntityRef _folder; + + #region Extensibility Method Definitions + partial void OnLoaded(); + partial void OnValidate(System.Data.Linq.ChangeAction action); + partial void OnCreated(); + partial void Onfile_idChanging(int value); + partial void Onfile_idChanged(); + partial void Onparent_folder_idChanging(int value); + partial void Onparent_folder_idChanged(); + partial void OnnameChanging(string value); + partial void OnnameChanged(); + partial void OntypeChanging(string value); + partial void OntypeChanged(); + partial void OnsizeChanging(long value); + partial void OnsizeChanged(); + partial void Onindexed_onChanging(System.DateTime value); + partial void Onindexed_onChanged(); + #endregion + + public file() + { + this._folder = default(EntityRef); + OnCreated(); + } + + [Column(Storage="_file_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] + public int file_id + { + get + { + return this._file_id; + } + set + { + if ((this._file_id != value)) + { + this.Onfile_idChanging(value); + this.SendPropertyChanging(); + this._file_id = value; + this.SendPropertyChanged("file_id"); + this.Onfile_idChanged(); + } + } + } + + [Column(Storage="_parent_folder_id", DbType="Int NOT NULL")] + public int parent_folder_id + { + get + { + return this._parent_folder_id; + } + set + { + if ((this._parent_folder_id != value)) + { + if (this._folder.HasLoadedOrAssignedValue) + { + throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); + } + this.Onparent_folder_idChanging(value); + this.SendPropertyChanging(); + this._parent_folder_id = value; + this.SendPropertyChanged("parent_folder_id"); + this.Onparent_folder_idChanged(); + } + } + } + + [Column(Storage="_name", DbType="NVarChar(255)")] + public string name + { + get + { + return this._name; + } + set + { + if ((this._name != value)) + { + this.OnnameChanging(value); + this.SendPropertyChanging(); + this._name = value; + this.SendPropertyChanged("name"); + this.OnnameChanged(); + } + } + } + + [Column(Storage="_type", DbType="VarChar(250)")] + public string type + { + get + { + return this._type; + } + set + { + if ((this._type != value)) + { + this.OntypeChanging(value); + this.SendPropertyChanging(); + this._type = value; + this.SendPropertyChanged("type"); + this.OntypeChanged(); + } + } + } + + [Column(Storage="_size", DbType="BigInt NOT NULL")] + public long size + { + get + { + return this._size; + } + set + { + if ((this._size != value)) + { + this.OnsizeChanging(value); + this.SendPropertyChanging(); + this._size = value; + this.SendPropertyChanged("size"); + this.OnsizeChanged(); + } + } + } + + [Column(Storage="_indexed_on", DbType="DateTime NOT NULL")] + public System.DateTime indexed_on + { + get + { + return this._indexed_on; + } + set + { + if ((this._indexed_on != value)) + { + this.Onindexed_onChanging(value); + this.SendPropertyChanging(); + this._indexed_on = value; + this.SendPropertyChanged("indexed_on"); + this.Onindexed_onChanged(); + } + } + } + + [Association(Name="folder_file", Storage="_folder", ThisKey="parent_folder_id", OtherKey="folder_id", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")] + public folder folder + { + get + { + return this._folder.Entity; + } + set + { + folder previousValue = this._folder.Entity; + if (((previousValue != value) + || (this._folder.HasLoadedOrAssignedValue == false))) + { + this.SendPropertyChanging(); + if ((previousValue != null)) + { + this._folder.Entity = null; + previousValue.files.Remove(this); + } + this._folder.Entity = value; + if ((value != null)) + { + value.files.Add(this); + this._parent_folder_id = value.folder_id; + } + else + { + this._parent_folder_id = default(int); + } + this.SendPropertyChanged("folder"); + } + } + } + + public event PropertyChangingEventHandler PropertyChanging; + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void SendPropertyChanging() + { + if ((this.PropertyChanging != null)) + { + this.PropertyChanging(this, emptyChangingEventArgs); + } + } + + protected virtual void SendPropertyChanged(String propertyName) + { + if ((this.PropertyChanged != null)) + { + this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } + } + + [Table(Name="dbo.folder")] + public partial class folder : INotifyPropertyChanging, INotifyPropertyChanged + { + + private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); + + private int _folder_id; + + private System.Nullable _parent_id; + + private string _path; + + private System.DateTime _indexed_on; + + private EntitySet _files; + + private EntitySet _folders; + + private EntityRef _folder1; + + #region Extensibility Method Definitions + partial void OnLoaded(); + partial void OnValidate(System.Data.Linq.ChangeAction action); + partial void OnCreated(); + partial void Onfolder_idChanging(int value); + partial void Onfolder_idChanged(); + partial void Onparent_idChanging(System.Nullable value); + partial void Onparent_idChanged(); + partial void OnpathChanging(string value); + partial void OnpathChanged(); + partial void Onindexed_onChanging(System.DateTime value); + partial void Onindexed_onChanged(); + #endregion + + public folder() + { + this._files = new EntitySet(new Action(this.attach_files), new Action(this.detach_files)); + this._folders = new EntitySet(new Action(this.attach_folders), new Action(this.detach_folders)); + this._folder1 = default(EntityRef); + OnCreated(); + } + + [Column(Storage="_folder_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] + public int folder_id + { + get + { + return this._folder_id; + } + set + { + if ((this._folder_id != value)) + { + this.Onfolder_idChanging(value); + this.SendPropertyChanging(); + this._folder_id = value; + this.SendPropertyChanged("folder_id"); + this.Onfolder_idChanged(); + } + } + } + + [Column(Storage="_parent_id", DbType="Int")] + public System.Nullable parent_id + { + get + { + return this._parent_id; + } + set + { + if ((this._parent_id != value)) + { + if (this._folder1.HasLoadedOrAssignedValue) + { + throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); + } + this.Onparent_idChanging(value); + this.SendPropertyChanging(); + this._parent_id = value; + this.SendPropertyChanged("parent_id"); + this.Onparent_idChanged(); + } + } + } + + [Column(Storage="_path", DbType="NVarChar(266) NOT NULL", CanBeNull=false)] + public string path + { + get + { + return this._path; + } + set + { + if ((this._path != value)) + { + this.OnpathChanging(value); + this.SendPropertyChanging(); + this._path = value; + this.SendPropertyChanged("path"); + this.OnpathChanged(); + } + } + } + + [Column(Storage="_indexed_on", DbType="DateTime NOT NULL")] + public System.DateTime indexed_on + { + get + { + return this._indexed_on; + } + set + { + if ((this._indexed_on != value)) + { + this.Onindexed_onChanging(value); + this.SendPropertyChanging(); + this._indexed_on = value; + this.SendPropertyChanged("indexed_on"); + this.Onindexed_onChanged(); + } + } + } + + [Association(Name="folder_file", Storage="_files", ThisKey="folder_id", OtherKey="parent_folder_id")] + public EntitySet files + { + get + { + return this._files; + } + set + { + this._files.Assign(value); + } + } + + [Association(Name="folder_folder", Storage="_folders", ThisKey="folder_id", OtherKey="parent_id")] + public EntitySet folders + { + get + { + return this._folders; + } + set + { + this._folders.Assign(value); + } + } + + [Association(Name="folder_folder", Storage="_folder1", ThisKey="parent_id", OtherKey="folder_id", IsForeignKey=true)] + public folder folder1 + { + get + { + return this._folder1.Entity; + } + set + { + folder previousValue = this._folder1.Entity; + if (((previousValue != value) + || (this._folder1.HasLoadedOrAssignedValue == false))) + { + this.SendPropertyChanging(); + if ((previousValue != null)) + { + this._folder1.Entity = null; + previousValue.folders.Remove(this); + } + this._folder1.Entity = value; + if ((value != null)) + { + value.folders.Add(this); + this._parent_id = value.folder_id; + } + else + { + this._parent_id = default(Nullable); + } + this.SendPropertyChanged("folder1"); + } + } + } + + public event PropertyChangingEventHandler PropertyChanging; + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void SendPropertyChanging() + { + if ((this.PropertyChanging != null)) + { + this.PropertyChanging(this, emptyChangingEventArgs); + } + } + + protected virtual void SendPropertyChanged(String propertyName) + { + if ((this.PropertyChanged != null)) + { + this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } + + private void attach_files(file entity) + { + this.SendPropertyChanging(); + entity.folder = this; + } + + private void detach_files(file entity) + { + this.SendPropertyChanging(); + entity.folder = null; + } + + private void attach_folders(folder entity) + { + this.SendPropertyChanging(); + entity.folder1 = this; + } + + private void detach_folders(folder entity) + { + this.SendPropertyChanging(); + entity.folder1 = null; + } + } +} +#pragma warning restore 1591 diff --git a/src/LanSpider/Database/Manager.cs b/src/LanSpider/Database/Manager.cs index 9698fb4..5666d49 100644 --- a/src/LanSpider/Database/Manager.cs +++ b/src/LanSpider/Database/Manager.cs @@ -1,18 +1,24 @@ using System; using System.Collections.Generic; using System.Data; -using System.Data.SQLite; + using System.IO; using Extensions; using LanSpider.Properties; +using System.Diagnostics; +using System.Data.SqlClient; +using System.Configuration; namespace LanSpider.Database { public class Manager : IDisposable { - private readonly SQLiteConnection _conn; + private readonly SqlConnection _conn; + + private readonly Epam_IndexDataContext _context; + private readonly int _rootId = 1; public int RootId @@ -25,7 +31,7 @@ namespace LanSpider.Database #region Static schema methods - private static bool SchemaExists( SQLiteConnection connection ) + /*private static bool SchemaExists( SQLiteConnection connection ) { SQLiteCommand cmd = new SQLiteCommand( null, connection ); cmd.CommandText = "SELECT name FROM sqlite_master WHERE name=@table;"; @@ -42,18 +48,21 @@ namespace LanSpider.Database cmd.CommandText = Resources.create_db; cmd.ExecuteNonQuery(); } + */ #endregion Static schema methods - public Manager( string dbFileName ) + public Manager() { - _conn = new SQLiteConnection( String.Format( "Data Source={0};", dbFileName ) ); + _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString); _conn.Open(); + //_context = new Epam_IndexDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString); + // TO DO: - if ( !SchemaExists( _conn ) ) + /*if ( !SchemaExists( _conn ) ) { CreateSchema( _conn ); - } + }*/ } public void Dispose() @@ -65,18 +74,17 @@ namespace LanSpider.Database { List roots = new List(); - using ( SQLiteCommand cmd = new SQLiteCommand( _conn ) ) + using (SqlCommand cmd = new SqlCommand("SELECT path FROM folder WHERE parent_id = @root_id AND parent_id != folder_id", _conn)) { cmd.CommandType = CommandType.Text; - cmd.CommandText = "SELECT path FROM folder WHERE parent_id = ? AND parent_id != folder_id;"; - cmd.Parameters.Add( new SQLiteParameter( "@root_id", _rootId ) ); + cmd.Parameters.Add(new SqlParameter("root_id", _rootId)); - using ( SQLiteDataReader results = cmd.ExecuteReader() ) + using (SqlDataReader results = cmd.ExecuteReader()) { - while ( results.Read() ) + while (results.Read()) { - string x = (string)results.GetValue( 0 ); - roots.Add( x ); + string x = (string)results.GetValue(0); + roots.Add(x); } } } @@ -84,105 +92,166 @@ namespace LanSpider.Database return roots; } - private void IndexFile( int shareId, FileInfo file ) + private void IndexFile(int shareId, FileInfo file) { - IndexFile( shareId, file, null ); + IndexFile(shareId, file, null); } - private void IndexFile( int shareId, FileInfo file, SQLiteTransaction trans ) + private void IndexFile(int shareId, FileInfo file, DataTable dt) { - using ( SQLiteCommand cmd = new SQLiteCommand( _conn ) ) + DataRow row = dt.NewRow(); + row["parent_folder_id"] = shareId; + row["name"] = file.Name; + row["type"] = file.Extension; + try + { + row["size"] = file.Length; + } + catch (FileNotFoundException) + { + row["size"] = -1; + } + dt.Rows.Add(row); + /*using ( SqlCommand cmd = new SqlCommand(@"INSERT INTO ""file"" (parent_folder_id, name, type, size) VALUES (@parent_folder_id, @name, @type, @size)", _conn ) ) { if ( trans != null ) { cmd.Transaction = trans; } - cmd.CommandType = CommandType.Text; - cmd.CommandText = "INSERT OR REPLACE INTO file(parent_folder_id, name, type, size) VALUES (?, ?, ?, ?)"; - - cmd.Parameters.Add( new SQLiteParameter( "parent_folder_id", shareId ) ); - cmd.Parameters.Add( new SQLiteParameter( "name", file.Name ) ); - cmd.Parameters.Add( new SQLiteParameter( "type", file.Extension ) ); + cmd.CommandType = CommandType.Text; + cmd.Parameters.Add( new SqlParameter( "parent_folder_id", shareId ) ); + cmd.Parameters.Add(new SqlParameter("name", file.Name)); + cmd.Parameters.Add(new SqlParameter("type", file.Extension)); try { - cmd.Parameters.Add( new SQLiteParameter( "size", file.Length ) ); + cmd.Parameters.Add(new SqlParameter("size", file.Length)); } catch ( FileNotFoundException ) { - cmd.Parameters.Add( new SQLiteParameter( "size", -1 ) ); + cmd.Parameters.Add(new SqlParameter("size", -1)); } cmd.ExecuteNonQuery(); - } + }*/ } - private int GetShareId( string share ) + private int GetShareId(string share) { - return GetShareId( share, null ); + return GetShareId(share, null); } - private int GetShareId( string share, SQLiteTransaction trans ) + private int GetShareId(string share, SqlTransaction trans) { int shareId = 0; - using ( SQLiteCommand cmd = new SQLiteCommand( _conn ) ) + using (SqlCommand cmd = new SqlCommand("SELECT folder_id FROM folder WHERE path=@path;", _conn)) { - if ( trans != null ) + if (trans != null) { cmd.Transaction = trans; } cmd.CommandType = CommandType.Text; - cmd.CommandText = "SELECT folder_id FROM folder WHERE path=?;"; - cmd.Parameters.Add( new SQLiteParameter( "path", share ) ); + cmd.Parameters.Add(new SqlParameter("path", share)); object val = cmd.ExecuteScalar(); - if ( val == null || val == DBNull.Value ) + if (val == null || val == DBNull.Value) { int parentId; - if ( share.Count( '\\' ) == 3 ) // top level share + if (share.Count('\\') == 3) // top level share { parentId = RootId; } else { - int index = share.LastIndexOf( '\\' ); - string parentName = share.Substring( 0, index ); - parentId = GetShareId( parentName ); + int index = share.LastIndexOf('\\'); + string parentName = share.Substring(0, index); + parentId = GetShareId(parentName, trans); } - - cmd.CommandText = "INSERT INTO folder(path, parent_id) VALUES (?, ?);"; - cmd.Parameters.Add( new SQLiteParameter( "parent_id", parentId ) ); + cmd.CommandType = CommandType.StoredProcedure; + cmd.CommandText = "AddFolder"; + cmd.Parameters.Add(new SqlParameter("parent_id", parentId)); + cmd.Parameters.Add(new SqlParameter("folder_id", SqlDbType.Int)).Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); - - cmd.Parameters.Clear(); - cmd.CommandText = "SELECT last_insert_rowid();"; - shareId = (int) (long) cmd.ExecuteScalar(); + shareId = (int)cmd.Parameters["folder_id"].Value; } + else + return (int)val; } return shareId; } - public void SaveShareStructure( string shareName, IEnumerable fileInfos ) + public void SaveShareStructure(string shareName, IEnumerable fileInfos) { - SQLiteTransaction trans = _conn.BeginTransaction(); + SqlTransaction trans = _conn.BeginTransaction(); try { - foreach ( FileInfo fileInfo in fileInfos ) + SaveShareStructure(shareName, fileInfos, trans); + trans.Commit(); + } + catch (Exception ex) + { + trans.Rollback(); + Trace.WriteLine("Share " + shareName + " will not be saved: " + ex.Message); + Console.Error.WriteLine("Share " + shareName + " will not be saved: " + ex.Message); + //throw; + } + } + + public void SaveShareStructure(string shareName, IEnumerable fileInfos, SqlTransaction trans) + { + using (DataTable dt = new DataTable("[File]")) + { + SqlBulkCopy bulk = new SqlBulkCopy(_conn, SqlBulkCopyOptions.Default, trans); + bulk.DestinationTableName = dt.TableName; + bulk.BatchSize = 1000; + dt.Columns.Add(new DataColumn("file_id", typeof(int))); + dt.Columns.Add(new DataColumn("parent_folder_id", typeof(int))); + dt.Columns.Add(new DataColumn("name", typeof(string))); + dt.Columns.Add(new DataColumn("type", typeof(string))); + dt.Columns.Add(new DataColumn("size", typeof(long))); + + foreach (FileInfo fileInfo in fileInfos) { - int shareId = GetShareId( fileInfo.DirectoryName, trans ); - IndexFile( shareId, fileInfo ); + // TO DO: GetShare as SP + int shareId = GetShareId(fileInfo.DirectoryName, trans); + IndexFile(shareId, fileInfo, dt); + } + bulk.WriteToServer(dt); + dt.Clear(); + GC.Collect(); + } + } + + public void SaveKnownShareStructure(string shareName, IEnumerable fileInfos) + { + SqlTransaction trans = _conn.BeginTransaction(); + + try + { + using (var cmd = new SqlCommand(@"DELETE FROM [FILE] WHERE parent_folder_id in (SELECT folder_id FROM Folder WHERE path like @share +'\%' or path = @share)", _conn)) + { + cmd.CommandType = CommandType.Text; + cmd.Transaction = trans; + cmd.CommandTimeout = 0; + cmd.Parameters.Add(new SqlParameter("share", shareName)); + cmd.ExecuteNonQuery(); + //cmd.CommandText = @"DELETE FROM FOLDER WHERE folder_id in (select folder_id from FOLDER WHERE PATH like @share + '\%')"; + //cmd.ExecuteNonQuery(); } + SaveShareStructure(shareName, fileInfos, trans); trans.Commit(); } - catch(Exception) + catch (Exception ex) { trans.Rollback(); + Trace.WriteLine("Share " + shareName + " will not be saved: " + ex.Message); + Console.Error.WriteLine("Share " + shareName + " will not be saved: " + ex.Message); //throw; } } diff --git a/src/LanSpider/LanSpider.cs b/src/LanSpider/LanSpider.cs index 339577c..6a78195 100644 --- a/src/LanSpider/LanSpider.cs +++ b/src/LanSpider/LanSpider.cs @@ -20,12 +20,14 @@ namespace LanSpider private List _computers; private List _knownShares; private List _unknownShares; + private List _availableShares; + private List _knownAvailableShares; private readonly Manager _dbman; - public LanSpider( INetworkBrowser browser ) + public LanSpider(INetworkBrowser browser) { _browser = browser; - _dbman = new Manager( "index.db3" ); + _dbman = new Manager(); } #region Private functions @@ -33,7 +35,7 @@ namespace LanSpider private List DiscoverNetworkComputers() { var x = _browser.GetNetworkComputers(); - List result = new List( x ); + List result = new List(x); return result; } @@ -41,31 +43,45 @@ namespace LanSpider #region Methods - private List GetUnknownShares() + private List GetAvailableShares() { Consumer> consumer = new Consumer>(); - Console.Write( "Indexing network computers ..." ); + Console.Write("Indexing network computers ..."); DateTime start = DateTime.Now; WorkManager> manager - = new WorkManager>( _browser.GetShares, consumer.Consume ); + = new WorkManager>(_browser.GetShares, consumer.Consume); manager.WorkProgressChanged += OnShareDiscoveryProgressChanged; - manager.RunSync( Computers ); + manager.RunSync(Computers); TimeSpan duration = DateTime.Now - start; - Console.WriteLine( "\rIndexing network computers done in {0}:{1}.", (int) duration.TotalMinutes, - duration.Seconds ); + Console.WriteLine("\rIndexing network computers done in {0}:{1}.", (int)duration.TotalMinutes, + duration.Seconds); - IEnumerable unknownShares + /*IEnumerable unknownShares = from bunch in consumer.Results from share in bunch - where !KnownShares.Contains( share ) - select share; + where !KnownShares.Contains(share) + select share;*/ + Console.WriteLine(consumer.Results.Count()); + IEnumerable availableShares + = from bunch in consumer.Results + from share in bunch + select share; + return new List(availableShares); + } - return new List( unknownShares ); + private List GetUnknownShares() + { + return (from share in _availableShares where !KnownShares.Contains(share) select share).ToList(); + } + + private List GetKnownAvailableShares() + { + return (from share in _availableShares where KnownShares.Contains(share) select share).ToList(); } #endregion Methods @@ -76,7 +92,7 @@ namespace LanSpider { get { - if ( _computers == null ) + if (_computers == null) { _computers = DiscoverNetworkComputers(); } @@ -89,7 +105,7 @@ namespace LanSpider { get { - if ( _knownShares == null ) + if (_knownShares == null) { _knownShares = _dbman.GetRootFolders(); } @@ -102,7 +118,7 @@ namespace LanSpider { get { - if ( _unknownShares == null ) + if (_unknownShares == null) { _unknownShares = GetUnknownShares(); } @@ -111,6 +127,32 @@ namespace LanSpider } } + private List KnownAvailableShares + { + get + { + if (_knownAvailableShares == null) + { + _knownAvailableShares = GetKnownAvailableShares(); + } + return _knownAvailableShares; + } + } + + private List AvailableShares + { + get + { + if (_availableShares == null) + { + _availableShares = GetAvailableShares(); + } + + return _availableShares; + } + } + + #endregion Private properties #region Public properties @@ -140,11 +182,11 @@ namespace LanSpider /// /// Gets the number of unknown shares (those shares to index). /// - public int NumUnknownShares + public int NumAvailableShares { get { - return UnknownShares.Count; + return AvailableShares.Count; } } @@ -159,59 +201,78 @@ namespace LanSpider #endregion - public void IndexUnknownShares() + private void IndexShares(List shares) { WorkManager> manager - = new WorkManager>( IndexShare, SaveShareIndex ); + = new WorkManager>(IndexShare, SaveShareIndex); manager.WorkProgressChanged += OnShareIndexProgressChanged; - manager.RunSync( UnknownShares ); + manager.RunSync(shares); } - private void SaveShareIndex( IEnumerable shareIndex ) + public void IndexUnknownShares() { - if ( shareIndex.Count() > 0 ) - { - FileInfo file = shareIndex.First(); + IndexShares(UnknownShares); + } + + public void IndexKnownAvailableShares() + { + IndexShares(KnownAvailableShares); + } - int lastIndex = 0; - for ( int i = 0; i < 3; ++i ) + private void SaveShareIndex(IEnumerable shareIndex) + { + using (Manager dbman = new Manager()) + { + if (shareIndex.Count() > 0) { - lastIndex = file.FullName.IndexOf( '\\', lastIndex + 1 ); - } + FileInfo file = shareIndex.First(); - string shareName = file.FullName.Substring( 0, lastIndex ); + int lastIndex = 0; + for (int i = 0; i < 3; ++i) + { + lastIndex = file.FullName.IndexOf('\\', lastIndex + 1); + } - _dbman.SaveShareStructure( shareName, shareIndex ); + string shareName = file.FullName.Substring(0, lastIndex); + if (KnownShares.Contains(shareName)) + { + dbman.SaveKnownShareStructure(shareName, shareIndex); + } + else + { + dbman.SaveShareStructure(shareName, shareIndex); + } + } } } - private IEnumerable IndexShare( string shareName ) + private IEnumerable IndexShare(string shareName) { List listing = new List(); - if ( shareName.Count( '\\' ) >= 3 ) + if (shareName.Count('\\') >= 3) { try { - foreach ( string directory in Directory.GetDirectories( shareName ) ) + foreach (string directory in Directory.GetDirectories(shareName)) { - listing.AddRange( IndexShare( directory ) ); + listing.AddRange(IndexShare(directory)); } - foreach ( string fileName in Directory.GetFiles( shareName ) ) + foreach (string fileName in Directory.GetFiles(shareName)) { - listing.Add( new FileInfo( fileName ) ); + listing.Add(new FileInfo(fileName)); } } - catch ( UnauthorizedAccessException ex ) + catch (UnauthorizedAccessException ex) { - Console.Error.WriteLine( "Access denied to {0}", shareName ); + Console.Error.WriteLine("Access denied to {0}", shareName); } - catch ( IOException ex ) + catch (IOException ex) { - Console.Error.WriteLine( "Cannot read {0}: {1}", shareName, ex.Message ); + Console.Error.WriteLine("Cannot read {0}: {1}", shareName, ex.Message); } } @@ -220,17 +281,17 @@ namespace LanSpider #region Event handlers - private static void OnShareDiscoveryProgressChanged( object sender, ProgressChangedEventArgs e ) + private static void OnShareDiscoveryProgressChanged(object sender, ProgressChangedEventArgs e) { - Console.Write( "\rIndexing network computers: {0}%", e.ProgressPercentage ); + Console.Write("\rIndexing network computers: {0}%", e.ProgressPercentage); } - private static void OnShareIndexProgressChanged( object sender, ProgressChangedEventArgs e ) + private static void OnShareIndexProgressChanged(object sender, ProgressChangedEventArgs e) { - string progress = "\rIndexing network shares: {0}%, ".Fmt( e.ProgressPercentage ); - string infoFormat = "{{0,-{0}}}".Fmt( Console.WindowWidth - progress.Length - 1 ); + string progress = "\rIndexing network shares: {0}%, ".Fmt(e.ProgressPercentage); + string infoFormat = "{{0,-{0}}}".Fmt(Console.WindowWidth - progress.Length - 1); - Console.Write( progress + infoFormat, e.UserState ); + Console.Write(progress + infoFormat, e.UserState); } #endregion Event handlers diff --git a/src/LanSpider/LanSpider.csproj b/src/LanSpider/LanSpider.csproj index 40acd03..fcb1d04 100644 --- a/src/LanSpider/LanSpider.csproj +++ b/src/LanSpider/LanSpider.csproj @@ -3,7 +3,7 @@ Debug AnyCPU - 9.0.21022 + 9.0.30729 2.0 {E7913104-ABCA-4A62-A5AE-050184E9BB7C} Exe @@ -33,6 +33,7 @@ + 3.5 @@ -61,6 +62,14 @@ + + Epam_Index.dbml + + + True + True + Epam_Index.dbml + @@ -73,6 +82,9 @@ + + Epam_Index.dbml + ResXFileCodeGenerator Designer @@ -94,6 +106,12 @@ + + + MSLinqToSQLGenerator + Epam_Index.designer.cs + Designer + diff --git a/src/LanSpider/Multithreading/WorkManager.cs b/src/LanSpider/Multithreading/WorkManager.cs index 254c9c7..2da6852 100644 --- a/src/LanSpider/Multithreading/WorkManager.cs +++ b/src/LanSpider/Multithreading/WorkManager.cs @@ -92,11 +92,12 @@ namespace LanSpider.Multithreading } } - lock ( _outputHandle ) + // TO DO: lock ( _outputHandle ) { _consumerMethod( result ); } } } + } } \ No newline at end of file diff --git a/src/LanSpider/Program.cs b/src/LanSpider/Program.cs index 773635c..409550f 100644 --- a/src/LanSpider/Program.cs +++ b/src/LanSpider/Program.cs @@ -12,12 +12,20 @@ namespace LanSpider { using (LanSpider spider = new LanSpider(NetworkBrowser.Instance)) { + try + { + Console.WriteLine("Found {0} computers in network.", spider.NumComputers); + Console.WriteLine("{0} shares known.", spider.NumKnownShares); + Console.WriteLine("Have {0} shares to index.", spider.NumAvailableShares); - Console.WriteLine( "Found {0} computers in network.", spider.NumComputers ); - Console.WriteLine( "{0} shares known.", spider.NumKnownShares ); - Console.WriteLine( "Have {0} shares to index.", spider.NumUnknownShares ); - - spider.IndexUnknownShares(); + spider.IndexUnknownShares(); + spider.IndexKnownAvailableShares(); + } + catch (Exception ex) + { + Console.WriteLine("{0}", ex); + Console.ReadKey(); + } } } } diff --git a/src/LanSpider/Properties/Settings.Designer.cs b/src/LanSpider/Properties/Settings.Designer.cs index 83d240c..15ea23f 100644 --- a/src/LanSpider/Properties/Settings.Designer.cs +++ b/src/LanSpider/Properties/Settings.Designer.cs @@ -8,23 +8,30 @@ // //------------------------------------------------------------------------------ -namespace LanSpider.Properties -{ - - +namespace LanSpider.Properties { + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute( "Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0" )] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase - { - - private static Settings defaultInstance = ( (Settings)( global::System.Configuration.ApplicationSettingsBase.Synchronized( new Settings() ) ) ); - - public static Settings Default - { - get - { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { return defaultInstance; } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)] + [global::System.Configuration.DefaultSettingValueAttribute("Data Source=10.17.33.200;Initial Catalog=EpamSharesIndex;Persist Security Info=Tr" + + "ue;User ID=esi;Password=esi__esi")] + public string EpamSharesIndexConnectionString { + get { + return ((string)(this["EpamSharesIndexConnectionString"])); + } + } } } diff --git a/src/LanSpider/Properties/Settings.settings b/src/LanSpider/Properties/Settings.settings index abf36c5..9747e70 100644 --- a/src/LanSpider/Properties/Settings.settings +++ b/src/LanSpider/Properties/Settings.settings @@ -1,7 +1,14 @@  - - - - - - + + + + + <?xml version="1.0" encoding="utf-16"?> +<SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + <ConnectionString>Data Source=10.17.33.200;Initial Catalog=EpamSharesIndex;Persist Security Info=True;User ID=esi;Password=esi__esi</ConnectionString> + <ProviderName>System.Data.SqlClient</ProviderName> +</SerializableConnectionString> + Data Source=10.17.33.200;Initial Catalog=EpamSharesIndex;Persist Security Info=True;User ID=esi;Password=esi__esi + + + \ No newline at end of file diff --git a/src/LanSpider/Resources/create_db.sql b/src/LanSpider/Resources/create_db.sql dissimilarity index 63% index ac1748c..544a064 100644 --- a/src/LanSpider/Resources/create_db.sql +++ b/src/LanSpider/Resources/create_db.sql @@ -1,27 +1,32 @@ -CREATE TABLE IF NOT EXISTS [folder] ( - [folder_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - [parent_id] INTEGER NOT NULL, - [path] NVARCHAR(266) UNIQUE NOT NULL, - [indexed_on] TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - - CONSTRAINT FK_FOLDER_PARENT_FOLDER_ID_TO_FOLDER_FOLDER_ID - FOREIGN KEY (folder_id) REFERENCES folder(folder_id) ON DELETE CASCADE, - - CONSTRAINT IDX_FOLDER_PATH - UNIQUE (path) -); - -CREATE TABLE IF NOT EXISTS [file] ( - [file_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - [parent_folder_id] INTEGER NOT NULL, - [name] NVARCHAR(255), - [type] VARCHAR(8), - [size] INTEGER NOT NULL, - [indexed_on] TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - - CONSTRAINT FK_FILE_PARENT_FOLDER_ID_TO_FOLDER_FOLDER_ID - FOREIGN KEY (parent_folder_id) REFERENCES folder(folder_id) ON DELETE CASCADE -); - -INSERT OR IGNORE INTO folder (folder_id, parent_id, path) - VALUES (1, 1, 'root'); +CREATE TABLE "folder" ( + [folder_id] INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), + [parent_id] INTEGER, + [path] NVARCHAR(266) UNIQUE NOT NULL, + [indexed_on] DATETIME DEFAULT GETDATE() NOT NULL, + + CONSTRAINT FK_FOLDER_PARENT_FOLDER_ID_TO_FOLDER_FOLDER_ID + FOREIGN KEY (parent_id) REFERENCES folder(folder_id) ON DELETE NO ACTION, + + CONSTRAINT IDX_FOLDER_PATH + UNIQUE (path) +); + +CREATE TABLE "file" ( + [file_id] INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), + [parent_folder_id] INTEGER NOT NULL, + [name] NVARCHAR(255), + [type] VARCHAR(250), + [size] BIGINT NOT NULL, + [indexed_on] DATETIME DEFAULT GETDATE() NOT NULL, + + CONSTRAINT FK_FILE_PARENT_FOLDER_ID_TO_FOLDER_FOLDER_ID + FOREIGN KEY (parent_folder_id) REFERENCES folder(folder_id) ON DELETE CASCADE, + + CONSTRAINT IDX_NAME_PARENT_FOLDER_ID + UNIQUE (parent_folder_id, name) +); + +SET IDENTITY_INSERT FOLDER ON; + +INSERT INTO folder (folder_id, parent_id, path) + VALUES (1, null, 'root'); \ No newline at end of file -- 2.11.4.GIT