Esempio n. 1
0
        /// <summary>
        /// Gets the alias. Note: this is the more exceptional case, and should probably removed altogether.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="direction">The direction.</param>
        /// <returns>
        /// The alias represented by the specified id.
        /// </returns>
        public static EntityAlias GetAlias(long id, Direction direction = Direction.Forward)
        {
            // Obsolete .. people shouldn't be resolving IDs to aliases

            var        key = new Tuple <long, Direction>(id, direction);
            CacheEntry entry;

            if (_idCache.TryGetValue(key, out entry))
            {
                return(entry == null ? null : entry.Alias);
            }

            EntityAlias alias = GetAliasByIdFromDatabase(id, direction);

            if (alias == null)
            {
                _idCache[key] = null;
            }
            else
            {
                entry = new CacheEntry {
                    Alias = alias, Id = id, Direction = direction
                };
                _aliasCache[alias] = entry;
                _idCache[key]      = entry;
            }
            return(alias);
        }
Esempio n. 2
0
        /// <summary>
        ///     Gets the id.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <returns>
        ///     The entity id represented by the specified alias.
        /// </returns>
        public static long GetId(string alias)
        {
            EntityAlias entityAlias = new EntityAlias(alias);
            long        id          = GetId(entityAlias);

            return(id);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the alias by id from database.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="direction">The direction.</param>
        /// <returns>
        /// The entity alias if found, null otherwise
        /// </returns>
        private static EntityAlias GetAliasByIdFromDatabase(long id, Direction direction)
        {
            EntityAlias alias;

            using (DatabaseContext ctx = DatabaseContext.GetContext( ))
                using (IDbCommand command = ctx.CreateCommand())
                {
                    /////
                    // TODO: Replace with a stored procedure call.
                    /////
                    command.CommandText = @"-- Entity: Resolve ID to alias
                        SELECT a.Namespace, a.Data
                        FROM dbo.Data_Alias a
                        WHERE a.EntityId = @id AND a.AliasMarkerId = @direction AND a.TenantId = @tenantId";

                    ctx.AddParameter(command, "@id", DbType.Int64, id);
                    ctx.AddParameter(command, "@direction", DbType.Int32, direction == Direction.Forward ? 0 : 1);
                    ctx.AddParameter(command, "@tenantId", DbType.Int64, RequestContext.TenantId);

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        if (reader != null && reader.Read())
                        {
                            alias = new EntityAlias(reader.GetString(0), reader.GetString(1));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }

            return(alias);
        }
Esempio n. 4
0
        /// <summary>
        /// Preload all aliases in the current tenant.
        /// </summary>
        public static void PreloadAliases()
        {
            using (Profiler.Measure("Preload Aliases"))
                using (DatabaseContext ctx = DatabaseContext.GetContext())
                    using (IDbCommand command = ctx.CreateCommand())
                    {
                        /////
                        // TODO: Replace with a stored procedure call.
                        /////
                        command.CommandText = @"-- Entity: Preload aliases
                        SELECT Namespace, Data Alias, EntityId, AliasMarkerId
                        FROM dbo.Data_Alias
                        WHERE TenantId = @tenantId AND Data IS NOT NULL";

                        ctx.AddParameter(command, "@tenantId", DbType.Int64, RequestContext.TenantId);

                        using (IDataReader reader = command.ExecuteReader())
                        {
                            while (reader != null && reader.Read())
                            {
                                string    ns        = reader.GetString(0);
                                string    alias     = reader.GetString(1);
                                long      id        = reader.GetInt64(2);
                                Direction direction = reader.GetInt32(3) == 0 ? Direction.Forward : Direction.Reverse;

                                EntityAlias entityAlias = new EntityAlias(ns, alias);
                                CacheEntry  cacheEntry  = new CacheEntry {
                                    Id = id, Direction = direction, Alias = entityAlias
                                };
                                _aliasCache[entityAlias] = cacheEntry;
                            }
                        }
                    }
        }
Esempio n. 5
0
 private long GetId(ref long stored, string ns, string alias)
 {
     if (stored == 0)
     {
         // (Unfortunately) presently the fastest way to get an ID.
         EntityAlias ea = new EntityAlias(ns, alias, true);
         stored = EntityIdentificationCache.GetId(ea);
     }
     return(stored);
 }
Esempio n. 6
0
        /// <summary>
        ///     Gets the id.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <returns>
        ///     The entity id represented by the specified alias.
        /// </returns>
        public static long GetId(EntityAlias alias)
        {
            long id;

            if (!TryGetId(alias, out id))
            {
                throw new ArgumentException(@"The alias " + alias + @" does not represent a known entity.", "alias");
            }

            return(id);
        }
Esempio n. 7
0
        /// <summary>
        ///     Tries to get the id.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <param name="id">The id.</param>
        /// <returns>True if the id was found; false otherwise.</returns>
        public static bool TryGetId(EntityAlias alias, out long id)
        {
            CacheEntry entry;

            if (TryGetEntry(alias, out entry))
            {
                id = entry.Id;
                return(true);
            }
            id = -1;
            return(false);
        }
Esempio n. 8
0
        /// <summary>
        ///     Converts the current entity alias to its corresponding entity id.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <returns>
        ///     Entity id of the current entity alias.
        /// </returns>
        public static long ToEntityId(EntityAlias alias)
        {
            if (alias == null || alias.Alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            if (alias.Alias.Length == 0)
            {
                throw new ArgumentException(@"alias was empty", "alias");
            }

            return(EntityIdentificationCache.GetId(alias));
        }
Esempio n. 9
0
        /// <summary>
        ///     Gets the direction of an alias.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <returns>
        ///     The Direction represented by the specified alias.
        /// </returns>
        public static Direction GetDirection(EntityAlias alias)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            CacheEntry entry;

            if (TryGetEntry(alias, out entry))
            {
                return(entry.Direction);
            }

            throw new ArgumentException(string.Format("The specified alias does not represent a known entity: {0}:{1} ", alias.Namespace, alias.Alias), "alias");
        }
Esempio n. 10
0
        /// <summary>
        /// Resolves the identifier.
        /// </summary>
        public long ResolveId( )
        {
            if (_id == 0 && !_serializing)
            {
                if (_entity != null)
                {
                    _id = _entity.Id;
                }
                else if (Alias != null)
                {
                    EntityAlias ea = new EntityAlias(Namespace ?? "core", Alias, true);
                    _id = EntityIdentificationCache.GetId(ea);
                }
                else
                {
                    return(0);
                }
            }

            return(_id);
        }
Esempio n. 11
0
        /// <summary>
        /// Tries to get the id.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <param name="cacheEntry">The cache entry.</param>
        /// <returns>
        /// True if the id was found; false otherwise.
        /// </returns>
        private static bool TryGetEntry(EntityAlias alias, out CacheEntry cacheEntry)
        {
            // Check cache
            if (_aliasCache.TryGetValue(alias, out cacheEntry))
            {
                return(true);
            }

            // Check database
            using (DatabaseContext ctx = DatabaseContext.GetContext())
                using (IDbCommand command = ctx.CreateCommand())
                {
                    command.CommandText = "dbo.spResolveAlias";
                    command.CommandType = CommandType.StoredProcedure;

                    ctx.AddParameter(command, "@alias", DbType.String, alias.Alias);
                    ctx.AddParameter(command, "@namespace", DbType.String, alias.Namespace);
                    ctx.AddParameter(command, "@tenantId", DbType.Int64, RequestContext.TenantId);

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        if (reader != null && reader.Read())
                        {
                            long      id        = reader.GetInt64(0);
                            Direction direction = reader.GetInt32(1) == 0 ? Direction.Forward : Direction.Reverse;

                            cacheEntry = new CacheEntry {
                                Id = id, Direction = direction, Alias = alias
                            };
                            _aliasCache[alias] = cacheEntry;
                        }
                        else
                        {
                            // TODO: should we cache alias misses?
                            return(false);
                        }
                    }
                }
            return(true);
        }
Esempio n. 12
0
 /// <summary>
 ///     Gets the id.
 /// </summary>
 /// <param name="alias">The alias.</param>
 /// <returns>
 ///     The entity id represented by the specified alias.
 /// </returns>
 public static bool AliasIsCached(EntityAlias alias)
 {
     return(_aliasCache.ContainsKey(alias));
 }