Ejemplo n.º 1
0
		private void InitializeCommands()
		{
			m_moveBlockToNewVolumeCommand = m_connection.CreateCommand();
			
			m_moveBlockToNewVolumeCommand.CommandText = @"UPDATE ""Block"" SET ""VolumeID"" = ? WHERE ""Hash"" = ? AND ""Size"" = ?";
			m_moveBlockToNewVolumeCommand.AddParameters(3);
		}
Ejemplo n.º 2
0
 public AthleteRepository(System.Data.IDbCommand command)
 {
     if (command == null) {
     throw new System.ArgumentNullException("command", "command cannot be null");
      } else {
     this.command = command;
      }
 }
Ejemplo n.º 3
0
			public void Dispose()
			{
        		m_lookup = null;
				if (m_command != null)
					try { m_command.Dispose(); }
					finally { m_command = null; }
			}
Ejemplo n.º 4
0
            public DirectBlockMarker(System.Data.IDbConnection connection, string blocktablename, string filetablename, string statstablename)
            {
                m_insertblockCommand = connection.CreateCommand();
                m_resetfileCommand = connection.CreateCommand();
                m_updateAsRestoredCommand = connection.CreateCommand();
                m_updateFileAsDataVerifiedCommand = connection.CreateCommand();
                m_statUpdateCommand = connection.CreateCommand();

                m_insertblockCommand.Transaction = connection.BeginTransaction();
                m_resetfileCommand.Transaction = m_insertblockCommand.Transaction;
                m_updateAsRestoredCommand.Transaction = m_insertblockCommand.Transaction;
                m_updateFileAsDataVerifiedCommand.Transaction = m_insertblockCommand.Transaction;
                m_statUpdateCommand.Transaction = m_insertblockCommand.Transaction;

                m_blocktablename = blocktablename;
                m_filetablename = filetablename;

                m_insertblockCommand.CommandText = string.Format(
                      @"UPDATE ""{0}"" SET ""Restored"" = 1 "
                    + @" WHERE ""FileID"" = ? AND ""Index"" = ? AND ""Hash"" = ? AND ""Size"" = ? AND ""Metadata"" = ? AND ""Restored"" = 0 "
                    , m_blocktablename);
                m_insertblockCommand.AddParameters(5);

                m_resetfileCommand.CommandText = string.Format(
                      @"UPDATE ""{0}"" SET ""Restored"" = 0 WHERE ""FileID"" = ? "
                    , m_blocktablename);
                m_resetfileCommand.AddParameters(1);

                m_updateAsRestoredCommand.CommandText = string.Format(
                      @"UPDATE ""{0}"" SET ""Restored"" = 1 WHERE ""FileID"" = ? AND ""Metadata"" <= ? "
                    , m_blocktablename);
                m_updateAsRestoredCommand.AddParameters(2);

                m_updateFileAsDataVerifiedCommand.CommandText = string.Format(
                      @"UPDATE ""{0}"" SET ""DataVerified"" = 1 WHERE ""ID"" = ?"
                    , m_filetablename);
                m_updateFileAsDataVerifiedCommand.AddParameters(1); 

                if (statstablename != null)
                {
                    // Fields in Stats: TotalFiles, TotalBlocks, TotalSize
                    //                  FilesFullyRestored, FilesPartiallyRestored, BlocksRestored, SizeRestored
                    m_statUpdateCommand.CommandText = string.Format(@"SELECT SUM(""FilesFullyRestored""), SUM(""SizeRestored"") FROM ""{0}"" ", statstablename);
                }
                else // very slow fallback if stats tables were not created
                    m_statUpdateCommand.CommandText = string.Format(@"SELECT COUNT(DISTINCT ""FileID""), SUM(""Size"") FROM ""{0}"" WHERE ""Restored"" = 1 ", m_blocktablename);

            }
Ejemplo n.º 5
0
            public void Dispose()
            {
                if (m_insertPreviousElementCommand != null)
                {
                    try { m_insertPreviousElementCommand.Dispose(); }
                    catch {}
                    finally { m_insertPreviousElementCommand = null; }
                }

                if (m_insertCurrentElementCommand != null)
                {
                    try { m_insertCurrentElementCommand.Dispose(); }
                    catch {}
                    finally { m_insertCurrentElementCommand = null; }
                }
                
                if (m_transaction != null)
                {
                    try { m_transaction.Rollback(); }
                    catch {}
                    finally
                    {
                        m_previousTable = null;
                        m_currentTable= null;
                        m_transaction = null;
                    }
                }
            }
Ejemplo n.º 6
0
			public BlockQuery(System.Data.IDbConnection con, Options options, System.Data.IDbTransaction transaction)
			{
				m_command = con.CreateCommand();
				m_command.Transaction = transaction;
				
				if (options.BlockHashLookupMemory > 0)
				{
					m_lookup = new HashLookupHelper<long>((ulong)options.BlockHashLookupMemory);
					using(var reader = m_command.ExecuteReader(@"SELECT ""Hash"", ""Size"" FROM ""Block"" "))
					while (reader.Read())
					{
						var hash = reader.GetValue(0).ToString();
						var size = Convert.ToInt64(reader.GetValue(1));
						m_lookup.Add(hash, size, size);
					}
				}
				
				m_command.Parameters.Clear();
				m_command.CommandText = @"SELECT ""VolumeID"" FROM ""Block"" WHERE ""Hash"" = ? AND ""Size"" = ? ";
				m_command.AddParameters(2);
			}
Ejemplo n.º 7
0
            public void Dispose()
            {
                if (m_tablename != null)
                {
                    try
                    {
                        using(var cmd = m_connection.CreateCommand(m_transaction.Parent))
                            cmd.ExecuteNonQuery(string.Format(@"DROP TABLE IF EXISTS ""{0}"" ", m_transaction));
                    }
                    catch { }
                    finally { m_tablename = null; }
                }

                if (m_insertCommand != null)
                    try { m_insertCommand.Dispose(); }
                    catch {}
                    finally { m_insertCommand = null; }
            }
Ejemplo n.º 8
0
 public StorageHelper(System.Data.IDbConnection con)
 {
     m_connection = con;
     m_previousTable = "Previous-" + Library.Utility.Utility.ByteArrayAsHexString(Guid.NewGuid().ToByteArray());
     m_currentTable = "Current-" + Library.Utility.Utility.ByteArrayAsHexString(Guid.NewGuid().ToByteArray());
     
     m_transaction = m_connection.BeginTransaction();
     
     using(var cmd = m_connection.CreateCommand())
     {
         cmd.Transaction = m_transaction;
         
         cmd.ExecuteNonQuery(string.Format(@"CREATE TEMPORARY TABLE ""{0}"" (""Path"" TEXT NOT NULL, ""FileHash"" TEXT NULL, ""MetaHash"" TEXT NOT NULL, ""Size"" INTEGER NOT NULL, ""Type"" INTEGER NOT NULL) ", m_previousTable));
         cmd.ExecuteNonQuery(string.Format(@"CREATE TEMPORARY TABLE ""{0}"" (""Path"" TEXT NOT NULL, ""FileHash"" TEXT NULL, ""MetaHash"" TEXT NOT NULL, ""Size"" INTEGER NOT NULL, ""Type"" INTEGER NOT NULL) ", m_currentTable));
     }
     
     m_insertPreviousElementCommand = m_connection.CreateCommand();
     m_insertPreviousElementCommand.Transaction = m_transaction;
     m_insertPreviousElementCommand.CommandText = string.Format(@"INSERT INTO ""{0}"" (""Path"", ""FileHash"", ""MetaHash"", ""Size"", ""Type"") VALUES (?,?,?,?,?)", m_previousTable);
     m_insertPreviousElementCommand.AddParameters(5);
     
     m_insertCurrentElementCommand = m_connection.CreateCommand();
     m_insertCurrentElementCommand.Transaction = m_transaction;
     m_insertCurrentElementCommand.CommandText = string.Format(@"INSERT INTO ""{0}"" (""Path"", ""FileHash"", ""MetaHash"", ""Size"", ""Type"") VALUES (?,?,?,?,?)", m_currentTable);
     m_insertCurrentElementCommand.AddParameters(5);
 }
Ejemplo n.º 9
0
		public CommandEventArgs(System.Data.IDbCommand command)
		{
			Command = command;
		}
Ejemplo n.º 10
0
            public MissingBlockList(string volumename, System.Data.IDbConnection connection, System.Data.IDbTransaction transaction)
            {
                m_connection = connection;
                m_transaction = new TemporaryTransactionWrapper(m_connection, transaction);
                m_volumename = volumename;
                var tablename = "MissingBlocks-" + Library.Utility.Utility.ByteArrayAsHexString(Guid.NewGuid().ToByteArray());
                using(var cmd = m_connection.CreateCommand())
                {
                    cmd.Transaction = m_transaction.Parent;
                    cmd.ExecuteNonQuery(string.Format(@"CREATE TEMPORARY TABLE ""{0}"" (""Hash"" TEXT NOT NULL, ""Size"" INTEGER NOT NULL, ""Restored"" INTEGER NOT NULL) ", tablename));
                    m_tablename = tablename;

                    var blockCount = cmd.ExecuteNonQuery(string.Format(@"INSERT INTO ""{0}"" (""Hash"", ""Size"", ""Restored"") SELECT DISTINCT ""Block"".""Hash"", ""Block"".""Size"", 0 AS ""Restored"" FROM ""Block"",""Remotevolume"" WHERE ""Block"".""VolumeID"" = ""Remotevolume"".""ID"" AND ""Remotevolume"".""Name"" = ? ", m_tablename), volumename);
                    if (blockCount == 0)
                        throw new Exception(string.Format("Unexpected empty block volume: {0}", volumename));

                    cmd.ExecuteNonQuery(string.Format(@"CREATE UNIQUE INDEX ""{0}-Ix"" ON ""{0}"" (""Hash"", ""Size"", ""Restored"")", tablename));
                }

                m_insertCommand = m_connection.CreateCommand();
                m_insertCommand.Transaction = m_transaction.Parent;
                m_insertCommand.CommandText = string.Format(@"UPDATE ""{0}"" SET ""Restored"" = ? WHERE ""Hash"" = ? AND ""Size"" = ? AND ""Restored"" = ? ", tablename);
                m_insertCommand.AddParameters(4);
            }
Ejemplo n.º 11
0
            public void Dispose()
            {
                if (m_insertblockCommand != null)
                    try { m_insertblockCommand.Dispose(); }
                    catch { }
                    finally { m_insertblockCommand = null; }

                if (m_resetfileCommand != null)
                    try { m_resetfileCommand.Dispose(); }
                    catch { }
                    finally { m_resetfileCommand = null; }

                if (m_updateAsRestoredCommand != null)
                    try { m_updateAsRestoredCommand.Dispose(); }
                    catch { }
                    finally { m_updateAsRestoredCommand = null; }

                if (m_updateFileAsDataVerifiedCommand != null)
                    try { m_updateFileAsDataVerifiedCommand.Dispose(); }
                    catch { }
                    finally { m_updateFileAsDataVerifiedCommand = null; }

                if (m_statUpdateCommand != null)
                    try { m_statUpdateCommand.Dispose(); }
                    catch { }
                    finally { m_statUpdateCommand = null; }
            }
Ejemplo n.º 12
0
 public BlockMarker(System.Data.IDbConnection connection, string blocktablename, string filetablename)
 {
     m_insertblockCommand = connection.CreateCommand();
     m_resetfileCommand  = connection.CreateCommand();
     m_updateAsRestoredCommand = connection.CreateCommand();
     m_statUpdateCommand = connection.CreateCommand();
     
     m_insertblockCommand.Transaction = connection.BeginTransaction();
     m_resetfileCommand.Transaction = m_insertblockCommand.Transaction;
     m_updateAsRestoredCommand.Transaction = m_insertblockCommand.Transaction;
     m_statUpdateCommand.Transaction = m_insertblockCommand.Transaction;
     
     m_blocktablename = blocktablename;
     m_updateTable = "UpdatedBlocks-" + Library.Utility.Utility.ByteArrayAsHexString(Guid.NewGuid().ToByteArray());
     
     m_insertblockCommand.ExecuteNonQuery(string.Format(@"CREATE TEMPORARY TABLE ""{0}"" (""FileID"" INTEGER NOT NULL, ""Index"" INTEGER NOT NULL, ""Hash"" TEXT NOT NULL, ""Size"" INTEGER NOT NULL)", m_updateTable));
     m_insertblockCommand.CommandText = string.Format(@"INSERT INTO ""{0}"" (""FileID"", ""Index"", ""Hash"", ""Size"") VALUES (?, ?, ?, ?) ", m_updateTable);
     m_insertblockCommand.AddParameters(4);
                     
     m_resetfileCommand.CommandText = string.Format(@"DELETE FROM ""{0}"" WHERE ""FileID"" = ?", m_updateTable);
     m_resetfileCommand.AddParameters(1);
     
     m_updateAsRestoredCommand.CommandText = string.Format(@"INSERT INTO ""{0}"" (""FileID"", ""Index"", ""Hash"", ""Size"") SELECT ""FileID"", ""Index"", ""Hash"", ""Size"" FROM ""{1}"" WHERE ""{1}"".""FileID"" = ?", m_updateTable, m_blocktablename);
     m_updateAsRestoredCommand.AddParameters(1);
     
     m_statUpdateCommand.CommandText = string.Format(@"SELECT COUNT(DISTINCT ""FileID""), SUM(""Size"") FROM ""{0}"" WHERE ""Restored"" = 1 OR ""ID"" IN (SELECT ""{0}"".""ID"" FROM ""{0}"", ""{1}"" WHERE ""{0}"".""FileID"" = ""{1}"".""FileID"" AND ""{0}"".""Index"" = ""{1}"".""Index"" AND ""{0}"".""Hash"" = ""{1}"".""Hash"" AND ""{0}"".""Size"" = ""{1}"".""Size"" )", m_blocktablename, m_updateTable);
 }
Ejemplo n.º 13
0
 public void Commit(ILogWriter log)
 {
     var tr = m_insertblockCommand.Transaction;
     m_insertblockCommand.Dispose();
     m_insertblockCommand = null;
     using (new Logging.Timer("CommitBlockMarker"))
         tr.Commit();
     tr.Dispose();
 }
Ejemplo n.º 14
0
 public virtual void Dispose()
 {
     if (m_tablename != null)
         try 
         { 
             using(var cmd = m_connection.CreateCommand())
             {
                 cmd.Transaction = m_transaction;
                 cmd.ExecuteNonQuery(string.Format(@"DROP TABLE IF EXISTS ""{0}""", m_tablename));
             }
         }
         catch {}
         finally { m_tablename = null; }
         
     if (m_insertCommand != null)
         try { m_insertCommand.Dispose(); }
         catch {}
         finally { m_insertCommand = null; }
         
     if (m_transaction != null)
         try { m_transaction.Rollback(); }
         catch {}
         finally { m_transaction = null; }
 }
Ejemplo n.º 15
0
              public Basiclist(System.Data.IDbConnection connection, string volumename)
              {
                m_connection = connection;
                m_volumename = volumename;
                m_transaction = m_connection.BeginTransaction();
                var tablename = TABLE_PREFIX + "-" + Library.Utility.Utility.ByteArrayAsHexString(Guid.NewGuid().ToByteArray());

                using(var cmd = m_connection.CreateCommand())
                {
                    cmd.Transaction = m_transaction;
                    cmd.ExecuteNonQuery(string.Format(@"CREATE TEMPORARY TABLE ""{0}"" {1}", tablename, TABLEFORMAT));
                    m_tablename = tablename;
                }
                
                m_insertCommand = m_connection.CreateCommand();
                m_insertCommand.Transaction = m_transaction;
                m_insertCommand.CommandText = string.Format(@"INSERT INTO ""{0}"" {1}", m_tablename, INSERTCOMMAND);
                m_insertCommand.AddParameters(INSERTARGUMENTS);
            }
Ejemplo n.º 16
0
            public void Dispose()
            {
	            if (m_updateTable != null)
	            {
	            	try 
	            	{
	            		m_insertblockCommand.Parameters.Clear();
	            		m_insertblockCommand.ExecuteNonQuery(string.Format(@"DROP TABLE IF EXISTS ""{0}"" ", m_updateTable));
	            	}
	            	catch { }
	            	finally { m_updateTable = null; }
	            }
                            
                if (m_insertblockCommand != null)
                    try { m_insertblockCommand.Dispose(); }
                    catch { }
                    finally { m_insertblockCommand = null; }
                    
                if (m_resetfileCommand != null)
                    try { m_resetfileCommand.Dispose(); }
                    catch { }
                    finally { m_resetfileCommand = null; }
                    
                if (m_updateAsRestoredCommand != null)
                    try { m_updateAsRestoredCommand.Dispose(); }
                    catch { }
                    finally { m_updateAsRestoredCommand = null; }
                    
                if (m_statUpdateCommand != null)
                    try { m_statUpdateCommand.Dispose(); }
                    catch { }
                    finally { m_statUpdateCommand = null; }
                
            }
Ejemplo n.º 17
0
            public void Commit(ILogWriter log)
            {
                m_insertblockCommand.Parameters.Clear();
                var rc = m_insertblockCommand.ExecuteNonQuery(string.Format(@"UPDATE ""{0}"" SET ""Restored"" = 1 WHERE ""ID"" IN (SELECT ""{0}"".""ID"" FROM ""{0}"", ""{1}"" WHERE ""{0}"".""FileID"" = ""{1}"".""FileID"" AND ""{0}"".""Index"" = ""{1}"".""Index"" AND ""{0}"".""Hash"" = ""{1}"".""Hash"" AND ""{0}"".""Size"" = ""{1}"".""Size"" )", m_blocktablename, m_updateTable));
                var nc = Convert.ToInt64(m_insertblockCommand.ExecuteScalar(string.Format(@"SELECT COUNT(*) FROM ""{0}"" ", m_updateTable)));
                    		
                if (rc != nc)
                    log.AddWarning(string.Format("Inconsistency while marking blocks as updated. Updated blocks: {0}, Registered blocks: {1}", rc, nc), null);
                
                m_insertblockCommand.ExecuteNonQuery(string.Format(@"DROP TABLE IF EXISTS ""{0}"" ", m_updateTable));
                m_updateTable = null;

                var tr = m_insertblockCommand.Transaction;
                m_insertblockCommand.Dispose();
                m_insertblockCommand = null;
                using(new Logging.Timer("CommitBlockMarker"))
                    tr.Commit();
                tr.Dispose();
            }