github.com/cayleygraph/cayley@v0.7.7/graph/values.go (about)

     1  package graph
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  
     7  	"github.com/cayleygraph/quad"
     8  )
     9  
    10  // Ref defines an opaque "quad store reference" type. However the backend wishes
    11  // to implement it, a Ref is merely a token to a quad or a node that the
    12  // backing store itself understands, and the base iterators pass around.
    13  //
    14  // For example, in a very traditional, graphd-style graph, these are int64s
    15  // (guids of the primitives). In a very direct sort of graph, these could be
    16  // pointers to structs, or merely quads, or whatever works best for the
    17  // backing store.
    18  //
    19  // These must be comparable, or return a comparable version on Key.
    20  type Ref interface {
    21  	// Key returns a dynamic type that is comparable according to the Go language specification.
    22  	// The returned value must be unique for each receiver value.
    23  	Key() interface{}
    24  }
    25  
    26  // Value is an alias for Ref.
    27  //
    28  // Deprecated: use Ref instead.
    29  type Value = Ref
    30  
    31  func HashOf(s quad.Value) (out ValueHash) {
    32  	if s == nil {
    33  		return
    34  	}
    35  	quad.HashTo(s, out[:])
    36  	return
    37  }
    38  
    39  var _ Ref = ValueHash{}
    40  
    41  // ValueHash is a hash of a single value.
    42  type ValueHash [quad.HashSize]byte
    43  
    44  func (h ValueHash) Valid() bool {
    45  	return h != ValueHash{}
    46  }
    47  func (h ValueHash) Key() interface{} { return h }
    48  func (h ValueHash) String() string {
    49  	if !h.Valid() {
    50  		return ""
    51  	}
    52  	return hex.EncodeToString(h[:])
    53  }
    54  
    55  // PreFetchedValue is an optional interface for graph.Ref to indicate that
    56  // quadstore has already loaded a value into memory.
    57  type PreFetchedValue interface {
    58  	Ref
    59  	NameOf() quad.Value
    60  }
    61  
    62  func PreFetched(v quad.Value) PreFetchedValue {
    63  	return fetchedValue{v}
    64  }
    65  
    66  type fetchedValue struct {
    67  	Val quad.Value
    68  }
    69  
    70  func (v fetchedValue) IsNode() bool       { return true }
    71  func (v fetchedValue) NameOf() quad.Value { return v.Val }
    72  func (v fetchedValue) Key() interface{}   { return v.Val }
    73  
    74  // Keyer provides a method for comparing types that are not otherwise comparable.
    75  // The Key method must return a dynamic type that is comparable according to the
    76  // Go language specification. The returned value must be unique for each receiver
    77  // value.
    78  //
    79  // Deprecated: Ref contains the same method now.
    80  type Keyer = Ref
    81  
    82  // ToKey prepares Ref to be stored inside maps, calling Key() if necessary.
    83  func ToKey(v Ref) interface{} {
    84  	if v == nil {
    85  		return nil
    86  	}
    87  	return v.Key()
    88  }
    89  
    90  var _ Ref = QuadHash{}
    91  
    92  type QuadHash struct {
    93  	Subject   ValueHash
    94  	Predicate ValueHash
    95  	Object    ValueHash
    96  	Label     ValueHash
    97  }
    98  
    99  func (q QuadHash) Dirs() [4]ValueHash {
   100  	return [4]ValueHash{
   101  		q.Subject,
   102  		q.Predicate,
   103  		q.Object,
   104  		q.Label,
   105  	}
   106  }
   107  func (q QuadHash) Key() interface{} { return q }
   108  func (q QuadHash) Get(d quad.Direction) ValueHash {
   109  	switch d {
   110  	case quad.Subject:
   111  		return q.Subject
   112  	case quad.Predicate:
   113  		return q.Predicate
   114  	case quad.Object:
   115  		return q.Object
   116  	case quad.Label:
   117  		return q.Label
   118  	}
   119  	panic(fmt.Errorf("unknown direction: %v", d))
   120  }
   121  func (q *QuadHash) Set(d quad.Direction, h ValueHash) {
   122  	switch d {
   123  	case quad.Subject:
   124  		q.Subject = h
   125  	case quad.Predicate:
   126  		q.Predicate = h
   127  	case quad.Object:
   128  		q.Object = h
   129  	case quad.Label:
   130  		q.Label = h
   131  	default:
   132  		panic(fmt.Errorf("unknown direction: %v", d))
   133  	}
   134  }