Streamed implementation. Limited input buffer.
[LanSpider.git] / src / AltSpider / AltModel.cs
blob6e0ef08a37ec058e31477a106511e17bf9b05c62
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Common;
5 using System.Diagnostics;
7 using MySql.Data.MySqlClient;
9 namespace AltSpider
11 public class AltModel : IDisposable
13 private readonly string _connectionString;
14 private readonly DbConnection _connection;
16 public AltModel()
18 DbConnectionStringBuilder dbcsb
19 = new DbConnectionStringBuilder
21 { "Data Source", "localhost" },
22 { "User ID", "root" },
23 { "Database", "share_index" },
24 { "CharSet", "utf8" }
27 _connectionString = dbcsb.ConnectionString;
29 _connection = new MySqlConnection( _connectionString );
30 _connection.Open();
33 /// <summary>
34 /// Saves given path to the database.
35 /// </summary>
36 /// <param name="path"></param>
37 /// <returns>Leaf node ID</returns>
38 /// <exception cref="ApplicationException">Thrown if saving failed.</exception>
39 public int SavePath( string path )
41 IEnumerable<string> getTokens = GetTokens( path );
43 int parentId = 1;
45 foreach ( string token in getTokens )
47 parentId = SaveNode( token, parentId );
48 if ( parentId == -1 )
50 throw new ApplicationException( String.Format( "Saving node {0} of {1} failed.", token, path ) );
54 return parentId;
57 /// <summary>
58 /// Adds new node to the if does not exist in the database otherwise, loads the node.
59 /// </summary>
60 /// <param name="nodeName"></param>
61 /// <param name="parentNodeId"></param>
62 /// <returns>Child node ID</returns>
63 private int SaveNode( string nodeName, int parentNodeId )
65 DbCommand cmd = _connection.CreateCommand();
66 cmd.CommandText = "proc_save_node";
67 cmd.CommandType = CommandType.StoredProcedure;
69 DbParameter nameParam = cmd.CreateParameter();
70 nameParam.Direction = ParameterDirection.Input;
71 nameParam.ParameterName = "$name";
72 nameParam.Value = nodeName;
74 DbParameter parendIdParam = cmd.CreateParameter();
75 parendIdParam.Direction = ParameterDirection.Input;
76 parendIdParam.ParameterName = "$parent_id";
77 parendIdParam.Value = parentNodeId;
79 DbParameter childIdParam = cmd.CreateParameter();
80 childIdParam.Direction = ParameterDirection.Output;
81 childIdParam.ParameterName = "$child_node_id";
83 cmd.Parameters.AddRange( new[] { nameParam, parendIdParam, childIdParam } );
85 cmd.ExecuteNonQuery();
87 int childNodeId = -1;
88 if ( childIdParam.Value != null && childIdParam.Value != DBNull.Value )
90 childNodeId = (int) childIdParam.Value;
93 cmd.Dispose();
95 return childNodeId;
98 /// <summary>
99 /// Split given path into separate tokens (directory and file names).
100 /// </summary>
101 /// <param name="path"></param>
102 /// <returns></returns>
103 private static IEnumerable<string> GetTokens( string path )
105 string[] patrs = path.Substring( 1 ).Split( new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries );
106 patrs[ 0 ] = path[ 0 ] + patrs[ 0 ];
107 return patrs;
110 public void Dispose()
112 if ( _connection.State != ConnectionState.Closed )
116 _connection.Close();
118 catch ( DbException ex )
120 Trace.WriteLine( ex.Message, "Closing connection" );
125 /// <summary>
126 /// Save file info into the database.
127 /// </summary>
128 /// <param name="nodeId">File node id</param>
129 /// <param name="size">File size in bytes</param>
130 /// <param name="ctime">File creation time</param>
131 /// <param name="mtime">File's last modification time</param>
132 /// <param name="atime">File's last access time</param>
133 public void SaveFileInfo( int nodeId, long size, DateTime ctime, DateTime mtime, DateTime atime )
135 DbCommand cmd = _connection.CreateCommand();
136 cmd.CommandText = "proc_save_file_info";
137 cmd.CommandType = CommandType.StoredProcedure;
139 DbParameter idParam = cmd.CreateParameter();
140 idParam.Direction = ParameterDirection.Input;
141 idParam.ParameterName = "$id";
142 idParam.Value = nodeId;
144 DbParameter sizeParam = cmd.CreateParameter();
145 sizeParam.Direction = ParameterDirection.Input;
146 sizeParam.ParameterName = "$size";
147 sizeParam.Value = size;
149 DbParameter ctimeParam = cmd.CreateParameter();
150 ctimeParam.Direction = ParameterDirection.Input;
151 ctimeParam.ParameterName = "$ctime";
152 ctimeParam.Value = ctime;
154 DbParameter mtimeParam = cmd.CreateParameter();
155 mtimeParam.Direction = ParameterDirection.Input;
156 mtimeParam.ParameterName = "$mtime";
157 mtimeParam.Value = mtime;
159 DbParameter atimeParam = cmd.CreateParameter();
160 atimeParam.Direction = ParameterDirection.Input;
161 atimeParam.ParameterName = "$atime";
162 atimeParam.Value = atime;
164 cmd.Parameters.AddRange( new[] { idParam, sizeParam, ctimeParam, mtimeParam, atimeParam } );
166 cmd.ExecuteNonQuery();
167 cmd.Dispose();
170 /// <summary>
171 /// Get list of shares stored in the database.
172 /// </summary>
173 /// <returns>List of shares</returns>
174 public List<string> GetShares()
176 List<string> shares = new List<string>();
178 DbCommand cmd = _connection.CreateCommand();
179 cmd.CommandText = "SELECT path FROM view_shares";
180 cmd.CommandType = CommandType.Text;
182 DbDataReader reader = cmd.ExecuteReader();
183 if ( reader.HasRows )
185 while ( reader.Read() )
187 shares.Add( reader[ "path" ] as string );
190 reader.Close();
191 cmd.Dispose();
193 return shares;