CI: Add silent prescan SonarCloud job
[ric-plt/sdlgo.git] / doc.go
1 /*
2    Copyright (c) 2019 AT&T Intellectual Property.
3    Copyright (c) 2018-2019 Nokia.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 /*
24 Package sdlgo provides a lightweight, high-speed interface for accessing shared data storage.
25
26 Shared Data Layer (SDL) is a concept where applications can use and share data using a common
27 storage. The storage must be optimised for very high tranactional throughput and very low
28 latency. Sdlgo is a library which provides applications an API to read and write data
29 to a common storage using key-value paradigm. In addition to this, sdlgo provides an
30 event mechanism that can be used to notify listeners that data was changed.
31
32 This SDL version assumes that the DBAAS service provided by O-RAN community is
33 working as a storage backend.
34
35 All functions except receiving of notifications are safe for concurrent usage by
36 multiple goroutines.
37
38 Namespace
39
40 A shared data layer connection is instantiated to a namespace and data is
41 always read and modified within the namespace. Namespace provides data isolation across clients.
42 Namespace instantiation happens when the sdlgo instance is created.
43 E.g. the following code sets the SDL instance to use "example" as a namespace
44   sdl := sdlgo.NewSdlInstance("example", sdlgo.NewDatabase())
45 Applications can use several namespaces at the same time by creating several sdlgo instances.
46
47 Database connection
48
49 In addition to creating the SDL instance, application is responsible for creating the backend
50 database connection with NewDatabase() method. When the connection is created, sdlgo shall open a
51 tcp connection to backend database immediatelly. The resulting connection may be used with several
52 SDL instances if application needs to use several namespaces at a time. E.g.
53   connection := sdlgo.NewDatabase()
54   ns1 := sdlgo.NewSdlInstance("ns1", connection)
55   ns2 := sdlgo.NewSdlInstance("ns2", connection)
56 For the backend database connection a circuit breaker design is used. If the connection fails,
57 an error is returned immediatelly to application. Restoration of the connection happens
58 automatically by SDL and application should retry the operation again after a while.
59
60 Database service is discovered by using environment variables DBAAS_SERVICE_HOST and
61 DBAAS_SERVICE_PORT. If not set, localhost and port 6379 are used by default.
62
63 Keys and data
64
65 Clients save key-value pairs. Keys are allways strings. The types of the values must be of a basic
66 type, i.e. string, integer or byte array or slice. This means that the internal structures, like
67 protobufs or JSON objects, must be serialised to a byte array or slice before passing it to SDL.
68 Clients are responsible for managing the keys within a namespace.
69
70 Some examples on how to set the data using different kind of input parameters:
71
72 Basic usage, keys and values are given as own parameters (with mixed data types)
73   err := s.Set("key1", "value1", "key2", 2)
74
75 Keys and values inside a slice (again with mixed types, thus empty interface used as a type)
76   exampleSlice := []interface{"key1", "value1", "key2", 2}
77   err := s.Set(exampleSlice)
78
79 Data stored to a byte array
80   data := make([]byte), 3
81   data[0] = 1
82   data[1] = 2
83   data[2] = 3
84   s.Set("key", data)
85
86 Keys and values stored into a map (byte array "data" used from previous example)
87   mapData := map[string]interface{
88     "key1" : "data",
89     "key2" : 2,
90     "key3" : data,
91   }
92
93 When data is read from SDL storage, a map is returned where the requested key works as map key.
94 If the key was not found, the value for the given key is nil. It is possible to request several
95 key with one Get() call.
96
97 Groups
98
99 SDL groups are unordered collections of members where each member is unique. Using the SDL API
100 it is possible to add/remove members from a group, remove the whole group or do queries like the
101 size of a group and if member belongs to a group. Like key-value storage, groups are per namespace.
102
103 Events
104
105 Events are a publish-subscribe pattern to indicate interested parties that there has been a change
106 in data. Delivery of the events are not guaranteed. In SDL, events are happening via channels and
107 channels are per namespace. It is possible to publish several kinds of events through one channel.
108
109 In order to publish changes to SDL data, the publisher need to call an API function that supports
110 publishing. E.g.
111    err := sdl.SetAndPublish([]string{"channel1", "event1", "channel2", "event2"}, "key", "value")
112 This example will publish event1 to channel1 and event2 in channel2 after writing the data.
113
114 When subscribing the channels, the application needs to first create an SDL instance for the desired
115 namespace. The subscription happens using the SubscribeChannel() API function. The parameters for
116 the function takes a callback function and one or many channels to be subscribed. When an event is
117 received for the given channel, the given callback function shall be called with one or many events.
118 It is possible to make several subscriptions for different channels using different callback
119 functions if different kind of handling is required.
120
121   sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
122
123   cb1 := func(channel string, event ...string) {
124     fmt.Printf("cb1: Received %s from channel %s\n", event, channel)
125   }
126
127   cb2 := func(channel string, event ...string) {
128     fmt.Printf("cb2: Received %s from channel %s\n", event, channel)
129   }
130
131   sdl.SubscribeChannel(cb1, "channel1", "channel2")
132   sdl.SubscribeChannel(cb2, "channel3")
133
134 This example subscribes three channels from "namespace" and assigns cb1 for channel1 and channel2
135 whereas channel3 is assigned to cb2.
136
137 The callbacks are called from a context of a goroutine that is listening for the events. When
138 application receives events, the preferred way to do the required processing of an event (e.g. read
139 from SDL) is to do it in another context, e.g. by triggering applications own goroutine using Go
140 channels. By doing like this, blocking the receive routine and possibly loosing events, can be
141 avoided.
142 */
143 package sdlgo