Skip to content
db.go 5.07 KiB
Newer Older
Michel Roy's avatar
Michel Roy committed
/*
 * Copyright (c) 2019  InterDigital Communications, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package couchdb

import (
	"context"
Michel Roy's avatar
Michel Roy committed

	"github.com/flimzy/kivik"
	_ "github.com/go-kivik/couchdb"
	"github.com/go-kivik/couchdb/chttp"
Michel Roy's avatar
Michel Roy committed

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)

const defaultCouchAddr = "http://meep-couchdb-svc-couchdb.default.svc.cluster.local:5984/"

Michel Roy's avatar
Michel Roy committed
var client *Client

// Client - Implements a couchDB client
type Client struct {
	addr     string
	dbClient *kivik.Client
}

// Connector - Implements a CouchDB connector
type Connector struct {
	dbName   string
	dbHandle *kivik.DB
}

func authenticateConnection(dbClient *kivik.Client) {
	for {
		time.Sleep(9 * time.Minute)
		err := dbClient.Authenticate(context.TODO(), &chttp.CookieAuth{Username: "admin", Password: "admin"})
		if err != nil {
			log.Debug("Re-Authentication failed", err)
		}
	}
}

Michel Roy's avatar
Michel Roy committed
// NewConnector - Creates and initialize a CouchDB connector to a database
func NewConnector(addr string, dbName string) (rc *Connector, err error) {
	rc = new(Connector)

	// Connect to CouchDB
	if client == nil {
		log.Debug("Establish new couchDB client connection")
		c := new(Client)
		if addr == "" {
			c.addr = defaultCouchAddr
		} else {
			c.addr = addr
		}
		c.dbClient, err = kivik.New(context.TODO(), "couch", addr)
Michel Roy's avatar
Michel Roy committed
		if err != nil {
			return nil, err
		}

		err = c.dbClient.Authenticate(context.TODO(), &chttp.CookieAuth{Username: "admin", Password: "admin"})
		if err != nil {
			return nil, err
		}

		go authenticateConnection(c.dbClient)

Michel Roy's avatar
Michel Roy committed
		client = c
	}

	rc.dbName = dbName
	// Create DB if not exist
	exists, err := client.dbClient.DBExists(context.TODO(), rc.dbName)
	if err != nil {
		return nil, err
	}
	if !exists {
		log.Debug("Create DB: " + dbName)
		err = client.dbClient.CreateDB(context.TODO(), dbName)
		if err != nil {
			return nil, err
		}
	}
	// Open DB
	log.Debug("Open DB: " + dbName)
	rc.dbHandle, err = client.dbClient.DB(context.TODO(), dbName)
	if err != nil {
		return nil, err
	}
	return rc, nil
}

// getDocument - Get document from DB
Michel Roy's avatar
Michel Roy committed
func (dbCon *Connector) GetDoc(returnNilOnNotFound bool, docName string) (doc []byte, err error) {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Get document from DB: " + docName)
	row, err := dbCon.dbHandle.Get(context.TODO(), docName)
	if err != nil {
		// that's a call to the couch DB.. in order not to return nil, we override it
		if returnNilOnNotFound {
			// specifically for the case where there is nothing.. so the document object will be empty
			return nil, nil
		}
		return nil, err
	}
	// Decode JSON-encoded document
	err = row.ScanDoc(&doc)
	return doc, err
}

// getDocList - Get document list from DB
Simon Pastor's avatar
Simon Pastor committed
func (dbCon *Connector) GetDocList() (docNameList []string, docList [][]byte, err error) {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Get all docs from DB")
	rows, err := dbCon.dbHandle.AllDocs(context.TODO())
	if err != nil {
Simon Pastor's avatar
Simon Pastor committed
		return nil, nil, err
Michel Roy's avatar
Michel Roy committed
	}

	// Loop through docs and populate doc list to return
	log.Debug("Loop through docs")
	for rows.Next() {
		var doc []byte
Michel Roy's avatar
Michel Roy committed
		doc, err = dbCon.GetDoc(false, rows.ID())
Michel Roy's avatar
Michel Roy committed
		if err == nil {
			// Append to list
			docList = append(docList, doc)
Simon Pastor's avatar
Simon Pastor committed
			docNameList = append(docNameList, rows.ID())
Simon Pastor's avatar
Simon Pastor committed
	return docNameList, docList, nil
Michel Roy's avatar
Michel Roy committed
}

// addDoc - Add scenario to DB
Michel Roy's avatar
Michel Roy committed
func (dbCon *Connector) AddDoc(docName string, doc []byte) (string, error) {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Add new doc to DB: " + docName)
	rev, err := dbCon.dbHandle.Put(context.TODO(), docName, doc)
	if err != nil {
		return "", err
	}

	return rev, nil
}

// updateDoc - Update a document in DB
Michel Roy's avatar
Michel Roy committed
func (dbCon *Connector) UpdateDoc(docName string, doc []byte) (string, error) {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Update doc from DB: " + docName)
	// Remove previous version
Michel Roy's avatar
Michel Roy committed
	err := dbCon.DeleteDoc(docName)
Michel Roy's avatar
Michel Roy committed
	if err != nil {
		return "", err
	}

	// Add updated version
Michel Roy's avatar
Michel Roy committed
	rev, err := dbCon.AddDoc(docName, doc)
Michel Roy's avatar
Michel Roy committed
	if err != nil {
		return "", err
	}

	return rev, nil
}

// deleteDoc - Remove a document from DB
Michel Roy's avatar
Michel Roy committed
func (dbCon *Connector) DeleteDoc(docName string) error {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Delete doc from DB: " + docName)
	// Get latest Rev of stored document
	rev, err := dbCon.dbHandle.Rev(context.TODO(), docName)
	if err != nil {
		return err
	}

	// Remove doc from couchDB
	_, err = dbCon.dbHandle.Delete(context.TODO(), docName, rev)
	if err != nil {
		return err
	}

	return nil
}

// deleteAllDocs - Remove all documents from DB
Michel Roy's avatar
Michel Roy committed
func (dbCon *Connector) DeleteAllDocs() error {
Michel Roy's avatar
Michel Roy committed
	log.Debug("Delete all docs from DB")
	// Retrieve all scenarios from DB
	rows, err := dbCon.dbHandle.AllDocs(context.TODO())
	if err != nil {
		return err
	}

	// Loop through docs and remove each one
	for rows.Next() {
Michel Roy's avatar
Michel Roy committed
		_ = dbCon.DeleteDoc(rows.ID())
Michel Roy's avatar
Michel Roy committed
	}

	return nil
}