Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1310)

Side by Side Diff: service/datastore/dumper/dumper.go

Issue 2302743002: Interface update, per-method Contexts. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // Package dumper implements a very VERY dumb datastore-dumping debugging aid. 5 // Package dumper implements a very VERY dumb datastore-dumping debugging aid.
6 // You shouldn't plan on having this work with the production datastore with any 6 // You shouldn't plan on having this work with the production datastore with any
7 // appreciable amount of data. 7 // appreciable amount of data.
8 // 8 //
9 // This will take an arbitrary query (or even a query for every entity in the 9 // This will take an arbitrary query (or even a query for every entity in the
10 // entire datastore), and print every entity to some output stream. 10 // entire datastore), and print every entity to some output stream.
11 package dumper 11 package dumper
12 12
13 import ( 13 import (
14 "fmt" 14 "fmt"
15 "io" 15 "io"
16 "os" 16 "os"
17 "sort" 17 "sort"
18 "strings" 18 "strings"
19 19
20 » "github.com/luci/gae/service/datastore" 20 » ds "github.com/luci/gae/service/datastore"
21
21 "golang.org/x/net/context" 22 "golang.org/x/net/context"
22 ) 23 )
23 24
24 // Key is a key into a PropFilterMap 25 // Key is a key into a PropFilterMap
25 type Key struct { 26 type Key struct {
26 Kind string 27 Kind string
27 PropName string 28 PropName string
28 } 29 }
29 30
30 // A PropFilterMap maps from Kind+PropertyName tuples to a formatting function. You 31 // A PropFilterMap maps from Kind+PropertyName tuples to a formatting function. You
31 // may use this to specially format particular properties. 32 // may use this to specially format particular properties.
32 type PropFilterMap map[Key]func(datastore.Property) string 33 type PropFilterMap map[Key]func(ds.Property) string
33 34
34 // KindFilterMap maps from a Kind to a formatting function. You may use this to 35 // KindFilterMap maps from a Kind to a formatting function. You may use this to
35 // specially format particular Kinds. If this function returns an empty string, 36 // specially format particular Kinds. If this function returns an empty string,
36 // the default formatting function (including any PropFilterMap entries) will be 37 // the default formatting function (including any PropFilterMap entries) will be
37 // used. 38 // used.
38 type KindFilterMap map[string]func(*datastore.Key, datastore.PropertyMap) string 39 type KindFilterMap map[string]func(*ds.Key, ds.PropertyMap) string
39 40
40 // Config is a configured dumper. 41 // Config is a configured dumper.
41 type Config struct { 42 type Config struct {
42 // OutStream is the output stream to use. If this is nil, os.Stdout will be 43 // OutStream is the output stream to use. If this is nil, os.Stdout will be
43 // used. 44 // used.
44 OutStream io.Writer 45 OutStream io.Writer
45 46
46 // WithSpecial, if true, includes entities which have kinds that begin a nd 47 // WithSpecial, if true, includes entities which have kinds that begin a nd
47 // end with "__". By default, these entities are skipped. 48 // end with "__". By default, these entities are skipped.
48 WithSpecial bool 49 WithSpecial bool
49 50
50 // PropFilters is an optional property filter map for controlling the 51 // PropFilters is an optional property filter map for controlling the
51 // rendering of certain Kind/Property values. 52 // rendering of certain Kind/Property values.
52 PropFilters PropFilterMap 53 PropFilters PropFilterMap
53 54
54 // KindFilters is an optional kind filter for controlling the rendering of 55 // KindFilters is an optional kind filter for controlling the rendering of
55 // certain Kind values. 56 // certain Kind values.
56 KindFilters KindFilterMap 57 KindFilters KindFilterMap
57 } 58 }
58 59
59 // Query will dump everything matching the provided query. 60 // Query will dump everything matching the provided query.
60 // 61 //
61 // If the provided query is nil, a kindless query without any filters will be 62 // If the provided query is nil, a kindless query without any filters will be
62 // used. 63 // used.
63 func (cfg Config) Query(c context.Context, q *datastore.Query) (n int, err error ) { 64 func (cfg Config) Query(c context.Context, q *ds.Query) (n int, err error) {
64 » ds := datastore.Get(c)
65
66 if q == nil { 65 if q == nil {
67 » » q = datastore.NewQuery("") 66 » » q = ds.NewQuery("")
68 } 67 }
69 68
70 out := cfg.OutStream 69 out := cfg.OutStream
71 if out == nil { 70 if out == nil {
72 out = os.Stdout 71 out = os.Stdout
73 } 72 }
74 73
75 » fmtVal := func(kind, name string, prop datastore.Property) string { 74 » fmtVal := func(kind, name string, prop ds.Property) string {
76 if fn := cfg.PropFilters[Key{kind, name}]; fn != nil { 75 if fn := cfg.PropFilters[Key{kind, name}]; fn != nil {
77 return fn(prop) 76 return fn(prop)
78 } 77 }
79 return prop.String() 78 return prop.String()
80 } 79 }
81 80
82 prnt := func(format string, args ...interface{}) (err error) { 81 prnt := func(format string, args ...interface{}) (err error) {
83 var amt int 82 var amt int
84 amt, err = fmt.Fprintf(out, format, args...) 83 amt, err = fmt.Fprintf(out, format, args...)
85 n += amt 84 n += amt
86 return 85 return
87 } 86 }
88 87
89 » prop := func(kind, name string, vals []datastore.Property) (err error) { 88 » prop := func(kind, name string, vals []ds.Property) (err error) {
90 if len(vals) <= 1 { 89 if len(vals) <= 1 {
91 return prnt(" %q: [%s]\n", name, fmtVal(kind, name, val s[0])) 90 return prnt(" %q: [%s]\n", name, fmtVal(kind, name, val s[0]))
92 } 91 }
93 if err = prnt(" %q: [\n %s", name, fmtVal(kind, name, vals[0 ])); err != nil { 92 if err = prnt(" %q: [\n %s", name, fmtVal(kind, name, vals[0 ])); err != nil {
94 return 93 return
95 } 94 }
96 for _, v := range vals[1:] { 95 for _, v := range vals[1:] {
97 if err = prnt(",\n %s", fmtVal(kind, name, v)); err ! = nil { 96 if err = prnt(",\n %s", fmtVal(kind, name, v)); err ! = nil {
98 return 97 return
99 } 98 }
100 } 99 }
101 return prnt("\n ]\n") 100 return prnt("\n ]\n")
102 } 101 }
103 102
104 » err = ds.Run(q, func(pm datastore.PropertyMap) error { 103 » err = ds.Run(c, q, func(pm ds.PropertyMap) error {
105 » » key := datastore.GetMetaDefault(pm, "key", nil).(*datastore.Key) 104 » » key := ds.GetMetaDefault(pm, "key", nil).(*ds.Key)
106 if !cfg.WithSpecial && strings.HasPrefix(key.Kind(), "__") && st rings.HasSuffix(key.Kind(), "__") { 105 if !cfg.WithSpecial && strings.HasPrefix(key.Kind(), "__") && st rings.HasSuffix(key.Kind(), "__") {
107 return nil 106 return nil
108 } 107 }
109 if err := prnt("\n%s:\n", key); err != nil { 108 if err := prnt("\n%s:\n", key); err != nil {
110 return err 109 return err
111 } 110 }
112 pm, _ = pm.Save(false) 111 pm, _ = pm.Save(false)
113 112
114 // See if we have a KindFilter for this 113 // See if we have a KindFilter for this
115 if flt, ok := cfg.KindFilters[key.Kind()]; ok { 114 if flt, ok := cfg.KindFilters[key.Kind()]; ok {
(...skipping 17 matching lines...) Expand all
133 return err 132 return err
134 } 133 }
135 } 134 }
136 return nil 135 return nil
137 }) 136 })
138 return 137 return
139 } 138 }
140 139
141 // Query dumps the provided query to stdout without special entities and with 140 // Query dumps the provided query to stdout without special entities and with
142 // default rendering. 141 // default rendering.
143 func Query(c context.Context, q *datastore.Query) { 142 func Query(c context.Context, q *ds.Query) {
144 Config{}.Query(c, q) 143 Config{}.Query(c, q)
145 } 144 }
146 145
147 // All dumps all entities to stdout without special entities and with default 146 // All dumps all entities to stdout without special entities and with default
148 // rendering. 147 // rendering.
149 func All(c context.Context) { 148 func All(c context.Context) {
150 Config{}.Query(c, nil) 149 Config{}.Query(c, nil)
151 } 150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698