Beispiel #1
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));
 }
Beispiel #2
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);
        }
        /// <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);
        }
        /// <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();
        }
Beispiel #5
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();
        }
Beispiel #6
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);
            }
        }
Beispiel #7
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);
                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);

            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);
        }
Beispiel #8
0
        /// <summary>
        /// Gets the metadataset ID from the filehash
        /// </summary>
        /// <returns><c>true</c>, if metadataset should be recorded, false if it already exists.</returns>
        /// <param name="filehash">The metadata hash.</param>
        /// <param name="size">The size of the metadata.</param>
        /// <param name="metadataid">The ID of the metadataset.</param>
        /// <param name="transaction">An optional transaction.</param>
        public bool GetMetadatasetID(string filehash, long size, out long metadataid, System.Data.IDbTransaction transaction = null)
        {
            if (size > 0)
            {
                m_findmetadatasetCommand.Transaction = transaction;
                metadataid = m_findmetadatasetCommand.ExecuteScalarInt64(null, -1, filehash, size);
                return(metadataid != -1);
            }

            metadataid = -2;
            return(false);
        }
Beispiel #9
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();
        }
Beispiel #10
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);
            }
        }
Beispiel #11
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, long blocksetid, out long metadataid, System.Data.IDbTransaction transaction = null)
        {
            if (GetMetadatasetID(filehash, size, out metadataid, transaction))
            {
                return(false);
            }

            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);
            }
        }
Beispiel #12
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);
        }
Beispiel #13
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);
                metadataid = m_findMetadatasetCommand.ExecuteScalarInt64(-1);
                if (metadataid != -1)
                {
                    return(metadataid);
                }
            }

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

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

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

            return(metadataid);
        }
        /// <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);
            }
        }
Beispiel #15
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 hash, long size, out long metadataid, System.Data.IDbTransaction transaction = null)
        {
            if (size > 0)
            {
                if (m_metadataLookup != null)
                {
                    if (m_metadataLookup.TryGet(hash, size, out metadataid))
                    {
                        return(false);
                    }
                }
                else
                {
                    m_findmetadatasetCommand.Transaction = transaction;
                    metadataid = m_findmetadatasetCommand.ExecuteScalarInt64(null, -1, hash, size);
                    if (metadataid != -1)
                    {
                        return(false);
                    }
                }


                long blocksetid;
                AddBlockset(hash, size, (int)size, new string[] { hash }, null, 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();
                    if (m_metadataLookup != null)
                    {
                        m_metadataLookup.Add(hash, size, metadataid);
                    }
                }

                return(true);
            }

            metadataid = -2;
            return(false);
        }
        /// <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)
        {
            m_findblockCommand.Transaction = transaction;
            m_findblockCommand.SetParameterValue(0, key);
            m_findblockCommand.SetParameterValue(1, size);
            var 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();
                return(true);
            }
            else
            {
                //Update lookup cache if required
                return(false);
            }
        }
Beispiel #17
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>
        /// <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)
        {
            long exsize;

            if (m_blockCache != null && m_blockCache.TryGetValue(key, out exsize))
            {
                if (exsize == size)
                {
                    return(false);
                }

                Logging.Log.WriteWarningMessage(LOGTAG, "HashCollisionsFound", null, "Found hash collision on {0}, sizes {1} vs {2}. Disabling cache from now on.", key, size, exsize);
                m_blockCache = null;
            }

            m_findblockCommand.Transaction = transaction;
            m_findblockCommand.SetParameterValue(0, key);
            m_findblockCommand.SetParameterValue(1, size);
            var 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_blockCache != null)
                {
                    m_blockCache.Add(key, size);
                }
                return(true);
            }
            else
            {
                //Update lookup cache if required
                return(false);
            }
        }
        private long GetPreviousFilesetID(System.Data.IDbCommand cmd, DateTime timestamp, long filesetid)
        {
            var lastFilesetId = cmd.ExecuteScalarInt64(@"SELECT ""ID"" FROM ""Fileset"" WHERE ""Timestamp"" < ? AND ""ID"" != ? ORDER BY ""Timestamp"" DESC ", -1, NormalizeDateTimeToEpochSeconds(timestamp), filesetid);

            return(lastFilesetId);
        }
Beispiel #19
0
 public long GetRemoteVolumeID(string file, System.Data.IDbTransaction transaction = null)
 {
     m_selectremotevolumeIdCommand.Transaction = transaction;
     return(m_selectremotevolumeIdCommand.ExecuteScalarInt64(null, -1, file));
 }
        /// <summary>
        /// Adds a blockset to the database, returns a value indicating if the blockset is new
        /// </summary>
        /// <param name="filehash">The hash of the blockset</param>
        /// <param name="size">The size of the blockset</param>
        /// <param name="fragmentoffset">The fragmentoffset for the last block</param>
        /// <param name="fragmenthash">The hash of the fragment</param>
        /// <param name="hashes">The list of hashes</param>
        /// <param name="blocksetid">The id of the blockset, new or old</param>
        /// <returns>True if the blockset was created, false otherwise</returns>
        public bool AddBlockset(string filehash, long size, int blocksize, IEnumerable <string> hashes, IEnumerable <string> blocklistHashes, out long blocksetid, System.Data.IDbTransaction transaction = null)
        {
            m_findblocksetCommand.Transaction = transaction;
            blocksetid = m_findblocksetCommand.ExecuteScalarInt64(null, -1, filehash, size);
            if (blocksetid != -1)
            {
                return(false); //Found it
            }
            using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
            {
                m_insertblocksetCommand.Transaction = tr.Parent;
                m_insertblocksetCommand.SetParameterValue(0, size);
                m_insertblocksetCommand.SetParameterValue(1, filehash);
                blocksetid = m_insertblocksetCommand.ExecuteScalarInt64();

                long ix = 0;
                if (blocklistHashes != null)
                {
                    m_insertblocklistHashesCommand.SetParameterValue(0, blocksetid);
                    m_insertblocklistHashesCommand.Transaction = tr.Parent;
                    foreach (var bh in blocklistHashes)
                    {
                        m_insertblocklistHashesCommand.SetParameterValue(1, ix);
                        m_insertblocklistHashesCommand.SetParameterValue(2, bh);
                        m_insertblocklistHashesCommand.ExecuteNonQuery();
                        ix++;
                    }
                }

                m_insertblocksetentryCommand.SetParameterValue(0, blocksetid);
                m_insertblocksetentryCommand.Transaction = tr.Parent;

                m_insertblocksetentryFastCommand.SetParameterValue(0, blocksetid);
                m_insertblocksetentryFastCommand.Transaction = tr.Parent;

                ix = 0;
                long remainsize = size;
                foreach (var h in hashes)
                {
                    var exsize = remainsize < blocksize ? remainsize : blocksize;
                    m_insertblocksetentryCommand.SetParameterValue(1, ix);
                    m_insertblocksetentryCommand.SetParameterValue(2, h);
                    m_insertblocksetentryCommand.SetParameterValue(3, exsize);
                    var c = m_insertblocksetentryCommand.ExecuteNonQuery();
                    if (c != 1)
                    {
                        m_result.AddError(string.Format("Checking errors, related to #1400. Unexpected result count: {0}, expected {1}, hash: {2}, size: {3}, blocksetid: {4}, ix: {5}, fullhash: {6}, fullsize: {7}", c, 1, h, exsize, blocksetid, ix, filehash, size), null);
                        using (var cmd = m_connection.CreateCommand(tr.Parent))
                        {
                            var bid = cmd.ExecuteScalarInt64(@"SELECT ""ID"" FROM ""Block"" WHERE ""Hash"" = ?", -1, h);
                            if (bid == -1)
                            {
                                throw new Exception(string.Format("Could not find any blocks with the given hash: {0}", h));
                            }
                            foreach (var rd in cmd.ExecuteReaderEnumerable(@"SELECT ""Size"" FROM ""Block"" WHERE ""Hash"" = ?", h))
                            {
                                m_result.AddError(string.Format("Found block with ID {0} and hash {1} and size {2}", bid, h, rd.ConvertValueToInt64(0, -1)), null);
                            }
                        }

                        throw new Exception(string.Format("Unexpected result count: {0}, expected {1}, check log for more messages", c, 1));
                    }

                    ix++;
                    remainsize -= blocksize;
                }

                tr.Commit();
            }

            return(true);
        }
 private long GetLastFilesetID(System.Data.IDbCommand cmd)
 {
     return(cmd.ExecuteScalarInt64(@"SELECT ""ID"" FROM ""Fileset"" ORDER BY ""Timestamp"" DESC LIMIT 1", -1));
 }
Beispiel #22
0
        public long AddBlockset(string fullhash, long size, IEnumerable <string> blocklisthashes, long expectedblocklisthashes, 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);
                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);

            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);

                long c = 0;
                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)
                {
                    m_result.AddWarning(string.Format("Extra blocklist hashes detected on blockset {2}. Expected {0} blocklist hashes, but found {1}", expectedblocklisthashes, c, blocksetid), null);
                }
            }

            return(blocksetid);
        }
Beispiel #23
0
        public void UpdateBlock(string hash, long size, long volumeID, System.Data.IDbTransaction transaction)
        {
            var currentVolumeId = -2L;

            if (m_blockHashLookup != null)
            {
                if (!m_blockHashLookup.TryGet(hash, size, out currentVolumeId))
                {
                    currentVolumeId = -2;
                }
            }
            else
            {
                m_findHashBlockCommand.Transaction = transaction;
                m_findHashBlockCommand.SetParameterValue(0, hash);
                m_findHashBlockCommand.SetParameterValue(1, size);
                currentVolumeId = m_findHashBlockCommand.ExecuteScalarInt64(-2);
            }

            if (currentVolumeId == volumeID)
            {
                return;
            }

            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();

                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(hash, size, volumeID);
                }
            }
            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));
                }

                if (m_blockHashLookup != null)
                {
                    m_blockHashLookup.Add(hash, size, volumeID);
                }
            }
            else
            {
                m_insertDuplicateBlockCommand.Transaction = transaction;
                m_insertDuplicateBlockCommand.SetParameterValue(0, hash);
                m_insertDuplicateBlockCommand.SetParameterValue(1, size);
                m_insertDuplicateBlockCommand.SetParameterValue(2, volumeID);
                m_insertDuplicateBlockCommand.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// Adds a blockset to the database, returns a value indicating if the blockset is new
        /// </summary>
        /// <param name="filehash">The hash of the blockset</param>
        /// <param name="size">The size of the blockset</param>
        /// <param name="fragmentoffset">The fragmentoffset for the last block</param>
        /// <param name="fragmenthash">The hash of the fragment</param>
        /// <param name="hashes">The list of hashes</param>
        /// <param name="blocksetid">The id of the blockset, new or old</param>
        /// <returns>True if the blockset was created, false otherwise</returns>
        public bool AddBlockset(string filehash, long size, int blocksize, IEnumerable <string> hashes, IEnumerable <string> blocklistHashes, out long blocksetid, System.Data.IDbTransaction transaction = null)
        {
            if (m_fileHashLookup != null)
            {
                if (m_fileHashLookup.TryGet(filehash, size, out blocksetid))
                {
                    return(false);
                }
            }
            else
            {
                m_findblocksetCommand.Transaction = transaction;
                blocksetid = m_findblocksetCommand.ExecuteScalarInt64(null, -1, filehash, size);
                if (blocksetid != -1)
                {
                    return(false); //Found it
                }
            }

            using (var tr = new TemporaryTransactionWrapper(m_connection, transaction))
            {
                m_insertblocksetCommand.Transaction = tr.Parent;
                m_insertblocksetCommand.SetParameterValue(0, size);
                m_insertblocksetCommand.SetParameterValue(1, filehash);
                blocksetid = m_insertblocksetCommand.ExecuteScalarInt64();
                if (m_fileHashLookup != null)
                {
                    m_fileHashLookup.Add(filehash, size, blocksetid);
                }

                long ix = 0;
                if (blocklistHashes != null)
                {
                    m_insertblocklistHashesCommand.SetParameterValue(0, blocksetid);
                    m_insertblocklistHashesCommand.Transaction = tr.Parent;
                    foreach (var bh in blocklistHashes)
                    {
                        m_insertblocklistHashesCommand.SetParameterValue(1, ix);
                        m_insertblocklistHashesCommand.SetParameterValue(2, bh);
                        m_insertblocklistHashesCommand.ExecuteNonQuery();
                        ix++;
                    }
                }

                m_insertblocksetentryCommand.SetParameterValue(0, blocksetid);
                m_insertblocksetentryCommand.Transaction = tr.Parent;

                m_insertblocksetentryFastCommand.SetParameterValue(0, blocksetid);
                m_insertblocksetentryFastCommand.Transaction = tr.Parent;

                ix = 0;
                long remainsize = size;
                foreach (var h in hashes)
                {
                    var exsize = remainsize < blocksize ? remainsize : blocksize;
                    var found  = false;
                    if (m_blockHashLookup != null)
                    {
                        KeyValuePair <long, long> id;
                        if (m_blockHashLookup.TryGet(h, exsize, out id) && id.Value == exsize)
                        {
                            m_insertblocksetentryFastCommand.SetParameterValue(1, ix);
                            m_insertblocksetentryFastCommand.SetParameterValue(2, id.Key);
                            var cx = m_insertblocksetentryFastCommand.ExecuteNonQuery();
                            if (cx != 1)
                            {
                                throw new Exception(string.Format("Unexpected result count: {0}, expected {1}", cx, 1));
                            }
                            found = true;
                        }
                    }

                    if (!found)
                    {
                        m_insertblocksetentryCommand.SetParameterValue(1, ix);
                        m_insertblocksetentryCommand.SetParameterValue(2, h);
                        m_insertblocksetentryCommand.SetParameterValue(3, exsize);
                        var c = m_insertblocksetentryCommand.ExecuteNonQuery();
                        if (c != 1)
                        {
                            throw new Exception(string.Format("Unexpected result count: {0}, expected {1}", c, 1));
                        }
                    }

                    ix++;
                    remainsize -= blocksize;
                }

                tr.Commit();
            }

            return(true);
        }