func (m *MemDB) InsertPipelineRun(p *gaia.PipelineRun) error {
	txn := m.db.Txn(true)
	if err := txn.Insert(pipelineRunTable, p); err != nil {
		gaia.Cfg.Logger.Error("failed to insert pipeline run via insert", "error", err.Error())
		return err
	}
	txn.Commit()
	return nil
}
InsertPipelineRun inserts a pipeline run into the memdb.
package memdb
import (
	"errors"
	"time"
	"github.com/gaia-pipeline/gaia"
	"github.com/gaia-pipeline/gaia/helper/stringhelper"
	"github.com/gaia-pipeline/gaia/store"
	"github.com/gaia-pipeline/gaia/workers/docker"
	memdb "github.com/hashicorp/go-memdb"
)
const (
	workerTableName = "worker"
	pipelineRunTable = "pipelinerun"
	dockerWorkerTableName = "dockerworker"
)
type MemDB struct {
	db *memdb.MemDB
	store store.GaiaStore
}
type GaiaMemDB interface {
	SyncStore() error
	GetAllWorker() []*gaia.Worker
	UpsertWorker(w *gaia.Worker, persist bool) error
	GetWorker(id string) (*gaia.Worker, error)
	DeleteWorker(id string, persist bool) error
	InsertPipelineRun(p *gaia.PipelineRun) error
	PopPipelineRun(tags []string) (*gaia.PipelineRun, error)
	DeletePipelineRun(runID string) error
	InsertDockerWorker(w *docker.Worker) error
	GetDockerWorker(workerID string) (*docker.Worker, error)
	DeleteDockerWorker(workerID string) error
	GetAllDockerWorker() ([]*docker.Worker, error)
}
func InitMemDB(s store.GaiaStore) (GaiaMemDB, error) {
	if s == nil {
		return nil, errors.New("store is nil")
	}
	db, err := memdb.NewMemDB(memDBSchema)
	if err != nil {
		return nil, err
	}
	return &MemDB{db: db, store: s}, nil
}
func (m *MemDB) SyncStore() error {
	worker, err := m.store.WorkerGetAll()
	if err != nil {
		gaia.Cfg.Logger.Error("failed to load worker from store", "error", err.Error())
		return err
	}
	for _, w := range worker {
		if err = m.UpsertWorker(w, false); err != nil {
			return err
		}
	}
	return nil
}
func (m *MemDB) GetAllWorker() []*gaia.Worker {
	var workers []*gaia.Worker
	txn := m.db.Txn(false)
	defer txn.Abort()
	iter, err := txn.Get(workerTableName, "id_prefix")
	if err != nil {
		gaia.Cfg.Logger.Error("failed to get worker objects from memdb via getallworker", "error", err.Error())
		return workers
	}
	for {
		item := iter.Next()
		if item == nil {
			break
		}
		w, ok := item.(*gaia.Worker)
		if !ok {
			gaia.Cfg.Logger.Error("failed to convert worker into worker obj", "raw", item)
			continue
		}
		workers = append(workers, w)
	}
	return workers
}
func (m *MemDB) UpsertWorker(w *gaia.Worker, persist bool) error {
	txn := m.db.Txn(true)
	raw, err := txn.First(workerTableName, "id", w.UniqueID)
	if err != nil {
		gaia.Cfg.Logger.Error("failed to lookup worker via upsert", "error", err.Error())
		return err
	}
	if raw != nil {
		err = txn.Delete(workerTableName, raw)
		if err != nil {
			gaia.Cfg.Logger.Error("failed to delete worker via upsert", "error", err.Error())
			return err
		}
	}
	if err := txn.Insert(workerTableName, w); err != nil {
		gaia.Cfg.Logger.Error("failed to insert worker via upsert", "error", err.Error())
		return err
	}
	if persist {
		if err = m.store.WorkerPut(w); err != nil {
			gaia.Cfg.Logger.Error("failed to store worker in the store via upsert", "error", err.Error())
			txn.Abort()
			return err
		}
	}
	txn.Commit()
	return nil
}
func (m *MemDB) GetWorker(id string) (*gaia.Worker, error) {
	txn := m.db.Txn(false)
	defer txn.Abort()
	raw, err := txn.First(workerTableName, "id", id)
	if err != nil {
		gaia.Cfg.Logger.Error("failed to get worker from memdb", "error", err.Error(), "id", id)
		return nil, err
	}
	if raw == nil {
		return nil, nil
	}
	w, ok := raw.(*gaia.Worker)
	if !ok {
		gaia.Cfg.Logger.Error("failed to convert worker into worker obj", "raw", raw)
		return nil, errors.New("failed to convert worker into worker obj")
	}
	return w, nil
}
func (m *MemDB) DeleteWorker(id string, persist bool) error {
	txn := m.db.Txn(true)
	raw, err := txn.First(workerTableName, "id", id)
	if err != nil {
		gaia.Cfg.Logger.Error("failed to load worker from memdb", "error", err.Error(), "id", id)
		return err
	}
	if raw != nil {
		err = txn.Delete(workerTableName, raw)
		if err != nil {
			gaia.Cfg.Logger.Error("failed to delete worker from memdb", "error", err.Error(), "id", id)
			return err
		}
	}
	if persist {
		err = m.store.WorkerDelete(id)
		if err != nil {
			gaia.Cfg.Logger.Error("failed to delete worker from store via memdb delete", "error", err.Error(), "id", id)
			return err
		}
	}
	txn.Commit()
	return nil
}