예제 #1
0
 private bool CompleteTransaction(StandardObjectStoreTransaction transaction, bool abort)
 {
     if (transaction == null)
         throw new Exception();
     lock (transaction)
     {
         if (!abort)
         {
             try
             {
                 BlobDatabase.BeginTransaction();
                 ObjectDatabase.BeginTransaction();
                 foreach (var x in transaction.PendingTransactions)
                 {
                     try
                     {
                         if (ObjectDatabase.InsertSafe(x.Data))
                         {
                             long? blobID = null;
                             if (x.Filename != null)
                             {
                                 string fn = Path.Combine(TempFolder.FullName, x.Filename);
                                 var info = new FileInfo(fn);
                                 if (info.Length < 16 * 1024)
                                 {
                                     MemoryStream ms = new MemoryStream();
                                     using (var fsi = info.OpenRead())
                                     {
                                         fsi.CopyTo(ms);
                                     }
                                     Blobject obj = new Blobject() { Data = ms.ToArray() };
                                     if (!BlobDatabase.InsertSafe(obj))
                                         throw new Exception();
                                     blobID = obj.Id;
                                     Blobsize bs = new Blobsize() { BlobID = obj.Id, Length = obj.Data.Length };
                                     if (!BlobDatabase.InsertSafe(bs))
                                         throw new Exception();
                                 }
                                 if (blobID.HasValue)
                                 {
                                     x.Data.BlobID = blobID;
                                     ObjectDatabase.Update(x.Data);
                                     transaction.Cleanup.Add(x.Filename);
                                 }
                                 else
                                 {
                                     if (!GetFileForDataID(x.Data.Lookup).Exists)
                                     {
                                         if (!System.IO.File.Exists(fn))
                                             throw new Exception();
                                         System.IO.File.Move(fn, GetFileForDataID(x.Data.Lookup).FullName);
                                     }
                                 }
                             }
                             else
                             {
                                 if (x.Payload.Length < 16 * 1024)
                                 {
                                     Blobject obj = new Blobject() { Data = x.Payload };
                                     if (!BlobDatabase.InsertSafe(obj))
                                         throw new Exception();
                                     blobID = obj.Id;
                                     Blobsize bs = new Blobsize() { BlobID = obj.Id, Length = obj.Data.Length };
                                     if (!BlobDatabase.InsertSafe(bs))
                                         throw new Exception();
                                 }
                                 if (blobID.HasValue)
                                 {
                                     x.Data.BlobID = blobID;
                                     ObjectDatabase.Update(x.Data);
                                 }
                                 else
                                 {
                                     var info = GetFileForDataID(x.Data.Lookup);
                                     if (!info.Exists)
                                     {
                                         using (var stream = info.OpenWrite())
                                             stream.Write(x.Payload, 0, x.Payload.Length);
                                     }
                                 }
                             }
                         }
                     }
                     catch (SQLite.SQLiteException ex)
                     {
                         if (ex.Result != SQLite.SQLite3.Result.Constraint)
                             throw ex;
                     }
                 }
                 transaction.PendingTransactions.Clear();
                 BlobDatabase.Commit();
                 ObjectDatabase.Commit();
                 transaction.m_PendingBytes = 0;
                 transaction.m_PendingCount = 0;
             }
             catch
             {
                 BlobDatabase.Rollback();
                 ObjectDatabase.Rollback();
                 throw;
             }
         }
         foreach (var x in transaction.Cleanup)
         {
             string fn = Path.Combine(TempFolder.FullName, x);
             if (System.IO.File.Exists(fn))
                 System.IO.File.Delete(fn);
             TempFiles.Remove(x);
         }
         transaction.Cleanup.Clear();
     }
     return true;
 }
예제 #2
0
        public override bool Open(Area owner)
        {
            Owner = owner;
            if (!DataFolder.Exists)
            {
                DataFolder.Create();
            }
            ObjectDatabase = new SQLite.SQLiteConnection(DataFile.FullName, SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.FullMutex | SQLite.SQLiteOpenFlags.ReadWrite);
            BlobDatabase = new SQLite.SQLiteConnection(BlobFile.FullName, SQLite.SQLiteOpenFlags.FullMutex | SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.ReadWrite);
            InitializeDBTypes();

            var version = ObjectDatabase.Table<StandardObjectStoreMetadata>().FirstOrDefault();
            if (version == null)
            {
                ObjectDatabase.BeginExclusive();
                Printer.PrintMessage("Upgrading object store database...");
                var records = owner.GetAllRecords();
                foreach (var x in records)
                {
                    ImportRecordFromFlatStore(x);
                }
                var meta = new StandardObjectStoreMetadata();
                meta.Version = 2;
                ObjectDatabase.InsertSafe(meta);
                ObjectDatabase.Commit();
            }
            else if (version.Version < 3)
            {
                Printer.PrintMessage("Upgrading object store database...");
                foreach (var x in BlobDatabase.Table<Blobject>())
                {
                    Blobsize bs = new Blobsize() { BlobID = x.Id, Length = x.Data.Length };
                    BlobDatabase.Insert(bs);
                }
                ObjectDatabase.BeginExclusive();
                ObjectDatabase.DropTable<StandardObjectStoreMetadata>();
                ObjectDatabase.CreateTable<StandardObjectStoreMetadata>();
                var meta = new StandardObjectStoreMetadata();
                meta.Version = 3;
                ObjectDatabase.InsertSafe(meta);
                ObjectDatabase.Commit();
            }
            return true;
        }