| 95 | } |
| 96 | |
| 97 | func (r Registration) quadIndexes(options graph.Options) []string { |
| 98 | indexes := make([]string, 0, 10) |
| 99 | if r.ConditionalIndexes { |
| 100 | indexes = append(indexes, |
| 101 | `CREATE UNIQUE INDEX spo_unique ON quads (subject_hash, predicate_hash, object_hash) WHERE label_hash IS NULL;`, |
| 102 | `CREATE UNIQUE INDEX spol_unique ON quads (subject_hash, predicate_hash, object_hash, label_hash) WHERE label_hash IS NOT NULL;`, |
| 103 | ) |
| 104 | } else { |
| 105 | indexes = append(indexes, |
| 106 | `CREATE UNIQUE INDEX spo_unique ON quads (subject_hash, predicate_hash, object_hash);`, |
| 107 | `CREATE UNIQUE INDEX spol_unique ON quads (subject_hash, predicate_hash, object_hash, label_hash);`, |
| 108 | ) |
| 109 | } |
| 110 | if !r.NoForeignKeys { |
| 111 | indexes = append(indexes, |
| 112 | `ALTER TABLE quads ADD CONSTRAINT subject_hash_fk FOREIGN KEY (subject_hash) REFERENCES nodes (hash);`, |
| 113 | `ALTER TABLE quads ADD CONSTRAINT predicate_hash_fk FOREIGN KEY (predicate_hash) REFERENCES nodes (hash);`, |
| 114 | `ALTER TABLE quads ADD CONSTRAINT object_hash_fk FOREIGN KEY (object_hash) REFERENCES nodes (hash);`, |
| 115 | `ALTER TABLE quads ADD CONSTRAINT label_hash_fk FOREIGN KEY (label_hash) REFERENCES nodes (hash);`, |
| 116 | ) |
| 117 | } |
| 118 | quadIndexes := [][3]quad.Direction{ |
| 119 | {quad.Subject, quad.Predicate, quad.Object}, |
| 120 | {quad.Object, quad.Predicate, quad.Subject}, |
| 121 | {quad.Predicate, quad.Object, quad.Subject}, |
| 122 | {quad.Object, quad.Subject, quad.Predicate}, |
| 123 | } |
| 124 | factor, _ := options.IntKey("db_fill_factor", 50) |
| 125 | for _, ind := range quadIndexes { |
| 126 | var ( |
| 127 | name string |
| 128 | cols []string |
| 129 | ) |
| 130 | for _, d := range ind { |
| 131 | name += string(d.Prefix()) |
| 132 | cols = append(cols, d.String()+"_hash") |
| 133 | } |
| 134 | q := fmt.Sprintf(`CREATE INDEX %s_index ON quads (%s)`, |
| 135 | name, strings.Join(cols, ", ")) |
| 136 | if r.FillFactor { |
| 137 | q += fmt.Sprintf(" WITH (FILLFACTOR = %d)", factor) |
| 138 | } |
| 139 | indexes = append(indexes, q+";") |
| 140 | } |
| 141 | return indexes |
| 142 | } |