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

Add single database connection functionality added, unit test, example added...

Add single database connection functionality added, unit test,  example added for the same, and readme file changes done.
parent b0be9a36
Branches
3 merge requests!275#20 Add single database connection functionality added, unit test, added example and added go 1.20.8 support.,!274Add single database connection functionality added, unit test, example added...,!272Coreimmudb add single database connection functionality
Pipeline #10773 failed with stages
in 0 seconds
Showing with 143 additions and 125 deletions
......@@ -2,63 +2,71 @@
### **Steps for connecting to Immudb**<br>
- There are two ways to connect to the database using package.
- The package provides three methods to establish a database connection. If an error occurs during the connection establishment then error is returned.
1. **`coreimmudb.InitUsingJson()`** - Provide JSON file path to read ImmuDB config details.<br>
```
[
{
"hostName": "ImmuDbHost1",
"server": "127.0.0.1",
"port": 3322,
"database": "defaultdb",
"username": "immudb",
"password": "immudb",
"dir": "",
"isDefault": true,
"isDisabled": false
}
]
```
Hostname (`hostName`) - Hostname for the database connection.<br>
Server (`server`) - Database server address.<br>
Port (`port`) - Port on which ImmuDB running.<br>
Database (`database`) - Database name.<br>
Username (`username`) - Database username<br>
Password (`password`) - Database password<br>
Dir (`dir`) - Storing .identity (database server identity) and .state file <br>
IsDefault (`isDefault`) - Default database selection<br>
IsDisabled (`isDisabled`) - Skip connection <br>
```
[
{
"hostName": "ImmuDbHost1",
"server": "127.0.0.1",
"port": 3322,
"database": "defaultdb",
"username": "immudb",
"password": "immudb",
"dir": "",
"maxRecvMsgSize: 4,
"isDefault": true,
"isDisabled": false
}
]
```
2. `coreimmudb.InitImmuDBConnections()` - Provide Immudb config details in `[]coreimmudb.ImmuHost`
```
[
coreimmudb.ImmuHost{
"HostName": "ImmuDbHost1",
"Server": "127.0.0.1",
"Port": 3322,
"database": "defaultdb",
"Username": "immudb",
"Password": "immudb",
"Dir": "",
"IsDefault": true,
"IsDisabled": false
}
]
```
Hostname (`hostName`) - Hostname for the database connection.<br>
Server (`server`) - Database server address.<br>
Port (`port`) - Port on which ImmuDB running.<br>
Database (`database`) - Database name.<br>
Username (`username`) - Database username<br>
Password (`password`) - Database password<br>
Dir (`dir`) - Storing .identity (database server identity) and .state file <br>
IsDefault (`isDefault`) - Default database selection<br>
IsDisabled (`isDisabled`) - Skip connection <br>
```
[
coreimmudb.ImmuHost{
"HostName": "ImmuDbHost1",
"Server": "127.0.0.1",
"Port": 3322,
"database": "defaultdb",
"Username": "immudb",
"Password": "immudb",
"Dir": "",
"MaxRecvMsgSize": 4,
"IsDefault": true,
"IsDisabled": false
}
]
```
3. `coreimmudb.AddDBConnection` - Provide Immudb connection details using `coreimmudb.ImmuHost`
```
coreimmudb.ImmuHost{
"HostName": "ImmuDbHost1",
"Server": "127.0.0.1",
"Port": 3322,
"database": "defaultdb",
"Username": "immudb",
"Password": "immudb",
"Dir": "",
"MaxRecvMsgSize": 4,
"IsDefault": true,
"IsDisabled": false
}
```
## **Parameters**<br>
Hostname (`hostName`) - Hostname for the database connection.<br>
Server (`server`) - Database server address.<br>
Port (`port`) - Port on which ImmuDB running.<br>
Database (`database`) - Database name.<br>
Username (`username`) - Database username<br>
Password (`password`) - Database password<br>
Dir (`dir`) - Storing .identity (database server identity) and .state file <br>
MaxRecvMsgSize (`maxRecvMsgSize`) - Recieve message maximum size in MB (Default: 4MB) <br>
IsDefault (`isDefault`) - Default database selection<br>
IsDisabled (`isDisabled`) - Skip connection <br>
<br>
### **Disconnecting ImmuDB connection**<br>
......
......@@ -83,53 +83,16 @@ func InitUsingJson(filePath string) error {
}
instances = make(map[string]immudb.ImmuClient)
mutex.Lock()
defer mutex.Unlock()
for _, host := range hosts {
if host.IsDisabled { // Not connecting to database if isDisabled == true
continue
}
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = host.Server
opts.Port = host.Port
if host.Dir != "" {
// Checking if folder name contains extension
if filepath.Ext(host.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION " + filepath.Ext(host.Dir))
}
path := strings.Split(host.Dir, "/")
// Hiding folder by adding prefix '.'
if !strings.HasPrefix(path[len(path)-1], ".") {
path[len(path)-1] = "." + path[len(path)-1]
}
host.Dir = strings.Join(path, "/")
if _, err := os.Stat(host.Dir); os.IsNotExist(err) { // Checking directory is present and accessible
if err = os.Mkdir(host.Dir, 0740); err != nil {
return err
}
}
opts.Dir = host.Dir
}
if host.MaxRecvMsgSize > 0 { // Setting maximum receive message size if provided
opts.MaxRecvMsgSize = host.MaxRecvMsgSize * 1024 * 1024
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
err := AddDBConnection(host)
if err != nil {
instances = make(map[string]immudb.ImmuClient)
return err
}
instances[host.HostName] = client
if host.IsDefault {
DefaultHostName = host.HostName
}
}
return nil
}
......@@ -137,51 +100,65 @@ func InitUsingJson(filePath string) error {
// InitNewImmuDbSession - Initializes connection to ImmuDB server using []ImmuHost data
func InitImmuDBConnections(hosts []ImmuHost) error {
instances = make(map[string]immudb.ImmuClient)
mutex.Lock()
defer mutex.Unlock()
for _, host := range hosts {
if host.IsDisabled { // Not connecting to database if isDisabled == true
continue
}
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = host.Server
opts.Port = host.Port
if host.Dir != "" {
// Checking if folder name contains extension
if filepath.Ext(host.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION " + filepath.Ext(host.Dir))
}
path := strings.Split(host.Dir, "/")
// Hiding folder by adding prefix '.'
if !strings.HasPrefix(path[len(path)-1], ".") {
path[len(path)-1] = "." + path[len(path)-1]
}
host.Dir = strings.Join(path, "/")
if _, err := os.Stat(host.Dir); os.IsNotExist(err) { // Checking directory is present and accessible
if err = os.Mkdir(host.Dir, 0740); err != nil {
return err
}
}
opts.Dir = host.Dir
}
if host.MaxRecvMsgSize > 0 { // Setting maximum receive message size if provided
opts.MaxRecvMsgSize = host.MaxRecvMsgSize * 1024 * 1024
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
err := AddDBConnection(host)
if err != nil {
instances = make(map[string]immudb.ImmuClient)
return err
}
instances[host.HostName] = client
if host.IsDefault {
DefaultHostName = host.HostName
}
return nil
}
// AddDBConnection - Initializes single connection of ImmuDB server using ImmuHost data
func AddDBConnection(host ImmuHost) error {
mutex.Lock()
defer mutex.Unlock()
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = host.Server
opts.Port = host.Port
if host.Dir != "" {
// Checking if folder name contains extension
if filepath.Ext(host.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION " + filepath.Ext(host.Dir))
}
path := strings.Split(host.Dir, "/")
// Hiding folder by adding prefix '.'
if !strings.HasPrefix(path[len(path)-1], ".") {
path[len(path)-1] = "." + path[len(path)-1]
}
host.Dir = strings.Join(path, "/")
if _, err := os.Stat(host.Dir); os.IsNotExist(err) { // Checking directory is present and accessible
if err = os.Mkdir(host.Dir, 0740); err != nil {
return err
}
}
opts.Dir = host.Dir
}
if host.MaxRecvMsgSize > 0 { // Setting maximum receive message size if provided
opts.MaxRecvMsgSize = host.MaxRecvMsgSize * 1024 * 1024
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
if err != nil {
return err
}
instances[host.HostName] = client
if host.IsDefault {
DefaultHostName = host.HostName
}
return nil
}
......
......@@ -94,6 +94,39 @@ func ExampleInitImmuDBConnections() {
loggermdl.LogInfo("Immudb connection established!")
}
// Test - AddDBConnection
func TestAddDBConnection(t *testing.T) {
type args struct {
host ImmuHost
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{host: ImmuHost{HostName: "Host1", Server: "127.0.0.1", Port: 3322, Database: "defaultdb", Username: "username", Password: "password"}}, wantErr: false}, // PASS
{name: "Test 2", args: args{host: ImmuHost{HostName: "Host2", Server: "127.0.0.1", Port: 3322, Database: "defaultdb", Username: "username", Password: "WrongPassword"}}, wantErr: true}, // FAIL - Wrong credentails
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := AddDBConnection(tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("AddDBConnection() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func ExampleAddDBConnection() {
host1 := ImmuHost{HostName: "Host1", Server: "127.0.0.1", Port: 3322, Database: "defaultdb", Username: "username", Password: "password", Dir: "./immudb", IsDefault: true}
// Establishing database connection for above host details
err := AddDBConnection(host1)
if err != nil {
loggermdl.LogError(err)
return
}
loggermdl.LogInfo("Immudb single connection successfully established!")
}
// Test to disconnect ImmuDb connection / session
func TestDeleteImmuDbSession(t *testing.T) {
type args struct {
......
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