示例#1
0
        /// <summary>Replaces or installs a database from a file.</summary>
        /// <remarks>
        /// Replaces or installs a database from a file.
        /// This is primarily used to install a canned database on first launch of an app, in which case
        /// you should first check .exists to avoid replacing the database if it exists already. The
        /// canned database would have been copied into your app at build time.
        /// </remarks>
        /// <param name="name">The name of the target Database to replace or create.</param>
        /// <param name="databaseStream">Stream on the source Database file.</param>
        /// <param name="attachmentStreams">
        /// Map of the associated source Attachments, or null if there are no attachments.
        /// The Map key is the name of the attachment, the map value is an InputStream for
        /// the attachment contents. If you wish to control the order that the attachments
        /// will be processed, use a LinkedHashMap, SortedMap or similar and the iteration order
        /// will be honoured.
        /// </param>
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public void ReplaceDatabase(String name, Stream databaseStream, IDictionary <String, Stream> attachmentStreams)
        {
            try {
                using (var database = GetDatabaseWithoutOpening(name, false)) {
                    var dstAttachmentsPath = database.AttachmentStorePath;

                    using (var destStream = File.OpenWrite(database.Path)) {
                        databaseStream.CopyTo(destStream);
                    }

                    UpgradeDatabase(new FileInfo(database.Path));

                    if (System.IO.Directory.Exists(dstAttachmentsPath))
                    {
                        System.IO.Directory.Delete(dstAttachmentsPath, true);
                    }
                    System.IO.Directory.CreateDirectory(dstAttachmentsPath);

                    var attachmentsFile = new FilePath(dstAttachmentsPath);

                    if (attachmentStreams != null)
                    {
                        StreamUtils.CopyStreamsToFolder(attachmentStreams, attachmentsFile);
                    }
                    database.Open();
                }
            } catch (Exception e) {
                Log.E(Database.TAG, string.Empty, e);
                throw new CouchbaseLiteException(StatusCode.InternalServerError);
            }
        }
示例#2
0
 /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
 private void ReplaceDatabase(string databaseName, InputStream databaseStream, IEnumerator
                              <KeyValuePair <string, InputStream> > attachmentStreams)
 {
     try
     {
         Database     database           = GetDatabase(databaseName);
         string       dstAttachmentsPath = database.GetAttachmentStorePath();
         OutputStream destStream         = new FileOutputStream(new FilePath(database.GetPath()));
         StreamUtils.CopyStream(databaseStream, destStream);
         FilePath attachmentsFile = new FilePath(dstAttachmentsPath);
         FileDirUtils.DeleteRecursive(attachmentsFile);
         attachmentsFile.Mkdirs();
         if (attachmentStreams != null)
         {
             StreamUtils.CopyStreamsToFolder(attachmentStreams, attachmentsFile);
         }
         database.Open();
         database.ReplaceUUIDs();
     }
     catch (FileNotFoundException e)
     {
         Log.E(Database.Tag, string.Empty, e);
         throw new CouchbaseLiteException(Status.InternalServerError);
     }
     catch (IOException e)
     {
         Log.E(Database.Tag, string.Empty, e);
         throw new CouchbaseLiteException(Status.InternalServerError);
     }
 }
        /// <summary>Replaces or installs a database from a file.</summary>
        /// <remarks>
        /// Replaces or installs a database from a file.
        /// This is primarily used to install a canned database on first launch of an app, in which case
        /// you should first check .exists to avoid replacing the database if it exists already. The
        /// canned database would have been copied into your app bundle at build time.
        /// </remarks>
        /// <param name="databaseName">The name of the target Database to replace or create.</param>
        /// <param name="databaseStream">InputStream on the source Database file.</param>
        /// <param name="attachmentStreams">
        /// Map of the associated source Attachments, or null if there are no attachments.
        /// The Map key is the name of the attachment, the map value is an InputStream for
        /// the attachment contents. If you wish to control the order that the attachments
        /// will be processed, use a LinkedHashMap, SortedMap or similar and the iteration order
        /// will be honoured.
        /// </param>
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public void ReplaceDatabase(String name, Stream databaseStream, IDictionary <String, Stream> attachmentStreams)
        {
            var result = true;

            try {
                var database           = GetDatabase(name);
                var dstAttachmentsPath = database.AttachmentStorePath;

                var destStream = File.OpenWrite(database.Path);
                databaseStream.CopyTo(destStream);

                var dstAttachmentsDirectory = new DirectoryInfo(dstAttachmentsPath);
                //FileDirUtils.DeleteRecursive(attachmentsFile);
                System.IO.Directory.Delete(dstAttachmentsPath, true);
                dstAttachmentsDirectory.Create();

                var attachmentsFile = new FilePath(dstAttachmentsPath);

                if (attachmentStreams != null)
                {
                    StreamUtils.CopyStreamsToFolder(attachmentStreams, attachmentsFile);
                }

                database.ReplaceUUIDs();
            } catch (Exception e) {
                Log.E(Database.Tag, string.Empty, e);
                throw new CouchbaseLiteException(StatusCode.InternalServerError);
            }
        }
示例#4
0
        public void ReplaceDatabase(string name, Stream databaseStream, IDictionary <string, Stream> attachmentStreams)
        {
            if (name == null)
            {
                Log.To.Database.E(TAG, "name cannot be null in ReplaceDatabase, throwing...");
                throw new ArgumentNullException("name");
            }

            if (databaseStream == null)
            {
                Log.To.Database.E(TAG, "databaseStream cannot be null in ReplaceDatabase, throwing...");
                throw new ArgumentNullException("databaseStream");
            }

            try {
                var tempPath = Path.Combine(Path.GetTempPath(), name + "-upgrade");
                if (System.IO.Directory.Exists(tempPath))
                {
                    System.IO.Directory.Delete(tempPath, true);
                }

                System.IO.Directory.CreateDirectory(tempPath);
                var fileStream = File.OpenWrite(Path.Combine(tempPath, name + DatabaseSuffixv1));
                databaseStream.CopyTo(fileStream);
                fileStream.Dispose();
                var success = UpgradeDatabase(new FileInfo(Path.Combine(tempPath, name + DatabaseSuffixv1)));
                if (!success)
                {
                    Log.To.Upgrade.W(TAG, "Unable to replace database (upgrade failed)");
                    System.IO.Directory.Delete(tempPath, true);
                    return;
                }

                System.IO.Directory.Delete(tempPath, true);

                var db = GetDatabase(name, true);
                var attachmentsPath = db.AttachmentStorePath;
                if (attachmentStreams != null)
                {
                    StreamUtils.CopyStreamsToFolder(attachmentStreams, attachmentsPath);
                }
            } catch (Exception e) {
                throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG, "Error replacing database");
            }
        }
        public void ReplaceDatabase(string name, Stream databaseStream, IDictionary <string, Stream> attachmentStreams)
        {
            try {
                var tempPath = Path.Combine(Path.GetTempPath(), name + "-upgrade");
                if (System.IO.Directory.Exists(tempPath))
                {
                    System.IO.Directory.Delete(tempPath, true);
                }

                System.IO.Directory.CreateDirectory(tempPath);
                var fileStream = File.OpenWrite(Path.Combine(tempPath, name + DatabaseSuffixv1));
                databaseStream.CopyTo(fileStream);
                fileStream.Dispose();
                var success = UpgradeDatabase(new FileInfo(Path.Combine(tempPath, name + DatabaseSuffixv1)));
                if (!success)
                {
                    Log.E(TAG, "Unable to replace database (upgrade failed)");
                    System.IO.Directory.Delete(tempPath, true);
                    return;
                }

                System.IO.Directory.Delete(tempPath, true);

                var db = GetDatabaseWithoutOpening(name, true);
                var attachmentsPath = db.AttachmentStorePath;
                if (attachmentStreams != null)
                {
                    StreamUtils.CopyStreamsToFolder(attachmentStreams, new FilePath(attachmentsPath));
                }
            } catch (Exception e) {
                Log.E(Database.TAG, string.Empty, e);
                throw new CouchbaseLiteException("Error upgrading database", e)
                      {
                          Code = StatusCode.Exception
                      };
            }
        }