/// <summary> /// DeleteAsync /// </summary> /// <param name="keys"></param> /// <param name="versions"></param> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task DeleteAsync <T>(IEnumerable <string> keys, IEnumerable <int> versions) where T : KVStoreEntity, new() { ThrowIf.NullOrEmpty(versions, nameof(versions)); if (keys.Count() != versions.Count()) { throw Exceptions.VersionsKeysNotEqualError(); } KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { await _engine.EntityDeleteAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !, keys, versions ).ConfigureAwait(false); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, keys: keys, values: versions, innerException: ex); } }
/// <summary> /// DeleteAllAsync /// </summary> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task DeleteAllAsync <T>() where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { await _engine.EntityDeleteAllAsync( entityDef.KVStoreName, entityDef.EntityType.FullName ! ).ConfigureAwait(false); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, null, ex); } }
/// <summary> /// 反应Version变化 /// </summary> /// <exception cref="KVStoreException"></exception> public async Task <IEnumerable <T?> > GetAllAsync <T>() where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { IEnumerable <Tuple <string?, int> > tuples = await _engine.EntityGetAllAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !).ConfigureAwait(false); return(MapTupleToEntity <T>(tuples)); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(type: typeof(T).FullName, storeName: entityDef.KVStoreName, key: null, innerException: ex); } }
private static string GetEntityKey <T>(T item, KVStoreEntityDef entityDef) where T : KVStoreEntity, new() { StringBuilder builder = new StringBuilder(); int count = entityDef.KeyPropertyInfos.Count; for (int i = 0; i < count; ++i) { builder.Append(entityDef.KeyPropertyInfos[i].GetValue(item)); if (i != count - 1) { builder.Append('_'); } } return(builder.ToString()); }
/// <summary> /// 反应Version变化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <param name="lastUser"></param> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task UpdateAsync <T>(IEnumerable <T> items, string lastUser) where T : KVStoreEntity, new() { if (!items.Any()) { return; } ThrowIf.NotValid(items, nameof(items)); KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { IEnumerable <int> originalVersions = items.Select(t => t.Version).ToArray(); foreach (var t in items) { t.LastUser = lastUser; t.LastTime = TimeUtil.UtcNow; } await _engine.EntityUpdateAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !, items.Select(t => GetEntityKey(t, entityDef)), items.Select(t => SerializeUtil.ToJson(t)), originalVersions).ConfigureAwait(false); //反应Version变化 foreach (var t in items) { t.Version++; } } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, items, ex); } }
public string GetEntityKey <T>(T item) where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); return(GetEntityKey(item, entityDef)); }