fabric

package module
v0.0.0-...-8eb1b2f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 8 Imported by: 0

README

Fabric

GoDoc Go Report Card

Fabric is a triple-store written in Go. Fabric provides simple functions and store options to deal with "Subject->Predicate->Object" relations or so called triples.

Usage

Get fabric by using go get -u github.com/spy16/fabric (Fabric as a library has no external dependencies)

// See next snippet for using persistent SQL backend
fab := fabric.New(&fabric.InMemoryStore{})

fab.Insert(context.Background(), fabric.Triple{
    Source: "Bob",
    Predicate: "Knows",
    Target: "John",
})

fab.Query(context.Background(), fabric.Query{
    Source: fabric.Clause{
        Type: "equal",
        Value: "Bob",
    },
})

To use a SQL database for storing the triples, use the following snippet:

db, err := sql.Open("sqlite3", "fabric.db")
if err != nil {
    panic(err)
}

store := &fabric.SQLStore{DB: db}
store.Setup(context.Background()) // to create required tables

fab := fabric.New(store)

Fabric SQLStore uses Go's standard database/sql package. So any SQL database supported through this interface (includes most major SQL databases) should work.

Additional store support can be added by implementing the Store interface.

type Store interface {
    Insert(ctx context.Context, tri Triple) error
    Query(ctx context.Context, q Query) ([]Triple, error)
    Delete(ctx context.Context, q Query) (int, error)
}

Optional Counter and ReWeighter can be implemented by the store implementations to support extended query options.

REST API

The server package exposes REST APIs (/triples endpoint) which can be used to query, insert/delete or reweight triples using any HTTP client.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotSupported = errors.New("not supported")

ErrNotSupported is returned when an operation is not supported.

Functions

func ExportDOT

func ExportDOT(name string, triples []Triple) string

ExportDOT exports the given set of triples in DOT format.

Types

type Clause

type Clause struct {
	// Type represents the operation that should be used. Examples include equal,
	// gt, lt etc. Supported operations are dictated by store implementations.
	Type string

	// Value that should be used as the right operand for the operation.
	Value string
}

Clause represents a query clause. Zero value of this struct will be used as 'Any' clause which matches any value.

func (Clause) IsAny

func (cl Clause) IsAny() bool

IsAny returns true if cl is a nil clause or both Op and Value are empty.

func (Clause) String

func (cl Clause) String() string

type Counter

type Counter interface {
	// Count should return the number of triples in the store that match the
	// given query.
	Count(ctx context.Context, query Query) (int, error)
}

Counter can be implemented by Store implementations to support count operations. In case, this interface is not implemented, count queries will not be supported.

type Fabric

type Fabric struct {
	// contains filtered or unexported fields
}

Fabric provides functions to query and manage triples.

func New

func New(store Store) *Fabric

New returns a new instance of fabric with given store implementation.

func (*Fabric) Count

func (f *Fabric) Count(ctx context.Context, query Query) (int, error)

Count returns the number of triples matching the query. If the store does not implement the Counter interface, standard Query method will be used to fetch all triples and the result set length is returned.

func (*Fabric) Delete

func (f *Fabric) Delete(ctx context.Context, query Query) (int, error)

Delete removes all the triples from the store matching the given query and returns the number of items deleted.

func (*Fabric) Insert

func (f *Fabric) Insert(ctx context.Context, tri Triple) error

Insert validates the triple and persists it to the store.

func (*Fabric) Query

func (f *Fabric) Query(ctx context.Context, query Query) ([]Triple, error)

Query finds all the triples matching the given query.

func (*Fabric) ReWeight

func (f *Fabric) ReWeight(ctx context.Context, query Query, delta float64, replace bool) (int, error)

ReWeight performs weight updates on all triples matching the query, if the store implements ReWeighter interface. Otherwise, returns ErrNotSupported.

type InMemoryStore

type InMemoryStore struct {
	// contains filtered or unexported fields
}

InMemoryStore implements the Store interface using the golang map type.

func (*InMemoryStore) Count

func (mem *InMemoryStore) Count(ctx context.Context, query Query) (int, error)

Count returns the number of triples in the store matching the given query.

func (*InMemoryStore) Delete

func (mem *InMemoryStore) Delete(ctx context.Context, query Query) (int, error)

Delete removes all the triples that match the given query.

func (*InMemoryStore) Insert

func (mem *InMemoryStore) Insert(ctx context.Context, tri Triple) error

Insert stores the triple into the in-memory map.

func (*InMemoryStore) Query

func (mem *InMemoryStore) Query(ctx context.Context, query Query) ([]Triple, error)

Query returns all the triples matching the given query.

func (*InMemoryStore) ReWeight

func (mem *InMemoryStore) ReWeight(ctx context.Context, query Query, delta float64, replace bool) (int, error)

ReWeight re-weights all the triples matching the query.

type Query

type Query struct {
	Source    Clause `json:"source,omitempty"`
	Predicate Clause `json:"predicate,omitempty"`
	Target    Clause `json:"target,omitempty"`
	Weight    Clause `json:"weight,omitempty"`
	Limit     int    `json:"limit,omitempty"`
}

Query represents a query to identify one or more triples.

func (Query) IsAny

func (q Query) IsAny() bool

IsAny returns true if all clauses are any clauses.

func (Query) Map

func (q Query) Map() map[string]Clause

Map returns a map version of the query with all the any clauses removed.

type ReWeighter

type ReWeighter interface {
	// ReWeight should update all the triples matching the query as described
	// by delta and replace flag. If replace is true, weight of all the triples
	// should be set to delta. Otherwise, delta should be added to the current
	// weights.
	ReWeight(ctx context.Context, query Query, delta float64, replace bool) (int, error)
}

ReWeighter can be implemented by Store implementations to support weight updates. In case, this interface is not implemented, update queries will not be supported.

type SQLStore

type SQLStore struct {
	DB *sql.DB
}

SQLStore implements Store interface using the Go standard library sql package.

func (*SQLStore) Count

func (ss *SQLStore) Count(ctx context.Context, query Query) (int, error)

Count returns the number of triples that match the given query.

func (*SQLStore) Delete

func (ss *SQLStore) Delete(ctx context.Context, query Query) (int, error)

Delete removes all the triples from the database that match the query.

func (*SQLStore) Insert

func (ss *SQLStore) Insert(ctx context.Context, tri Triple) error

Insert persists the given triple into the triples table.

func (*SQLStore) Query

func (ss *SQLStore) Query(ctx context.Context, query Query) ([]Triple, error)

Query converts the given query object into SQL SELECT and fetches all the triples.

func (*SQLStore) ReWeight

func (ss *SQLStore) ReWeight(ctx context.Context, query Query, delta float64, replace bool) (int, error)

ReWeight updates the weight of all the triples matching the given query.

func (*SQLStore) Setup

func (ss *SQLStore) Setup(ctx context.Context) error

Setup runs appropriate queries to setup all the required tables.

type Store

type Store interface {
	// Insert should insert the given triple into the store.
	Insert(ctx context.Context, tri Triple) error

	// Query should return triples from store that match the given clauses.
	// Possible keys of the clauses map are: source, target, predicate, weight
	Query(ctx context.Context, q Query) ([]Triple, error)

	// Delete should delete triples from store that match the given clauses.
	// Clauses will follow same format as used in Query() method.
	Delete(ctx context.Context, q Query) (int, error)
}

Store implementation should provide functions for managing persistence of triples.

type Triple

type Triple struct {
	Source    string  `json:"source" yaml:"source" db:"source"`
	Predicate string  `json:"predicate" yaml:"predicate" db:"predicate"`
	Target    string  `json:"target" yaml:"target" db:"target"`
	Weight    float64 `json:"weight" yaml:"weight" db:"weight"` // extension field
}

Triple represents a subject-predicate-object.

func (Triple) String

func (tri Triple) String() string

func (Triple) Validate

func (tri Triple) Validate() error

Validate ensures the entity names are valid.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL