use lru cache for storing account access and org key map

This commit is contained in:
amalraj.manigmail.com 2018-11-07 20:36:42 +08:00
parent 6c34dc5199
commit 3cb4bc2536
1 changed files with 33 additions and 54 deletions

View File

@ -1,8 +1,8 @@
package types
import (
"sync"
import (
"github.com/ethereum/go-ethereum/common"
"github.com/hashicorp/golang-lru"
)
type AccessType uint8
@ -15,38 +15,27 @@ const (
)
type PermStruct struct {
AcctId common.Address
AcctId common.Address
AcctAccess AccessType
}
type OrgStruct struct {
OrgId string
Keys []string
Keys []string
}
type PermAccountsMap map[common.Address][] *PermStruct
var AcctMap, AcctMapErr = lru.NewARC(100)
type PermOrgKeyMap map[string][] *OrgStruct
var OrgKeyMap, OrgKeyMapErr = lru.NewARC(100)
var AcctMap = make(map[common.Address] *PermStruct)
var OrgKeyMap = make(map[string] *OrgStruct)
func AddAccountAccess(acctId common.Address, access uint8) {
mu := sync.RWMutex{}
mu.Lock()
AcctMap[acctId] = &PermStruct {AcctId : acctId, AcctAccess : AccessType(access)}
mu.Unlock()
func AddAccountAccess(acctId common.Address, access uint8) {
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, AcctAccess: AccessType(access)})
}
func GetAcctAccess(acctId common.Address) AccessType {
mu := sync.RWMutex{}
if len(AcctMap) != 0 {
if _, ok := AcctMap[acctId]; ok {
mu.RLock()
acctAccess := AcctMap[acctId].AcctAccess
mu.RUnlock()
return acctAccess
if AcctMap.Len() != 0 {
if val, ok := AcctMap.Get(acctId); ok {
vo := val.(*PermStruct)
return vo.AcctAccess
}
}
if len(AcctMap) == 0 {
@ -56,50 +45,40 @@ func GetAcctAccess(acctId common.Address) AccessType {
}
}
func AddOrgKey(orgId string, keys string){
if len(OrgKeyMap) != 0 {
if _, ok := OrgKeyMap[orgId]; ok {
func AddOrgKey(orgId string, key string) {
if OrgKeyMap.Len() != 0 {
if val, ok := OrgKeyMap.Get(orgId); ok {
// Org record exists. Append the key only
OrgKeyMap[orgId].Keys = append (OrgKeyMap[orgId].Keys, keys)
vo := val.(*OrgStruct)
vo.Keys = append(vo.Keys, key)
return
}
}
// first record into the map or firts record for the org
var locKeys []string
locKeys = append(locKeys, keys);
OrgKeyMap[orgId] = &OrgStruct {OrgId : orgId, Keys : locKeys}
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
}
func DeleteOrgKey(orgId string, keys string){
if len(OrgKeyMap) != 0 {
if _, ok := OrgKeyMap[orgId]; ok {
for i, keyVal := range OrgKeyMap[orgId].Keys{
if keyVal == keys {
OrgKeyMap[orgId].Keys = append(OrgKeyMap[orgId].Keys[:i], OrgKeyMap[orgId].Keys[i+1:]...)
break
}
func DeleteOrgKey(orgId string, key string) {
if val, ok := OrgKeyMap.Get(orgId); ok {
vo := val.(*OrgStruct)
for i, keyVal := range vo.Keys {
if keyVal == key {
vo.Keys = append(vo.Keys[:i], vo.Keys[i+1:]...)
break
}
}
}
}
func ResolvePrivateForKeys(orgId string ) []string {
func ResolvePrivateForKeys(orgId string) []string {
var keys []string
mu := sync.RWMutex{}
if len(OrgKeyMap) != 0 {
if _, ok := OrgKeyMap[orgId]; ok {
if len(OrgKeyMap[orgId].Keys) > 0{
mu.RLock()
keys = OrgKeyMap[orgId].Keys
mu.RUnlock()
} else {
keys = append(keys, orgId)
}
return keys
if val, ok := OrgKeyMap.Get(orgId); ok {
vo := val.(*OrgStruct)
if len(vo.Keys) > 0 {
keys = vo.Keys
} else {
keys = append(keys, orgId)
}
return keys
}
keys = append(keys, orgId)
return keys