igraph

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

package igraph provides Index

Index

Constants

This section is empty.

Variables

View Source
var ErrFinalized = errors.New("IGraph: Finalized")

Functions

func MarshalTriple

func MarshalTriple(triple IndexTriple) ([]byte, error)

func UnmarshalTriple

func UnmarshalTriple(dest *IndexTriple, src []byte) error

Types

type DiskEngine

type DiskEngine struct {
	imap.DiskMap
}

DiskEngine represents an engine that stores everything on disk

func (DiskEngine) Data

func (de DiskEngine) Data() (imap.HashMap[impl.ID, impl.Datum], error)

func (DiskEngine) Inverses

func (de DiskEngine) Inverses() (imap.HashMap[impl.ID, impl.ID], error)

func (DiskEngine) Language added in v0.0.3

func (de DiskEngine) Language() (imap.HashMap[impl.ID, impl.Language], error)

func (DiskEngine) POSIndex

func (de DiskEngine) POSIndex() (ThreeStorage, error)

func (DiskEngine) PSOIndex

func (de DiskEngine) PSOIndex() (ThreeStorage, error)

func (DiskEngine) Triples

func (de DiskEngine) Triples() (imap.HashMap[impl.ID, IndexTriple], error)

type Engine

type Engine interface {
	imap.Map

	Data() (imap.HashMap[impl.ID, impl.Datum], error)
	Language() (imap.HashMap[impl.ID, impl.Language], error)
	Triples() (imap.HashMap[impl.ID, IndexTriple], error)
	Inverses() (imap.HashMap[impl.ID, impl.ID], error)
	PSOIndex() (ThreeStorage, error)
	POSIndex() (ThreeStorage, error)
}

Engine represents an object that creates storages for an IGraph

type Index

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

Index represents a searchable index of a directed labeled graph with optionally attached Data.

Labels are used for nodes and edges. This means that the graph is defined by triples of the form (subject Label, predicate Label, object Label). See [AddTriple].

Datum is used for data associated with the specific nodes. See [AddDatum].

The zero value represents an empty index, but is otherwise not ready to be used. To fill an index, it first needs to be [Reset], and then [Finalize]d.

Index may not be modified concurrently, however it is possible to run several queries concurrently.

func (*Index) AddData

func (index *Index) AddData(subject, predicate impl.Label, object impl.Datum) error

AddData inserts a non-language subject-predicate-data triple into the index. Adding multiple items to a specific subject with a specific predicate is supported.

Reset must have been called, or this function may panic. After all Add operations have finished, Finalize must be called.

func (*Index) AddLangData added in v0.0.3

func (index *Index) AddLangData(subject, predicate impl.Label, object impl.Datum, lang impl.Language) error

AddLangData inserts a language-specific subject-predicate-data triple into the index. Adding multiple items to a specific subject with a specific predicate is supported.

Reset must have been called, or this function may panic. After all Add operations have finished, Finalize must be called.

func (*Index) AddTriple

func (index *Index) AddTriple(subject, predicate, object impl.Label) error

AddTriple inserts a subject-predicate-object triple into the index. Adding a triple more than once has no effect.

Reset must have been called, or this function may panic. After all Add operations have finished, Finalize must be called.

func (*Index) Close

func (index *Index) Close() error

Close closes any storages attached to this storage

func (*Index) Compact

func (index *Index) Compact() error

Compact informs the implementation to perform any internal optimizations.

func (*Index) Finalize

func (index *Index) Finalize() error

Finalize finalizes any adding operations into this graph.

Finalize must be called before any query is performed, but after any calls to the Add* methods. Calling finalize multiple times is invalid.

func (*Index) IdentityMap

func (index *Index) IdentityMap(storage imap.HashMap[impl.Label, impl.Label]) error

IdentityMap writes all Labels for which has a semantically equivalent label. See imap.Storage.IdentityMap.

func (*Index) MarkIdentical

func (index *Index) MarkIdentical(new, old impl.Label) error

MarkIdentical identifies the new and old labels. See imap.IMap.MarkIdentical.

func (*Index) MarkInverse

func (index *Index) MarkInverse(left, right impl.Label) error

MarkInverse marks the left and right Labels as inverse properties of each other. After calls to MarkInverse, no more calls to MarkIdentical should be made.

Each label is assumed to have at most one inverse. A label may not be it's own inverse.

This means that each call to AddTriple(s, left, o) will also result in a call to AddTriple(o, right, s).

func (*Index) PathsStarting

func (index *Index) PathsStarting(predicate, object impl.Label) (*Paths, error)

PathsStarting creates a new [PathSet] that represents all one-element paths starting at a vertex which is connected to object with the given predicate

func (*Index) Reset

func (index *Index) Reset(engine Engine) (err error)

Reset resets this index and prepares all internal structures for use.

func (*Index) SetDataMask

func (index *Index) SetDataMask(predicates map[impl.Label]struct{}) error

SetDataMask sets the masks for data

func (*Index) SetPredicateMask

func (index *Index) SetPredicateMask(predicates map[impl.Label]struct{}) error

SetPredicateMask sets the masks for predicates

func (*Index) Stats

func (index *Index) Stats() Stats

Stats returns statistics from this graph

func (*Index) Triple

func (index *Index) Triple(id impl.ID) (triple Triple, err error)

Triple returns the triple with the given id

func (*Index) TripleCount

func (index *Index) TripleCount() (count uint64, err error)

TripleCount returns the total number of (distinct) triples in this graph. Triples which have been identified will only count once.

type IndexTriple

type IndexTriple struct {
	Role  // Why was this triple stored?
	Items [3]imap.TripleID
}

IndexTriple represents a triple stored inside the index

type MemoryEngine

type MemoryEngine struct {
	imap.MemoryMap
}

MemoryEngine represents an engine that stores everything in memory

func (MemoryEngine) Data

func (MemoryEngine) Inverses

func (MemoryEngine) Inverses() (imap.HashMap[impl.ID, impl.ID], error)

func (MemoryEngine) Language added in v0.0.3

func (MemoryEngine) Language() (imap.HashMap[impl.ID, impl.Language], error)

func (MemoryEngine) POSIndex

func (MemoryEngine) POSIndex() (ThreeStorage, error)

func (MemoryEngine) PSOIndex

func (MemoryEngine) PSOIndex() (ThreeStorage, error)

func (MemoryEngine) Triples

func (MemoryEngine) Triples() (imap.HashMap[impl.ID, IndexTriple], error)

type Path

type Path struct {
	Datum    impl.Datum
	Language impl.Language
	Nodes    []impl.Label
	Edges    []impl.Label
	Triples  []Triple
	HasDatum bool
}

Path represents a path inside a GraphIndex

func (Path) String

func (path Path) String() string

String turns this result into a string

NOTE(twiesing): This is for debugging only, and ignores all errors. It should not be used in production code.

func (Path) Value

func (path Path) Value() (value impl.Datum)

Value returns the value corresponding to a field represented by this path.

The value returned tries the following options in order:

- the datum corresponding to the path - the last node, interpreted as a datum - the zero value of the datum type

type Paths

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

Paths represents a set of paths in a related GraphIndex. It implements a very simple sparql-like query engine.

A Paths object is stateful. A Paths object should only be created from a GraphIndex; the zero value is invalid. It can be further refined using the [Connected] and [Ending] methods.

func (*Paths) Connected

func (set *Paths) Connected(predicate impl.Label) error

Connected extends the sets of in this PathSet by those which continue the existing paths using an edge labeled with predicate.

func (*Paths) Ending

func (set *Paths) Ending(predicate, object impl.Label) error

Ending restricts this set of paths to those that end in a node which is connected to object via predicate.

func (*Paths) Paths

func (set *Paths) Paths() iterator.Iterator[Path]

Paths returns an iterator over paths contained in this Paths. It may only be called once, afterwards further calls may be invalid.

func (*Paths) Size

func (set *Paths) Size() (int, error)

Size returns the number of elements in this path.

NOTE(twiesing): This potentially takes a lot of memory, because we need to expand the stream.

type Role

type Role uint8

Role represents the role of the triple

const (
	// Regular represents a regular (non-inferred) triple
	Regular Role = iota

	// Inverse represents an inferred inverse triple
	Inverse

	// Data represents a data triple
	Data
)

type Stats

type Stats struct {
	DirectTriples     uint64
	DatumTriples      uint64
	MaskedPredTriples uint64
	MaskedDataTriples uint64
	InverseTriples    uint64
	ConflictTriples   uint64
}

Stats holds statistics about triples in the index

func (Stats) String

func (stats Stats) String() string

type ThreeDiskHash

type ThreeDiskHash struct {
	DB *leveldb.DB
}

ThreeHash implements ThreeStorage in memory

func (*ThreeDiskHash) Add

func (tlm *ThreeDiskHash) Add(a, b, c impl.ID, l impl.ID, conflict func(old, new impl.ID) (impl.ID, error)) (conflicted bool, err error)

func (*ThreeDiskHash) Close

func (tlm *ThreeDiskHash) Close() (err error)

func (ThreeDiskHash) Compact

func (tlm ThreeDiskHash) Compact() error

func (*ThreeDiskHash) Count

func (tlm *ThreeDiskHash) Count() (total int64, err error)

func (*ThreeDiskHash) Fetch

func (tlm *ThreeDiskHash) Fetch(a, b impl.ID, f func(c impl.ID, l impl.ID) error) error

func (ThreeDiskHash) Finalize

func (tlm ThreeDiskHash) Finalize() error

func (*ThreeDiskHash) Has

func (tlm *ThreeDiskHash) Has(a, b, c impl.ID) (id impl.ID, ok bool, err error)

type ThreeHash

type ThreeHash map[impl.ID]map[impl.ID]*ThreeItem

ThreeHash implements ThreeStorage in memory

func (ThreeHash) Add

func (tlm ThreeHash) Add(a, b, c impl.ID, l impl.ID, conflict func(old, new impl.ID) (impl.ID, error)) (conflicted bool, err error)

func (*ThreeHash) Close

func (tlm *ThreeHash) Close() error

func (*ThreeHash) Compact

func (th *ThreeHash) Compact() error

func (ThreeHash) Count

func (tlm ThreeHash) Count() (total int64, err error)

func (ThreeHash) Fetch

func (tlm ThreeHash) Fetch(a, b impl.ID, f func(c impl.ID, l impl.ID) error) error

func (ThreeHash) Finalize

func (tlm ThreeHash) Finalize() error

func (ThreeHash) Has

func (tlm ThreeHash) Has(a, b, c impl.ID) (impl.ID, bool, error)

type ThreeItem

type ThreeItem struct {
	Data map[impl.ID]impl.ID
	Keys []impl.ID
}

type ThreeStorage

type ThreeStorage interface {
	io.Closer

	// Add adds a new mapping for the given (a, b, c).
	//
	// l acts as a label for the insert.
	// when the given edge already exists, the conflict function should be called to resolve the conflict
	Add(a, b, c impl.ID, l impl.ID, conflict func(old, new impl.ID) (impl.ID, error)) (conflicted bool, err error)

	// Count counts the overall number of entries in the index
	Count() (int64, error)

	// Compact indicates to the caller to perform internal optimizations of all data structures.
	Compact() error

	// Finalize informs the storage that no more mutable calls will be made.
	// A mutable call is one to Compact or Add.
	Finalize() error

	// Fetch iterates over all triples (a, b, c) in c-order.
	// l is the last label that was created for the triple.
	// If an error occurs, iteration stops and is returned to the caller
	Fetch(a, b impl.ID, f func(c impl.ID, l impl.ID) error) error

	// Has checks if the given mapping exists and returns the label (if any)
	Has(a, b, c impl.ID) (impl.ID, bool, error)
}

func NewDiskHash

func NewDiskHash(path string) (ThreeStorage, error)

type Triple

type Triple struct {
	// the literal SPO for this triple, as found in the original data.
	Subject   impl.Label
	Predicate impl.Label
	Object    impl.Label

	// the canonical (normalized for sameAs) for this triple.
	// FIXME: change this to say canonical in the name
	SSubject   impl.Label
	SPredicate impl.Label
	SObject    impl.Label

	// Datum and Language hold the data value of this triple and the corresponding language
	Datum    impl.Datum
	Language impl.Language

	// ID uniquely identifies this triple.
	// Two triples are identical iff their IDs are identical.
	ID impl.ID

	// Why was this triple inserted?
	Role Role
}

Triple represents a triple found inside a graph

func (Triple) Compare

func (triple Triple) Compare(other Triple) int

Compare compares this triple to another triple based on it's id

func (Triple) Inferred

func (triple Triple) Inferred() bool

Inferred returns if this triple has been inferred

func (Triple) Triple added in v0.0.3

func (triple Triple) Triple(canonical bool) (spo rdf.Triple, err error)

Triple returns this Triple as an rdf triple

Jump to

Keyboard shortcuts

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