/// <summary> /// Get the lenght of the file. /// </summary> /// <param name="name">The name of the file.</param> /// <returns>The length of the file.</returns> public override long FileLength(string name) { ICommandBuilder builder = _sqlStorageProviderUtility.GetCommandBuilder2(); QueryBuilder queryBuilder = new QueryBuilder(builder); string query = queryBuilder.SelectFrom("SearchIndex", new string[] { "Size" }); query = queryBuilder.Where(query, "Wiki", WhereOperator.Equals, "Wiki"); query = queryBuilder.AndWhere(query, "Name", WhereOperator.Equals, "Name"); List <Parameter> parameters = new List <Parameter>(2); parameters.Add(new Parameter(ParameterType.String, "Wiki", _wiki)); parameters.Add(new Parameter(ParameterType.String, "Name", name)); DbCommand command = builder.GetCommand(_connString, query, parameters); DbDataReader reader = _sqlStorageProviderUtility.ExecuteReader(command); if (reader != null) { long fileLength = 0; if (reader.Read()) { fileLength = (long)reader["Size"]; } _sqlStorageProviderUtility.CloseReader(command, reader); return(fileLength); } else { return(0); } }
/// <summary> /// Determines whether this instance is locked. /// </summary> /// <returns><c>true</c> if this instance is locked; otherwise, <c>false</c>.</returns> public override bool IsLocked() { ICommandBuilder builder = _sqlStorageProviderUtility.GetCommandBuilder2(); QueryBuilder queryBuilder = new QueryBuilder(builder); string query = queryBuilder.SelectFrom("SearchIndexLock"); query = queryBuilder.Where(query, "Wiki", WhereOperator.Equals, "Wiki"); query = queryBuilder.AndWhere(query, "Name", WhereOperator.Equals, "Name"); List <Parameter> parameters = new List <Parameter>(2); parameters.Add(new Parameter(ParameterType.String, "Wiki", _wiki)); parameters.Add(new Parameter(ParameterType.String, "Name", _name)); DbCommand command = builder.GetCommand(_connString, query, parameters); DbDataReader reader = _sqlStorageProviderUtility.ExecuteReader(command); if (reader != null) { string result = null; if (reader.Read()) { result = reader["Value"] as string; } _sqlStorageProviderUtility.CloseReader(command, reader); return(result == "locked"); } else { return(false); } }
/// <summary> /// Initializes a new instance of the <see cref="SqlIndexInput"/> class. /// </summary> /// <param name="sqlServerDirectory">The Sql Server Directory object.</param> /// <param name="sqlStorageProviderUtility">The SQL storage provider utility.</param> /// <param name="connString">The connection string.</param> /// <param name="wiki">The wiki.</param> /// <param name="name">The name of the file.</param> public SqlIndexInput(SqlDirectory sqlServerDirectory, ISqlStorageProviderUtility sqlStorageProviderUtility, string connString, string wiki, string name) { _sqlServerDirectory = sqlServerDirectory; _sqlStorageProviderUtility = sqlStorageProviderUtility; bool fFileNeeded = false; if (!CacheDirectory.FileExists(name)) { fFileNeeded = true; } if (name.Contains("segments")) { fFileNeeded = true; } if (fFileNeeded) { StreamOutput fileStream = _sqlServerDirectory.CreateCachedOutputAsStream(name); ICommandBuilder builder = _sqlStorageProviderUtility.GetCommandBuilder2(); DbConnection connection = builder.GetConnection(connString); DbTransaction transaction = _sqlStorageProviderUtility.BeginTransaction(connection); if (!FileExists(transaction, wiki, name)) { _sqlStorageProviderUtility.RollbackTransaction(transaction); _sqlStorageProviderUtility.CloseDbConnection(connection); throw new FileNotFoundException(); } QueryBuilder queryBuilder = new QueryBuilder(builder); string query = queryBuilder.SelectFrom("SearchIndex", new string[] { "Size", "Data" }); query = queryBuilder.Where(query, "Wiki", WhereOperator.Equals, "Wiki"); query = queryBuilder.AndWhere(query, "Name", WhereOperator.Equals, "Name"); List <Parameter> parameters = new List <Parameter>(2); parameters.Add(new Parameter(ParameterType.String, "Wiki", wiki)); parameters.Add(new Parameter(ParameterType.String, "Name", name)); DbCommand command = builder.GetCommand(transaction, query, parameters); DbDataReader reader = _sqlStorageProviderUtility.ExecuteReader(command); if (reader != null) { bool done = false; if (reader.Read()) { int read = _sqlStorageProviderUtility.ReadBinaryColumn(reader, "Data", fileStream); done = (long)read == (long)reader["Size"]; } _sqlStorageProviderUtility.CloseReader(reader); if (!done) { _sqlStorageProviderUtility.RollbackTransaction(transaction); } _sqlStorageProviderUtility.CommitTransaction(transaction); } else { _sqlStorageProviderUtility.RollbackTransaction(transaction); } fileStream.Flush(); fileStream.Close(); // and open it as an input _indexInput = CacheDirectory.OpenInput(name); } else { _indexInput = CacheDirectory.OpenInput(name); } }