Commit ef482c1b authored by Akshay Mahendrakar's avatar Akshay Mahendrakar
Browse files

Immudb config map removed

AddNewImmuDbConnection - method removed
parent 45d1cd57
Branches
Tags
2 merge requests!270Core ImmuDB package and TOTP plugin - Implementation,!269Core ImmuDB package and TOTP plugin - Implementation
Showing with 17 additions and 71 deletions
......@@ -11,7 +11,6 @@ import (
)
var (
configs map[string]*ImmuHost // Immudb config details with host
instances map[string]immudb.ImmuClient // Immudb client connection with host
defaultHostName string
mutex sync.Mutex
......@@ -38,14 +37,11 @@ type KeyValue struct {
}
func init() {
configs = make(map[string]*ImmuHost)
instances = make(map[string]immudb.ImmuClient)
}
// InitNewImmuDbSession - Initializes connection to ImmuDB server using []ImmuHost data
func InitNewImmuDbSession(hosts []ImmuHost) error {
configs = make(map[string]*ImmuHost)
func InitImmuDBConnections(hosts []ImmuHost) error {
instances = make(map[string]immudb.ImmuClient)
mutex.Lock()
defer mutex.Unlock()
......@@ -61,11 +57,9 @@ func InitNewImmuDbSession(hosts []ImmuHost) error {
client = immudb.NewClient().WithOptions(dbConfig)
err := client.OpenSession(context.TODO(), []byte(v.Username), []byte(v.Password), v.Database)
if err != nil {
configs = make(map[string]*ImmuHost)
instances = make(map[string]immudb.ImmuClient)
return err
}
configs[v.HostName] = &v
instances[v.HostName] = client
if v.IsDefault {
defaultHostName = v.HostName
......@@ -74,33 +68,6 @@ func InitNewImmuDbSession(hosts []ImmuHost) error {
return nil
}
// AddNewImmuDbConnection - Creating and connecting to ImmuDB server using ImmuHost details
func AddNewImmuDbConnection(host ImmuHost) error {
mutex.Lock()
defer mutex.Unlock()
if host.IsDisabled {
return nil
}
var client immudb.ImmuClient
dbconfig := immudb.DefaultOptions()
dbconfig.Address = host.Address
dbconfig.Port = host.Port
client = immudb.NewClient().WithOptions(dbconfig)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
if err != nil {
return err
}
configs[host.HostName] = &host
instances[host.HostName] = client
if host.IsDefault {
defaultHostName = host.HostName
}
return nil
}
// DeleteImmuDbSession - Disconnecting Immu Database connection
func DeleteImmuDbSession(hostName string) error {
mutex.Lock()
......@@ -115,7 +82,7 @@ func DeleteImmuDbSession(hostName string) error {
}
// Get ImmuDB connection - immudb.ImmuClient
func getImmuDbConnection(hostName string) (immudb.ImmuClient, error) {
func GetImmuDbConnection(hostName string) (immudb.ImmuClient, error) {
mutex.Lock()
defer mutex.Unlock()
// Returning if there are no immudb connections present
......@@ -152,7 +119,7 @@ func GetImmuDAOWithHost(hostname string) *ImmuDAO {
// GetKeyData - Get single value for provided key
func (i *ImmuDAO) GetKeyData(key []byte) (*schema.Entry, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -161,7 +128,7 @@ func (i *ImmuDAO) GetKeyData(key []byte) (*schema.Entry, error) {
// GetVerifiedKeyData - Get single value for provided key with additional server-provided proof validation.
func (i *ImmuDAO) GetVerifiedKeyData(key []byte) (*schema.Entry, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -170,7 +137,7 @@ func (i *ImmuDAO) GetVerifiedKeyData(key []byte) (*schema.Entry, error) {
// GetAllKeysData - Getting all keys data
func (i *ImmuDAO) GetAllKeysData(keys [][]byte) (*schema.Entries, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -184,7 +151,7 @@ func (i *ImmuDAO) GetAllKeysData(keys [][]byte) (*schema.Entries, error) {
Desc - Sorting in descending order
*/
func (i *ImmuDAO) GetKeyHistory(key []byte, offset uint64, limit int32, desc bool) (*schema.Entries, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -203,7 +170,7 @@ func (i *ImmuDAO) GetKeyHistory(key []byte, offset uint64, limit int32, desc boo
// SetKeyData - Commits provided value for provided key
func (i *ImmuDAO) SetKeyData(key, value []byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -212,7 +179,7 @@ func (i *ImmuDAO) SetKeyData(key, value []byte) (*schema.TxHeader, error) {
// SetVerifiedKeyData - Commits provided value for provided key and also requests a server-generated proof
func (i *ImmuDAO) SetVerifiedKeyData(key, value []byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -222,7 +189,7 @@ func (i *ImmuDAO) SetVerifiedKeyData(key, value []byte) (*schema.TxHeader, error
// SetAllKeysData - Set multiple keys with their values in a single transaction.
func (i *ImmuDAO) SetAllKeysData(kv []KeyValue) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -236,7 +203,7 @@ func (i *ImmuDAO) SetAllKeysData(kv []KeyValue) (*schema.TxHeader, error) {
// DeleteKey - Key is deleted logically from Database, physically it is present in database
func (i *ImmuDAO) DeleteKey(keys [][]byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
client, err := GetImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
......@@ -249,7 +216,7 @@ func (i *ImmuDAO) DeleteKey(keys [][]byte) (*schema.TxHeader, error) {
// ExpirableKeySet - Sets value for the key with expiration time
func (i *ImmuDAO) ExpirableKeySet(key, value []byte, expiresAt time.Time) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName)
client, err := GetImmuDbConnection(i.HostName)
if err != nil {
return nil, err
}
......
......@@ -12,7 +12,7 @@ import (
func init() {
// Creating and connecting two Immu database connection: ImmuDbHost1,ImmuDbHost4
// err := InitNewImmuDbSession([]ImmuHost{{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false},
// err := InitImmuDBConnections([]ImmuHost{{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false},
// {"ImmuDbHost4", "localhost", 3322, "defaultdb", "immudb", "immudb", false, false}})
// if err != nil {
// panic(err)
......@@ -20,8 +20,8 @@ func init() {
}
// Test - InitNewImmuDbSession
func TestInitNewImmuDbSession(t *testing.T) {
// Test - InitImmuDBConnections
func TestInitImmuDBConnections(t *testing.T) {
type args struct {
hosts []ImmuHost
}
......@@ -37,29 +37,8 @@ func TestInitNewImmuDbSession(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := InitNewImmuDbSession(tt.args.hosts); (err != nil) != tt.wantErr {
t.Errorf("InitNewImmuDbSession() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAddNewImmuDbConnection(t *testing.T) {
type args struct {
host ImmuHost
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{host: ImmuHost{"LocalHostDB1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false}}, wantErr: false}, // PASS
{name: "Test 2", args: args{host: ImmuHost{"LocalHostDB2", "localhost", 3422, "defaultdbname", "wrongimmudb", "immudb", true, false}}, wantErr: true}, // FAIL - Wrong connection details
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := AddNewImmuDbConnection(tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("AddNewImmuDbConnection() error = %v, wantErr %v", err, tt.wantErr)
if err := InitImmuDBConnections(tt.args.hosts); (err != nil) != tt.wantErr {
t.Errorf("InitImmuDBConnections() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
......@@ -108,7 +87,7 @@ func Test_getImmuDbConnection(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getImmuDbConnection(tt.args.hostName)
got, err := GetImmuDbConnection(tt.args.hostName)
if (err != nil) != tt.wantErr {
t.Errorf("getImmuDbConnection() error = %v, wantErr %v", err, tt.wantErr)
return
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment