Convert Type fields to use graph.Type
Add text encoding methods to replace string storage.
This commit is contained in:
parent
62013d3dfc
commit
e2eea6c283
18 changed files with 84 additions and 20 deletions
|
|
@ -182,7 +182,7 @@ func (it *AllIterator) Describe() graph.Description {
|
|||
size, _ := it.Size()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: size,
|
||||
Direction: it.dir,
|
||||
|
|
|
|||
|
|
@ -294,7 +294,7 @@ func (it *Iterator) Describe() graph.Description {
|
|||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Name: it.qs.NameOf(&Token{it.bucket, it.checkID}),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: it.size,
|
||||
Direction: it.dir,
|
||||
|
|
|
|||
|
|
@ -17,11 +17,12 @@ package graph
|
|||
// Define the general iterator interface.
|
||||
|
||||
import (
|
||||
"github.com/google/cayley/quad"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
"github.com/google/cayley/quad"
|
||||
)
|
||||
|
||||
type Tagger struct {
|
||||
|
|
@ -144,7 +145,7 @@ type Iterator interface {
|
|||
type Description struct {
|
||||
UID uint64 `json:",omitempty"`
|
||||
Name string `json:",omitempty"`
|
||||
Type string `json:",omitempty"`
|
||||
Type Type `json:",omitempty"`
|
||||
Tags []string `json:",omitempty"`
|
||||
Size int64 `json:",omitempty"`
|
||||
Direction quad.Direction `json:",omitempty"`
|
||||
|
|
@ -267,9 +268,27 @@ func (t Type) String() string {
|
|||
return types[t]
|
||||
}
|
||||
|
||||
func (t *Type) MarshalText() (text []byte, err error) {
|
||||
if *t < 0 || int(*t) >= len(types) {
|
||||
return nil, fmt.Errorf("graph: illegal iterator type: %d", *t)
|
||||
}
|
||||
return []byte(types[*t]), nil
|
||||
}
|
||||
|
||||
func (t *Type) UnmarshalText(text []byte) error {
|
||||
s := string(text)
|
||||
for i, c := range types[1:] {
|
||||
if c == s {
|
||||
*t = Type(i + 1)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("graph: unknown iterator label: %q", text)
|
||||
}
|
||||
|
||||
type StatsContainer struct {
|
||||
UID uint64
|
||||
Type string
|
||||
Type Type
|
||||
IteratorStats
|
||||
SubIts []StatsContainer
|
||||
}
|
||||
|
|
@ -277,7 +296,7 @@ type StatsContainer struct {
|
|||
func DumpStats(it Iterator) StatsContainer {
|
||||
var out StatsContainer
|
||||
out.IteratorStats = it.Stats()
|
||||
out.Type = it.Type().String()
|
||||
out.Type = it.Type()
|
||||
out.UID = it.UID()
|
||||
for _, sub := range it.SubIterators() {
|
||||
out.SubIts = append(out.SubIts, DumpStats(sub))
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ func (it *Int64) TagResults(dst map[string]graph.Value) {
|
|||
func (it *Int64) Describe() graph.Description {
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ func (it *And) Describe() graph.Description {
|
|||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterator: &primary,
|
||||
Iterators: subIts,
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ func (it *Fixed) Describe() graph.Description {
|
|||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Name: value,
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: fixed,
|
||||
Size: int64(len(it.values)),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ func (it *HasA) Describe() graph.Description {
|
|||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Direction: it.dir,
|
||||
Iterator: &primary,
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ func (it *Null) Optimize() (graph.Iterator, bool) { return it, false }
|
|||
func (it *Null) Describe() graph.Description {
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ func (it *LinksTo) Describe() graph.Description {
|
|||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Direction: it.dir,
|
||||
Iterator: &primary,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ func (it *Materialize) Describe() graph.Description {
|
|||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: int64(len(it.values)),
|
||||
Iterator: &primary,
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ func (it *Optional) Describe() graph.Description {
|
|||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterator: &primary,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ func (it *Or) Describe() graph.Description {
|
|||
}
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterators: subIts,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ func (it *Comparison) Describe() graph.Description {
|
|||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Iterator: &primary,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
45
graph/iterator_test.go
Normal file
45
graph/iterator_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2014 The Cayley Authors. All rights reserved.
|
||||
//
|
||||
// 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 graph
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTypeEncodingRoundtrip(t *testing.T) {
|
||||
for i := Invalid; i <= Materialize; i++ {
|
||||
text, err := i.MarshalText()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error when marshaling %s: %v", i, err)
|
||||
}
|
||||
if string(text) != i.String() {
|
||||
t.Errorf("Unexpected MarshalText result, got:%q expect:%q", i, text)
|
||||
}
|
||||
var m Type
|
||||
err = m.UnmarshalText(text)
|
||||
if i == Invalid {
|
||||
if err == nil || err.Error() != `graph: unknown iterator label: "invalid"` {
|
||||
t.Errorf("Unexpected error when unmarshaling %q: %v", text, err)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error when unmarshaling %q: %v", text, err)
|
||||
}
|
||||
}
|
||||
if m != i {
|
||||
t.Errorf("Unexpected UnmarshalText result, got:Type(%d) expect:Type(%d)", m, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -161,7 +161,7 @@ func (it *AllIterator) Describe() graph.Description {
|
|||
size, _ := it.Size()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: size,
|
||||
Direction: it.dir,
|
||||
|
|
|
|||
|
|
@ -253,7 +253,7 @@ func (it *Iterator) Describe() graph.Description {
|
|||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Name: it.qs.NameOf(Token(it.checkID)),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: size,
|
||||
Direction: it.dir,
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ func (it *Iterator) Describe() graph.Description {
|
|||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Name: it.data,
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: size,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -217,7 +217,7 @@ func (it *Iterator) Describe() graph.Description {
|
|||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Name: fmt.Sprintf("%s/%s", it.name, it.hash),
|
||||
Type: it.Type().String(),
|
||||
Type: it.Type(),
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue