Ejemplo n.º 1
0
        public void SetContents(string path, HttpStatusCode status, string contentType, string etag, DateTime?modified, byte[] contents)
        {
            if (contents.Length == 0)
            {
                Console.Error.WriteLine("{0} - {1}  (Content is empty)", (int)status, path);
            }

            ContentRecord rec           = _data[path];
            ITransactable pendingUpdate = null;

            try
            {
                ContentRecord.Builder builder = rec.ToBuilder()
                                                .SetContentUri(path)
                                                .SetLastCrawled(CrawlTime)
                                                .SetLastValid(CrawlTime)
                                                .SetHttpStatus((uint)status)
                ;

                builder.ClearContentRedirect();
                builder.SetContentType(contentType);
                builder.SetContentLength((uint)contents.Length);
                if (!String.IsNullOrEmpty(etag))
                {
                    builder.SetETag(etag);
                }

                string hash = Hash.SHA256(contents).ToString();
                if (hash != builder.HashOriginal)
                {
                    Modified = true;
                    builder.SetHashOriginal(hash);
                    builder.SetDateModified(CrawlTime);
                    pendingUpdate = _data.WriteContent(builder, contents);
                }

                if (_data.AddOrUpdate(path, rec = builder.Build()))
                {
                    if (pendingUpdate != null)
                    {
                        pendingUpdate.Commit();
                        pendingUpdate.Dispose();
                        pendingUpdate = null;
                    }
                }
            }
            finally
            {
                if (pendingUpdate != null)
                {
                    pendingUpdate.Rollback();
                    pendingUpdate.Dispose();
                }
            }

            ProcessFileContent(rec, contents);
        }