Convert Type fields to use graph.Type

Add text encoding methods to replace string storage.
This commit is contained in:
kortschak 2014-09-05 09:49:15 +09:30
parent 62013d3dfc
commit e2eea6c283
18 changed files with 84 additions and 20 deletions

View file

@ -182,7 +182,7 @@ func (it *AllIterator) Describe() graph.Description {
size, _ := it.Size() size, _ := it.Size()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: size, Size: size,
Direction: it.dir, Direction: it.dir,

View file

@ -294,7 +294,7 @@ func (it *Iterator) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Name: it.qs.NameOf(&Token{it.bucket, it.checkID}), Name: it.qs.NameOf(&Token{it.bucket, it.checkID}),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: it.size, Size: it.size,
Direction: it.dir, Direction: it.dir,

View file

@ -17,11 +17,12 @@ package graph
// Define the general iterator interface. // Define the general iterator interface.
import ( import (
"github.com/google/cayley/quad" "fmt"
"strings" "strings"
"sync" "sync"
"github.com/barakmich/glog" "github.com/barakmich/glog"
"github.com/google/cayley/quad"
) )
type Tagger struct { type Tagger struct {
@ -144,7 +145,7 @@ type Iterator interface {
type Description struct { type Description struct {
UID uint64 `json:",omitempty"` UID uint64 `json:",omitempty"`
Name string `json:",omitempty"` Name string `json:",omitempty"`
Type string `json:",omitempty"` Type Type `json:",omitempty"`
Tags []string `json:",omitempty"` Tags []string `json:",omitempty"`
Size int64 `json:",omitempty"` Size int64 `json:",omitempty"`
Direction quad.Direction `json:",omitempty"` Direction quad.Direction `json:",omitempty"`
@ -267,9 +268,27 @@ func (t Type) String() string {
return types[t] 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 { type StatsContainer struct {
UID uint64 UID uint64
Type string Type Type
IteratorStats IteratorStats
SubIts []StatsContainer SubIts []StatsContainer
} }
@ -277,7 +296,7 @@ type StatsContainer struct {
func DumpStats(it Iterator) StatsContainer { func DumpStats(it Iterator) StatsContainer {
var out StatsContainer var out StatsContainer
out.IteratorStats = it.Stats() out.IteratorStats = it.Stats()
out.Type = it.Type().String() out.Type = it.Type()
out.UID = it.UID() out.UID = it.UID()
for _, sub := range it.SubIterators() { for _, sub := range it.SubIterators() {
out.SubIts = append(out.SubIts, DumpStats(sub)) out.SubIts = append(out.SubIts, DumpStats(sub))

View file

@ -81,7 +81,7 @@ func (it *Int64) TagResults(dst map[string]graph.Value) {
func (it *Int64) Describe() graph.Description { func (it *Int64) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
} }
} }

View file

@ -116,7 +116,7 @@ func (it *And) Describe() graph.Description {
primary := it.primaryIt.Describe() primary := it.primaryIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Iterator: &primary, Iterator: &primary,
Iterators: subIts, Iterators: subIts,

View file

@ -107,7 +107,7 @@ func (it *Fixed) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Name: value, Name: value,
Type: it.Type().String(), Type: it.Type(),
Tags: fixed, Tags: fixed,
Size: int64(len(it.values)), Size: int64(len(it.values)),
} }

View file

@ -131,7 +131,7 @@ func (it *HasA) Describe() graph.Description {
primary := it.primaryIt.Describe() primary := it.primaryIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Direction: it.dir, Direction: it.dir,
Iterator: &primary, Iterator: &primary,

View file

@ -80,7 +80,7 @@ func (it *Null) Optimize() (graph.Iterator, bool) { return it, false }
func (it *Null) Describe() graph.Description { func (it *Null) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
} }
} }

View file

@ -109,7 +109,7 @@ func (it *LinksTo) Describe() graph.Description {
primary := it.primaryIt.Describe() primary := it.primaryIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Direction: it.dir, Direction: it.dir,
Iterator: &primary, Iterator: &primary,
} }

View file

@ -119,7 +119,7 @@ func (it *Materialize) Describe() graph.Description {
primary := it.subIt.Describe() primary := it.subIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: int64(len(it.values)), Size: int64(len(it.values)),
Iterator: &primary, Iterator: &primary,

View file

@ -121,7 +121,7 @@ func (it *Optional) Describe() graph.Description {
primary := it.subIt.Describe() primary := it.subIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Iterator: &primary, Iterator: &primary,
} }

View file

@ -117,7 +117,7 @@ func (it *Or) Describe() graph.Description {
} }
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Iterators: subIts, Iterators: subIts,
} }

View file

@ -192,7 +192,7 @@ func (it *Comparison) Describe() graph.Description {
primary := it.subIt.Describe() primary := it.subIt.Describe()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Iterator: &primary, Iterator: &primary,
} }
} }

45
graph/iterator_test.go Normal file
View 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)
}
}
}

View file

@ -161,7 +161,7 @@ func (it *AllIterator) Describe() graph.Description {
size, _ := it.Size() size, _ := it.Size()
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: size, Size: size,
Direction: it.dir, Direction: it.dir,

View file

@ -253,7 +253,7 @@ func (it *Iterator) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Name: it.qs.NameOf(Token(it.checkID)), Name: it.qs.NameOf(Token(it.checkID)),
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: size, Size: size,
Direction: it.dir, Direction: it.dir,

View file

@ -162,7 +162,7 @@ func (it *Iterator) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Name: it.data, Name: it.data,
Type: it.Type().String(), Type: it.Type(),
Tags: it.tags.Tags(), Tags: it.tags.Tags(),
Size: size, Size: size,
} }

View file

@ -217,7 +217,7 @@ func (it *Iterator) Describe() graph.Description {
return graph.Description{ return graph.Description{
UID: it.UID(), UID: it.UID(),
Name: fmt.Sprintf("%s/%s", it.name, it.hash), Name: fmt.Sprintf("%s/%s", it.name, it.hash),
Type: it.Type().String(), Type: it.Type(),
Size: size, Size: size,
} }
} }