|
@@ -29,28 +29,28 @@ const (
|
|
|
`
|
|
|
)
|
|
|
|
|
|
-// Entity with a unique id
|
|
|
+// Entity with a unique id.
|
|
|
type Entity struct {
|
|
|
id string
|
|
|
}
|
|
|
|
|
|
-// An Edge connects two entities together
|
|
|
+// An Edge connects two entities together.
|
|
|
type Edge struct {
|
|
|
EntityID string
|
|
|
Name string
|
|
|
ParentID string
|
|
|
}
|
|
|
|
|
|
-// Entities stores the list of entities
|
|
|
+// Entities stores the list of entities.
|
|
|
type Entities map[string]*Entity
|
|
|
|
|
|
-// Edges stores the relationships between entities
|
|
|
+// Edges stores the relationships between entities.
|
|
|
type Edges []*Edge
|
|
|
|
|
|
-// WalkFunc is a function invoked to process an individual entity
|
|
|
+// WalkFunc is a function invoked to process an individual entity.
|
|
|
type WalkFunc func(fullPath string, entity *Entity) error
|
|
|
|
|
|
-// Database is a graph database for storing entities and their relationships
|
|
|
+// Database is a graph database for storing entities and their relationships.
|
|
|
type Database struct {
|
|
|
conn *sql.DB
|
|
|
mux sync.RWMutex
|
|
@@ -80,7 +80,7 @@ func IsNonUniqueNameError(err error) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-// NewDatabase creates a new graph database initialized with a root entity
|
|
|
+// NewDatabase creates a new graph database initialized with a root entity.
|
|
|
func NewDatabase(conn *sql.DB) (*Database, error) {
|
|
|
if conn == nil {
|
|
|
return nil, fmt.Errorf("Database connection cannot be nil")
|
|
@@ -130,12 +130,12 @@ func NewDatabase(conn *sql.DB) (*Database, error) {
|
|
|
return db, nil
|
|
|
}
|
|
|
|
|
|
-// Close the underlying connection to the database
|
|
|
+// Close the underlying connection to the database.
|
|
|
func (db *Database) Close() error {
|
|
|
return db.conn.Close()
|
|
|
}
|
|
|
|
|
|
-// Set the entity id for a given path
|
|
|
+// Set the entity id for a given path.
|
|
|
func (db *Database) Set(fullPath, id string) (*Entity, error) {
|
|
|
db.mux.Lock()
|
|
|
defer db.mux.Unlock()
|
|
@@ -171,7 +171,7 @@ func (db *Database) Set(fullPath, id string) (*Entity, error) {
|
|
|
return e, nil
|
|
|
}
|
|
|
|
|
|
-// Exists returns true if a name already exists in the database
|
|
|
+// Exists returns true if a name already exists in the database.
|
|
|
func (db *Database) Exists(name string) bool {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -198,14 +198,14 @@ func (db *Database) setEdge(parentPath, name string, e *Entity, tx *sql.Tx) erro
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// RootEntity returns the root "/" entity for the database
|
|
|
+// RootEntity returns the root "/" entity for the database.
|
|
|
func (db *Database) RootEntity() *Entity {
|
|
|
return &Entity{
|
|
|
id: "0",
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Get returns the entity for a given path
|
|
|
+// Get returns the entity for a given path.
|
|
|
func (db *Database) Get(name string) *Entity {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -242,8 +242,8 @@ func (db *Database) get(name string) (*Entity, error) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-// List all entities by from the name
|
|
|
-// The key will be the full path of the entity
|
|
|
+// List all entities by from the name.
|
|
|
+// The key will be the full path of the entity.
|
|
|
func (db *Database) List(name string, depth int) Entities {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -282,7 +282,7 @@ func (db *Database) Walk(name string, walkFunc WalkFunc, depth int) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Children returns the children of the specified entity
|
|
|
+// Children returns the children of the specified entity.
|
|
|
func (db *Database) Children(name string, depth int) ([]WalkMeta, error) {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -295,7 +295,7 @@ func (db *Database) Children(name string, depth int) ([]WalkMeta, error) {
|
|
|
return db.children(e, name, depth, nil)
|
|
|
}
|
|
|
|
|
|
-// Parents returns the parents of a specified entity
|
|
|
+// Parents returns the parents of a specified entity.
|
|
|
func (db *Database) Parents(name string) ([]string, error) {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -307,7 +307,7 @@ func (db *Database) Parents(name string) ([]string, error) {
|
|
|
return db.parents(e)
|
|
|
}
|
|
|
|
|
|
-// Refs returns the refrence count for a specified id
|
|
|
+// Refs returns the refrence count for a specified id.
|
|
|
func (db *Database) Refs(id string) int {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -319,7 +319,7 @@ func (db *Database) Refs(id string) int {
|
|
|
return count
|
|
|
}
|
|
|
|
|
|
-// RefPaths returns all the id's path references
|
|
|
+// RefPaths returns all the id's path references.
|
|
|
func (db *Database) RefPaths(id string) Edges {
|
|
|
db.mux.RLock()
|
|
|
defer db.mux.RUnlock()
|
|
@@ -347,7 +347,7 @@ func (db *Database) RefPaths(id string) Edges {
|
|
|
return refs
|
|
|
}
|
|
|
|
|
|
-// Delete the reference to an entity at a given path
|
|
|
+// Delete the reference to an entity at a given path.
|
|
|
func (db *Database) Delete(name string) error {
|
|
|
db.mux.Lock()
|
|
|
defer db.mux.Unlock()
|
|
@@ -446,6 +446,7 @@ func (db *Database) Rename(currentName, newName string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+// WalkMeta stores the walk metadata.
|
|
|
type WalkMeta struct {
|
|
|
Parent *Entity
|
|
|
Entity *Entity
|
|
@@ -522,7 +523,7 @@ func (db *Database) parents(e *Entity) (parents []string, err error) {
|
|
|
return parents, nil
|
|
|
}
|
|
|
|
|
|
-// Return the entity based on the parent path and name
|
|
|
+// Return the entity based on the parent path and name.
|
|
|
func (db *Database) child(parent *Entity, name string) *Entity {
|
|
|
var id string
|
|
|
if err := db.conn.QueryRow("SELECT entity_id FROM edge WHERE parent_id = ? AND name = ?;", parent.id, name).Scan(&id); err != nil {
|
|
@@ -531,12 +532,12 @@ func (db *Database) child(parent *Entity, name string) *Entity {
|
|
|
return &Entity{id}
|
|
|
}
|
|
|
|
|
|
-// ID returns the id used to reference this entity
|
|
|
+// ID returns the id used to reference this entity.
|
|
|
func (e *Entity) ID() string {
|
|
|
return e.id
|
|
|
}
|
|
|
|
|
|
-// Paths returns the paths sorted by depth
|
|
|
+// Paths returns the paths sorted by depth.
|
|
|
func (e Entities) Paths() []string {
|
|
|
out := make([]string, len(e))
|
|
|
var i int
|