blob: ad3f3cf5f72ef5ea0fd411f4810e258a46a04eb9 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// 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 database implements the persistence layer for the gpu debugger tools.
package database
import (
"android.googlesource.com/platform/tools/gpu/framework/id"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
// Database is the interface to a resource store.
type Database interface {
// Store adds a key-value pair to the database.
// It is an error if the id is already mapped to an object.
Store(log.Context, id.ID, interface{}) error
// Resolve attempts to resolve the final value associated with an id.
// It will traverse all Lazy objects, blocking until they are ready.
Resolve(log.Context, id.ID) (interface{}, error)
// Containts returns true if the database has an entry for the specified id.
Contains(log.Context, id.ID) bool
}
// Store is a helper that stores v to the database with the id calculated by
// the Hash function.
func Store(ctx log.Context, v interface{}, d Database) (id.ID, error) {
id, err := Hash(v)
if err != nil {
return id, err
}
return id, d.Store(ctx, id, v)
}
// Resolve is a helper that resolves id from the database.
func Resolve(ctx log.Context, id id.ID, d Database) (interface{}, error) {
return d.Resolve(ctx, id)
}
// Build stores lazy into d, and then resolves and returns the lazy-built
// object.
func Build(ctx log.Context, lazy Lazy, d Database) (interface{}, error) {
id, err := Store(ctx, lazy, d)
if err != nil {
return nil, err
}
return d.Resolve(ctx, id)
}