69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
/*
|
|
* This file is part of solvar. (https://git.redxen.eu/caskd/solvar)
|
|
* Copyright (c) 2022 Alex-David Denes
|
|
*
|
|
* solvar 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.
|
|
*
|
|
* solvar 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 solvar. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package solvar
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
type idCacheMap map[Identifier]([]*Object)
|
|
type idCache struct {
|
|
l *sync.Mutex
|
|
m idCacheMap
|
|
}
|
|
|
|
func (ic *idCache) add(id Identifier, a *Object, wg *sync.WaitGroup) {
|
|
ic.l.Lock()
|
|
defer ic.l.Unlock()
|
|
defer wg.Done()
|
|
|
|
ic.m[id] = append(ic.m[id], a) // We don't care if we got a actual existing object or not
|
|
}
|
|
|
|
func (ic *idCache) Query(id Identifier) (o []*Object, err error) {
|
|
if ic.l == nil {
|
|
err = errors.New("Cache hasn't been initialised")
|
|
return
|
|
}
|
|
ic.l.Lock()
|
|
defer ic.l.Unlock()
|
|
|
|
o, ok := ic.m[id]
|
|
if !ok {
|
|
err = errors.New("Object not found")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (d *DependencyGraph) BuildCache() {
|
|
d.ic.l = new(sync.Mutex)
|
|
d.ic.m = make(idCacheMap)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
for oi, ov := range d.ol {
|
|
for _, lid := range ov.Id {
|
|
wg.Add(1)
|
|
go d.ic.add(lid, &d.ol[oi], wg)
|
|
}
|
|
}
|
|
wg.Wait()
|
|
}
|