forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
156 lines (135 loc) · 3.05 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2012 Google Inc.
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 (
"fmt"
"io"
"log"
"strings"
"sync"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/index"
"camlistore.org/pkg/schema"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
type node struct {
br blob.Ref
g *graph
size int64
blob *schema.Blob
edges []blob.Ref
}
func (n *node) dotName() string {
return strings.Replace(n.br.String(), "-", "_", -1)
}
func (n *node) dotLabel() string {
name := n.displayName()
if n.blob == nil {
return fmt.Sprintf("%s\n%d bytes", name, n.size)
}
return name + "\n" + n.blob.Type()
}
func (n *node) color() string {
if n.br == n.g.root {
return "#a0ffa0"
}
if n.blob == nil {
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()
rc, err := fetch(n.g.src, n.br)
check(err)
defer rc.Close()
sniff := index.NewBlobSniffer(n.br)
n.size, err = io.Copy(sniff, rc)
check(err)
sniff.Parse()
blob, ok := sniff.SchemaBlob()
if !ok {
return
}
n.blob = blob
for _, part := range blob.ByteParts() {
n.addEdge(part.BlobRef)
n.addEdge(part.BytesRef)
}
}
func (n *node) addEdge(dst blob.Ref) {
if !dst.Valid() {
return
}
n.g.startLoadNode(dst)
n.edges = append(n.edges, dst)
}
type graph struct {
src blob.StreamingFetcher
root blob.Ref
mu sync.Mutex // guards n
n map[string]*node
wg sync.WaitGroup
}
func (g *graph) startLoadNode(br blob.Ref) {
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()
}
func printGraph(src blob.StreamingFetcher, root blob.Ref) {
g := &graph{
src: src,
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")
}