2018-01-02 18:06:50 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SharedResult is a result that can be cloned
|
|
|
|
type SharedResult struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
main Result
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSharedResult(main Result) *SharedResult {
|
|
|
|
return &SharedResult{main: main}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SharedResult) Clone() Result {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
r1, r2 := dup(r.main)
|
|
|
|
r.main = r1
|
|
|
|
return r2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SharedResult) Release(ctx context.Context) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
return r.main.Release(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dup(res Result) (Result, Result) {
|
|
|
|
sem := int64(0)
|
|
|
|
return &splitResult{Result: res, sem: &sem}, &splitResult{Result: res, sem: &sem}
|
|
|
|
}
|
|
|
|
|
|
|
|
type splitResult struct {
|
|
|
|
released int64
|
|
|
|
sem *int64
|
2019-08-06 00:10:30 +00:00
|
|
|
Result
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *splitResult) Release(ctx context.Context) error {
|
|
|
|
if atomic.AddInt64(&r.released, 1) > 1 {
|
|
|
|
err := errors.Errorf("releasing already released reference")
|
|
|
|
logrus.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if atomic.AddInt64(r.sem, 1) == 2 {
|
|
|
|
return r.Result.Release(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCachedResult combines a result and cache key into cached result
|
2018-07-23 21:27:35 +00:00
|
|
|
func NewCachedResult(res Result, k []ExportableCacheKey) CachedResult {
|
2018-04-13 20:52:27 +00:00
|
|
|
return &cachedResult{res, k}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachedResult struct {
|
|
|
|
Result
|
2018-07-23 21:27:35 +00:00
|
|
|
k []ExportableCacheKey
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 21:27:35 +00:00
|
|
|
func (cr *cachedResult) CacheKeys() []ExportableCacheKey {
|
2018-04-13 20:52:27 +00:00
|
|
|
return cr.k
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
|
|
|
|
func NewSharedCachedResult(res CachedResult) *SharedCachedResult {
|
|
|
|
return &SharedCachedResult{
|
|
|
|
SharedResult: NewSharedResult(res),
|
|
|
|
CachedResult: res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SharedCachedResult) Clone() CachedResult {
|
|
|
|
return &clonedCachedResult{Result: r.SharedResult.Clone(), cr: r.CachedResult}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SharedCachedResult) Release(ctx context.Context) error {
|
|
|
|
return r.SharedResult.Release(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clonedCachedResult struct {
|
|
|
|
Result
|
|
|
|
cr CachedResult
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
func (ccr *clonedCachedResult) ID() string {
|
|
|
|
return ccr.Result.ID()
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
func (ccr *clonedCachedResult) CacheKeys() []ExportableCacheKey {
|
|
|
|
return ccr.cr.CacheKeys()
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SharedCachedResult struct {
|
|
|
|
*SharedResult
|
|
|
|
CachedResult
|
|
|
|
}
|