X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=sdl.go;h=312654226d66a0c122874a2cf50fc6998134b488;hb=refs%2Fchanges%2F16%2F1316%2F1;hp=426c2c46e468c2edb755f544dc2145868a73a591;hpb=a10caff26de79e06caac2d00d3c11218e2d7ee87;p=ric-plt%2Fsdlgo.git diff --git a/sdl.go b/sdl.go index 426c2c4..3126542 100644 --- a/sdl.go +++ b/sdl.go @@ -42,21 +42,31 @@ type SdlInstance struct { iDatabase } +//Database struct is a holder for the internal database instance. Applications +//can use this exported data type to locally store a reference to database +//instance returned from NewDabase() function. +type Database struct { + instance iDatabase +} + //NewDatabase creates a connection to database that will be used -//as a backend for the key-value storage. The returned value shall -//be given as a parameter when calling NewKeyValStorage -func NewDatabase() *sdlgoredis.DB { - return sdlgoredis.Create() +//as a backend for the key-value storage. The returned value +//can be reused between multiple SDL instances in which case each instance +//is using the same connection. +func NewDatabase() *Database { + return &Database{ + instance: sdlgoredis.Create(), + } } //NewSdlInstance creates a new sdl instance using the given namespace. //The database used as a backend is given as a parameter -func NewSdlInstance(NameSpace string, db iDatabase) *SdlInstance { +func NewSdlInstance(NameSpace string, db *Database) *SdlInstance { return &SdlInstance{ nameSpace: NameSpace, nsPrefix: "{" + NameSpace + "},", eventSeparator: "___", - iDatabase: db, + iDatabase: db.instance, } } @@ -78,6 +88,8 @@ func NewSdlInstance(NameSpace string, db iDatabase) *SdlInstance { //callback as quickly as possible. E.g. reading in callback context should be avoided //and using of Go signals is recommended. Also it should be noted that in case of several //events received from different channels, callbacks are called in series one by one. +// +//This function is NOT SAFE FOR CONCURRENT USE by multiple goroutines. func (s *SdlInstance) SubscribeChannel(cb func(string, ...string), channels ...string) error { s.SubscribeChannelDB(cb, s.nsPrefix, s.eventSeparator, s.setNamespaceToChannels(channels...)...) return nil @@ -418,7 +430,7 @@ func (s *SdlInstance) RemoveAllAndPublish(channelsAndEvents []string) error { if len(channelsAndEvents) == 0 { return s.Del(keys) } - if err := s.checkChannelsAndEvents("RemoveIfAndPublish", channelsAndEvents); err != nil { + if err := s.checkChannelsAndEvents("RemoveAllAndPublish", channelsAndEvents); err != nil { return err } channelsAndEventsPrepared := s.prepareChannelsAndEvents(channelsAndEvents) @@ -590,7 +602,7 @@ type Lock struct { } type iDatabase interface { - SubscribeChannelDB(cb sdlgoredis.ChannelNotificationCb, channelPrefix, eventSeparator string, channels ...string) + SubscribeChannelDB(cb func(string, ...string), channelPrefix, eventSeparator string, channels ...string) UnsubscribeChannelDB(channels ...string) MSet(pairs ...interface{}) error MSetMPub(channelsAndEvents []string, pairs ...interface{}) error