public Task <BinaryCacheEntity> LoadBinaryCacheEntityAsync(int versionId, int propertyTypeId, SnDataContext dataContext)
        {
            var db        = DataProvider.DB;
            var binaryDoc =
                db.BinaryProperties.FirstOrDefault(r => r.VersionId == versionId && r.PropertyTypeId == propertyTypeId);

            if (binaryDoc == null)
            {
                return(null);
            }

            var fileDoc = db.Files.FirstOrDefault(x => x.FileId == binaryDoc.FileId);

            if (fileDoc == null)
            {
                return(null);
            }
            if (fileDoc.Staging)
            {
                return(null);
            }

            var length           = fileDoc.Size;
            var binaryPropertyId = binaryDoc.BinaryPropertyId;
            var fileId           = fileDoc.FileId;

            var providerName     = fileDoc.BlobProvider;
            var providerTextData = fileDoc.BlobProviderData;

            var rawData = fileDoc.Buffer;

            var provider = BlobStorageBase.GetProvider(providerName);
            var context  = new BlobStorageContext(provider, providerTextData)
            {
                VersionId      = versionId,
                PropertyTypeId = propertyTypeId,
                FileId         = fileId,
                Length         = length,
            };

            var result = new BinaryCacheEntity
            {
                Length           = length,
                RawData          = rawData,
                BinaryPropertyId = binaryPropertyId,
                FileId           = fileId,
                Context          = context
            };

            return(STT.Task.FromResult(result));
        }
示例#2
0
        internal static Stream GetBinaryStream(int nodeId, int versionId, int propertyTypeId)
        {
            BinaryCacheEntity binaryCacheEntity = null;

            if (TransactionScope.IsActive)
            {
                binaryCacheEntity = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId);
                return(new SnStream(binaryCacheEntity.Context, binaryCacheEntity.RawData));
            }

            // Try to load cached binary entity
            var cacheKey = BinaryCacheEntity.GetCacheKey(versionId, propertyTypeId);

            binaryCacheEntity = (BinaryCacheEntity)DistributedApplication.Cache.Get(cacheKey);
            if (binaryCacheEntity == null)
            {
                // Not in cache, load it from the database
                binaryCacheEntity = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId);

                // insert the binary cache entity into the
                // cache only if we know the node id
                if (binaryCacheEntity != null && nodeId != 0)
                {
                    if (!RepositoryEnvironment.WorkingMode.Populating)
                    {
                        var head = NodeHead.Get(nodeId);
                        DistributedApplication.Cache.Insert(cacheKey, binaryCacheEntity,
                                                            CacheDependencyFactory.CreateBinaryDataDependency(nodeId, head.Path, head.NodeTypeId));
                    }
                }
            }

            // Not found even in the database
            if (binaryCacheEntity == null)
            {
                return(null);
            }
            if (binaryCacheEntity.Length == -1)
            {
                return(null);
            }
            return(new SnStream(binaryCacheEntity.Context, binaryCacheEntity.RawData));
        }
示例#3
0
        internal static Stream GetBinaryStream2(int nodeId, int versionId, int propertyTypeId)
        {
            FileStreamData fileStreamData = null;

            if (TransactionScope.IsActive)
            {
                // Aktív tranzakció hekk
                var bce = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId, out fileStreamData);

                //If we received the necessary information for serving file stream, use that. If filestream data
                //is null, we MUST NOT use SqlFileStream here, because the data is in the database.
                if (bce.UseFileStream && fileStreamData != null)
                {
                    return(new ContentRepository.Storage.Data.SqlFileStream(bce.Length, bce.BinaryPropertyId, fileStreamData));
                }

                return(bce.RawData == null
                    ? new RepositoryStream(bce.Length, bce.BinaryPropertyId)
                    : new RepositoryStream(bce.BinaryPropertyId, bce.Length, bce.RawData));
            }

            // Try to load cached binary entity
            var cacheKey          = BinaryCacheEntity.GetCacheKey(versionId, propertyTypeId);
            var binaryCacheEntity = (BinaryCacheEntity)DistributedApplication.Cache.Get(cacheKey);

            if (binaryCacheEntity == null)
            {
                //Not in cache, load it from the database
                binaryCacheEntity = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId, out fileStreamData);

                //insert the binary cache entity into the
                //cache only if we know the node id
                if (binaryCacheEntity != null && nodeId != 0)
                {
                    if (!RepositoryConfiguration.WorkingMode.Populating)
                    {
                        DistributedApplication.Cache.Insert(cacheKey, binaryCacheEntity, new NodeIdDependency(nodeId));
                    }
                }
            }

            //Not found even in the database
            if (binaryCacheEntity == null)
            {
                return(null);
            }
            if (binaryCacheEntity.Length == -1)
            {
                return(null);
            }

            //We can use SqlFilestream to load the binary from the filesystem.
            //Transaction is handled inside the SqlFileStream.
            if (binaryCacheEntity.UseFileStream)
            {
                return(new ContentRepository.Storage.Data.SqlFileStream(binaryCacheEntity.Length, binaryCacheEntity.BinaryPropertyId));
            }

            var stream = binaryCacheEntity.RawData == null
               ? new RepositoryStream(binaryCacheEntity.Length, binaryCacheEntity.BinaryPropertyId)
               : new RepositoryStream(binaryCacheEntity.BinaryPropertyId, binaryCacheEntity.Length, binaryCacheEntity.RawData);

            return(stream);
        }