Comparison of -short benchmarks in cayley.
$ benchcmp pointer.bench concrete.bench
benchmark old ns/op new ns/op delta
BenchmarkNamePredicate 1673276 1655093 -1.09%
BenchmarkLargeSetsNoIntersection 318985907 261499984 -18.02%
BenchmarkNetAndSpeed 104403743 41516981 -60.23%
BenchmarkKeanuAndNet 17309258 16857513 -2.61%
BenchmarkKeanuAndSpeed 20159161 19282833 -4.35%
Comparison of pathological cases are not so happy.
benchmark old ns/op new ns/op delta
BenchmarkVeryLargeSetsSmallIntersection 55269775527 246084606672 +345.24%
BenchmarkHelplessContainsChecker 23436501319 24308906949 +3.72%
Profiling the worst case:
Pointer:
Total: 6121 samples
1973 32.2% 32.2% 1973 32.2% runtime.findfunc
773 12.6% 44.9% 773 12.6% readvarint
510 8.3% 53.2% 511 8.3% step
409 6.7% 59.9% 410 6.7% runtime.gentraceback
390 6.4% 66.2% 391 6.4% pcvalue
215 3.5% 69.8% 215 3.5% runtime.funcdata
181 3.0% 72.7% 181 3.0% checkframecopy
118 1.9% 74.6% 119 1.9% runtime.funcspdelta
96 1.6% 76.2% 96 1.6% runtime.topofstack
76 1.2% 77.5% 76 1.2% scanblock
Concrete:
Total: 25027 samples
9437 37.7% 37.7% 9437 37.7% runtime.findfunc
3853 15.4% 53.1% 3853 15.4% readvarint
2366 9.5% 62.6% 2366 9.5% step
2186 8.7% 71.3% 2186 8.7% runtime.gentraceback
1816 7.3% 78.5% 1816 7.3% pcvalue
1016 4.1% 82.6% 1016 4.1% runtime.funcdata
859 3.4% 86.0% 859 3.4% checkframecopy
506 2.0% 88.1% 506 2.0% runtime.funcspdelta
410 1.6% 89.7% 410 1.6% runtime.topofstack
303 1.2% 90.9% 303 1.2% runtime.newstack
151 lines
4 KiB
Go
151 lines
4 KiB
Go
// 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 quad defines quad and triple handling.
|
|
package quad
|
|
|
|
// Defines the struct which makes the TripleStore possible -- the triple.
|
|
//
|
|
// At its heart, it consists of three fields -- Subject, Predicate, and Object.
|
|
// Three IDs that relate to each other. That's all there is to it. The triples
|
|
// are the links in the graph, and the existence of node IDs is defined by the
|
|
// fact that some triple in the graph mentions them.
|
|
//
|
|
// This means that a complete representation of the graph is equivalent to a
|
|
// list of triples. The rest is just indexing for speed.
|
|
//
|
|
// Adding fields to the triple is not to be taken lightly. You'll see I mention
|
|
// label, but don't as yet use it in any backing store. In general, there
|
|
// can be features that can be turned on or off for any store, but I haven't
|
|
// decided how to allow/disallow them yet. Another such example would be to add
|
|
// a forward and reverse index field -- forward being "order the list of
|
|
// objects pointed at by this subject with this predicate" such as first and
|
|
// second children, top billing, what have you.
|
|
//
|
|
// There will never be that much in this file except for the definition, but
|
|
// the consequences are not to be taken lightly. But do suggest cool features!
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
var (
|
|
ErrInvalid = errors.New("invalid N-Quad")
|
|
ErrIncomplete = errors.New("incomplete N-Quad")
|
|
)
|
|
|
|
// Our triple struct, used throughout.
|
|
type Quad struct {
|
|
Subject string `json:"subject"`
|
|
Predicate string `json:"predicate"`
|
|
Object string `json:"object"`
|
|
Label string `json:"label,omitempty"`
|
|
}
|
|
|
|
// Direction specifies an edge's type.
|
|
type Direction byte
|
|
|
|
// List of the valid directions of a triple.
|
|
const (
|
|
Any Direction = iota
|
|
Subject
|
|
Predicate
|
|
Object
|
|
Label
|
|
)
|
|
|
|
func (d Direction) Prefix() byte {
|
|
switch d {
|
|
case Any:
|
|
return 'a'
|
|
case Subject:
|
|
return 's'
|
|
case Predicate:
|
|
return 'p'
|
|
case Label:
|
|
return 'c'
|
|
case Object:
|
|
return 'o'
|
|
default:
|
|
return '\x00'
|
|
}
|
|
}
|
|
|
|
func (d Direction) String() string {
|
|
switch d {
|
|
case Any:
|
|
return "any"
|
|
case Subject:
|
|
return "subject"
|
|
case Predicate:
|
|
return "predicate"
|
|
case Label:
|
|
return "label"
|
|
case Object:
|
|
return "object"
|
|
default:
|
|
return fmt.Sprint("illegal direction:", byte(d))
|
|
}
|
|
}
|
|
|
|
// TODO(kortschak) Consider writing methods onto the concrete type
|
|
// instead of the pointer. This needs benchmarking to make the decision.
|
|
|
|
// Per-field accessor for triples
|
|
func (q Quad) Get(d Direction) string {
|
|
switch d {
|
|
case Subject:
|
|
return q.Subject
|
|
case Predicate:
|
|
return q.Predicate
|
|
case Label:
|
|
return q.Label
|
|
case Object:
|
|
return q.Object
|
|
default:
|
|
panic(d.String())
|
|
}
|
|
}
|
|
|
|
func (q Quad) Equals(o Quad) bool {
|
|
return q == o
|
|
}
|
|
|
|
// Pretty-prints a triple.
|
|
func (q Quad) String() string {
|
|
return fmt.Sprintf("%s -- %s -> %s", q.Subject, q.Predicate, q.Object)
|
|
}
|
|
|
|
func (q Quad) IsValid() bool {
|
|
return q.Subject != "" && q.Predicate != "" && q.Object != ""
|
|
}
|
|
|
|
// TODO(kortschak) NTriple looks like a good candidate for conversion
|
|
// to MarshalText() (text []byte, err error) and then move parsing code
|
|
// from nquads to here to provide UnmarshalText(text []byte) error.
|
|
|
|
// Prints a triple in N-Quad format.
|
|
func (q Quad) NTriple() string {
|
|
if q.Label == "" {
|
|
//TODO(barakmich): Proper escaping.
|
|
return fmt.Sprintf("%s %s %s .", q.Subject, q.Predicate, q.Object)
|
|
} else {
|
|
return fmt.Sprintf("%s %s %s %s .", q.Subject, q.Predicate, q.Object, q.Label)
|
|
}
|
|
}
|
|
|
|
type Unmarshaler interface {
|
|
Unmarshal() (Quad, error)
|
|
}
|