public async Task <string> Post(Uri uri, object data)
        {
            var w    = Stopwatch.StartNew();
            var json = Jsonizer.Serialize(data);

            while (true)
            {
                using (var content = new StringContent(json, Encoding.UTF8, "application/json"))
                    using (var response = await _client.PostAsync(uri, content))
                    {
                        var message = await response.Content.ReadAsStringAsync();

                        if (response.IsSuccessStatusCode)
                        {
                            return(message);
                        }
                        if (response.StatusCode.EqualsAny(HttpStatusCode.BadGateway, HttpStatusCode.InternalServerError))
                        {
                            if (w.Elapsed.TotalMinutes < 15)
                            {
                                await Task.Delay(5000);

                                continue;
                            }
                        }
                        Loggers.Error(message);
                        response.EnsureSuccessStatusCode();
                        return(null);
                    }
            }
        }
 public T Deserialize <T>(string json)
 {
     try
     {
         //still need to find a way to detect if properties are missing
         return(Jsonizer.Deserialize <T>(json));
     }
     catch (Exception e)
     {
         Loggers.Warn(e.Message);
     }
     return(Jsonizer.Deserialize <T>(json));
 }
Exemple #3
0
        private async Task InitializeFromZip(ZipArchive zip)
        {
            foreach (var entry in zip.Entries)
            {
                using var stream = entry.Open();
                var item = await Jsonizer.DeserializeAsync <T>(stream);

                foreach (var upsert in _upserters)
                {
                    await upsert(new JsonTransaction <T>
                    {
                        Item  = item,
                        Write = false,
                    });
                }
            }
        }
Exemple #4
0
 public override string ToString() => Jsonizer.Serialize(this);
Exemple #5
0
        public Func <Key, ValueTask <T> > CreatePrimaryKey <Key>(Expression <Func <T, Key> > keySelectorExpression)
        {
            keySelectorExpression.NotNull(nameof(keySelectorExpression));
            if (!_primaryKeyCreated.TrySet(true))
            {
                throw new Exception("Primary Key already exists");
            }

            var keySelector = keySelectorExpression.Compile();
            var index       = new AtomicDictionary <Key, T>();

            _items = index.Values;
            _upserters.Add(async transaction =>
            {
                var key = keySelector(transaction.Item);
                if (key is null)
                {
                    throw new Exception($"Upsert failed, key is null. Key: {keySelectorExpression.ToString()}, Document: {typeof(T).FullName}");
                }
                if (transaction.Write && _settings.DataDir.IsNotNullOrWhiteSpace())
                {
                    var zipFile = GetFile(key.ToString());
                    var zipPath = zipFile.FullName;
                    using (await _fileLocks.GetOrAdd(zipPath).EnterAsync())
                    {
                        var tempFile = new FileInfo($"{zipPath}.temp");
                        using (var zip = ZipFile.Open(tempFile.FullName, ZipArchiveMode.Update))
                        {
                            var name         = zipFile.Name.SkipLast(zipFile.Extension.Length).AsString();
                            using var stream = zip.CreateEntry(name, CompressionLevel.Optimal).Open();
                            await Jsonizer.SerializeAsync(transaction.Item, stream);
                        }
                        await tempFile.MoveAsync(zipFile);
                    }
                }
                else if (index.ContainsKey(key))
                {
                    throw new Exception($"Duplicate entry found in db.json files. Key: {key}");
                }
                index[key] = transaction.Item;
            });
            _deleters.Add(async item =>
            {
                var key = keySelector(item);
                if (key is null)
                {
                    throw new Exception($"Delete failed, key is null. Key: {keySelectorExpression.ToString()}, Document: {typeof(T).FullName}");
                }
                if (index.TryRemove(key) && _settings.DataDir.IsNotNullOrWhiteSpace())
                {
                    var zip = GetFile(key.ToString());
                    using (await _fileLocks.GetOrAdd(zip.FullName).EnterAsync())
                        await zip.DeleteAsync();
                }
            });
            return(async key =>
            {
                await Initialize();

                return index.TryGetValue(key, out var item) ? item : default;
            });
        }