styx

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: MIT Imports: 15 Imported by: 6

README

styx

Experimental graph store. Gateway to the Underworld.

Styx is like a key/value store for graph data. It takes RDF datasets in, and then you get data out with WHERE clauses where the pattern and result are expressed as RDF graphs.

Interfaces

type Styx interface {
	Query(query []*ld.Quad, domain []*ld.BlankNode, index []ld.Node) (Iterator, error)
	Set(uri string, dataset []*ld.Quad) error
	Get(uri string) ([]*ld.Quad, error)
	Delete(uri string) error
	List(uri string) List
	Close() error
	Log()
}

type Iterator interface {
	Len() int
	Graph() []*ld.Quad
	Get(node *ld.BlankNode) ld.Node
	Domain() []*ld.BlankNode
	Index() []ld.Node
	Next(node *ld.BlankNode) ([]*ld.BlankNode, error)
	Seek(index []ld.Node) error
	Close()
}

type List interface {
	URI() string
	Next()
	Valid() bool
	Close()
}

Usage

Typical usage will look something like this:

package main

import (
	styx "github.com/underlay/styx"
	ld "github.com/piprate/json-gold/ld"
)

var sampleData = []byte(`{
	"@context": { "@vocab": "http://schema.org/" },
	"@type": "Person",
	"name": "Johnanthan Appleseed",
	"knows": {
		"@id": "http://people.com/jane-doe"
	}
}`)

var sampleQuery = []byte(`{
	"@context": { "@vocab": "http://schema.org/" },
	"name": "Johnanthan Appleseed",
	"knows": { }
}`)


func main() {
	// Open a database at a path with a given URI scheme.
	// Passing an empty string for the path will open an in-memory instance
	tagScheme := styx.NewPrefixTagScheme("http://example.com/")
	db, _ := styx.OpenDB("/tmp/styx", tagScheme)
	defer db.Close()

	_ = db.SetJSONLD("http://example.com/d1", sampleData, false)

	iterator, err := db.Query()
	if err == styx.ErrEndOfSolutions {
		return
	}
	defer iterator.Close()
	for d := iterator.Domain(); err == nil; d, err = iterator.Next(nil) {
		for _, b := range d {
			fmt.Printf("%s: %s\n", b.Attribute, iterator.Get(b).GetValue())
		}
		fmt.Println("---")
	}
}

Documentation

Index

Constants

View Source
const Algorithm = "URDNA2015"

Algorithm has to be URDNA2015

View Source
const DatasetPrefix = byte('/')

DatasetPrefix keys track the Multihashes of the documents in the database

View Source
const Format = "application/n-quads"

Format has to be application/n-quads

View Source
const IDToValuePrefix = byte('<')

IDToValuePrefix keys translate uint64 ids to string IRIs

View Source
const SequenceBandwidth = 512

SequenceBandwidth sets the lease block size of the ID counter

View Source
const UnaryPrefix = byte('u')

UnaryPrefix keys translate ld.Node values to uint64 ids

View Source
const ValueToIDPrefix = byte('>')

ValueToIDPrefix keys translate string IRIs to uint64 ids

Variables

View Source
var BinaryPrefixes = [6]byte{'i', 'j', 'k', 'l', 'm', 'n'}

BinaryPrefixes address the binary indices

View Source
var ErrEndOfSolutions = errors.New("No more solutions")

ErrEndOfSolutions is a generic out-of-reuslts signal

View Source
var ErrInvalidDomain = errors.New("Invalid domain")

ErrInvalidDomain means that provided domain included blank nodes that were not in the query

View Source
var ErrInvalidIndex = errors.New("Invalid index")

ErrInvalidIndex means that provided index included blank nodes or that it was too long

View Source
var ErrInvalidInput = errors.New("Invalid dataset")

ErrInvalidInput indicates that a given dataset was invalid

View Source
var ErrTagScheme = errors.New("URI did not validate the tag scheme")

ErrTagScheme indicates that a given URI did not validate the database's tag scheme

View Source
var SequenceKey = []byte(":")

SequenceKey to store the id counter

View Source
var TernaryPrefixes = [3]byte{'a', 'b', 'c'}

TernaryPrefixes address the ternary indices

Functions

This section is empty.

Types

type Iterator

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

An Iterator exposes Next and Seek operations

func MakeConstraintGraph

func MakeConstraintGraph(
	pattern []*ld.Quad,
	domain []*ld.BlankNode, index []ld.Node,
	tag TagScheme,
	txn *badger.Txn,
) (iterator *Iterator, err error)

MakeConstraintGraph populates, scores, sorts, and connects a new constraint graph

func (*Iterator) Close

func (g *Iterator) Close()

Close the iterator

func (*Iterator) Collect

func (g *Iterator) Collect() [][]ld.Node

Collect calls Next(nil) on the iterator until there are no more solutions, and returns all the results in a slice.

func (*Iterator) Domain

func (g *Iterator) Domain() []*ld.BlankNode

Domain returns the total ordering of variables used by the iterator

func (*Iterator) Get

func (g *Iterator) Get(node *ld.BlankNode) (n ld.Node)

Get the value for a particular blank node

func (*Iterator) Graph

func (g *Iterator) Graph() []*ld.Quad

Graph returns a []*ld.Quad representation of the iterator's current value

func (*Iterator) Index

func (g *Iterator) Index() []ld.Node

Index returns the iterator's current value as an ordered slice of ld.Nodes

func (*Iterator) Len

func (g *Iterator) Len() int

Sort interface functions

func (*Iterator) Less

func (g *Iterator) Less(a, b int) bool

TODO: put more thought into the sorting heuristic. Right now the variables are sorted their norm: in increasing order of their length-normalized sum of the squares of the counts of all their constraints (of any degree).

func (*Iterator) Log

func (g *Iterator) Log()

Log pretty-prints the contents of the database

func (*Iterator) Next

func (g *Iterator) Next(node *ld.BlankNode) ([]*ld.BlankNode, error)

Next advances the iterator to the next result that differs in the given node. If nil is passed, the last node in the domain is used.

func (*Iterator) Seek

func (g *Iterator) Seek(index []ld.Node) error

Seek advances the iterator to the first result greater than or equal to the given index path

func (*Iterator) String

func (g *Iterator) String() string

func (*Iterator) Swap

func (g *Iterator) Swap(a, b int)

type List

type List interface {
	URI() string
	Next()
	Valid() bool
	Close()
}

A List is an iterator over dataset URIs

type Options

type Options struct {
	Path      string
	TagScheme TagScheme
	Canonize  bool
}

Options are the initialization options passed to Styx

type Permutation

type Permutation uint8

Permutation is a permutation of a triple

const (
	// SPO is the subject-predicate-object permutation
	SPO Permutation = iota
	// POS is the predicate-object-subject permutation
	POS
	// OSP is the object-subject-predicate permutation
	OSP
	// SOP is the subject-object-predicate permutation
	SOP
	// PSO is the predicate-subject-object permutation
	PSO
	// OPS is the object-predicate-subject permutation
	OPS
)

type Statement

type Statement struct {
	Origin iri
	Index  uint64
	Graph  Value
}

A Statement is a reference to a specific quad in a specific dataset

func (*Statement) Marshal

func (statement *Statement) Marshal(values valueCache, txn *badger.Txn) string

Marshal serializes the statement into bytes

func (*Statement) URI

func (statement *Statement) URI(values valueCache, txn *badger.Txn) (uri string)

URI returns a URI representation of the quad using path notation

type Store

type Store struct {
	Badger   *badger.DB
	Sequence *badger.Sequence
	Options  *Options
}

A Store is a database instance

func NewStore

func NewStore(options *Options) (*Store, error)

NewStore opens a styx database

func (*Store) Close

func (s *Store) Close() (err error)

Close the database

func (*Store) Delete

func (db *Store) Delete(uri string) (err error)

Delete a dataset from the database

func (*Store) Get

func (db *Store) Get(uri string) ([]*ld.Quad, error)

Get a dataset from the database

func (*Store) List

func (db *Store) List(uri string) List

List lists the datasets in the database

func (*Store) Log

func (s *Store) Log()

Log will print the *entire database contents* to log

func (*Store) Query

func (s *Store) Query(pattern []*ld.Quad, domain []*ld.BlankNode, index []ld.Node) (*Iterator, error)

Query satisfies the Styx interface

func (*Store) QueryJSONLD

func (s *Store) QueryJSONLD(query interface{}) (*Iterator, error)

QueryJSONLD exposes a JSON-LD query interface

func (*Store) Set

func (db *Store) Set(uri string, dataset []*ld.Quad) (err error)

Set is the entrypoint to inserting stuff

func (*Store) SetDataset

func (db *Store) SetDataset(uri string, dataset *ld.RDFDataset, canonize bool) error

SetDataset sets a piprate/json-gold dataset struct

func (*Store) SetJSONLD

func (db *Store) SetJSONLD(uri string, input interface{}, canonize bool) error

SetJSONLD sets a JSON-LD document

type TagScheme

type TagScheme interface {
	Test(uri string) bool
	Parse(uri string) (tag string, fragment string)
}

A TagScheme is an interface for testing whether a given URI is a dataset URI or not

func NewPrefixTagScheme

func NewPrefixTagScheme(prefix string) TagScheme

NewPrefixTagScheme creates a tag scheme that tests for the given prefix

type Term

type Term string

Term is the shorthand type for values (terms of any kind)

type V

type V struct {
	Term
	// contains filtered or unexported fields
}

V is a struct that caches a variable's total state

type Value

type Value interface {
	Term() Term
	Node(origin iri, values valueCache, txn *badger.Txn) ld.Node

	// We don't actually use JSON or NQuads but they might be nice to have in the future
	JSON(origin iri, values valueCache, txn *badger.Txn) interface{}
	NQuads(origin iri, values valueCache, txn *badger.Txn) string
}

A Value is an RDF term

Jump to

Keyboard shortcuts

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