예제 #1
0
        private byte[] GetAsssemblyFromDB(string assemblyName)
        {
            try
            {
                if (string.IsNullOrEmpty(ApplicationConfig.ConnectionString))
                {
                    return(null);
                }
                _logger.Info(string.Format("Loading Assemmbly:{0}, from Download ", assemblyName));

                var dataResponse = _dbProxy.Get(CommonConst.Collection.DLLS, new RawQuery(GetFilter(assemblyName)));

                if (dataResponse.Count > 0)
                {
                    var id           = dataResponse[0][CommonConst.CommonField.DISPLAY_ID].ToString();
                    var assemblyData = _keyValueStorage.Get <string>(CommonConst.Collection.DLLS, id);
                    return(Convert.FromBase64String(assemblyData));
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"Error on GetAsssemblyFromDB {assemblyName}, Error: {ex.Message}", ex);
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Must throw exception.
        /// </summary>
        /// <param name="storage"></param>
        public static async Task TestGetNull(IKeyValueStorage storage)
        {
            var key = "value_TestGetNull";
            await storage.Set(key, null);

            var value = await storage.Get <string>(key);

            Assert.IsNull(value);
        }
예제 #3
0
        public static void TaskWaitDefaultCall(IKeyValueStorage storage)
        {
            var key = "D991231h323";

            storage.Set(key, 100).Wait();
            var val = storage.Get <int>(key).Result;

            Assert.AreEqual(val, 100);
        }
예제 #4
0
        public static async Task DefaultCall(IKeyValueStorage storage)
        {
            var key = "D99h323";
            await storage.Set(key, 100);

            var val = await storage.Get <int>(key);

            Assert.AreEqual(val, 100);
        }
        public Task <SimilarityResult> GetSimilarityScoreAsync(Dictionary <string, double> tfIdfSourceContext, Document targetDocument)
        {
            var tfIdfSourceVector = tfIdfSourceContext.Values.ToList();
            var tfIdfTargetVector = tfIdfSourceContext.Keys.Select(t =>
            {
                var key = _tfIdfCacheKey(targetDocument, t);

                return(_keyValueStorage.Contains(key)
                        ? _keyValueStorage.Get <double>(key)
                        : 0d);
            })
                                    .ToList();

            return(Task.FromResult(new SimilarityResult
            {
                Id = targetDocument.Id,
                Score = GetCosineSimilarity(tfIdfSourceVector, tfIdfTargetVector)
            }));
        }
예제 #6
0
        private void InstallDlls(ModuleInstallRequest request)
        {
            var dllFilter = @"{name: /^lib\/netstandard2.0/, " + CommonConst.CommonField.MODULE_NAME + ": '" + request.Name + "', " + CommonConst.CommonField.VERSION + ": '" + request.Version + "'}";

            foreach (var item in _dbService.Get(CommonConst.Collection.MODULE_FILE_UPLOAD_CACHE, new RawQuery(dllFilter)))
            {
                var fileSourceId = item[CommonConst.CommonField.DISPLAY_ID].ToString();
                var fileName     = item[CommonConst.CommonField.NAME].ToString();
                var fileSize     = int.Parse(item[CommonConst.CommonField.FILE_SIZE].ToString());
                var contentType  = Mime.GetMimeType(fileName);
                var fileData     = JObjectHelper.GetJObjectDbDataFromFile(fileName, contentType, "lib/netstandard2.0/", request.Name, fileSize);
                fileData[CommonConst.CommonField.VERSION] = request.Version;
                var id       = fileData[CommonConst.CommonField.DISPLAY_ID].ToString();
                var data     = _keyValueStorage.Get <string>(CommonConst.Collection.MODULE_FILE_UPLOAD_CACHE, fileSourceId);
                var assembly = Assembly.Load(Convert.FromBase64String(data));
                fileData[CommonConst.CommonField.NAME] = assembly.FullName;
                WriteToDB(fileData, request.Name, CommonConst.Collection.DLLS, CommonConst.CommonField.FILE_PATH);
                InstallRoutes(request, assembly);
                _keyValueStorage.Put <string>(CommonConst.Collection.DLLS, id, data);
            }
        }
예제 #7
0
        public static byte[] GetContent(IDBService dbProxy, ILogger logger, string path, IKeyValueStorage keyValueStorage)
        {
            var staticFilePath = CommonUtility.GetAppConfigValue("StaticFilePath");

            if (!string.IsNullOrEmpty(staticFilePath))
            {
                string filePath = $"{staticFilePath}{path}";
                if (File.Exists(filePath))
                {
                    return(File.ReadAllBytes(filePath));
                }
            }
            string  wwwrootpath = ApplicationConfig.AppWWWRootPath;
            JObject document    = null;

            if (dbProxy.IsConnected)
            {
                document = (JObject)dbProxy.Get(CommonConst.Collection.STATIC_CONTECT, GetFilter(path)).First;
            }

            if (document != null)
            {
                var data = document[CommonConst.CommonField.DATA];
                if (data == null)
                {
                    data = keyValueStorage.Get <string>(CommonConst.Collection.STATIC_CONTECT, document[CommonConst.CommonField.DISPLAY_ID].ToString());
                }
                if (data != null)
                {
                    if (CommonUtility.IsTextConent(document[CommonConst.CommonField.CONTENT_TYPE].ToString()))
                    {
                        return(Encoding.ASCII.GetBytes(data.ToString()));
                    }
                    else
                    {
                        byte[] dataByte = System.Convert.FromBase64String(data.ToString());
                        return(dataByte);
                    }
                }
            }
            else
            {
                string filePath = GetFullPath(path);

                if (File.Exists(filePath))
                {
                    return(File.ReadAllBytes(filePath));
                }
            }
            return(null);
        }
예제 #8
0
        /// <summary>
        /// Return object casted to type.
        /// <para></para>
        /// If key was not defined or was removed - throw exception.
        /// <para></para>
        /// If object was set to null - return null.
        /// </summary>
        public static async Task <T> Get <T>(this IKeyValueStorage @this, string key)
        {
            object value = null;

            try
            {
                value = await @this.Get(typeof(T), key);

                return((T)value);
            }
            catch (Exception ex)
            {
                throw new StorageException($"Can`t cast returned value '{value}' to type {typeof(T).Name}.", ex);
            }
        }
예제 #9
0
        /// <summary>
        /// Return value or default Type instance (null for class and default value for struct).
        /// </summary>
        public static async Task <T> GetOrDefault <T>(this IKeyValueStorage @this, string key)
        {
            if (await @this.ContainsKey(key))
            {
                var value = await @this.Get(typeof(T), key);

                if (value == null)
                {
                    return(default(T));
                }
                return((T)value);
            }
            else
            {
                return(default(T));
            }
        }
예제 #10
0
        public static string GetStringContent(IDBService dbProxy, ILogger _logger, string path, IKeyValueStorage keyValueStorage)
        {
            JObject document       = null;
            var     staticFilePath = CommonUtility.GetAppConfigValue("StaticFilePath");

            if (!string.IsNullOrEmpty(staticFilePath))
            {
                string filePath = $"{staticFilePath}{path}";
                if (File.Exists(filePath))
                {
                    return(File.ReadAllText(filePath));
                }
            }


            if (dbProxy.IsConnected)
            {
                document = (JObject)dbProxy.Get(CommonConst.Collection.STATIC_CONTECT, GetFilter(path)).First;
            }
            if (document != null)
            {
                var data = document[CommonConst.CommonField.DATA];
                if (data != null)
                {
                    return(data.ToString());
                }
                else
                {
                    var dataFromFile = keyValueStorage.Get <string>(CommonConst.Collection.STATIC_CONTECT, document[CommonConst.CommonField.DISPLAY_ID].ToString());
                    if (dataFromFile != null)
                    {
                        return(CommonUtility.GetStringFromBase64(dataFromFile));
                    }
                }
            }
            else
            {
                string filePath = GetFullPath(path);
                System.Console.WriteLine(filePath);
                if (File.Exists(filePath))
                {
                    return(File.ReadAllText(filePath));
                }
            }
            return(null);
        }
예제 #11
0
        public static async Task ReadTest(IKeyValueStorage storage)
        {
            await storage.Clear();

            var rd = new Random();

            for (int i = 0; i < 50; i++)
            {
                await storage.Set("somekey" + i, "qwwwwwwww");
            }

            await storage.Set("mykey", "val");

            for (int i = 0; i < 500; i++)
            {
                var val = await storage.Get <string>("mykey");

                Assert.AreEqual("val", val);
            }
            await storage.Clear();
        }
예제 #12
0
        public static async Task SynchronizationTest(IKeyValueStorage storage)
        {
            await storage.Clear();

            var rd = new Random();

            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    await storage.Set("key" + rd.Next(10000, 99999).ToString(), "qwwwwwwww");
                }


                string key     = "key" + i.ToString();
                string prevKey = "key" + (i - 1).ToString();

                //await storage.Clear();
                await storage.Set(prevKey, null);

                var prevVal = await storage.GetOrDefault <string>(prevKey);

                if (prevVal != null)
                {
                    Assert.Fail($"Not null value after cleaning. Iteration {i}.");
                }

                await storage.Set(key, "val");

                var val = await storage.Get <string>(key);

                Assert.AreEqual(val, "val");
                var isContains = await storage.ContainsKey(key);

                Assert.IsTrue(isContains);
            }

            await storage.Clear();
        }
예제 #13
0
        public static async Task ComplexObjectTest(IKeyValueStorage storage)
        {
            var dictLikeComplexObj = new Dictionary <string, object>()
            {
                { "Val1", "10" },
                { "Val2", "aerertavt" },
                {
                    "Val3",
                    new Dictionary <string, object>()
                    {
                        { "Val1", 11 },
                        { "Val2", "fhg hgh fhxghhg" },
                    }
                }
            };
            await storage.Set("complex", dictLikeComplexObj);

            var obj = await storage.Get <ComplexType>("complex");

            Assert.AreEqual(obj.Val1, 10);
            Assert.AreEqual(obj.Val3.Val1, 11);
        }
예제 #14
0
        public static async Task TestScopes(IKeyValueStorage storage)
        {
            await storage.Set("ComplexType.Val1", 100);

            await storage.Set("ComplexType.Val2", "First scope string");

            await storage.Set("ComplexType.Val3.Val1", 200);

            await storage.Set("ComplexType.Val3.Val2", "Second scope string");

            await storage.Set("ComplexType.Val3.Val3.Val1", 300);

            await storage.Set("ComplexType.Val3.Val3.Val2", "Third scope string");

            var num = await storage.Get <int>("ComplexType.Val1");

            Assert.AreEqual(num, 100);
            num = await storage.Get <int>("ComplexType.Val3.Val1");

            Assert.AreEqual(num, 200);

            var str = await storage.Get <string>("ComplexType.Val3.Val2");

            Assert.AreEqual(str, "Second scope string");

            var complexType = await storage.Get <ComplexType>("ComplexType");

            Assert.AreEqual(complexType.Val3.Val3.Val1, 300);
            complexType = await storage.Get <ComplexType>("ComplexType.Val3");

            Assert.AreEqual(complexType.Val3.Val1, 300);

            await storage.Remove("ComplexType.Val3.Val3.Val1");

            var isContains = await storage.ContainsKey("ComplexType.Val3.Val3.Val1");

            Assert.IsFalse(isContains);
            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsTrue(isContains);

            await storage.Remove("ComplexType.Val3.Val3.Val2");

            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsFalse(isContains);
            isContains = await storage.ContainsKey("ComplexType");

            Assert.IsTrue(isContains);

            await storage.Set("ComplexType", 999);

            num = await storage.Get <int>("ComplexType");

            Assert.AreEqual(num, 999);
            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val1"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val2"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val1"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val3"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val3.Val1"));
            Assert.IsFalse(isContains);
        }
예제 #15
0
        private void btnGet_Click(object sender, EventArgs e)
        {
            var key = GetKey();

            tbValue.Text = _storage.Get(key);
        }