예제 #1
0
        /// <summary>
        /// Create a volume
        /// </summary>
        /// <param name="createVolumeRequest">Parameters for create</param>
        /// <param name="userId">User id</param>
        /// <exception cref="VolumeException">If cannot create a volume</exception>
        /// <returns>gRPC reply with volume id</returns>
        public async Task <VolumeReply> Create(CreateVolumeRequest createVolumeRequest, string userId)
        {
            logger.LogInformation("Requested for creaton of a volume"); // english

            string      volumeId = IdentityFabric.GenVolumeId();
            VolumeModel volume   = VolumeAdapter.Volume(volumeId, createVolumeRequest);

            logger.LogInformation($"Created the VolumeModel with Id {volumeId}");

            using IConnectionMultiplexer redis = redisService.Connect();
            IDatabase db = redis.GetDatabase();

            logger.LogInformation($"Connected to the Redis servier");

            ITransaction transaction = db.CreateTransaction();

            logger.LogInformation($"Bigin transaction");

            string redisKey = GetVolumeKey(userId, volumeId);

            foreach (PropertyInfo propInfo in volume.GetType().GetProperties())
            {
                _ = transaction.HashSetAsync(redisKey, propInfo.Name, propInfo.GetValue(volume).ToString());
            }

            if (await transaction.ExecuteAsync() == false)
            {
                logger.LogError("Cannot create a volume. Transaction failed");
                throw new VolumeException("Cannot create a volume");
            }
            logger.LogInformation($"Creansaction completed successfully");
            return(VolumeAdapter.Volume(volume));
        }
예제 #2
0
        /// <summary>
        /// Gets volume by id
        /// </summary>
        /// <param name="volumeId">Volume id (vol-xxxxxxxxx)</param>
        /// <param name="userId">User id</param>
        /// <returns></returns>
        public async Task <VolumeReply> Get(string volumeId, string userId)
        {
            logger.LogInformation($"Searching the volume with id {volumeId}");
            logger.LogDebug($"Try to connect to the Redis");

            using IConnectionMultiplexer redis = redisService.Connect();
            var db = redis.GetDatabase();

            logger.LogDebug($"Connected to the Redis");

            string redisKey = GetVolumeKey(userId, volumeId);

            if (db.KeyExists(redisKey, CommandFlags.None) == false)
            {
                logger.LogError($"Cannot find a volume with id {redisKey}");
                throw new VolumeException($"Cannot find a volume with id {redisKey}");
            }
            logger.LogInformation($"The volume with id {redisKey} is exists in the store. " +
                                  $"Reqding the values..");

            VolumeModel volumeModel = new VolumeModel();

            foreach (PropertyInfo property in volumeModel.GetType().GetProperties())
            {
                RedisValue value = await db.HashGetAsync(redisKey, property.Name);

                property.SetValue(volumeModel, Convert.ChangeType(value, property.PropertyType));
            }

            logger.LogInformation($"The volume successfully read from the store");
            return(VolumeAdapter.Volume(volumeModel));
        }
예제 #3
0
        /// <summary>
        /// List of volumes by user Id
        /// </summary>
        /// <param name="userId">Use Id</param>
        /// <returns></returns>
        public async Task <IEnumerable <VolumeReply> > List(string userId)
        {
            using IConnectionMultiplexer redisConnection = redisService.Connect();
            var db = redisConnection.GetDatabase();

            logger.LogDebug("Connected to Redis");

            string redisKey = GetVolumeKey(userId);

            logger.LogDebug($"Searching by key pattern: {redisKey}");

            EndPoint endPoint = redisConnection.GetEndPoints().First() ?? throw new VolumeException($"Cannot find the endpoint");

            RedisKey[] keys = redisConnection.GetServer(endPoint).Keys(pattern: redisKey).ToArray();
            logger.LogDebug($"Found {keys.Length} keys");

            List <VolumeModel> volumes = new List <VolumeModel>();

            foreach (RedisKey key in keys)
            {
                logger.LogDebug($"Reading the key {key}");
                VolumeModel volumeModel = new VolumeModel();
                foreach (PropertyInfo property in volumeModel.GetType().GetProperties())
                {
                    var value = await db.HashGetAsync(key, property.Name);

                    property.SetValue(volumeModel, Convert.ChangeType(value, property.PropertyType));
                }
                volumes.Add(volumeModel);
            }

            logger.LogDebug($"All volumes ejected successfully");
            return(volumes.ConvertAll(v => VolumeAdapter.Volume(v)));
        }