2013-11-15 18:55:45 -05:00
|
|
|
package graphdb
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2014-02-18 05:41:11 -05:00
|
|
|
"strings"
|
2013-11-04 13:28:38 -05:00
|
|
|
"sync"
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
createEntityTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS entity (
|
|
|
|
id text NOT NULL PRIMARY KEY
|
|
|
|
);`
|
|
|
|
|
|
|
|
createEdgeTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS edge (
|
|
|
|
"entity_id" text NOT NULL,
|
|
|
|
"parent_id" text NULL,
|
|
|
|
"name" text NOT NULL,
|
|
|
|
CONSTRAINT "parent_fk" FOREIGN KEY ("parent_id") REFERENCES "entity" ("id"),
|
|
|
|
CONSTRAINT "entity_fk" FOREIGN KEY ("entity_id") REFERENCES "entity" ("id")
|
|
|
|
);
|
2013-10-09 22:44:31 -04:00
|
|
|
`
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-10-09 22:44:31 -04:00
|
|
|
createEdgeIndices = `
|
2013-10-22 19:23:52 -04:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "name_parent_ix" ON "edge" (parent_id, name);
|
2013-10-04 22:25:15 -04:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Entity with a unique id.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Entity struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// An Edge connects two entities together.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Edge struct {
|
|
|
|
EntityID string
|
|
|
|
Name string
|
|
|
|
ParentID string
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Entities stores the list of entities.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Entities map[string]*Entity
|
2015-06-16 05:51:27 -04:00
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Edges stores the relationships between entities.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Edges []*Edge
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// WalkFunc is a function invoked to process an individual entity.
|
2013-10-04 22:25:15 -04:00
|
|
|
type WalkFunc func(fullPath string, entity *Entity) error
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Database is a graph database for storing entities and their relationships.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Database struct {
|
2013-10-22 19:23:52 -04:00
|
|
|
conn *sql.DB
|
2013-11-05 22:07:14 -05:00
|
|
|
mux sync.RWMutex
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// IsNonUniqueNameError processes the error to check if it's caused by
|
|
|
|
// a constraint violation.
|
|
|
|
// This is necessary because the error isn't the same across various
|
|
|
|
// sqlite versions.
|
2014-02-18 05:41:11 -05:00
|
|
|
func IsNonUniqueNameError(err error) bool {
|
|
|
|
str := err.Error()
|
|
|
|
// sqlite 3.7.17-1ubuntu1 returns:
|
|
|
|
// Set failure: Abort due to constraint violation: columns parent_id, name are not unique
|
|
|
|
if strings.HasSuffix(str, "name are not unique") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// sqlite-3.8.3-1.fc20 returns:
|
|
|
|
// Set failure: Abort due to constraint violation: UNIQUE constraint failed: edge.parent_id, edge.name
|
|
|
|
if strings.Contains(str, "UNIQUE constraint failed") && strings.Contains(str, "edge.name") {
|
|
|
|
return true
|
|
|
|
}
|
2014-06-19 15:24:22 -04:00
|
|
|
// sqlite-3.6.20-1.el6 returns:
|
|
|
|
// Set failure: Abort due to constraint violation: constraint failed
|
|
|
|
if strings.HasSuffix(str, "constraint failed") {
|
|
|
|
return true
|
|
|
|
}
|
2014-02-18 05:41:11 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewDatabase creates a new graph database initialized with a root entity.
|
2014-12-02 06:02:25 -05:00
|
|
|
func NewDatabase(conn *sql.DB) (*Database, error) {
|
2013-10-22 19:23:52 -04:00
|
|
|
if conn == nil {
|
|
|
|
return nil, fmt.Errorf("Database connection cannot be nil")
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-11-04 13:28:38 -05:00
|
|
|
db := &Database{conn: conn}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
// Create root entities
|
|
|
|
tx, err := conn.Begin()
|
|
|
|
if err != nil {
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 06:57:21 -05:00
|
|
|
|
|
|
|
if _, err := tx.Exec(createEntityTable); err != nil {
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec(createEdgeTable); err != nil {
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec(createEdgeIndices); err != nil {
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("DELETE FROM entity where id = ?", "0"); err != nil {
|
|
|
|
tx.Rollback()
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2014-12-02 06:02:25 -05:00
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("INSERT INTO entity (id) VALUES (?);", "0"); err != nil {
|
|
|
|
tx.Rollback()
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("DELETE FROM edge where entity_id=? and name=?", "0", "/"); err != nil {
|
|
|
|
tx.Rollback()
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("INSERT INTO edge (entity_id, name) VALUES(?,?);", "0", "/"); err != nil {
|
|
|
|
tx.Rollback()
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if err := tx.Commit(); err != nil {
|
2014-12-02 06:02:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Close the underlying connection to the database.
|
2013-10-22 19:23:52 -04:00
|
|
|
func (db *Database) Close() error {
|
|
|
|
return db.conn.Close()
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Set the entity id for a given path.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) Set(fullPath, id string) (*Entity, error) {
|
2013-11-04 13:28:38 -05:00
|
|
|
db.mux.Lock()
|
|
|
|
defer db.mux.Unlock()
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
tx, err := db.conn.Begin()
|
|
|
|
if err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 06:57:21 -05:00
|
|
|
|
2014-10-06 15:57:27 -04:00
|
|
|
var entityID string
|
2014-12-19 06:57:21 -05:00
|
|
|
if err := tx.QueryRow("SELECT id FROM entity WHERE id = ?;", id).Scan(&entityID); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
if err == sql.ErrNoRows {
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("INSERT INTO entity (id) VALUES(?);", id); err != nil {
|
|
|
|
tx.Rollback()
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-19 06:57:21 -05:00
|
|
|
tx.Rollback()
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e := &Entity{id}
|
|
|
|
|
|
|
|
parentPath, name := splitPath(fullPath)
|
2014-12-19 06:57:21 -05:00
|
|
|
if err := db.setEdge(parentPath, name, e, tx); err != nil {
|
|
|
|
tx.Rollback()
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if err := tx.Commit(); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Exists returns true if a name already exists in the database.
|
2013-10-28 19:58:59 -04:00
|
|
|
func (db *Database) Exists(name string) bool {
|
2013-11-05 22:07:14 -05:00
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
2013-11-05 18:26:07 -05:00
|
|
|
|
|
|
|
e, err := db.get(name)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return e != nil
|
2013-10-28 19:58:59 -04:00
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
func (db *Database) setEdge(parentPath, name string, e *Entity, tx *sql.Tx) error {
|
2013-10-22 19:23:52 -04:00
|
|
|
parent, err := db.get(parentPath)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parent.id == e.id {
|
|
|
|
return fmt.Errorf("Cannot set self as child")
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("INSERT INTO edge (parent_id, name, entity_id) VALUES (?,?,?);", parent.id, name, e.id); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// RootEntity returns the root "/" entity for the database.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) RootEntity() *Entity {
|
|
|
|
return &Entity{
|
|
|
|
id: "0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Get returns the entity for a given path.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) Get(name string) *Entity {
|
2013-11-05 22:07:14 -05:00
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
2013-11-05 18:26:07 -05:00
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
e, err := db.get(name)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
func (db *Database) get(name string) (*Entity, error) {
|
2013-10-04 22:25:15 -04:00
|
|
|
e := db.RootEntity()
|
|
|
|
// We always know the root name so return it if
|
|
|
|
// it is requested
|
|
|
|
if name == "/" {
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := split(name)
|
|
|
|
for i := 1; i < len(parts); i++ {
|
|
|
|
p := parts[i]
|
2013-10-28 22:19:31 -04:00
|
|
|
if p == "" {
|
|
|
|
continue
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
next := db.child(e, p)
|
2013-10-04 22:25:15 -04:00
|
|
|
if next == nil {
|
2013-10-24 19:49:28 -04:00
|
|
|
return nil, fmt.Errorf("Cannot find child for %s", name)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
e = next
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// List all entities by from the name.
|
|
|
|
// The key will be the full path of the entity.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) List(name string, depth int) Entities {
|
2013-11-05 22:07:14 -05:00
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
2013-11-05 18:26:07 -05:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
out := Entities{}
|
2013-10-28 19:58:59 -04:00
|
|
|
e, err := db.get(name)
|
|
|
|
if err != nil {
|
|
|
|
return out
|
|
|
|
}
|
2013-11-05 22:15:41 -05:00
|
|
|
|
|
|
|
children, err := db.children(e, name, depth, nil)
|
|
|
|
if err != nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range children {
|
2013-10-04 22:25:15 -04:00
|
|
|
out[c.FullPath] = c.Entity
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
// Walk through the child graph of an entity, calling walkFunc for each child entity.
|
|
|
|
// It is safe for walkFunc to call graph functions.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) Walk(name string, walkFunc WalkFunc, depth int) error {
|
2013-11-05 22:15:41 -05:00
|
|
|
children, err := db.Children(name, depth)
|
2013-10-28 19:58:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-05 22:15:41 -05:00
|
|
|
|
|
|
|
// Note: the database lock must not be held while calling walkFunc
|
|
|
|
for _, c := range children {
|
2013-10-04 22:25:15 -04:00
|
|
|
if err := walkFunc(c.FullPath, c.Entity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Children returns the children of the specified entity.
|
2013-11-05 22:15:41 -05:00
|
|
|
func (db *Database) Children(name string, depth int) ([]WalkMeta, error) {
|
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
|
|
|
|
|
|
|
e, err := db.get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.children(e, name, depth, nil)
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Parents returns the parents of a specified entity.
|
2014-07-14 19:19:37 -04:00
|
|
|
func (db *Database) Parents(name string) ([]string, error) {
|
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
|
|
|
|
|
|
|
e, err := db.get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db.parents(e)
|
|
|
|
}
|
|
|
|
|
2015-08-07 18:24:18 -04:00
|
|
|
// Refs returns the reference count for a specified id.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) Refs(id string) int {
|
2013-11-05 22:07:14 -05:00
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
2013-11-05 18:26:07 -05:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
var count int
|
2013-10-22 19:23:52 -04:00
|
|
|
if err := db.conn.QueryRow("SELECT COUNT(*) FROM edge WHERE entity_id = ?;", id).Scan(&count); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// RefPaths returns all the id's path references.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) RefPaths(id string) Edges {
|
2013-11-05 22:07:14 -05:00
|
|
|
db.mux.RLock()
|
|
|
|
defer db.mux.RUnlock()
|
2013-11-05 18:26:07 -05:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
refs := Edges{}
|
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
rows, err := db.conn.Query("SELECT name, parent_id FROM edge WHERE entity_id = ?;", id)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var name string
|
2014-10-06 15:57:27 -04:00
|
|
|
var parentID string
|
|
|
|
if err := rows.Scan(&name, &parentID); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return refs
|
|
|
|
}
|
|
|
|
refs = append(refs, &Edge{
|
|
|
|
EntityID: id,
|
|
|
|
Name: name,
|
2014-10-06 15:57:27 -04:00
|
|
|
ParentID: parentID,
|
2013-10-04 22:25:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Delete the reference to an entity at a given path.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (db *Database) Delete(name string) error {
|
2013-11-04 13:28:38 -05:00
|
|
|
db.mux.Lock()
|
|
|
|
defer db.mux.Unlock()
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
if name == "/" {
|
|
|
|
return fmt.Errorf("Cannot delete root entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
parentPath, n := splitPath(name)
|
2013-10-22 19:23:52 -04:00
|
|
|
parent, err := db.get(parentPath)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-25 19:49:49 -04:00
|
|
|
if _, err := db.conn.Exec("DELETE FROM edge WHERE parent_id = ? AND name = ?;", parent.id, n); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// Purge removes the entity with the specified id
|
2013-10-04 22:25:15 -04:00
|
|
|
// Walk the graph to make sure all references to the entity
|
|
|
|
// are removed and return the number of references removed
|
|
|
|
func (db *Database) Purge(id string) (int, error) {
|
2013-11-04 13:28:38 -05:00
|
|
|
db.mux.Lock()
|
|
|
|
defer db.mux.Unlock()
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
tx, err := db.conn.Begin()
|
|
|
|
if err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all edges
|
2014-12-19 06:57:21 -05:00
|
|
|
rows, err := tx.Exec("DELETE FROM edge WHERE entity_id = ?;", id)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
2014-12-19 06:57:21 -05:00
|
|
|
tx.Rollback()
|
2013-10-04 22:25:15 -04:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
changes, err := rows.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2015-04-14 17:10:17 -04:00
|
|
|
// Clear who's using this id as parent
|
|
|
|
refs, err := tx.Exec("DELETE FROM edge WHERE parent_id = ?;", id)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
refsCount, err := refs.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
// Delete entity
|
2014-12-19 06:57:21 -05:00
|
|
|
if _, err := tx.Exec("DELETE FROM entity where id = ?;", id); err != nil {
|
|
|
|
tx.Rollback()
|
2013-10-04 22:25:15 -04:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:57:21 -05:00
|
|
|
if err := tx.Commit(); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-12-19 06:57:21 -05:00
|
|
|
|
2015-04-14 17:10:17 -04:00
|
|
|
return int(changes + refsCount), nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename an edge for a given path
|
|
|
|
func (db *Database) Rename(currentName, newName string) error {
|
2013-11-04 13:28:38 -05:00
|
|
|
db.mux.Lock()
|
|
|
|
defer db.mux.Unlock()
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
parentPath, name := splitPath(currentName)
|
|
|
|
newParentPath, newEdgeName := splitPath(newName)
|
|
|
|
|
|
|
|
if parentPath != newParentPath {
|
|
|
|
return fmt.Errorf("Cannot rename when root paths do not match %s != %s", parentPath, newParentPath)
|
|
|
|
}
|
|
|
|
|
2013-10-22 19:23:52 -04:00
|
|
|
parent, err := db.get(parentPath)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-25 19:49:49 -04:00
|
|
|
rows, err := db.conn.Exec("UPDATE edge SET name = ? WHERE parent_id = ? AND name = ?;", newEdgeName, parent.id, name)
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i, err := rows.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
return fmt.Errorf("Cannot locate edge for %s %s", parent.id, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// WalkMeta stores the walk metadata.
|
2013-10-04 22:25:15 -04:00
|
|
|
type WalkMeta struct {
|
|
|
|
Parent *Entity
|
|
|
|
Entity *Entity
|
|
|
|
FullPath string
|
|
|
|
Edge *Edge
|
|
|
|
}
|
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
func (db *Database) children(e *Entity, name string, depth int, entities []WalkMeta) ([]WalkMeta, error) {
|
2013-10-28 19:58:59 -04:00
|
|
|
if e == nil {
|
2013-11-05 22:15:41 -05:00
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.conn.Query("SELECT entity_id, name FROM edge where parent_id = ?;", e.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-11-05 22:15:41 -05:00
|
|
|
defer rows.Close()
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
for rows.Next() {
|
2014-10-06 15:57:27 -04:00
|
|
|
var entityID, entityName string
|
|
|
|
if err := rows.Scan(&entityID, &entityName); err != nil {
|
2013-11-05 22:15:41 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-06 15:57:27 -04:00
|
|
|
child := &Entity{entityID}
|
2013-11-05 22:15:41 -05:00
|
|
|
edge := &Edge{
|
|
|
|
ParentID: e.id,
|
|
|
|
Name: entityName,
|
|
|
|
EntityID: child.id,
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
meta := WalkMeta{
|
|
|
|
Parent: e,
|
|
|
|
Entity: child,
|
|
|
|
FullPath: path.Join(name, edge.Name),
|
|
|
|
Edge: edge,
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
entities = append(entities, meta)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-11-05 22:15:41 -05:00
|
|
|
if depth != 0 {
|
2013-10-04 22:25:15 -04:00
|
|
|
nDepth := depth
|
|
|
|
if depth != -1 {
|
2015-06-16 05:51:27 -04:00
|
|
|
nDepth--
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-11-05 22:15:41 -05:00
|
|
|
entities, err = db.children(child, meta.FullPath, nDepth, entities)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 22:15:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return entities, nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-14 19:19:37 -04:00
|
|
|
func (db *Database) parents(e *Entity) (parents []string, err error) {
|
|
|
|
if e == nil {
|
|
|
|
return parents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.conn.Query("SELECT parent_id FROM edge where entity_id = ?;", e.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2014-10-06 15:57:27 -04:00
|
|
|
var parentID string
|
|
|
|
if err := rows.Scan(&parentID); err != nil {
|
2014-07-14 19:19:37 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-06 15:57:27 -04:00
|
|
|
parents = append(parents, parentID)
|
2014-07-14 19:19:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return parents, nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Return the entity based on the parent path and name.
|
2013-10-22 19:23:52 -04:00
|
|
|
func (db *Database) child(parent *Entity, name string) *Entity {
|
2013-10-04 22:25:15 -04:00
|
|
|
var id string
|
2013-10-25 19:49:49 -04:00
|
|
|
if err := db.conn.QueryRow("SELECT entity_id FROM edge WHERE parent_id = ? AND name = ?;", parent.id, name).Scan(&id); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Entity{id}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// ID returns the id used to reference this entity.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (e *Entity) ID() string {
|
|
|
|
return e.id
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Paths returns the paths sorted by depth.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (e Entities) Paths() []string {
|
|
|
|
out := make([]string, len(e))
|
|
|
|
var i int
|
|
|
|
for k := range e {
|
|
|
|
out[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sortByDepth(out)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|