2013-01-04 01:16:26 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2011 The Perkeep Authors
|
2013-01-04 01:16:26 +00:00
|
|
|
|
|
|
|
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 schema
|
|
|
|
|
|
|
|
import (
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2013-01-04 01:16:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2016-05-21 22:56:03 +00:00
|
|
|
"go4.org/syncutil"
|
|
|
|
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
2013-01-04 01:16:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A DirReader reads the entries of a "directory" schema blob's
|
|
|
|
// referenced "static-set" blob.
|
|
|
|
type DirReader struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher blob.Fetcher
|
2013-01-22 18:32:15 +00:00
|
|
|
ss *superset
|
2013-01-04 01:16:26 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
staticSet []blob.Ref
|
2013-01-04 01:16:26 +00:00
|
|
|
current int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDirReader creates a new directory reader and prepares to
|
|
|
|
// fetch the static-set entries
|
2018-01-16 23:03:16 +00:00
|
|
|
func NewDirReader(ctx context.Context, fetcher blob.Fetcher, dirBlobRef blob.Ref) (*DirReader, error) {
|
2013-01-22 18:32:15 +00:00
|
|
|
ss := new(superset)
|
2018-01-16 23:03:16 +00:00
|
|
|
err := ss.setFromBlobRef(ctx, fetcher, dirBlobRef)
|
2013-01-04 01:16:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ss.Type != "directory" {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: expected \"directory\" schema blob for %s, got %q", dirBlobRef, ss.Type)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
dr, err := ss.NewDirReader(fetcher)
|
|
|
|
if err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: creating DirReader for %s: %v", dirBlobRef, err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
dr.current = 0
|
|
|
|
return dr, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
func (b *Blob) NewDirReader(ctx context.Context, fetcher blob.Fetcher) (*DirReader, error) {
|
2013-01-22 17:32:40 +00:00
|
|
|
return b.ss.NewDirReader(fetcher)
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (ss *superset) NewDirReader(fetcher blob.Fetcher) (*DirReader, error) {
|
2013-01-04 01:16:26 +00:00
|
|
|
if ss.Type != "directory" {
|
|
|
|
return nil, fmt.Errorf("Superset not of type \"directory\"")
|
|
|
|
}
|
|
|
|
return &DirReader{fetcher: fetcher, ss: ss}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
func (ss *superset) setFromBlobRef(ctx context.Context, fetcher blob.Fetcher, blobRef blob.Ref) error {
|
2013-08-04 02:54:30 +00:00
|
|
|
if !blobRef.Valid() {
|
2014-11-03 12:40:37 +00:00
|
|
|
return errors.New("schema/dirreader: blobref invalid")
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
ss.BlobRef = blobRef
|
2018-01-16 23:03:16 +00:00
|
|
|
rc, _, err := fetcher.Fetch(ctx, blobRef)
|
2013-01-04 01:16:26 +00:00
|
|
|
if err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return fmt.Errorf("schema/dirreader: fetching schema blob %s: %v", blobRef, err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2014-03-14 19:11:08 +00:00
|
|
|
defer rc.Close()
|
|
|
|
if err := json.NewDecoder(rc).Decode(ss); err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return fmt.Errorf("schema/dirreader: decoding schema blob %s: %v", blobRef, err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticSet returns the whole of the static set members of that directory
|
2018-01-16 23:03:16 +00:00
|
|
|
func (dr *DirReader) StaticSet(ctx context.Context) ([]blob.Ref, error) {
|
2013-01-04 01:16:26 +00:00
|
|
|
if dr.staticSet != nil {
|
|
|
|
return dr.staticSet, nil
|
|
|
|
}
|
2013-02-10 20:58:51 +00:00
|
|
|
staticSetBlobref := dr.ss.Entries
|
2013-08-04 02:54:30 +00:00
|
|
|
if !staticSetBlobref.Valid() {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, errors.New("schema/dirreader: Invalid blobref")
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2017-05-02 15:39:48 +00:00
|
|
|
members, err := staticSet(ctx, staticSetBlobref, dr.fetcher)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dr.staticSet = members
|
|
|
|
return dr.staticSet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func staticSet(ctx context.Context, staticSetBlobref blob.Ref, fetcher blob.Fetcher) ([]blob.Ref, error) {
|
|
|
|
rsc, _, err := fetcher.Fetch(ctx, staticSetBlobref)
|
2013-01-04 01:16:26 +00:00
|
|
|
if err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: fetching schema blob %s: %v", staticSetBlobref, err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2013-09-20 11:23:28 +00:00
|
|
|
defer rsc.Close()
|
2013-01-22 18:32:15 +00:00
|
|
|
ss, err := parseSuperset(rsc)
|
2013-01-04 01:16:26 +00:00
|
|
|
if err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: decoding schema blob %s: %v", staticSetBlobref, err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
if ss.Type != "static-set" {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: expected \"static-set\" schema blob for %s, got %q", staticSetBlobref, ss.Type)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2017-05-02 15:39:48 +00:00
|
|
|
var members []blob.Ref
|
|
|
|
if len(ss.Members) > 0 {
|
|
|
|
// We have fileRefs or dirRefs in ss.Members, so we are either in the static-set
|
|
|
|
// of a small directory, or one of the "leaf" subsets of a large directory spread.
|
|
|
|
for _, member := range ss.Members {
|
|
|
|
if !member.Valid() {
|
|
|
|
return nil, fmt.Errorf("schema/dirreader: invalid (static-set member) blobref referred by \"static-set\" schema blob %v", staticSetBlobref)
|
|
|
|
}
|
|
|
|
members = append(members, member)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2017-05-02 15:39:48 +00:00
|
|
|
return members, nil
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2017-05-02 15:39:48 +00:00
|
|
|
// We are either at the top static-set of a large directory, or in a "non-leaf"
|
|
|
|
// subset of a large directory.
|
|
|
|
for _, toMerge := range ss.MergeSets {
|
|
|
|
if !toMerge.Valid() {
|
|
|
|
return nil, fmt.Errorf("schema/dirreader: invalid (static-set subset) blobref referred by \"static-set\" schema blob %v", staticSetBlobref)
|
|
|
|
}
|
|
|
|
// TODO(mpl): do it concurrently
|
|
|
|
subset, err := staticSet(ctx, toMerge, fetcher)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("schema/dirreader: could not get members of %q, subset of %v: %v", toMerge, staticSetBlobref, err)
|
|
|
|
}
|
|
|
|
members = append(members, subset...)
|
|
|
|
}
|
|
|
|
return members, nil
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir implements the Directory interface.
|
2018-01-16 23:03:16 +00:00
|
|
|
func (dr *DirReader) Readdir(ctx context.Context, n int) (entries []DirectoryEntry, err error) {
|
|
|
|
sts, err := dr.StaticSet(ctx)
|
2013-01-04 01:16:26 +00:00
|
|
|
if err != nil {
|
2014-11-03 12:40:37 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: can't get StaticSet: %v", err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
|
|
|
up := dr.current + n
|
|
|
|
if n <= 0 {
|
|
|
|
dr.current = 0
|
|
|
|
up = len(sts)
|
|
|
|
} else {
|
|
|
|
if n > (len(sts) - dr.current) {
|
|
|
|
err = io.EOF
|
|
|
|
up = len(sts)
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 18:17:38 +00:00
|
|
|
|
|
|
|
// TODO(bradfitz): push down information to the fetcher
|
|
|
|
// (e.g. cachingfetcher -> remote client http) that we're
|
|
|
|
// going to load a bunch, so the HTTP client (if not using
|
|
|
|
// SPDY) can do discovery and see if the server supports a
|
|
|
|
// batch handler, then get them all in one round-trip, rather
|
|
|
|
// than attacking the server with hundreds of parallel TLS
|
|
|
|
// setups.
|
|
|
|
|
|
|
|
type res struct {
|
|
|
|
ent DirectoryEntry
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
var cs []chan res
|
|
|
|
|
|
|
|
// Kick off all directory entry loads.
|
2016-05-21 22:56:03 +00:00
|
|
|
gate := syncutil.NewGate(20) // Limit IO concurrency
|
2013-02-18 18:17:38 +00:00
|
|
|
for _, entRef := range sts[dr.current:up] {
|
|
|
|
c := make(chan res, 1)
|
|
|
|
cs = append(cs, c)
|
2016-05-21 22:56:03 +00:00
|
|
|
gate.Start()
|
2013-08-04 02:54:30 +00:00
|
|
|
go func(entRef blob.Ref) {
|
2016-05-21 22:56:03 +00:00
|
|
|
defer gate.Done()
|
2018-01-16 23:03:16 +00:00
|
|
|
entry, err := NewDirectoryEntryFromBlobRef(ctx, dr.fetcher, entRef)
|
2013-02-18 18:17:38 +00:00
|
|
|
c <- res{entry, err}
|
|
|
|
}(entRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
res := <-c
|
|
|
|
if res.err != nil {
|
2014-11-24 17:41:50 +00:00
|
|
|
return nil, fmt.Errorf("schema/dirreader: can't create dirEntry: %v", res.err)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2013-02-18 18:17:38 +00:00
|
|
|
entries = append(entries, res.ent)
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|
2013-02-18 18:17:38 +00:00
|
|
|
return entries, nil
|
2013-01-04 01:16:26 +00:00
|
|
|
}
|