Split intersection and node iteration into two logical SQL iterators

This commit is contained in:
Barak Michener 2015-07-30 18:40:48 -04:00
parent 3e02bb2b71
commit 8830760df2
3 changed files with 259 additions and 99 deletions

View file

@ -31,6 +31,7 @@ type sqlQueryType int
const (
node sqlQueryType = iota
link
nodeIntersect
)
func init() {
@ -45,10 +46,9 @@ func newNodeTableName() string {
type SQLNodeIterator struct {
tableName string
linkIts []sqlItDir
nodetables []string
size int64
tagger graph.Tagger
linkIt sqlItDir
size int64
tagger graph.Tagger
result string
}
@ -57,12 +57,10 @@ func (n *SQLNodeIterator) sqlClone() sqlIterator {
m := &SQLNodeIterator{
tableName: n.tableName,
size: n.size,
}
for _, i := range n.linkIts {
m.linkIts = append(m.linkIts, sqlItDir{
dir: i.dir,
it: i.it.sqlClone(),
})
linkIt: sqlItDir{
dir: n.linkIt.dir,
it: n.linkIt.it.sqlClone(),
},
}
m.tagger.CopyFromTagger(n.Tagger())
return m
@ -81,7 +79,7 @@ func (n *SQLNodeIterator) Type() sqlQueryType {
}
func (n *SQLNodeIterator) Size(qs *QuadStore) (int64, bool) {
return qs.Size() / int64(len(n.linkIts)+1), true
return qs.Size() / 2, true
}
func (n *SQLNodeIterator) Describe() string {
@ -99,25 +97,10 @@ func (n *SQLNodeIterator) buildResult(result []string, cols []string) map[string
return m
}
func (n *SQLNodeIterator) makeNodeTableNames() {
if n.nodetables != nil {
return
}
n.nodetables = make([]string, len(n.linkIts))
for i, _ := range n.nodetables {
n.nodetables[i] = newNodeTableName()
}
}
func (n *SQLNodeIterator) getTables() []tableDef {
var out []tableDef
switch len(n.linkIts) {
case 0:
return []tableDef{tableDef{table: "quads", name: n.tableName}}
case 1:
out = n.linkIts[0].it.getTables()
default:
return n.buildSubqueries()
if n.linkIt.it != nil {
out = n.linkIt.it.getTables()
}
if len(out) == 0 {
out = append(out, tableDef{table: "quads", name: n.tableName})
@ -125,49 +108,19 @@ func (n *SQLNodeIterator) getTables() []tableDef {
return out
}
func (n *SQLNodeIterator) buildSubqueries() []tableDef {
var out []tableDef
n.makeNodeTableNames()
for i, it := range n.linkIts {
var td tableDef
// TODO(barakmich): This is a dirty hack. The real implementation is to
// separate SQL iterators to build a similar tree as we're doing here, and
// have a single graph.Iterator 'caddy' structure around it.
subNode := &SQLNodeIterator{
tableName: newTableName(),
linkIts: []sqlItDir{it},
}
var table string
table, td.values = subNode.buildSQL(true, nil)
td.table = fmt.Sprintf("\n(%s)", table[:len(table)-1])
td.name = n.nodetables[i]
out = append(out, td)
}
return out
}
func (n *SQLNodeIterator) tableID() tagDir {
switch len(n.linkIts) {
case 0:
if n.linkIt.it != nil {
return tagDir{
table: n.tableName,
dir: quad.Any,
tag: "__execd",
}
case 1:
return tagDir{
table: n.linkIts[0].it.tableID().table,
dir: n.linkIts[0].dir,
tag: "__execd",
}
default:
n.makeNodeTableNames()
return tagDir{
table: n.nodetables[0],
dir: quad.Any,
table: n.linkIt.it.tableID().table,
dir: n.linkIt.dir,
tag: "__execd",
}
}
return tagDir{
table: n.tableName,
dir: quad.Any,
tag: "__execd",
}
}
func (n *SQLNodeIterator) getLocalTags() []tagDir {
@ -186,21 +139,8 @@ func (n *SQLNodeIterator) getLocalTags() []tagDir {
func (n *SQLNodeIterator) getTags() []tagDir {
out := n.getLocalTags()
if len(n.linkIts) > 1 {
n.makeNodeTableNames()
for i, it := range n.linkIts {
for _, v := range it.it.getTags() {
out = append(out, tagDir{
tag: v.tag,
dir: quad.Any,
table: n.nodetables[i],
})
}
}
return out
}
for _, i := range n.linkIts {
out = append(out, i.it.getTags()...)
if n.linkIt.it != nil {
out = append(out, n.linkIt.it.getTags()...)
}
return out
}
@ -208,16 +148,10 @@ func (n *SQLNodeIterator) getTags() []tagDir {
func (n *SQLNodeIterator) buildWhere() (string, []string) {
var q []string
var vals []string
if len(n.linkIts) > 1 {
for _, tb := range n.nodetables[1:] {
q = append(q, fmt.Sprintf("%s.__execd = %s.__execd", n.nodetables[0], tb))
}
} else {
for _, i := range n.linkIts {
s, v := i.it.buildWhere()
q = append(q, s)
vals = append(vals, v...)
}
if n.linkIt.it != nil {
s, v := n.linkIt.it.buildWhere()
q = append(q, s)
vals = append(vals, v...)
}
query := strings.Join(q, " AND ")
return query, vals