public async Task ReadStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
        {
            try
            {
                var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
                var blob     = container.GetBlockBlobReference(blobName);

                var exists = await blob.ExistsAsync();

                if (!exists)
                {
                    return;
                }

                var text = await blob.DownloadTextAsync();

                if (string.IsNullOrWhiteSpace(text))
                {
                    return;
                }

                var data = JsonConvert.DeserializeObject(text, grainState.GetType(), settings);
                var dict = ((IGrainState)data).AsDictionary();
                grainState.SetAll(dict);
                grainState.Etag = blob.Properties.ETag;
            }
            catch (Exception ex)
            {
                Log.Error(0, ex.ToString());
            }
        }
		public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
		{
			var tableResult = await _table.ExecuteAsync(TableOperation.Retrieve<DynamicTableEntity>(grainReference.ToKeyString(), grainType));
			if (tableResult.Result == null)
			{
				return;
			}
			var entity = tableResult.Result as DynamicTableEntity;

			var serializer = new JsonSerializer();
			using (var memoryStream = new MemoryStream())
			{
				foreach (var propertyName in entity.Properties.Keys.Where(p => p.StartsWith("d")).OrderBy(p => p))
				{
					var dataPart = entity.Properties[propertyName];
					await memoryStream.WriteAsync(dataPart.BinaryValue, 0, dataPart.BinaryValue.Length);
				}

				memoryStream.Position = 0;
				using (var bsonReader = new BsonReader(memoryStream))
				{
					var data = serializer.Deserialize<Dictionary<string, object>>(bsonReader);
					grainState.SetAll(data);
				}
			}
		}
        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());
            }
        }
Пример #4
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);
        }
Пример #5
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)
        {
            var    setting = new JsonSerializerSettings();
            object data    = JsonConvert.DeserializeObject(entityData, grainState.GetType());
            var    dict    = ((IGrainState)data).AsDictionary();

            grainState.SetAll(dict);
        }
Пример #6
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#ReadStateAsync"/>
        public virtual async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Read Keys={0}", StorageProviderUtils.PrintKeys(keys));
            }

            string id = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain          storageGrain = GetStorageGrain(id);
            IDictionary <string, object> state        = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);

            grainState.SetAll(state);
        }
        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>()));
                }
            }
        }
        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>()));
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Deserialize from Azure storage format
        /// </summary>
        /// <param name="grainState">The grain state data to be deserialized in to</param>
        /// <param name="entity">The Azure table entity the stored data</param>
        internal void ConvertFromStorageFormat(IGrainState grainState, GrainStateEntity entity)
        {
            Dictionary <string, object> dataValues = null;

            try
            {
                if (entity.Data != null)
                {
                    // Rehydrate
                    dataValues = SerializationManager.DeserializeFromByteArray <Dictionary <string, object> >(entity.Data);
                }
                else if (entity.StringData != null)
                {
                    dataValues = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(entity.StringData, jsonSettings);
                }
                if (dataValues != null)
                {
                    grainState.SetAll(dataValues);
                }
                // Else, no data found
            }
            catch (Exception exc)
            {
                var sb = new StringBuilder();
                if (entity.Data != null)
                {
                    sb.AppendFormat("Unable to convert from storage format GrainStateEntity.Data={0}", entity.Data);
                }
                else if (entity.StringData != null)
                {
                    sb.AppendFormat("Unable to convert from storage format GrainStateEntity.StringData={0}", entity.StringData);
                }
                if (dataValues != null)
                {
                    int i = 1;
                    foreach (var dvKey in dataValues.Keys)
                    {
                        object dvValue = dataValues[dvKey];
                        sb.AppendLine();
                        sb.AppendFormat("Data #{0} Key={1} Value={2} Type={3}", i, dvKey, dvValue, dvValue.GetType());
                        i++;
                    }
                }
                Log.Error(0, sb.ToString(), exc);
                throw new AggregateException(sb.ToString(), exc);
            }
        }
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            try
            {
                var collection = await this.EnsureCollection(grainType);
                var documents = await this.Client.ReadDocumentFeedAsync(collection.DocumentsLink);
                var documentId = grainReference.ToKeyString();
                GrainStateDocument document = documents.Where(d => d.Id == documentId).FirstOrDefault();

                if(document != null)
                    grainState.SetAll(document.State);
            }
            catch (Exception ex)
            {
                Log.Error(0, "Error in ReadStateAsync", ex);
            }            
        }
Пример #11
0
        public async Task ReadStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, IGrainState grainState)
        {
            var GrainKey = GetGrainKey(grainType, grainReference);

            try
            {
                var Client = new CouchbaseClient();
                if (Client.KeyExists(GrainKey))
                {
                    var json = "";
                    await Task.Run(() => { json = Client.Get(GrainKey).ToString(); });

                    var data = JsonConvert.DeserializeObject <Dictionary <String, Object> >(json);
                    grainState.SetAll(data);
                }
            }
            catch (Exception ex)
            {
                Log.Error(0, "Error in ReadStateAsync", ex);
            }
        }
Пример #12
0
 /// <summary>
 /// Deserialize from Azure storage format
 /// </summary>
 /// <param name="grainState">The grain state data to be deserialized in to</param>
 /// <param name="entity">The Azure table entity the stored data</param>
 internal void ConvertFromStorageFormat(IGrainState grainState, GrainStateEntity entity)
 {
     Dictionary<string, object> dataValues = null;
     try
     {
         if (entity.Data != null)
         {
             // Rehydrate
             dataValues = SerializationManager.DeserializeFromByteArray<Dictionary<string, object>>(entity.Data);
         }
         else if (entity.StringData != null)
         {
             dataValues = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(entity.StringData, jsonSettings);
         }
         if (dataValues != null)
         {
             grainState.SetAll(dataValues);
         }
         // Else, no data found
     }
     catch (Exception exc)
     {
         var sb = new StringBuilder();
         if (entity.Data != null)
         {
             sb.AppendFormat("Unable to convert from storage format GrainStateEntity.Data={0}", entity.Data);
         }
         else if (entity.StringData != null)
         {
             sb.AppendFormat("Unable to convert from storage format GrainStateEntity.StringData={0}", entity.StringData);
         }
         if (dataValues != null)
         {
             int i = 1;
             foreach (var dvKey in dataValues.Keys)
             {
                 object dvValue = dataValues[dvKey];
                 sb.AppendLine();
                 sb.AppendFormat("Data #{0} Key={1} Value={2} Type={3}", i, dvKey, dvValue, dvValue.GetType());
                 i++;
             }
         }
         Log.Error(0, sb.ToString(), exc);
         throw new AggregateException(sb.ToString(), exc);
     }
 }
 /// <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);
 }
Пример #16
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);
 }