2012-10-28 18:32:25 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2012 The Perkeep Authors
|
2012-10-28 18:32:25 +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 main
|
|
|
|
|
|
|
|
import (
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2012-10-28 18:32:25 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
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"
|
|
|
|
"perkeep.org/pkg/index"
|
|
|
|
"perkeep.org/pkg/schema"
|
2012-10-28 18:32:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
2013-08-04 02:54:30 +00:00
|
|
|
br blob.Ref
|
2012-10-28 18:32:25 +00:00
|
|
|
g *graph
|
|
|
|
|
|
|
|
size int64
|
2013-01-22 17:32:40 +00:00
|
|
|
blob *schema.Blob
|
2013-08-04 02:54:30 +00:00
|
|
|
edges []blob.Ref
|
2012-10-28 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) dotName() string {
|
|
|
|
return strings.Replace(n.br.String(), "-", "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) dotLabel() string {
|
|
|
|
name := n.displayName()
|
2013-01-22 17:32:40 +00:00
|
|
|
if n.blob == nil {
|
2012-10-28 18:32:25 +00:00
|
|
|
return fmt.Sprintf("%s\n%d bytes", name, n.size)
|
|
|
|
}
|
2021-01-17 03:05:35 +00:00
|
|
|
return name + "\n" + string(n.blob.Type())
|
2012-10-28 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) color() string {
|
2013-08-04 02:54:30 +00:00
|
|
|
if n.br == n.g.root {
|
2012-10-28 18:32:25 +00:00
|
|
|
return "#a0ffa0"
|
|
|
|
}
|
2013-01-22 17:32:40 +00:00
|
|
|
if n.blob == nil {
|
2012-10-28 18:32:25 +00:00
|
|
|
return "#aaaaaa"
|
|
|
|
}
|
|
|
|
return "#a0a0ff"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) displayName() string {
|
|
|
|
s := n.br.String()
|
|
|
|
s = s[strings.Index(s, "-")+1:]
|
|
|
|
return s[:7]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) load() {
|
|
|
|
defer n.g.wg.Done()
|
2018-01-16 23:03:16 +00:00
|
|
|
rc, err := fetch(context.Background(), n.g.src, n.br)
|
2012-10-28 18:32:25 +00:00
|
|
|
check(err)
|
|
|
|
defer rc.Close()
|
2013-01-22 17:32:40 +00:00
|
|
|
sniff := index.NewBlobSniffer(n.br)
|
2012-10-28 18:32:25 +00:00
|
|
|
n.size, err = io.Copy(sniff, rc)
|
|
|
|
check(err)
|
|
|
|
sniff.Parse()
|
2013-01-22 17:32:40 +00:00
|
|
|
blob, ok := sniff.SchemaBlob()
|
2012-10-28 18:32:25 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2013-01-22 17:32:40 +00:00
|
|
|
n.blob = blob
|
|
|
|
for _, part := range blob.ByteParts() {
|
2012-10-28 18:32:25 +00:00
|
|
|
n.addEdge(part.BlobRef)
|
|
|
|
n.addEdge(part.BytesRef)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (n *node) addEdge(dst blob.Ref) {
|
|
|
|
if !dst.Valid() {
|
2012-10-28 18:32:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
n.g.startLoadNode(dst)
|
|
|
|
n.edges = append(n.edges, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
type graph struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
src blob.Fetcher
|
2013-08-04 02:54:30 +00:00
|
|
|
root blob.Ref
|
2012-10-28 18:32:25 +00:00
|
|
|
|
|
|
|
mu sync.Mutex // guards n
|
|
|
|
n map[string]*node
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (g *graph) startLoadNode(br blob.Ref) {
|
2012-10-28 18:32:25 +00:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
key := br.String()
|
|
|
|
if _, ok := g.n[key]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n := &node{
|
|
|
|
g: g,
|
|
|
|
br: br,
|
|
|
|
}
|
|
|
|
g.n[key] = n
|
|
|
|
g.wg.Add(1)
|
|
|
|
go n.load()
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func printGraph(src blob.Fetcher, root blob.Ref) {
|
2012-10-28 18:32:25 +00:00
|
|
|
g := &graph{
|
2013-01-04 01:55:30 +00:00
|
|
|
src: src,
|
2012-10-28 18:32:25 +00:00
|
|
|
root: root,
|
|
|
|
n: make(map[string]*node),
|
|
|
|
}
|
|
|
|
g.startLoadNode(root)
|
|
|
|
g.wg.Wait()
|
|
|
|
fmt.Println("digraph G {")
|
|
|
|
fmt.Println(" node [fontsize=10,fontname=Arial]")
|
|
|
|
fmt.Println(" edge [fontsize=10,fontname=Arial]")
|
|
|
|
|
|
|
|
for _, n := range g.n {
|
|
|
|
fmt.Printf("\n %s [label=%q,style=filled,fillcolor=%q]\n", n.dotName(), n.dotLabel(), n.color())
|
|
|
|
for i, e := range n.edges {
|
|
|
|
// TODO: create an edge type.
|
|
|
|
// Also, this edgeLabel is specific to file parts. Other schema
|
|
|
|
// types might not even have a concept of ordering. This is hack.
|
|
|
|
edgeLabel := fmt.Sprintf("%d", i)
|
|
|
|
if i == 0 {
|
|
|
|
edgeLabel = "first"
|
|
|
|
} else if i == len(n.edges)-1 {
|
|
|
|
edgeLabel = "last"
|
|
|
|
}
|
|
|
|
fmt.Printf(" %s -> %s [label=%q]\n", n.dotName(), g.n[e.String()].dotName(), edgeLabel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("}\n")
|
|
|
|
}
|