Esempio n. 1
0
        /// <summary>
        /// Get the id of the <paramref name="tags"/> from the database or
        /// create a new id for the <paramref name="tags"/> if an id is not found.
        /// </summary>
        /// <param name="name">
        /// The name of the <see cref="MetricConfig"/> that is associated with
        /// the <paramref name="tags"/>.
        /// </param>
        /// <param name="tags">
        /// The tags to get the id.
        /// </param>
        /// <returns>
        /// A number that uniquely identifies the tags within the metrics database.
        /// </returns>
        long TagsIdFromDatabase(string name, Tags tags)
        {
            int hash = Hash(name, tags);
            IEnumerable <long> ids = metrics_dao_.GetSeriesIds(name, hash, tags.Count);

            // The |GetTagsIds| return all the ids that has the same hash and
            // number of tags of the given |tags|. We need to find the tags group
            // that contains the same tags as |tags|.
            foreach (var id in ids)
            {
                if (IsSameTags(id, tags))
                {
                    return(id);
                }
            }

            // A matching tags was not found, lets create a new one. A transaction scope
            // is used to endure that all tags related with a given serie is registered.
            using (var scope = new TransactionScope(TransactionScopeOption.Required)) {
                long serie_id = metrics_dao_.RegisterSerie(name, hash, tags.Count);
                foreach (Tag tag in tags)
                {
                    metrics_dao_.RegisterTag(tag.Name, tag.Value, serie_id);
                }
                scope.Complete();
                return(serie_id);
            }
        }