Browse Source

ft: 添加transaction 事务执行

master
SisMaker 5 months ago
parent
commit
bc877917d4
1 changed files with 16 additions and 16 deletions
  1. +16
    -16
      src/eGLock.erl

+ 16
- 16
src/eGLock.erl View File

@ -124,13 +124,13 @@ lockApply(KeyOrKeys, MFAOrFun, TimeOut) ->
true -> true ->
try doApply(MFAOrFun) try doApply(MFAOrFun)
catch C:R:S -> catch C:R:S ->
{error, {lockApplyError, {C, R, S}}}
error({lockApplyError, KeyOrKeys, MFAOrFun, {C, R, S}})
after after
eNifLock:releaseLocks(KeyIxs), eNifLock:releaseLocks(KeyIxs),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, KeyOrKeys, MFAOrFun})
end; end;
_ -> _ ->
KeyIx = erlang:phash2(KeyOrKeys, ?eGLockSize), KeyIx = erlang:phash2(KeyOrKeys, ?eGLockSize),
@ -138,13 +138,13 @@ lockApply(KeyOrKeys, MFAOrFun, TimeOut) ->
true -> true ->
try doApply(MFAOrFun) try doApply(MFAOrFun)
catch C:R:S -> catch C:R:S ->
{error, {lockApplyError, {KeyOrKeys, MFAOrFun}, {C, R, S}}}
error({lockApplyError, KeyOrKeys, MFAOrFun, {C, R, S}})
after after
eNifLock:releaseLock(KeyIx), eNifLock:releaseLock(KeyIx),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, KeyOrKeys, MFAOrFun})
end end
end. end.
@ -170,13 +170,13 @@ lockGet({?undefTab, Key = GetKey}, TimeOut) ->
try try
#{{?undefTab, GetKey} => undefined} #{{?undefTab, GetKey} => undefined}
catch C:R:S -> catch C:R:S ->
{error, {lockGetError, {?undefTab, Key}, {C, R, S}}}
error({lockGetError, {?undefTab, Key}, {C, R, S}})
after after
eNifLock:releaseLock(KeyIx), eNifLock:releaseLock(KeyIx),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, {?undefTab, Key}})
end; end;
lockGet({EtsTab, Key} = GetKey, TimeOut) -> lockGet({EtsTab, Key} = GetKey, TimeOut) ->
KeyIx = erlang:phash2(GetKey, ?eGLockSize), KeyIx = erlang:phash2(GetKey, ?eGLockSize),
@ -185,13 +185,13 @@ lockGet({EtsTab, Key} = GetKey, TimeOut) ->
try try
#{GetKey => getEtsTabValue(EtsTab, Key, undefined)} #{GetKey => getEtsTabValue(EtsTab, Key, undefined)}
catch C:R:S -> catch C:R:S ->
{error, {lockGetError, GetKey, {C, R, S}}}
error({lockGetError, GetKey, {C, R, S}})
after after
eNifLock:releaseLock(KeyIx), eNifLock:releaseLock(KeyIx),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, GetKey})
end; end;
lockGet({?undefTab, Key = GetKey, DefValue}, TimeOut) -> lockGet({?undefTab, Key = GetKey, DefValue}, TimeOut) ->
KeyIx = erlang:phash2(GetKey, ?eGLockSize), KeyIx = erlang:phash2(GetKey, ?eGLockSize),
@ -200,13 +200,13 @@ lockGet({?undefTab, Key = GetKey, DefValue}, TimeOut) ->
try try
#{{?undefTab, GetKey} => getDefValue(DefValue)} #{{?undefTab, GetKey} => getDefValue(DefValue)}
catch C:R:S -> catch C:R:S ->
{error, {lockGetError, {?undefTab, Key, DefValue}, {C, R, S}}}
error({lockGetError, {?undefTab, Key, DefValue}, {C, R, S}})
after after
eNifLock:releaseLock(KeyIx), eNifLock:releaseLock(KeyIx),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, {?undefTab, Key, DefValue}})
end; end;
lockGet({EtsTab, Key, DefValue}, TimeOut) -> lockGet({EtsTab, Key, DefValue}, TimeOut) ->
GetKey = {EtsTab, Key}, GetKey = {EtsTab, Key},
@ -216,13 +216,13 @@ lockGet({EtsTab, Key, DefValue}, TimeOut) ->
try try
#{GetKey => getEtsTabValue(EtsTab, Key, DefValue)} #{GetKey => getEtsTabValue(EtsTab, Key, DefValue)}
catch C:R:S -> catch C:R:S ->
{error, {lockGetError, {EtsTab, Key, DefValue}, {C, R, S}}}
error({lockGetError, {EtsTab, Key, DefValue}, {C, R, S}})
after after
eNifLock:releaseLock(KeyIx), eNifLock:releaseLock(KeyIx),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, {EtsTab, Key, DefValue}})
end; end;
lockGet(EtsTabKeys, TimeOut) -> lockGet(EtsTabKeys, TimeOut) ->
{KeyIxs, KeysMap} = getKeyIxAndMaps(EtsTabKeys, [], #{}), {KeyIxs, KeysMap} = getKeyIxAndMaps(EtsTabKeys, [], #{}),
@ -231,13 +231,13 @@ lockGet(EtsTabKeys, TimeOut) ->
try try
#{OneGetKey => getEtsTabValue(OneEtsTab, OneKey, OneDefValue) || {OneEtsTab, OneKey} = OneGetKey := OneDefValue <- KeysMap} #{OneGetKey => getEtsTabValue(OneEtsTab, OneKey, OneDefValue) || {OneEtsTab, OneKey} = OneGetKey := OneDefValue <- KeysMap}
catch C:R:S -> catch C:R:S ->
{error, {lockGetError, EtsTabKeys, {C, R, S}}}
error({lockGetError, EtsTabKeys, {C, R, S}})
after after
eNifLock:releaseLocks(KeyIxs), eNifLock:releaseLocks(KeyIxs),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, EtsTabKeys})
end. end.
getKeyIxAndMaps([], IxAcc, KeysMap) -> {IxAcc, KeysMap}; getKeyIxAndMaps([], IxAcc, KeysMap) -> {IxAcc, KeysMap};
@ -290,13 +290,13 @@ transaction(EtsTabKeys, MFAOrFun, TimeOut) ->
catch catch
throw:Throw -> Throw; throw:Throw -> Throw;
C:R:S -> C:R:S ->
{error, {lockTransactionError, {MFAOrFun, EtsTabKeys}, {C, R, S}}}
error({lockTransactionError, EtsTabKeys, MFAOrFun, {C, R, S}})
after after
eNifLock:releaseLocks(KeyIxs), eNifLock:releaseLocks(KeyIxs),
ok ok
end; end;
lockTimeout -> lockTimeout ->
{error, lockTimeout}
error({lockTimeout, EtsTabKeys, MFAOrFun})
end. end.
getDefValue(undefined) -> undefined; getDefValue(undefined) -> undefined;

Loading…
Cancel
Save