public async Task StoreOrderedThings(string key, IEnumerable <Thing> things) { try { await Initialize(); var thingsArray = things.ToArray(); var compressor = new BaconographyPortable.Model.Compression.CompressionService(); var compressedBytes = compressor.Compress(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(thingsArray))); //var compressedBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(thingsArray)); var gottenBlob = await _blobStoreDb.GetAsync(Encoding.UTF8.GetBytes(key)); if (gottenBlob != null) { await _blobStoreDb.UpdateAsync(Encoding.UTF8.GetBytes(key), compressedBytes); } else { await _blobStoreDb.InsertAsync(Encoding.UTF8.GetBytes(key), compressedBytes); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
private async Task <IEnumerable <Thing> > RetrieveOrderedThingsBG(string key) { await Initialize(); bool badElement = false; try { var gottenBlob = await _blobStoreDb.GetAsync(Encoding.UTF8.GetBytes(key)); if (gottenBlob != null) { var compressor = new BaconographyPortable.Model.Compression.CompressionService(); var decompressedBytes = compressor.Decompress(gottenBlob.ToArray()); //var decompressedBytes = gottenBlob.ToArray(); IEnumerable <Thing> result = JsonConvert.DeserializeObject <Thing[]>(Encoding.UTF8.GetString(decompressedBytes, 0, decompressedBytes.Length)); return(result); } } catch { badElement = true; } if (badElement) { try { await _blobStoreDb.DeleteAsync(Encoding.UTF8.GetBytes(key)); } catch { } } return(Enumerable.Empty <Thing>()); }
public async Task<IEnumerable<Thing>> RetrieveOrderedThings(string key, TimeSpan maxAge) { await Initialize(); try { using (var blobCursor = await _blobStoreDb.SeekAsync(_blobStoreDb.GetKeys()[0], BitConverter.GetBytes(key.GetHashCode()), DBReadFlags.WaitOnLock)) { if (blobCursor != null) { var gottenBlob = blobCursor.Get(); var microseconds = BitConverter.ToInt64(gottenBlob, 4); var updatedTime = new DateTime(microseconds * 10).AddYears(1969); var blobAge = DateTime.Now - updatedTime; if (blobAge <= maxAge) { var compressor = new BaconographyPortable.Model.Compression.CompressionService(); var decompressedBytes = compressor.Decompress(gottenBlob, 12); IEnumerable<Thing> result = JsonConvert.DeserializeObject<Thing[]>(Encoding.UTF8.GetString(decompressedBytes, 0, decompressedBytes.Length)); return result; } } } } catch(Exception ex) { var errorString = DBError.TranslateError((uint)ex.HResult); Debug.WriteLine(errorString); } return null; }
public async Task StoreOrderedThings(string key, IEnumerable<Thing> things) { try { await Initialize(); var thingsArray = things.ToArray(); var compressor = new BaconographyPortable.Model.Compression.CompressionService(); var compressedBytes = compressor.Compress(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(thingsArray))); var recordBytes = new byte[compressedBytes.Length + 12]; Array.Copy(compressedBytes, 0, recordBytes, 12, compressedBytes.Length); //the 8 bytes not written here will be filled with the current time stamp by kdb Array.Copy(BitConverter.GetBytes(key.GetHashCode()), recordBytes, 4); if (_terminateSource.IsCancellationRequested) return; using (var blobCursor = await _blobStoreDb.SeekAsync(_blobStoreDb.GetKeys()[0], BitConverter.GetBytes(key.GetHashCode()), DBReadFlags.WaitOnLock)) { if (_terminateSource.IsCancellationRequested) return; if (blobCursor != null) { await blobCursor.UpdateAsync(recordBytes); } else { await _blobStoreDb.InsertAsync(recordBytes); } } } catch(Exception ex) { var errorText = DBError.TranslateError((uint)ex.HResult); //throw new Exception(errorText); Debug.WriteLine(errorText); Debug.WriteLine(ex.ToString()); } }