Something went wrong on our end
-
Christian Pointner authoredChristian Pointner authored
job_inventory.go 2.93 KiB
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package importer
import (
"log"
"sync"
)
type jobInventoryGroup struct {
jobs map[uint64]*Job
// add list of subscription channels
}
func newJobInventoryGroup() *jobInventoryGroup {
g := &jobInventoryGroup{}
g.jobs = make(map[uint64]*Job)
return g
}
type jobInventory struct {
infoLog *log.Logger
errLog *log.Logger
dbgLog *log.Logger
groups map[string]*jobInventoryGroup
mu sync.RWMutex
}
func (i *jobInventory) ListJobs(group string) (jobs Jobs) {
i.mu.RLock()
defer i.mu.RUnlock()
ig, exists := i.groups[group]
if !exists {
return
}
for _, j := range ig.jobs {
jobs = append(jobs, j)
}
return
}
func (i *jobInventory) InsertOrGetJob(group string, id uint64, jobIn *Job) (job *Job) {
i.mu.Lock()
defer i.mu.Unlock()
ig, exists := i.groups[group]
if !exists {
ig = newJobInventoryGroup()
ig.jobs[id] = jobIn
i.groups[group] = ig
i.dbgLog.Printf("importer: job-inventory added job(%s/%d)", group, id)
return jobIn
}
if job, exists = ig.jobs[id]; !exists {
ig.jobs[id] = jobIn
i.dbgLog.Printf("importer: job-inventory added job(%s/%d)", group, id)
return jobIn
}
return
}
func (i *jobInventory) GetJob(group string, id uint64) (job *Job, err error) {
i.mu.RLock()
defer i.mu.RUnlock()
ig, exists := i.groups[group]
if !exists {
return nil, ErrNotFound
}
if job, exists = ig.jobs[id]; !exists {
return nil, ErrNotFound
}
return
}
func (i *jobInventory) DeleteJob(group string, id uint64) (err error) {
i.mu.Lock()
defer i.mu.Unlock()
ig, exists := i.groups[group]
if !exists {
return ErrNotFound
}
if _, exists = ig.jobs[id]; !exists {
return ErrNotFound
}
delete(ig.jobs, id)
i.dbgLog.Printf("importer: job-inventory removed job(%s/%d)", group, id)
if len(ig.jobs) == 0 {
// TODO: only do this if there are no subscriptions
delete(i.groups, group)
i.dbgLog.Printf("importer: job-inventory also removed now empty group %s", group)
}
return
}
// TODO: handle subscriptions to new and deleted jobs
func newJobInventory(infoLog, errLog, dbgLog *log.Logger) *jobInventory {
i := &jobInventory{infoLog: infoLog, errLog: errLog, dbgLog: dbgLog}
i.groups = make(map[string]*jobInventoryGroup)
return i
}