/// <summary> /// Returns the <see langword="string"/> primary key of the grain. /// </summary> /// <param name="grain">The grain to find the primary key for.</param> /// <returns>A <see langword="string"/> representing the primary key for this grain.</returns> public static string GetPrimaryKeyString(this IAddressable grain) { var grainId = GetGrainId(grain); if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.GetPrimaryKeyString()); } return(grainId.Key.ToStringUtf8()); }
/// <summary> /// Returns the <see langword="long"/> representation of a grain primary key. /// </summary> /// <param name="grain">The grain to find the primary key for.</param> /// <returns>A <see langword="long"/> representing the primary key for this grain.</returns> /// <exception cref="InvalidOperationException">The provided grain does not have a <see cref="long"/>-based key.</exception> public static long GetPrimaryKeyLong(this IAddressable grain) { var grainId = GetGrainId(grain); if (grainId.TryGetIntegerKey(out var primaryKey)) { return(primaryKey); } if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.GetPrimaryKeyLong()); } throw new InvalidOperationException($"Unable to extract integer key from grain id {grainId}"); }
/// <summary> /// Returns whether part of the primary key is of type <see langword="long"/>. /// </summary> /// <param name="grain">The target grain.</param> /// <exception cref="InvalidOperationException">The provided grain does not have a <see cref="long"/>-based key.</exception> public static bool IsPrimaryKeyBasedOnLong(this IAddressable grain) { var grainId = GetGrainId(grain); if (grainId.TryGetIntegerKey(out _)) { return(true); } if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.IsLongKey); } return(false); }
/// <summary> /// Returns whether part of the primary key is of type long. /// </summary> /// <param name="grain">The target grain.</param> public static bool IsPrimaryKeyBasedOnLong(this IAddressable grain) { var grainId = GetGrainId(grain); if (grainId.TryGetIntegerKey(out _)) { return(true); } if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.IsLongKey); } throw new InvalidOperationException($"Unable to extract integer key from grain id {grainId}"); }
private async ValueTask <PlacementResult> GetOrPlaceActivationAsync( PlacementTarget target, PlacementStrategy strategy, IPlacementRuntime placementRuntime, IActivationSelector selector, IPlacementDirector director) { var placementResult = await selector.OnSelectActivation(strategy, target.GrainIdentity, placementRuntime); if (placementResult is object) { return(placementResult); } var siloAddress = await director.OnAddActivation(strategy, target, placementRuntime); string grainTypeName; if (LegacyGrainId.TryConvertFromGrainId(target.GrainIdentity, out var legacyId)) { grainTypeName = placementRuntime.GetGrainTypeName(legacyId.TypeCode); } else { grainTypeName = null; } ActivationId activationId; if (strategy.IsDeterministicActivationId) { // Use the grain id as the activation id. activationId = ActivationId.GetDeterministic(target.GrainIdentity); } else { activationId = ActivationId.NewId(); } return(PlacementResult.SpecifyCreation( siloAddress, activationId, strategy, grainTypeName)); }
/// <summary> /// Returns the <see cref="Guid"/> representation of a grain primary key. /// </summary> /// <param name="grain">The grain to find the primary key for.</param> /// <returns>A <see cref="Guid"/> representing the primary key for this grain.</returns> /// <exception cref="InvalidOperationException">The provided grain does not have a <see cref="Guid"/>-based key.</exception> public static Guid GetPrimaryKey(this IAddressable grain) { var grainId = GetGrainId(grain); if (grainId.TryGetGuidKey(out var guid)) { return(guid); } if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.GetPrimaryKey()); } if (grainId.TryGetIntegerKey(out var integerKey)) { var N1 = integerKey; return(new Guid(0, 0, 0, (byte)N1, (byte)(N1 >> 8), (byte)(N1 >> 16), (byte)(N1 >> 24), (byte)(N1 >> 32), (byte)(N1 >> 40), (byte)(N1 >> 48), (byte)(N1 >> 56))); } throw new InvalidOperationException($"Unable to extract GUID key from grain id {grainId}"); }
/// <summary> /// Returns the Guid representation of a grain primary key. /// </summary> /// <param name="grain">The grain to find the primary key for.</param> /// <param name="keyExt">The output parameter to return the extended key part of the grain primary key, if extended primary key was provided for that grain.</param> /// <returns>A Guid representing the primary key for this grain.</returns> public static Guid GetPrimaryKey(this IAddressable grain, out string keyExt) { var grainId = GetGrainId(grain); if (GrainIdKeyExtensions.TryGetGuidKey(grainId, out var guid, out keyExt)) { return(guid); } if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId)) { return(legacyId.GetPrimaryKey(out keyExt)); } if (GrainIdKeyExtensions.TryGetIntegerKey(grainId, out var integerKey, out keyExt)) { var N0 = 0L; var N1 = integerKey; return(new Guid((uint)(N0 & 0xffffffff), (ushort)(N0 >> 32), (ushort)(N0 >> 48), (byte)N1, (byte)(N1 >> 8), (byte)(N1 >> 16), (byte)(N1 >> 24), (byte)(N1 >> 32), (byte)(N1 >> 40), (byte)(N1 >> 48), (byte)(N1 >> 56))); } throw new InvalidOperationException($"Unable to extract GUID key from grain id {grainId}"); }