Ejemplo n.º 1
0
        private void AddEntry(long filesetid, long pathprefixid, string path, DateTime time, long blocksetid, long metadataid, System.Data.IDbTransaction transaction)
        {
            var fileid = -1L;

            m_findFilesetCommand.Transaction = transaction;
            m_findFilesetCommand.SetParameterValue(0, pathprefixid);
            m_findFilesetCommand.SetParameterValue(1, path);
            m_findFilesetCommand.SetParameterValue(2, blocksetid);
            m_findFilesetCommand.SetParameterValue(3, metadataid);
            fileid = m_findFilesetCommand.ExecuteScalarInt64(-1);

            if (fileid < 0)
            {
                m_insertFileCommand.Transaction = transaction;
                m_insertFileCommand.SetParameterValue(0, pathprefixid);
                m_insertFileCommand.SetParameterValue(1, path);
                m_insertFileCommand.SetParameterValue(2, blocksetid);
                m_insertFileCommand.SetParameterValue(3, metadataid);
                fileid = m_insertFileCommand.ExecuteScalarInt64(-1);
            }

            m_insertFilesetEntryCommand.Transaction = transaction;
            m_insertFilesetEntryCommand.SetParameterValue(0, filesetid);
            m_insertFilesetEntryCommand.SetParameterValue(1, fileid);
            m_insertFilesetEntryCommand.SetParameterValue(2, time.ToUniversalTime().Ticks);
            m_insertFilesetEntryCommand.ExecuteNonQuery();
        }
Ejemplo n.º 2
0
 public bool SetBlockRestored(string hash, long size)
 {
     m_insertCommand.SetParameterValue(0, hash);
     m_insertCommand.SetParameterValue(1, size);
     m_insertCommand.SetParameterValue(2, 1);
     return(m_insertCommand.ExecuteNonQuery() == 1);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a file record to the database
        /// </summary>
        /// <param name="filename">The path to the file</param>
        /// <param name="lastmodified">The time the file was modified</param>
        /// <param name="blocksetID">The ID of the hashkey for the file</param>
        /// <param name="metadataID">The ID for the metadata</param>
        /// <param name="transaction">The transaction to use for insertion, or null for no transaction</param>
        /// <param name="operationId">The operationId to use, or -1 to use the current operation</param>
        public void AddFile(string filename, DateTime lastmodified, long blocksetID, long metadataID, System.Data.IDbTransaction transaction)
        {
            var             fileidobj  = -1L;
            PathEntryKeeper entry      = null;
            var             entryFound = false;

            if (m_pathLookup != null)
            {
                if (entryFound = (m_pathLookup.TryFind(filename, out entry) && entry != null))
                {
                    var fid = entry.GetFilesetID(blocksetID, metadataID);
                    if (fid >= 0)
                    {
                        fileidobj = fid;
                    }
                }
            }
            else
            {
                m_findfilesetCommand.Transaction = transaction;
                m_findfilesetCommand.SetParameterValue(0, blocksetID);
                m_findfilesetCommand.SetParameterValue(1, metadataID);
                m_findfilesetCommand.SetParameterValue(2, filename);
                fileidobj = m_findfilesetCommand.ExecuteScalarInt64();
            }

            if (fileidobj == -1)
            {
                using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
                {
                    m_insertfileCommand.Transaction = tr.Parent;
                    m_insertfileCommand.SetParameterValue(0, filename);
                    m_insertfileCommand.SetParameterValue(1, blocksetID);
                    m_insertfileCommand.SetParameterValue(2, metadataID);
                    fileidobj = m_insertfileCommand.ExecuteScalarInt64();
                    tr.Commit();

                    // We do not need to update this, because we will not ask for the same file twice
                    if (m_pathLookup != null)
                    {
                        if (!entryFound)
                        {
                            entry = new PathEntryKeeper(-1, new DateTime(0, DateTimeKind.Utc), -1, null, -1);
                            entry.AddFilesetID(blocksetID, metadataID, fileidobj);
                            m_pathLookup.Insert(filename, entry);
                        }
                        else
                        {
                            entry.AddFilesetID(blocksetID, metadataID, fileidobj);
                        }
                    }
                }
            }

            m_insertfileOperationCommand.Transaction = transaction;
            m_insertfileOperationCommand.SetParameterValue(0, m_filesetId);
            m_insertfileOperationCommand.SetParameterValue(1, fileidobj);
            m_insertfileOperationCommand.SetParameterValue(2, lastmodified.ToUniversalTime().Ticks);
            m_insertfileOperationCommand.ExecuteNonQuery();
        }
Ejemplo n.º 4
0
        public bool UpdateBlockset(string hash, IEnumerable <string> blocklisthashes, System.Data.IDbTransaction transaction)
        {
            m_findblocklisthashCommand.Transaction = transaction;
            m_findblocklisthashCommand.SetParameterValue(0, hash);
            var r = m_findblocklisthashCommand.ExecuteScalar();

            if (r != null && r != DBNull.Value)
            {
                return(false);
            }

            m_insertBlockset.Transaction = transaction;
            m_insertBlockset.SetParameterValue(0, hash);

            var index = 0L;

            foreach (var s in blocklisthashes)
            {
                m_insertBlockset.SetParameterValue(1, s);
                m_insertBlockset.SetParameterValue(2, index++);
                m_insertBlockset.ExecuteNonQuery();
            }

            return(true);
        }
Ejemplo n.º 5
0
        public long AddMetadataset(string metahash, long metahashsize, IEnumerable <string> metablocklisthashes, long expectedmetablocklisthashes, System.Data.IDbTransaction transaction)
        {
            var metadataid = -1L;

            if (metahash == null)
            {
                return(metadataid);
            }

            m_findMetadatasetCommand.Transaction = transaction;
            m_findMetadatasetCommand.SetParameterValue(0, metahash);
            m_findMetadatasetCommand.SetParameterValue(1, metahashsize);
            metadataid = m_findMetadatasetCommand.ExecuteScalarInt64(-1);
            if (metadataid != -1)
            {
                return(metadataid);
            }

            var blocksetid = AddBlockset(metahash, metahashsize, metablocklisthashes, expectedmetablocklisthashes, transaction);

            m_insertMetadatasetCommand.Transaction = transaction;
            m_insertMetadatasetCommand.SetParameterValue(0, blocksetid);
            metadataid = m_insertMetadatasetCommand.ExecuteScalarInt64(-1);

            return(metadataid);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds a file record to the database
        /// </summary>
        /// <param name="filename">The path to the file</param>
        /// <param name="scantime">The time the file was scanned</param>
        /// <param name="blocksetID">The ID of the hashkey for the file</param>
        /// <param name="metadataID">The ID for the metadata</param>
        /// <param name="transaction">The transaction to use for insertion, or null for no transaction</param>
        /// <param name="operationId">The operationId to use, or -1 to use the current operation</param>
        public void AddFile(string filename, DateTime scantime, long blocksetID, long metadataID, System.Data.IDbTransaction transaction)
        {
            object          fileidobj  = null;
            PathEntryKeeper entry      = null;
            bool            entryFound = false;

            if (m_pathLookup != null)
            {
                if (entryFound = (m_pathLookup.TryFind(filename, out entry) && entry != null))
                {
                    var fid = entry.GetFilesetID(blocksetID, metadataID);
                    if (fid >= 0)
                    {
                        fileidobj = fid;
                    }
                }
            }
            else
            {
                m_findfilesetCommand.Transaction = transaction;
                m_findfilesetCommand.SetParameterValue(0, blocksetID);
                m_findfilesetCommand.SetParameterValue(1, metadataID);
                m_findfilesetCommand.SetParameterValue(2, filename);
                fileidobj = m_findfilesetCommand.ExecuteScalar();
            }

            if (fileidobj == null || fileidobj == DBNull.Value)
            {
                using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
                {
                    m_insertfileCommand.Transaction = tr.Parent;
                    m_insertfileCommand.SetParameterValue(0, filename);
                    m_insertfileCommand.SetParameterValue(1, blocksetID);
                    m_insertfileCommand.SetParameterValue(2, metadataID);
                    fileidobj = Convert.ToInt64(m_insertfileCommand.ExecuteScalar());
                    tr.Commit();

                    // We do not need to update this, because we will not ask for the same file twice
                    if (m_pathLookup != null)
                    {
                        if (!entryFound)
                        {
                            entry = new PathEntryKeeper(-1, DateTime.UtcNow);
                            entry.AddFilesetID(blocksetID, metadataID, Convert.ToInt64(fileidobj));
                            m_pathLookup.Insert(filename, entry);
                        }
                        else
                        {
                            entry.AddFilesetID(blocksetID, metadataID, Convert.ToInt64(fileidobj));
                        }
                    }
                }
            }

            m_insertfileOperationCommand.Transaction = transaction;
            m_insertfileOperationCommand.SetParameterValue(0, m_filesetId);
            m_insertfileOperationCommand.SetParameterValue(1, fileidobj);
            m_insertfileOperationCommand.SetParameterValue(2, NormalizeDateTimeToEpochSeconds(scantime));
            m_insertfileOperationCommand.ExecuteNonQuery();
        }
Ejemplo n.º 7
0
 public void AddIndexBlockLink(long indexVolumeID, long blockVolumeID, System.Data.IDbTransaction transaction)
 {
     m_insertIndexBlockLink.Transaction = transaction;
     m_insertIndexBlockLink.SetParameterValue(0, indexVolumeID);
     m_insertIndexBlockLink.SetParameterValue(1, blockVolumeID);
     m_insertIndexBlockLink.ExecuteNonQuery();
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Probes to see if a block already exists
 /// </summary>
 /// <param name="key">The block key</param>
 /// <param name="size">The size of the block</param>
 /// <returns>True if the block should be added to the current output</returns>
 public long FindBlockID(string key, long size, System.Data.IDbTransaction transaction = null)
 {
     m_findblockCommand.Transaction = transaction;
     m_findblockCommand.SetParameterValue(0, key);
     m_findblockCommand.SetParameterValue(1, size);
     return(m_findblockCommand.ExecuteScalarInt64(-1));
 }
Ejemplo n.º 9
0
 public void AddSmallBlocksetLink(string filehash, string blockhash, long blocksize, System.Data.IDbTransaction transaction)
 {
     m_insertSmallBlockset.Transaction = transaction;
     m_insertSmallBlockset.SetParameterValue(0, filehash);
     m_insertSmallBlockset.SetParameterValue(1, blockhash);
     m_insertSmallBlockset.SetParameterValue(2, blocksize);
     m_insertSmallBlockset.ExecuteNonQuery();
 }
Ejemplo n.º 10
0
            public bool UseBlock(string hash, long size, System.Data.IDbTransaction transaction)
            {
                m_command.Transaction = transaction;
                m_command.SetParameterValue(0, hash);
                m_command.SetParameterValue(1, size);
                var r = m_command.ExecuteScalar();

                return(r != null && r != DBNull.Value);
            }
Ejemplo n.º 11
0
        /// <summary>
        /// Adds a block to the local database, returning a value indicating if the value presents a new block
        /// </summary>
        /// <param name="key">The block key</param>
        /// <param name="archivename">The name of the archive that holds the data</param>
        /// <returns>True if the block should be added to the current output</returns>
        public bool AddBlock(string key, long size, long volumeid, System.Data.IDbTransaction transaction = null)
        {
            object r = null;

            if (m_blockHashLookup != null)
            {
                KeyValuePair <long, long> blockid;
                if (m_blockHashLookup.TryGet(key, size, out blockid))
                {
                    return(false);
                }
            }
            else
            {
                m_findblockCommand.Transaction = transaction;
                m_findblockCommand.SetParameterValue(0, key);
                m_findblockCommand.SetParameterValue(1, size);
                r = m_findblockCommand.ExecuteScalar();
            }

            if (r == null || r == DBNull.Value)
            {
                m_insertblockCommand.Transaction = transaction;
                m_insertblockCommand.SetParameterValue(0, key);
                m_insertblockCommand.SetParameterValue(1, volumeid);
                m_insertblockCommand.SetParameterValue(2, size);
                r = m_insertblockCommand.ExecuteScalar();
                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(key, size, new KeyValuePair <long, long>(Convert.ToInt64(r), size));
                }
                return(true);
            }
            else
            {
                //We add/update it now
                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(key, size, new KeyValuePair <long, long>(Convert.ToInt64(r), size));
                }

                //If the block is found and the volume is broken somehow.
                m_findremotevolumestateCommand.Transaction = transaction;
                r = m_findremotevolumestateCommand.ExecuteScalar(null, r);
                if (r != null && (r.ToString() == RemoteVolumeState.Temporary.ToString() || r.ToString() == RemoteVolumeState.Uploading.ToString() || r.ToString() == RemoteVolumeState.Uploaded.ToString() || r.ToString() == RemoteVolumeState.Verified.ToString()))
                {
                    return(false);
                }
                else
                {
                    m_updateblockCommand.Transaction = transaction;
                    m_updateblockCommand.ExecuteNonQuery(null, volumeid, key, size);
                    return(true);
                }
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Log an operation performed on the remote backend
 /// </summary>
 /// <param name="operation">The operation performed</param>
 /// <param name="path">The path involved</param>
 /// <param name="data">Any data relating to the operation</param>
 public void LogRemoteOperation(string operation, string path, string data, System.Data.IDbTransaction transaction)
 {
     m_insertremotelogCommand.Transaction = transaction;
     m_insertremotelogCommand.SetParameterValue(0, m_operationid);
     m_insertremotelogCommand.SetParameterValue(1, NormalizeDateTimeToEpochSeconds(DateTime.UtcNow));
     m_insertremotelogCommand.SetParameterValue(2, operation);
     m_insertremotelogCommand.SetParameterValue(3, path);
     m_insertremotelogCommand.SetParameterValue(4, data);
     m_insertremotelogCommand.ExecuteNonQuery();
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Log a debug message
 /// </summary>
 /// <param name="type">The message type</param>
 /// <param name="message">The message</param>
 /// <param name="exception">An optional exception</param>
 public void LogMessage(string type, string message, Exception exception, System.Data.IDbTransaction transaction)
 {
     m_insertlogCommand.Transaction = transaction;
     m_insertlogCommand.SetParameterValue(0, m_operationid);
     m_insertlogCommand.SetParameterValue(1, NormalizeDateTimeToEpochSeconds(DateTime.UtcNow));
     m_insertlogCommand.SetParameterValue(2, type);
     m_insertlogCommand.SetParameterValue(3, message);
     m_insertlogCommand.SetParameterValue(4, exception == null ? null : exception.ToString());
     m_insertlogCommand.ExecuteNonQuery();
 }
Ejemplo n.º 14
0
        private void AddEntry(FilelistEntryType type, long filesetid, string path, DateTime time, long blocksetid, string metahash, long metahashsize, System.Data.IDbTransaction transaction)
        {
            var fileid     = -1L;
            var metadataid = AddMetadataset(metahash, metahashsize, transaction);

            if (m_filesetLookup != null)
            {
                PathEntryKeeper e;
                if (m_filesetLookup.TryFind(path, out e))
                {
                    fileid = e.GetFilesetID(blocksetid, metadataid);
                }
            }
            else
            {
                m_findFilesetCommand.Transaction = transaction;
                m_findFilesetCommand.SetParameterValue(0, path);
                m_findFilesetCommand.SetParameterValue(1, blocksetid);
                m_findFilesetCommand.SetParameterValue(2, metadataid);
                var r = m_findFilesetCommand.ExecuteScalar();
                if (r != null && r != DBNull.Value)
                {
                    fileid = Convert.ToInt64(r);
                }
            }

            if (fileid < 0)
            {
                m_insertFileCommand.Transaction = transaction;
                m_insertFileCommand.SetParameterValue(0, path);
                m_insertFileCommand.SetParameterValue(1, blocksetid);
                m_insertFileCommand.SetParameterValue(2, metadataid);
                fileid = Convert.ToInt64(m_insertFileCommand.ExecuteScalar());
                if (m_filesetLookup != null)
                {
                    PathEntryKeeper e;
                    if (m_filesetLookup.TryFind(path, out e))
                    {
                        e.AddFilesetID(blocksetid, metadataid, fileid);
                    }
                    else
                    {
                        e = new PathEntryKeeper();
                        e.AddFilesetID(blocksetid, metadataid, fileid);
                        m_filesetLookup.Insert(path, e);
                    }
                }
            }

            m_insertFilesetEntryCommand.Transaction = transaction;
            m_insertFilesetEntryCommand.SetParameterValue(0, filesetid);
            m_insertFilesetEntryCommand.SetParameterValue(1, fileid);
            m_insertFilesetEntryCommand.SetParameterValue(2, NormalizeDateTimeToEpochSeconds(time));
            m_insertFilesetEntryCommand.ExecuteNonQuery();
        }
Ejemplo n.º 15
0
        public long AddBlockset(string fullhash, long size, IEnumerable <string> blocklisthashes, System.Data.IDbTransaction transaction)
        {
            var blocksetid = -1L;

            if (m_fileHashLookup != null)
            {
                if (m_fileHashLookup.TryGet(fullhash, size, out blocksetid))
                {
                    return(blocksetid);
                }
                else
                {
                    blocksetid = -1;
                }
            }
            else
            {
                m_findBlocksetCommand.Transaction = transaction;
                m_findBlocksetCommand.SetParameterValue(0, size);
                m_findBlocksetCommand.SetParameterValue(1, fullhash);
                var r = m_findBlocksetCommand.ExecuteScalar();
                if (r != null && r != DBNull.Value)
                {
                    return(Convert.ToInt64(r));
                }
            }

            m_insertBlocksetCommand.Transaction = transaction;
            m_insertBlocksetCommand.SetParameterValue(0, size);
            m_insertBlocksetCommand.SetParameterValue(1, fullhash);
            blocksetid = Convert.ToInt64(m_insertBlocksetCommand.ExecuteScalar());

            if (m_fileHashLookup != null)
            {
                m_fileHashLookup.Add(fullhash, size, blocksetid);
            }

            if (blocklisthashes != null)
            {
                var index = 0L;
                m_insertBlocklistHashCommand.Transaction = transaction;
                m_insertBlocklistHashCommand.SetParameterValue(0, blocksetid);
                foreach (var hash in blocklisthashes)
                {
                    if (!string.IsNullOrEmpty(hash))
                    {
                        m_insertBlocklistHashCommand.SetParameterValue(1, index++);
                        m_insertBlocklistHashCommand.SetParameterValue(2, hash);
                        m_insertBlocklistHashCommand.ExecuteNonQuery();
                    }
                }
            }

            return(blocksetid);
        }
Ejemplo n.º 16
0
        public void MoveBlockToNewVolume(string hash, long size, long volumeID, System.Data.IDbTransaction tr)
        {
            m_moveBlockToNewVolumeCommand.SetParameterValue(0, volumeID);
            m_moveBlockToNewVolumeCommand.SetParameterValue(1, hash);
            m_moveBlockToNewVolumeCommand.SetParameterValue(2, size);
            m_moveBlockToNewVolumeCommand.Transaction = tr;
            var c = m_moveBlockToNewVolumeCommand.ExecuteNonQuery();

            if (c != 1)
            {
                throw new Exception("Unexpected update result");
            }
        }
Ejemplo n.º 17
0
        private void AddEntry(FilelistEntryType type, long filesetid, string path, DateTime time, long blocksetid, string metahash, long metahashsize, System.Data.IDbTransaction transaction)
        {
            var fileid     = -1L;
            var metadataid = AddMetadataset(metahash, metahashsize, transaction);

            if (m_filesetLookup != null)
            {
                PathEntryKeeper e;
                if (m_filesetLookup.TryFind(path, out e))
                {
                    fileid = e.GetFilesetID(blocksetid, metadataid);
                }
            }
            else
            {
                m_findFilesetCommand.Transaction = transaction;
                m_findFilesetCommand.SetParameterValue(0, path);
                m_findFilesetCommand.SetParameterValue(1, blocksetid);
                m_findFilesetCommand.SetParameterValue(2, metadataid);
                fileid = m_findFilesetCommand.ExecuteScalarInt64(-1);
            }

            if (fileid < 0)
            {
                m_insertFileCommand.Transaction = transaction;
                m_insertFileCommand.SetParameterValue(0, path);
                m_insertFileCommand.SetParameterValue(1, blocksetid);
                m_insertFileCommand.SetParameterValue(2, metadataid);
                fileid = m_insertFileCommand.ExecuteScalarInt64(-1);
                if (m_filesetLookup != null)
                {
                    PathEntryKeeper e;
                    if (m_filesetLookup.TryFind(path, out e))
                    {
                        e.AddFilesetID(blocksetid, metadataid, fileid);
                    }
                    else
                    {
                        e = new PathEntryKeeper();
                        e.AddFilesetID(blocksetid, metadataid, fileid);
                        m_filesetLookup.Insert(path, e);
                    }
                }
            }

            m_insertFilesetEntryCommand.Transaction = transaction;
            m_insertFilesetEntryCommand.SetParameterValue(0, filesetid);
            m_insertFilesetEntryCommand.SetParameterValue(1, fileid);
            m_insertFilesetEntryCommand.SetParameterValue(2, time.ToUniversalTime().Ticks);
            m_insertFilesetEntryCommand.ExecuteNonQuery();
        }
Ejemplo n.º 18
0
        public long GetFileLastModified(string path, long filesetid, out DateTime oldModified)
        {
            m_selectfilelastmodifiedCommand.SetParameterValue(0, path);
            m_selectfilelastmodifiedCommand.SetParameterValue(1, filesetid);
            using (var rd = m_selectfilelastmodifiedCommand.ExecuteReader())
                if (rd.Read())
                {
                    oldModified = new DateTime(rd.ConvertValueToInt64(1), DateTimeKind.Utc);
                    return(rd.ConvertValueToInt64(0));
                }

            oldModified = new DateTime(0, DateTimeKind.Utc);
            return(-1);
        }
Ejemplo n.º 19
0
        public bool UpdateBlock(string hash, long size, long volumeID, System.Data.IDbTransaction transaction)
        {
            m_findHashBlockCommand.Transaction = transaction;
            m_findHashBlockCommand.SetParameterValue(0, hash);
            m_findHashBlockCommand.SetParameterValue(1, size);
            var currentVolumeId = m_findHashBlockCommand.ExecuteScalarInt64(-2);

            if (currentVolumeId == volumeID)
            {
                return(false);
            }

            if (currentVolumeId == -2)
            {
                //Insert
                m_insertBlockCommand.Transaction = transaction;
                m_insertBlockCommand.SetParameterValue(0, hash);
                m_insertBlockCommand.SetParameterValue(1, size);
                m_insertBlockCommand.SetParameterValue(2, volumeID);
                m_insertBlockCommand.ExecuteNonQuery();

                return(true);
            }
            else if (currentVolumeId == -1)
            {
                //Update
                m_updateBlockVolumeCommand.Transaction = transaction;
                m_updateBlockVolumeCommand.SetParameterValue(0, volumeID);
                m_updateBlockVolumeCommand.SetParameterValue(1, hash);
                m_updateBlockVolumeCommand.SetParameterValue(2, size);
                var c = m_updateBlockVolumeCommand.ExecuteNonQuery();
                if (c != 1)
                {
                    throw new Exception(string.Format("Failed to update table, found {0} entries for key {1} with size {2}", c, hash, size));
                }

                return(true);
            }
            else
            {
                m_insertDuplicateBlockCommand.Transaction = transaction;
                m_insertDuplicateBlockCommand.SetParameterValue(0, hash);
                m_insertDuplicateBlockCommand.SetParameterValue(1, size);
                m_insertDuplicateBlockCommand.SetParameterValue(2, volumeID);
                m_insertDuplicateBlockCommand.ExecuteNonQuery();

                return(false);
            }
        }
Ejemplo n.º 20
0
 public long RegisterRemoteVolume(string name, RemoteVolumeType type, RemoteVolumeState state, System.Data.IDbTransaction transaction = null)
 {
     using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
     {
         m_createremotevolumeCommand.SetParameterValue(0, m_operationid);
         m_createremotevolumeCommand.SetParameterValue(1, name);
         m_createremotevolumeCommand.SetParameterValue(2, type.ToString());
         m_createremotevolumeCommand.SetParameterValue(3, state.ToString());
         m_createremotevolumeCommand.SetParameterValue(4, 0);
         m_createremotevolumeCommand.Transaction = tr.Parent;
         var r = Convert.ToInt64(m_createremotevolumeCommand.ExecuteScalar());
         tr.Commit();
         return(r);
     }
 }
Ejemplo n.º 21
0
        public void RemoveRemoteVolume(string name, System.Data.IDbTransaction transaction = null)
        {
            using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
                using (var deletecmd = m_connection.CreateCommand())
                {
                    deletecmd.Transaction = tr.Parent;
                    var volumeid = GetRemoteVolumeID(name, tr.Parent);

                    // If the volume is a block or index volume, this will update the crosslink table, otherwise nothing will happen
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""IndexBlockLink"" WHERE ""BlockVolumeID"" = ? OR ""IndexVolumeID"" = ?", volumeid, volumeid);

                    // If the volume is a fileset, this will remove the fileset, otherwise nothing will happen
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""FilesetEntry"" WHERE ""FilesetID"" IN (SELECT ""ID"" FROM ""Fileset"" WHERE ""VolumeID"" = ?)", volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""Fileset"" WHERE ""VolumeID"" = ?", volumeid);

                    var subQuery = @"(SELECT DISTINCT ""BlocksetEntry"".""BlocksetID"" FROM ""BlocksetEntry"", ""Block"" WHERE ""BlocksetEntry"".""BlockID"" = ""Block"".""ID"" AND ""Block"".""VolumeID"" = ? UNION SELECT ""BlocksetID"" FROM ""BlocklistHash"" WHERE ""Hash"" IN (SELECT ""Hash"" FROM ""Block"" WHERE ""VolumeID"" = ?))";

                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""File"" WHERE ""BlocksetID"" IN " + subQuery + @" OR ""MetadataID"" IN " + subQuery, volumeid, volumeid, volumeid, volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""Metadataset"" WHERE ""BlocksetID"" IN " + subQuery, volumeid, volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""Blockset"" WHERE ""ID"" IN " + subQuery, volumeid, volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""BlocksetEntry"" WHERE ""BlocksetID"" IN " + subQuery, volumeid, volumeid);

                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""BlocklistHash"" WHERE ""Hash"" IN (SELECT ""Hash"" FROM ""Block"" WHERE ""VolumeID"" = ?)", volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""Block"" WHERE ""VolumeID"" = ?", volumeid);
                    deletecmd.ExecuteNonQuery(@"DELETE FROM ""DeletedBlock"" WHERE ""VolumeID"" = ?", volumeid);

                    m_removeremotevolumeCommand.SetParameterValue(0, name);
                    m_removeremotevolumeCommand.Transaction = tr.Parent;
                    m_removeremotevolumeCommand.ExecuteNonQuery();

                    tr.Commit();
                }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Adds a metadata set to the database, and returns a value indicating if the record was new
        /// </summary>
        /// <param name="hash">The metadata hash</param>
        /// <param name="metadataid">The id of the metadata set</param>
        /// <returns>True if the set was added to the database, false otherwise</returns>
        public bool AddMetadataset(string filehash, long size, int blocksize, IEnumerable <string> blockhashes, IEnumerable <string> blocklisthashes, out long metadataid, System.Data.IDbTransaction transaction = null)
        {
            if (size > 0)
            {
                m_findmetadatasetCommand.Transaction = transaction;
                metadataid = m_findmetadatasetCommand.ExecuteScalarInt64(null, -1, filehash, size);
                if (metadataid != -1)
                {
                    return(false);
                }


                long blocksetid;
                AddBlockset(filehash, size, blocksize, blockhashes, blocklisthashes, out blocksetid, transaction);

                using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
                {
                    m_insertmetadatasetCommand.Transaction = tr.Parent;
                    m_insertmetadatasetCommand.SetParameterValue(0, blocksetid);
                    metadataid = m_insertmetadatasetCommand.ExecuteScalarInt64();
                    tr.Commit();
                }

                return(true);
            }

            metadataid = -2;
            return(false);
        }
Ejemplo n.º 23
0
 public long GetFileEntry(string path, out DateTime oldScanned)
 {
     if (m_pathLookup != null)
     {
         PathEntryKeeper tmp;
         if (m_pathLookup.TryFind(path, out tmp) && tmp != null && tmp.FileID >= 0)
         {
             oldScanned = tmp.ScanTime;
             return(tmp.FileID);
         }
         else
         {
             oldScanned = DateTime.UtcNow;
             return(-1);
         }
     }
     else
     {
         m_selectfileSimpleCommand.SetParameterValue(0, path);
         using (var rd = m_selectfileSimpleCommand.ExecuteReader())
             if (rd.Read())
             {
                 oldScanned = ParseFromEpochSeconds(Convert.ToInt64(rd.GetValue(1)));
                 return(Convert.ToInt64(rd.GetValue(0)));
             }
             else
             {
                 oldScanned = DateTime.UtcNow;
                 return(-1);
             }
     }
 }
Ejemplo n.º 24
0
        public long AddBlockset(string fullhash, long size, IEnumerable <string> blocklisthashes, long expectedblocklisthashes, System.Data.IDbTransaction transaction)
        {
            m_findBlocksetCommand.Transaction = transaction;
            m_findBlocksetCommand.SetParameterValue(0, size);
            m_findBlocksetCommand.SetParameterValue(1, fullhash);
            var blocksetid = m_findBlocksetCommand.ExecuteScalarInt64(-1);

            if (blocksetid != -1)
            {
                return(blocksetid);
            }

            m_insertBlocksetCommand.Transaction = transaction;
            m_insertBlocksetCommand.SetParameterValue(0, size);
            m_insertBlocksetCommand.SetParameterValue(1, fullhash);
            blocksetid = m_insertBlocksetCommand.ExecuteScalarInt64(-1);

            long c = 0;

            if (blocklisthashes != null)
            {
                var index = 0L;
                m_insertBlocklistHashCommand.Transaction = transaction;
                m_insertBlocklistHashCommand.SetParameterValue(0, blocksetid);

                foreach (var hash in blocklisthashes)
                {
                    if (!string.IsNullOrEmpty(hash))
                    {
                        c++;
                        if (c <= expectedblocklisthashes)
                        {
                            m_insertBlocklistHashCommand.SetParameterValue(1, index++);
                            m_insertBlocklistHashCommand.SetParameterValue(2, hash);
                            m_insertBlocklistHashCommand.ExecuteNonQuery();
                        }
                    }
                }
            }

            if (c != expectedblocklisthashes)
            {
                Logging.Log.WriteWarningMessage(LOGTAG, "MismatchInBlocklistHashCount", null, "Mismatching number of blocklist hashes detected on blockset {2}. Expected {0} blocklist hashes, but found {1}", expectedblocklisthashes, c, blocksetid);
            }

            return(blocksetid);
        }
Ejemplo n.º 25
0
        private long AddMetadataset(string metahash, long metahashsize, System.Data.IDbTransaction transaction)
        {
            var metadataid = -1L;

            if (metahash == null)
            {
                return(metadataid);
            }

            if (m_metadataLookup != null)
            {
                if (m_metadataLookup.TryGet(metahash, metahashsize, out metadataid))
                {
                    return(metadataid);
                }
                else
                {
                    metadataid = -1;
                }
            }
            else
            {
                m_findMetadatasetCommand.Transaction = transaction;
                m_findMetadatasetCommand.SetParameterValue(0, metahash);
                m_findMetadatasetCommand.SetParameterValue(1, metahashsize);
                var r = m_findMetadatasetCommand.ExecuteScalar();
                if (r != null && r != DBNull.Value)
                {
                    return(Convert.ToInt64(r));
                }
            }

            var blocksetid = AddBlockset(metahash, metahashsize, null, transaction);

            m_insertMetadatasetCommand.Transaction = transaction;
            m_insertMetadatasetCommand.SetParameterValue(0, blocksetid);
            metadataid = Convert.ToInt64(m_insertMetadatasetCommand.ExecuteScalar());

            if (m_metadataLookup != null)
            {
                m_metadataLookup.Add(metahash, metahashsize, metadataid);
            }

            return(metadataid);
        }
Ejemplo n.º 26
0
        public long RegisterRemoteVolume(string name, RemoteVolumeType type, RemoteVolumeState state, TimeSpan deleteGraceTime, System.Data.IDbTransaction transaction)
        {
            using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
            {
                m_createremotevolumeCommand.SetParameterValue(0, m_operationid);
                m_createremotevolumeCommand.SetParameterValue(1, name);
                m_createremotevolumeCommand.SetParameterValue(2, type.ToString());
                m_createremotevolumeCommand.SetParameterValue(3, state.ToString());
                m_createremotevolumeCommand.SetParameterValue(4, 0);

                if (deleteGraceTime.Ticks <= 0)
                {
                    m_createremotevolumeCommand.SetParameterValue(5, 0);
                }
                else
                {
                    m_createremotevolumeCommand.SetParameterValue(5, (DateTime.UtcNow + deleteGraceTime).Ticks);
                }

                m_createremotevolumeCommand.Transaction = tr.Parent;
                var r = m_createremotevolumeCommand.ExecuteScalarInt64();
                tr.Commit();
                return(r);
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Adds a block to the local database, returning a value indicating if the value presents a new block
        /// </summary>
        /// <param name="key">The block key</param>
        /// <param name="archivename">The name of the archive that holds the data</param>
        /// <returns>True if the block should be added to the current output</returns>
        public bool AddBlock(string key, long size, long volumeid, System.Data.IDbTransaction transaction = null)
        {
            var r = -1L;

            if (m_blockHashLookup != null)
            {
                KeyValuePair <long, long> blockid;
                if (m_blockHashLookup.TryGet(key, size, out blockid))
                {
                    return(false);
                }
            }
            else
            {
                m_findblockCommand.Transaction = transaction;
                m_findblockCommand.SetParameterValue(0, key);
                m_findblockCommand.SetParameterValue(1, size);
                r = m_findblockCommand.ExecuteScalarInt64(-1);
            }

            if (r == -1L)
            {
                m_insertblockCommand.Transaction = transaction;
                m_insertblockCommand.SetParameterValue(0, key);
                m_insertblockCommand.SetParameterValue(1, volumeid);
                m_insertblockCommand.SetParameterValue(2, size);
                r = m_insertblockCommand.ExecuteScalarInt64();
                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(key, size, new KeyValuePair <long, long>(r, size));
                }
                return(true);
            }
            else
            {
                //Update lookup cache if required
                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(key, size, new KeyValuePair <long, long>(r, size));
                }

                return(false);
            }
        }
Ejemplo n.º 28
0
        public void UpdateRemoteVolume(string name, RemoteVolumeState state, long size, string hash, System.Data.IDbTransaction transaction = null)
        {
            m_updateremotevolumeCommand.Transaction = transaction;
            m_updateremotevolumeCommand.SetParameterValue(0, m_operationid);
            m_updateremotevolumeCommand.SetParameterValue(1, state.ToString());
            m_updateremotevolumeCommand.SetParameterValue(2, hash);
            m_updateremotevolumeCommand.SetParameterValue(3, size);
            m_updateremotevolumeCommand.SetParameterValue(4, name);
            var c = m_updateremotevolumeCommand.ExecuteNonQuery();

            if (c != 1)
            {
                throw new Exception("Unexpected number of remote volumes detected!");
            }

            if (state == RemoteVolumeState.Deleted)
            {
                RemoveRemoteVolume(name, transaction);
            }
        }
Ejemplo n.º 29
0
        public string GetFileHash(long fileid)
        {
            m_selectfileHashCommand.SetParameterValue(0, fileid);
            var r = m_selectfileHashCommand.ExecuteScalar();

            if (r == null || r == DBNull.Value)
            {
                return(null);
            }

            return(r.ToString());
        }
Ejemplo n.º 30
0
            public bool UseBlock(string hash, long size)
            {
                if (m_lookup != null)
                {
                    long nsize;
                    if (m_lookup.TryGet(hash, size, out nsize) && nsize == size)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }

                m_command.SetParameterValue(0, hash);
                m_command.SetParameterValue(1, size);
                var r = m_command.ExecuteScalar();

                return(r != null && r != DBNull.Value);
            }