public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (!(grainState is IAggregateState))
                throw new NotAggregateStateException(grainState.GetType());

            var stream = this.GetStreamName(grainType, grainReference);

            var sliceStart = 0;
            StreamEventsSlice currentSlice;

            do
            {
                var sliceCount = sliceStart + ReadPageSize;

                currentSlice = await this.Connection.ReadStreamEventsForwardAsync(stream, sliceStart, sliceCount, true);

                if (currentSlice.Status == SliceReadStatus.StreamNotFound)
                    return;

                if (currentSlice.Status == SliceReadStatus.StreamDeleted)
                    throw new StreamDeletedException();

                sliceStart = currentSlice.NextEventNumber;

                foreach (var @event in currentSlice.Events)
                {
                    dynamic deserialisedEvent = DeserializeEvent(@event.Event);
                    StateTransformer.ApplyEvent(deserialisedEvent, grainState as IAggregateState);
                }

            } while (!currentSlice.IsEndOfStream);
        }
        public async Task ReadStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
        {
            try
            {
                var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
                var blob = container.GetBlockBlobReference(blobName);
                var text = await blob.DownloadTextAsync();
                if (string.IsNullOrWhiteSpace(text))
                {
                    return;
                }

                var data = JsonConvert.DeserializeObject(text, grainState.GetType());
                var dict = ((IGrainState)data).AsDictionary();
                grainState.SetAll(dict);
            }
            catch (StorageException ex)
            {
                ;
            }
            catch (Exception ex)
            {
                Log.Error(0, ex.ToString());
            }
        }
 /// <summary>
 /// Reads persisted state from the backing store and deserializes it into the the target
 /// grain state object.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">A reference to an object to hold the persisted state of the grain.</param>
 /// <returns>Completion promise for this operation.</returns>
 public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     var entityData = await DataManager.Read(grainState.GetType().Name, grainReference.ToKeyString());
     if (entityData != null)
     {
         ConvertFromStorageFormat(grainState, entityData);
     }
 }
        public Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (!(grainState is IAggregateState))
                throw new NotAggregateStateException(grainState.GetType());

            var state = grainState as IAggregateState;
            var stream = this.GetStreamName(grainType, grainReference);

            return this.Connection.DeleteStreamAsync(stream, state.Version);
        }
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var stateName = grainState.GetType().Name;
            var key = grainReference.ToKeyString();
            var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);

            using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
            {
                var state = await session.LoadAsync<RavenJObject>(id);
                if (state != null)
                {
                    grainState.SetAll(state.ToDictionary(x => x.Key, x => x.Value.Value<object>()));
                }
            }
        }
 /// <summary>
 /// Writes the persisted state from a grain state object into its backing store.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">A reference to an object holding the persisted state of the grain.</param>
 /// <returns>Completion promise for this operation.</returns>
 public Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     var entityData = ConvertToStorageFormat(grainState);
     return DataManager.Write(grainState.GetType().Name, grainReference.ToKeyString(), entityData);
 }
 /// <summary>
 /// Removes grain state from its backing store, if found.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">An object holding the persisted state of the grain.</param>
 /// <returns></returns>
 public Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     DataManager.Delete(grainState.GetType().Name, grainReference.ToKeyString());
     return TaskDone.Done;
 }
 /// <summary>
 /// Constructs a grain state instance by deserializing a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be populated for storage.</param>
 /// <param name="entityData">JSON storage format representaiton of the grain state.</param>
 protected static void ConvertFromStorageFormat(IGrainState grainState, string entityData)
 {
     object data = JsonConvert.DeserializeObject(entityData, grainState.GetType(), GrainStateMongoDataManager.JsonSetting);
     var dict = ((IGrainState)data).AsDictionary();
     grainState.SetAll(dict);
 }
        public async Task ReadStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
        {
            var blobName = GetBlobName(grainType, grainId);

            if (this.Log.IsVerbose3)
            {
                this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_Reading, "Reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
            }

            try
            {
                var blob = container.GetBlockBlobReference(blobName);

                string json;

                try
                {
                    json = await blob.DownloadTextAsync().ConfigureAwait(false);
                }
                catch (StorageException exception)
                {
                    var errorCode = exception.RequestInformation.ExtendedErrorInformation.ErrorCode;
                    if (errorCode == BlobErrorCodeStrings.BlobNotFound)
                    {
                        if (this.Log.IsVerbose2)
                        {
                            this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_BlobNotFound, "BlobNotFound reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                        }
                        return;
                    }
                    if (errorCode == BlobErrorCodeStrings.ContainerNotFound)
                    {
                        if (this.Log.IsVerbose2)
                        {
                            this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_ContainerNotFound, "ContainerNotFound reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                        }
                        return;
                    }

                    throw;
                }

                if (string.IsNullOrWhiteSpace(json))
                {
                    if (this.Log.IsVerbose2)
                    {
                        this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_BlobEmpty, "BlobEmpty reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                    }
                    return;
                }

                var data = JsonConvert.DeserializeObject(json, grainState.GetType(), jsonSettings);
                var dict = ((GrainState)data).AsDictionary();
                grainState.SetAll(dict);
                grainState.Etag = blob.Properties.ETag;

                if (this.Log.IsVerbose3)
                {
                    this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_DataRead, "Read: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                }
            }
            catch (Exception ex)
            {
                Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_ReadError,
                          string.Format("Error reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4} Exception={5}", grainType, grainId, grainState.Etag, blobName, container.Name, ex.Message),
                          ex);
            }
        }
 /// <summary>
 /// Constructs a grain state instance by deserializing a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be populated for storage.</param>
 /// <param name="entityData">JSON storage format representaiton of the grain state.</param>
 protected static void ConvertFromStorageFormat(IGrainState grainState, string entityData)
 {
     var setting = new JsonSerializerSettings();
     object data = JsonConvert.DeserializeObject(entityData, grainState.GetType());
     var dict = ((IGrainState)data).AsDictionary();
     grainState.SetAll(dict);
 }
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var stateName = grainState.GetType().Name;
            var key = grainReference.ToKeyString();
            var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);

            using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
            {
                await session.StoreAsync(grainState, id);
                await session.SaveChangesAsync();
            }
        }
Пример #12
0
 /// <summary>
 /// Constructs a grain state instance by deserializing a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be populated for storage.</param>
 /// <param name="entityData">JSON storage format representaiton of the grain state.</param>
 protected static void ConvertFromStorageFormat(IGrainState grainState, string entityData)
 {
     JavaScriptSerializer deserializer = new JavaScriptSerializer();
     object data = deserializer.Deserialize(entityData, grainState.GetType());
     var dict = ((IGrainState)data).AsDictionary();
     grainState.SetAll(dict);
 }
 public Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
 {
     DataManager.Delete(grainState.GetType().Name, grainReference.ToKeyString());
     return(TaskDone.Done);
 }
        public Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var data = ConvertToStorageFormat(grainState);

            return(DataManager.Write(grainState.GetType().Name, grainReference.ToKeyString(), data));
        }
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var data = await DataManager.Read(grainState.GetType().Name, grainReference.ToKeyString());

            ConvertFromStorageFormat(grainState, data);
        }