Example #1
0
        public override void Close()
        {
            _fileMutex.WaitOne();
            try
            {
                string fileName = _name;

                // make sure it's all written out
                _indexOutput.Flush();

                long originalLength = _indexOutput.Length();
                _indexOutput.Close();

                Stream blobStream;

                // optionally put a compressor around the blob stream
                if (_azureDirectory.ShouldCompressFile(_name))
                {
                    blobStream = CompressStream(fileName, originalLength);
                }
                else
                {
                    blobStream = new StreamInput(CacheDirectory.OpenInput(fileName));
                }

                try
                {
                    // push the blobStream up to the cloud
                    _blob.UploadFromStream(blobStream);

                    // set the metadata with the original index file properties
                    _blob.Metadata["CachedLength"]       = originalLength.ToString();
                    _blob.Metadata["CachedLastModified"] = CacheDirectory.FileModified(fileName).ToString();
                    _blob.SetMetadata();
#if FULLDEBUG
                    Trace.WriteLine($"PUT {blobStream.Length} bytes to {_name} in cloud");
#endif
                }
                finally
                {
                    blobStream.Dispose();
                }

#if FULLDEBUG
                Trace.WriteLine($"CLOSED WRITESTREAM {_name}");
#endif
                // clean up
                _indexOutput   = null;
                _blobContainer = null;
                _blob          = null;
                GC.SuppressFinalize(this);
            }
            finally
            {
                _fileMutex.ReleaseMutex();
            }
        }
Example #2
0
 public override long Length()
 {
     return(_cacheDirIndexOutput.Length());
 }
Example #3
0
 public override long Length()
 {
     return(_indexOutput.Length());
 }
Example #4
0
        /// <summary>
        /// Closes the sql index output.
        /// </summary>
        public override void Close()
        {
            string fileName = _fileName;

            // make sure it's all written out
            _indexOutput.Flush();

            long originalLength = _indexOutput.Length();

            _indexOutput.Close();

            Stream fileStream = new StreamInput(CacheDirectory.OpenInput(fileName));

            try {
                // push the file stream up to the db.
                ICommandBuilder builder     = _sqlStorageProviderUtility.GetCommandBuilder2();
                DbConnection    connection  = builder.GetConnection(_connString);
                DbTransaction   transaction = _sqlStorageProviderUtility.BeginTransaction(connection);

                QueryBuilder queryBuilder = new QueryBuilder(builder);

                //bool fileExists = FileExists(transaction, _wiki, _fileName);

                // To achieve decent performance, an UPDATE query is issued if the file exists,
                // otherwise an INSERT query is issued

                string           query;
                List <Parameter> parameters;

                byte[] fileData = null;
                int    size     = Tools.ReadStream(fileStream, ref fileData, MaxFileSize);
                if (size < 0)
                {
                    _sqlStorageProviderUtility.RollbackTransaction(transaction);
                    throw new ArgumentException("Source Stream contains too much data", "sourceStream");
                }

                //if(fileExists) {
                //    query = queryBuilder.Update("SearchIndex", new string[] { "Size", "LastModified", "Data" }, new string[] { "Size", "LastModified", "Data" });
                //    query = queryBuilder.Where(query, "Wiki", WhereOperator.Equals, "Wiki");
                //    query = queryBuilder.AndWhere(query, "Name", WhereOperator.Equals, "Name");

                //    parameters = new List<Parameter>(5);
                //    parameters.Add(new Parameter(ParameterType.String, "Wiki", _wiki));
                //    parameters.Add(new Parameter(ParameterType.Int64, "Size", (long)originalLength));
                //    parameters.Add(new Parameter(ParameterType.DateTime, "LastModified", DateTime.Now.ToUniversalTime()));
                //    parameters.Add(new Parameter(ParameterType.ByteArray, "Data", fileData));
                //    parameters.Add(new Parameter(ParameterType.String, "Name", _fileName));
                //}
                //else {
                query = queryBuilder.InsertInto("SearchIndex", new string[] { "Wiki", "Name", "Size", "LastModified", "Data" },
                                                new string[] { "Wiki", "Name", "Size", "LastModified", "Data" });

                parameters = new List <Parameter>(5);
                parameters.Add(new Parameter(ParameterType.String, "Wiki", _wiki));
                parameters.Add(new Parameter(ParameterType.String, "Name", _fileName));
                parameters.Add(new Parameter(ParameterType.Int64, "Size", (long)originalLength));
                parameters.Add(new Parameter(ParameterType.DateTime, "LastModified", DateTime.Now.ToUniversalTime()));
                parameters.Add(new Parameter(ParameterType.ByteArray, "Data", fileData));
                //}

                DbCommand command = builder.GetCommand(transaction, query, parameters);

                int rows = _sqlStorageProviderUtility.ExecuteNonQuery(command, false);
                if (rows == 1)
                {
                    _sqlStorageProviderUtility.CommitTransaction(transaction);
                }
                else
                {
                    _sqlStorageProviderUtility.RollbackTransaction(transaction);
                }
            }
            finally {
                fileStream.Dispose();
            }

            // clean up
            _indexOutput = null;
            GC.SuppressFinalize(this);
        }
Example #5
0
        public override void Close()
        {
            _fileMutex.WaitOne();
            try
            {
                string fileName = _name;

                // make sure it's all written out
                _indexOutput.Flush();

                long originalLength = _indexOutput.Length();
                _indexOutput.Close();

                Stream blobStream;
#if COMPRESSBLOBS
                // optionally put a compressor around the blob stream
                if (_azureDirectory.ShouldCompressFile(_name))
                {
                    // unfortunately, deflate stream doesn't allow seek, and we need a seekable stream
                    // to pass to the blob storage stuff, so we compress into a memory stream
                    MemoryStream compressedStream = new MemoryStream();

                    try
                    {
                        IndexInput indexInput = CacheDirectory.OpenInput(fileName);
                        using (DeflateStream compressor = new DeflateStream(compressedStream, CompressionMode.Compress, true))
                        {
                            // compress to compressedOutputStream
                            byte[] bytes = new byte[indexInput.Length()];
                            indexInput.ReadBytes(bytes, 0, (int)bytes.Length);
                            compressor.Write(bytes, 0, (int)bytes.Length);
                        }
                        indexInput.Close();

                        // seek back to beginning of comrpessed stream
                        compressedStream.Seek(0, SeekOrigin.Begin);

                        Debug.WriteLine(string.Format("COMPRESSED {0} -> {1} {2}% to {3}",
                                                      originalLength,
                                                      compressedStream.Length,
                                                      ((float)compressedStream.Length / (float)originalLength) * 100,
                                                      _name));
                    }
                    catch
                    {
                        // release the compressed stream resources if an error occurs
                        compressedStream.Dispose();
                        throw;
                    }

                    blobStream = compressedStream;
                }
                else
#endif
                {
                    blobStream = new StreamInput(CacheDirectory.OpenInput(fileName));
                }

                try
                {
                    // push the blobStream up to the cloud
                    _blob.UploadFromStream(blobStream);

                    // set the metadata with the original index file properties
                    _blob.Metadata["CachedLength"]       = originalLength.ToString();
                    _blob.Metadata["CachedLastModified"] = CacheDirectory.FileModified(fileName).ToString();
                    _blob.SetMetadata();

                    Debug.WriteLine(string.Format("PUT {1} bytes to {0} in cloud", _name, blobStream.Length));
                }
                finally
                {
                    blobStream.Dispose();
                }

#if FULLDEBUG
                Debug.WriteLine(string.Format("CLOSED WRITESTREAM {0}", _name));
#endif
                // clean up
                _indexOutput   = null;
                _blobContainer = null;
                _blob          = null;
                GC.SuppressFinalize(this);
            }
            finally
            {
                _fileMutex.ReleaseMutex();
            }
        }