protected static SqlCommand LoadEntryCommand(RFCatalogKey itemKey, int version, SqlConnection connection, SqlTransaction transaction = null)
        {
            if (itemKey.StoreType != RFStoreType.Document)
            {
                throw new Exception(String.Format("Unrecognized store type {0}", itemKey.StoreType));
            }

            SqlCommand sqlCommand = null;

            if (transaction != null)
            {
                sqlCommand = CreateCommand("[RIFF].[GetDocument]", connection, transaction);
            }
            else
            {
                sqlCommand = CreateCommand("[RIFF].[GetDocument]", connection);
            }

            var keyString = itemKey.ToString();

            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("@KeyType", itemKey.GetType().FullName);
            sqlCommand.Parameters.AddWithValue("@SerializedKey", keyString);
            sqlCommand.Parameters.AddWithValue("@KeyHash", RFStringHelpers.QuickHash(keyString));
            sqlCommand.Parameters.AddWithValue("@Version", version);
            return(sqlCommand);
        }
        public override RFCatalogKeyMetadata GetKeyMetadata(RFCatalogKey key)
        {
            var keyType   = key.GetType().FullName;
            var keyString = key.ToString();
            var keyHash   = RFStringHelpers.QuickHash(keyString);

            //Log.Debug(this, "GetKeyMetadata {0}", keyType);
            try
            {
                var dataTable = new DataTable();
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    using (var getCommand = CreateCommand("[RIFF].[GetKeyMetadata]", connection))
                    {
                        getCommand.CommandType = CommandType.StoredProcedure;
                        getCommand.Parameters.AddWithValue("@KeyType", keyType);
                        getCommand.Parameters.AddWithValue("@SerializedKey", keyString);
                        getCommand.Parameters.AddWithValue("@KeyHash", keyHash);

                        using (var reader = getCommand.ExecuteReader(System.Data.CommandBehavior.SingleResult))
                        {
                            dataTable.Load(reader);
                        }
                    }
                }
                if (dataTable != null && dataTable.Rows != null && dataTable.Rows.Count > 0)
                {
                    var dataRow = dataTable.Rows[0];
                    return(new RFCatalogKeyMetadata
                    {
                        ContentType = dataRow["ContentType"].ToString(),
                        KeyType = dataRow["KeyType"].ToString(),
                        Key = RFXMLSerializer.DeserializeContract(dataRow["KeyType"].ToString(), dataRow["SerializedKey"].ToString()) as RFCatalogKey,
                        KeyReference = (long)dataRow["CatalogKeyID"],
                        Metadata = RFMetadata.Deserialize(dataRow["Metadata"].ToString()),
                        UpdateTime = (DateTimeOffset)dataRow["UpdateTime"],
                        IsValid = (bool)dataRow["IsValid"],
                        DataSize = (long)dataRow["DataSize"]
                    });
                }
            }
            catch (Exception ex)
            {
                Log.Exception(this, "Error retrieving key metadata", ex);
            }
            return(null);
        }
        public RFCatalogEntry LoadEntry(RFCatalogKey key, RFCatalogOptions options = null)
        {
            DefaultOptions(ref options);
            if (key is RFCatalogKey)
            {
                lock (_memoryStore)
                {
                    if (_memoryStore.ContainsKey(key.ToString()))
                    {
                        return(_memoryStore[key.ToString()]);
                    }
                }

                switch (options.DateBehaviour)
                {
                case RFDateBehaviour.NotSet:
                case RFDateBehaviour.Exact:
                {
                    var item = _catalog.LoadItem(key as RFCatalogKey, options.Version, options.IgnoreContent);
                    return((item != null && item.IsValid) ? item : null);
                }

                case RFDateBehaviour.Dateless:
                {
                    var keyToLoad = key.CreateForInstance(new RFGraphInstance
                        {
                            Name      = key.GraphInstance.Name,
                            ValueDate = null
                        });
                    var item = _catalog.LoadItem(keyToLoad, options.Version, options.IgnoreContent);
                    return((item != null && item.IsValid) ? item : null);
                }

                case RFDateBehaviour.Latest:
                case RFDateBehaviour.Previous:
                {
                    if (key.GraphInstance == null || !key.GraphInstance.ValueDate.HasValue)
                    {
                        throw new RFSystemException(this, "Unable to load latest date for key without date {0}", key);
                    }
                    var allKeys        = _catalog.GetKeyInstances(key);
                    var candidateDates = new SortedSet <RFDate>();

                    foreach (var candidateKey in allKeys.Where(k => k.Key.Name == key.GraphInstance.Name))
                    {
                        if (candidateKey.Value.GraphInstance.ValueDate.Value <= key.GraphInstance.ValueDate.Value)
                        {
                            if ((options.DateBehaviour == RFDateBehaviour.Latest) ||
                                (options.DateBehaviour == RFDateBehaviour.Previous && candidateKey.Value.GraphInstance.ValueDate.Value < key.GraphInstance.ValueDate.Value))
                            {
                                candidateDates.Add(candidateKey.Value.GraphInstance.ValueDate.Value);
                            }
                        }
                    }
                    if (candidateDates.Count == 0)
                    {
                        SystemLog.Warning(this, "No latest date instance item found for key {0}", key);
                        return(null);
                    }
                    foreach (var latestDate in candidateDates.OrderByDescending(d => d))
                    {
                        var keyToLoad = key.CreateForInstance(new RFGraphInstance
                            {
                                Name      = key.GraphInstance.Name,
                                ValueDate = latestDate
                            });
                        var item = _catalog.LoadItem(keyToLoad, options.Version, options.IgnoreContent);
                        if (item != null && item.IsValid)
                        {
                            return(item);
                        }
                    }
                    return(null);
                }

                default:
                    throw new RFSystemException(this, "Unsupported date behaviour in LoadEntry: {0}", options.DateBehaviour);
                }
            }
            else
            {
                throw new RFSystemException(this, "Unknown store key type {0}", key.ToString());
            }
        }