예제 #1
0
        internal void Install([NotNull] Database db)
        {
            Debug.Assert(db != null);

            if (Digest != null && _db == null)
            {
                _db = db;
            }

            if (_db != null)
            {
                if (db != _db)
                {
                    throw new InvalidOperationException(CouchbaseLiteErrorMessage.BlobDifferentDatabase);
                }

                return;
            }

            var       store = db.BlobStore;
            C4BlobKey key;

            if (_content != null)
            {
                LiteCoreBridge.Check(err =>
                {
                    C4BlobKey tmpKey;
                    var s = Native.c4blob_create(store, _content, null, &tmpKey, err);
                    key   = tmpKey;
                    return(s);
                });
            }
            else
            {
                if (_initialContentStream == null)
                {
                    throw new InvalidOperationException(CouchbaseLiteErrorMessage.BlobContentNull);
                }

                Length = 0;
                var contentStream = _initialContentStream;
                using (var blobOut = new BlobWriteStream(store)) {
                    contentStream.CopyTo(blobOut, ReadBufferSize);
                    blobOut.Flush();
                    Length = blobOut.Length > Int32.MaxValue ? 0 : (int)blobOut.Length;
                    key    = blobOut.Key;
                }

                _initialContentStream.Dispose();
                _initialContentStream = null;
            }

            Digest = Native.c4blob_keyToString(key);
            _db    = db;
        }
예제 #2
0
        private void Install([NotNull] Database db)
        {
            Debug.Assert(db != null);

            if (_db != null)
            {
                if (db != _db)
                {
                    throw new InvalidOperationException("Blob belongs to a different database");
                }

                return;
            }

            var       store = db.BlobStore;
            C4BlobKey key;

            if (_content != null)
            {
                LiteCoreBridge.Check(err =>
                {
                    C4BlobKey tmpKey;
                    var s = Native.c4blob_create(store, _content, null, &tmpKey, err);
                    key   = tmpKey;
                    return(s);
                });
            }
            else
            {
                if (_initialContentStream == null)
                {
                    throw new InvalidOperationException("No data available to write for install");
                }

                Length = 0;
                var contentStream = _initialContentStream;
                using (var blobOut = new BlobWriteStream(store)) {
                    contentStream.CopyTo(blobOut, ReadBufferSize);
                    blobOut.Flush();
                    key = blobOut.Key;
                }

                _initialContentStream.Dispose();
                _initialContentStream = null;
            }

            Digest = Native.c4blob_keyToString(key);
            _db    = db;
        }
예제 #3
0
        public unsafe void TestBlobStream()
        {
            byte[] bytes = null;
            using (var stream = typeof(BlobTest).GetTypeInfo().Assembly.GetManifestResourceStream("iTunesMusicLibrary.json"))
                using (var sr = new BinaryReader(stream)) {
                    bytes = sr.ReadBytes((int)stream.Length);
                }

            C4BlobKey key;
            long      tmp;

            using (var stream = new BlobWriteStream(Db.BlobStore)) {
                stream.CanSeek.Should().BeFalse();
                stream.Invoking(s => tmp        = s.Length).ShouldThrow <NotSupportedException>();
                stream.Invoking(s => tmp        = s.Position).ShouldThrow <NotSupportedException>();
                stream.Invoking(s => s.Position = 10).ShouldThrow <NotSupportedException>();
                stream.Invoking(s => s.Read(bytes, 0, bytes.Length)).ShouldThrow <NotSupportedException>();
                stream.Invoking(s => s.SetLength(100)).ShouldThrow <NotSupportedException>();
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
                key = stream.Key;
                var canread = stream.CanRead;
            }

            using (var stream = new BlobReadStream(Db.BlobStore, key)) {
                stream.Invoking(s => s.SetLength(100)).ShouldThrow <NotSupportedException>();
                stream.CanRead.Should().BeTrue();
                stream.CanSeek.Should().BeTrue();
                stream.CanWrite.Should().BeFalse();
                stream.Length.Should().Be(bytes.Length);
                stream.Position.Should().Be(0);
                stream.Seek(2, SeekOrigin.Begin);
                stream.Position.Should().Be(2);
                stream.ReadByte().Should().Be(bytes[2]);
                stream.Seek(1, SeekOrigin.Current);
                stream.Position.Should().Be(4); // ReadByte advanced the stream
                stream.ReadByte().Should().Be(bytes[4]);
                stream.Position = 0;
                stream.Seek(-2, SeekOrigin.End);
                stream.Position.Should().Be(bytes.Length - 2); // ReadByte advanced the stream
                stream.ReadByte().Should().Be(bytes[bytes.Length - 2]);
                stream.Flush();
            }
        }
예제 #4
0
        public unsafe void TestBlobStreamCopyTo()
        {
            byte[] bytes = null;
            using (var stream = typeof(BlobTest).GetTypeInfo().Assembly.GetManifestResourceStream("iTunesMusicLibrary.json"))
                using (var sr = new BinaryReader(stream)) {
                    bytes = sr.ReadBytes((int)stream.Length);
                }

            C4BlobKey key;

            using (var stream = typeof(BlobTest).GetTypeInfo().Assembly
                                .GetManifestResourceStream("iTunesMusicLibrary.json")) {
                using (var writeStream = new BlobWriteStream(Db.BlobStore)) {
                    stream.CopyTo(writeStream);
                    writeStream.Flush();
                    key = writeStream.Key;
                }
            }

            using (var stream = new BlobReadStream(Db.BlobStore, key)) {
                stream.Length.Should().Be(bytes.Length);
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            IntPtr       userData = new IntPtr(0);
            string       clipID;
            BinaryReader myReader;
            BinaryWriter myWriter;
            BlobStream   myStream;

            // For thsi test we are using a file as the input. *Any* stream could be used.
            // This would be your test PDF - in a file or memory stream.
            FileStream inputStream = new FileStream("e:\\testfile", FileMode.Open);

            FPLogger.ConsoleMessage("\nInput file length " + inputStream.Length);

            try
            {
                FPLogger log = new FPLogger();
                log.LogPath = "C:\\GenStreamsLog.txt";
                log.Start();

                // Create a test clip demonstrating how to write a blob using a stream
                FPPool myPool = new FPPool("cse1.centera.lab.emc.com");
                FPClip myClip = myPool.ClipCreate("GenericStreamWrite_testClip");
                FPTag  myTag  = myClip.AddTag("testTag");

                // Simple test of the BlobWriteStream - write contents of a local file
                // to Centera using a StreamReader and StreamWriter
                // We are using a temporary MemoryStream for our staging area
                myStream = new BlobWriteStream(myTag);
                myReader = new BinaryReader(inputStream);
                myWriter = new BinaryWriter(myStream);

                long writeTime1 = DateTime.Now.Ticks;

                myWriter.Write(myReader.ReadBytes((int)inputStream.Length));
                myWriter.Flush();
                ((BlobWriteStream)myStream).Close(); // If we don't do this it will stay open forever with keep alives!

                writeTime1 = DateTime.Now.Ticks - writeTime1;

                // If we assign values to existing FP objects we must explicitly close
                // them first to avoid potential "Object In Use" errors on future closure
                myTag.Close();

                myTag = myClip.AddTag("SlicedBlob");

                inputStream.Seek(0, SeekOrigin.Begin);
                long writeTime = DateTime.Now.Ticks;

                SlicedBlobWriter.Instance(inputStream, myTag, 8);

                writeTime = DateTime.Now.Ticks - writeTime;
                myTag.Close();

                clipID = myClip.Write();
                myClip.Close();

                FPLogger.ConsoleMessage("\nGenericStreamWrite test succeeded " + clipID);

                // Now we will test reading it back from the Centera			{
                myClip = myPool.ClipOpen(clipID, FPMisc.OPEN_ASTREE);
                myTag  = myClip.NextTag;

                // Here we create a stream to read back the blob
                long readTime1 = DateTime.Now.Ticks;
                myStream = new BlobReadStream(myTag);

                // This is only to test to verify when we read the content back it is the same as the
                // input file i.e. we are going to store it on the file system and then use file system tools
                // to compare the files
                myReader = new BinaryReader(myStream);
                myWriter = new BinaryWriter(new FileStream("c:\\testfile2.out", FileMode.Create));

                ((BlobReadStream)myStream).Join();
                myWriter.Write(myReader.ReadBytes((int)myTag.BlobSize));

                myWriter.Flush();

                myStream.Close();
                readTime1 = DateTime.Now.Ticks - readTime1;

                myTag.Close();

                // This shows how to read content back using multiple threads
                myTag = myClip.NextTag;
                long readTime = DateTime.Now.Ticks;
                SlicedBlobReader.Instance(new FileStream("c:\\testfile2.out.sliced", FileMode.Create), myTag, 8);
                readTime = DateTime.Now.Ticks - readTime;

                myTag.Close();
                myClip.Close();
                myPool.Close();

                FPLogger.ConsoleMessage("\nSerial tests - Write time: " + TimeSpan.FromTicks(writeTime1) + " Read time " + TimeSpan.FromTicks(readTime1));
                FPLogger.ConsoleMessage("\nMulti threaded tests - Write time: " + TimeSpan.FromTicks(writeTime) + " Read time " + TimeSpan.FromTicks(readTime));
            }
            catch (FPLibraryException e)
            {
                ErrorInfo err = e.errorInfo;
                FPLogger.ConsoleMessage("\nException thrown in FP Library: Error " + err.error + " " + err.message);
            }
        }