github.com/cayleygraph/cayley@v0.7.7/graph/graphmock/graphmock.go (about) 1 package graphmock 2 3 import ( 4 "context" 5 "strconv" 6 7 "github.com/cayleygraph/cayley/graph" 8 "github.com/cayleygraph/cayley/graph/iterator" 9 "github.com/cayleygraph/quad" 10 ) 11 12 var ( 13 _ graph.Ref = IntVal(0) 14 _ graph.Ref = StringNode("") 15 ) 16 17 type IntVal int 18 19 func (v IntVal) Key() interface{} { return v } 20 21 type StringNode string 22 23 func (s StringNode) Key() interface{} { return s } 24 25 // Oldstore is a mocked version of the QuadStore interface, for use in tests. 26 type Oldstore struct { 27 Parse bool 28 Data []string 29 Iter graph.Iterator 30 } 31 32 func (qs *Oldstore) valueAt(i int) quad.Value { 33 if !qs.Parse { 34 return quad.Raw(qs.Data[i]) 35 } 36 iv, err := strconv.Atoi(qs.Data[i]) 37 if err == nil { 38 return quad.Int(iv) 39 } 40 return quad.String(qs.Data[i]) 41 } 42 43 func (qs *Oldstore) ValueOf(s quad.Value) graph.Ref { 44 if s == nil { 45 return nil 46 } 47 for i := range qs.Data { 48 if va := qs.valueAt(i); va != nil && s.String() == va.String() { 49 return iterator.Int64Node(i) 50 } 51 } 52 return nil 53 } 54 55 func (qs *Oldstore) NewQuadWriter() (quad.WriteCloser, error) { 56 return nopWriter{}, nil 57 } 58 59 type nopWriter struct{} 60 61 func (nopWriter) WriteQuad(q quad.Quad) error { 62 return nil 63 } 64 65 func (nopWriter) WriteQuads(buf []quad.Quad) (int, error) { 66 return len(buf), nil 67 } 68 69 func (nopWriter) Close() error { 70 return nil 71 } 72 73 func (qs *Oldstore) ApplyDeltas([]graph.Delta, graph.IgnoreOpts) error { return nil } 74 75 func (qs *Oldstore) Quad(graph.Ref) quad.Quad { return quad.Quad{} } 76 77 func (qs *Oldstore) QuadIterator(d quad.Direction, i graph.Ref) graph.Iterator { 78 return qs.Iter 79 } 80 81 func (qs *Oldstore) QuadIteratorSize(ctx context.Context, d quad.Direction, val graph.Ref) (graph.Size, error) { 82 sz, exact := qs.Iter.Size() 83 return graph.Size{Size: sz, Exact: exact}, nil 84 } 85 86 func (qs *Oldstore) NodesAllIterator() graph.Iterator { return &iterator.Null{} } 87 88 func (qs *Oldstore) QuadsAllIterator() graph.Iterator { return &iterator.Null{} } 89 90 func (qs *Oldstore) NameOf(v graph.Ref) quad.Value { 91 switch v := v.(type) { 92 case iterator.Int64Node: 93 i := int(v) 94 if i < 0 || i >= len(qs.Data) { 95 return nil 96 } 97 return qs.valueAt(i) 98 case StringNode: 99 if qs.Parse { 100 return quad.String(v) 101 } 102 return quad.Raw(string(v)) 103 default: 104 return nil 105 } 106 } 107 108 func (qs *Oldstore) Size() int64 { return 0 } 109 110 func (qs *Oldstore) DebugPrint() {} 111 112 func (qs *Oldstore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) { 113 return &iterator.Null{}, false 114 } 115 116 func (qs *Oldstore) Close() error { return nil } 117 118 func (qs *Oldstore) QuadDirection(graph.Ref, quad.Direction) graph.Ref { 119 return iterator.Int64Node(0) 120 } 121 122 func (qs *Oldstore) RemoveQuad(t quad.Quad) {} 123 124 func (qs *Oldstore) Type() string { return "oldmockstore" } 125 126 type Store struct { 127 Data []quad.Quad 128 } 129 130 var _ graph.QuadStore = &Store{} 131 132 func (qs *Store) ValueOf(s quad.Value) graph.Ref { 133 for _, q := range qs.Data { 134 if q.Subject == s || q.Object == s { 135 return graph.PreFetched(s) 136 } 137 } 138 return nil 139 } 140 141 func (qs *Store) ApplyDeltas([]graph.Delta, graph.IgnoreOpts) error { return nil } 142 143 func (qs *Store) NewQuadWriter() (quad.WriteCloser, error) { 144 return nopWriter{}, nil 145 } 146 147 type quadValue struct { 148 q quad.Quad 149 } 150 151 func (q quadValue) Key() interface{} { 152 return q.q.String() 153 } 154 155 func (qs *Store) Quad(v graph.Ref) quad.Quad { return v.(quadValue).q } 156 157 func (qs *Store) NameOf(v graph.Ref) quad.Value { 158 if v == nil { 159 return nil 160 } 161 return v.(graph.PreFetchedValue).NameOf() 162 } 163 164 func (qs *Store) RemoveQuad(t quad.Quad) {} 165 166 func (qs *Store) Type() string { return "mockstore" } 167 168 func (qs *Store) QuadDirection(v graph.Ref, d quad.Direction) graph.Ref { 169 return graph.PreFetched(qs.Quad(v).Get(d)) 170 } 171 172 func (qs *Store) Close() error { return nil } 173 174 func (qs *Store) DebugPrint() {} 175 176 func (qs *Store) QuadIterator(d quad.Direction, i graph.Ref) graph.Iterator { 177 fixed := iterator.NewFixed() 178 v := i.(graph.PreFetchedValue).NameOf() 179 for _, q := range qs.Data { 180 if q.Get(d) == v { 181 fixed.Add(quadValue{q}) 182 } 183 } 184 return fixed 185 } 186 187 func (qs *Store) QuadIteratorSize(ctx context.Context, d quad.Direction, val graph.Ref) (graph.Size, error) { 188 v := val.(graph.PreFetchedValue).NameOf() 189 sz := graph.Size{Exact: true} 190 for _, q := range qs.Data { 191 if q.Get(d) == v { 192 sz.Size++ 193 } 194 } 195 return sz, nil 196 } 197 198 func (qs *Store) NodesAllIterator() graph.Iterator { 199 set := make(map[string]bool) 200 for _, q := range qs.Data { 201 for _, d := range quad.Directions { 202 n := qs.NameOf(graph.PreFetched(q.Get(d))) 203 if n != nil { 204 set[n.String()] = true 205 } 206 } 207 } 208 fixed := iterator.NewFixed() 209 for k := range set { 210 fixed.Add(graph.PreFetched(quad.Raw(k))) 211 } 212 return fixed 213 } 214 215 func (qs *Store) QuadsAllIterator() graph.Iterator { 216 fixed := iterator.NewFixed() 217 for _, q := range qs.Data { 218 fixed.Add(quadValue{q}) 219 } 220 return fixed 221 } 222 223 func (qs *Store) Stats(ctx context.Context, exact bool) (graph.Stats, error) { 224 set := make(map[string]struct{}) 225 for _, q := range qs.Data { 226 for _, d := range quad.Directions { 227 n := qs.NameOf(graph.PreFetched(q.Get(d))) 228 if n != nil { 229 set[n.String()] = struct{}{} 230 } 231 } 232 } 233 return graph.Stats{ 234 Nodes: graph.Size{Size: int64(len(set)), Exact: true}, 235 Quads: graph.Size{Size: int64(len(qs.Data)), Exact: true}, 236 }, nil 237 }