package graph
import (
"iter"
"sort"
chickpeas "github.com/freeeve/gochickpeas"
"github.com/freeeve/gochickpeas/gql/value"
"github.com/freeeve/gochickpeas/nodeset"
)
type SnapshotGraph struct {
g *chickpeas .Snapshot
}
func New (g *chickpeas .Snapshot ) *SnapshotGraph { return &SnapshotGraph {g : g } }
func (s *SnapshotGraph ) Snapshot () *chickpeas .Snapshot { return s .g }
func (s *SnapshotGraph ) NodeCount () uint32 { return s .g .NodeCount () }
func (s *SnapshotGraph ) IDSpace () uint32 { return s .g .CSRIDSpace () }
func (s *SnapshotGraph ) NodeExists (id NodeID ) bool { return s .g .NodeExists (chickpeas .NodeID (id )) }
func (s *SnapshotGraph ) AvgDegree (relType string , dir chickpeas .Direction ) float64 {
return s .g .AvgDegree (relType , dir )
}
func (s *SnapshotGraph ) AvgDegreeByLabel (label , relType string , dir chickpeas .Direction ) (float64 , bool ) {
return s .g .AvgDegreeByLabel (label , relType , dir )
}
func (s *SnapshotGraph ) Degree (node chickpeas .NodeID , dir chickpeas .Direction ) int {
return s .g .Degree (node , dir )
}
func (s *SnapshotGraph ) HasLabel (node chickpeas .NodeID , label string ) bool {
return s .g .HasLabel (node , label )
}
func (s *SnapshotGraph ) LabelNames () []string { return s .g .Labels () }
func (s *SnapshotGraph ) NodesWithLabel (label string ) *nodeset .Set {
set , ok := s .g .NodesWithLabel (label )
if !ok {
return nil
}
return set
}
func (s *SnapshotGraph ) LabelCardinality (label string ) uint64 {
set , ok := s .g .NodesWithLabel (label )
if !ok {
return 0
}
return uint64 (set .Len ())
}
func (s *SnapshotGraph ) NodesWithProperty (label , key string , v value .Value ) *nodeset .Set {
ev , ok := engineValue (s .g , v )
if !ok {
return nil
}
set , ok := s .g .NodesWithValue (label , key , ev )
if !ok {
return nil
}
return set
}
func (s *SnapshotGraph ) NodeProp (node chickpeas .NodeID , key string ) (value .Value , bool ) {
return propValue (s .g .Prop (node , key ))
}
func (s *SnapshotGraph ) NodePropEq (node chickpeas .NodeID , key string , v value .Value ) bool {
stored , ok := s .NodeProp (node , key )
if !ok {
return v .IsNull ()
}
return value .Equal (stored , v )
}
func (s *SnapshotGraph ) NodePropKeys (node chickpeas .NodeID ) []string {
keys := s .g .NodePropertyKeys (node )
sort .Strings (keys )
return keys
}
func (s *SnapshotGraph ) RelProp (pos uint32 , key string ) (value .Value , bool ) {
return propValue (s .g .RelProp (pos , key ))
}
func (s *SnapshotGraph ) RelEndpoints (pos uint32 ) (source , target chickpeas .NodeID , ok bool ) {
return s .g .RelEndpoints (pos )
}
func (s *SnapshotGraph ) RelTypeAt (pos uint32 ) (string , bool ) {
return s .g .RelTypeAt (pos )
}
func (s *SnapshotGraph ) Neighbors (node chickpeas .NodeID , dir chickpeas .Direction ) iter .Seq [chickpeas .NodeID ] {
return s .g .Neighbors (node , dir )
}
func (s *SnapshotGraph ) NeighborsByType (node chickpeas .NodeID , dir chickpeas .Direction , types []string ) iter .Seq [chickpeas .NodeID ] {
return s .g .Neighbors (node , dir , types ...)
}
func (s *SnapshotGraph ) NeighborsMatched (node chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) iter .Seq [chickpeas .NodeID ] {
return s .g .NeighborsMatch (node , dir , m .m )
}
func (s *SnapshotGraph ) Relationships (node chickpeas .NodeID , dir chickpeas .Direction , types []string ) iter .Seq2 [chickpeas .NodeID , uint32 ] {
return func (yield func (chickpeas .NodeID , uint32 ) bool ) {
for r := range s .g .Rels (node , dir , types ...) {
if !yield (r .Neighbor , r .Pos ) {
return
}
}
}
}
func (s *SnapshotGraph ) RelationshipsMatched (node chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) iter .Seq2 [chickpeas .NodeID , uint32 ] {
return func (yield func (chickpeas .NodeID , uint32 ) bool ) {
for r := range s .g .RelsMatch (node , dir , m .m ) {
if !yield (r .Neighbor , r .Pos ) {
return
}
}
}
}
func (s *SnapshotGraph ) AppendNeighborsMatched (dst []chickpeas .NodeID , node chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) []chickpeas .NodeID {
return s .g .AppendNeighborsEach (dst , node , dir , m .m )
}
func (s *SnapshotGraph ) AppendNeighborsByType (dst []chickpeas .NodeID , node chickpeas .NodeID , dir chickpeas .Direction , types []string ) []chickpeas .NodeID {
return s .g .AppendNeighborsEach (dst , node , dir , s .g .Match (types ...))
}
func (s *SnapshotGraph ) AppendRelationshipsMatched (nodes []chickpeas .NodeID , poss []uint32 , node chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) ([]chickpeas .NodeID , []uint32 ) {
for r := range s .g .RelsMatch (node , dir , m .m ) {
nodes = append (nodes , r .Neighbor )
poss = append (poss , r .Pos )
}
return nodes , poss
}
func (s *SnapshotGraph ) CountNeighborsMatched (u , v chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) int {
return s .g .CountNeighborsMatch (u , v , dir , m .m )
}
func (s *SnapshotGraph ) AppendRelsBetween (poss []uint32 , u , v chickpeas .NodeID , dir chickpeas .Direction , m *RelMatcher ) []uint32 {
return s .g .AppendRelsBetweenMatch (poss , u , v , dir , m .m )
}
func (s *SnapshotGraph ) ChainRootsVia (types []string , dir chickpeas .Direction , labels []string ) (chickpeas .RootsVia , bool ) {
if len (types ) != 1 {
return nil , false
}
for _ , l := range labels {
if roots , ok := s .g .ChainCollapseVia (types [0 ], dir , l ); ok {
return roots , true
}
}
return nil , false
}
func (s *SnapshotGraph ) FunctionalVia (types []string , dir chickpeas .Direction ) bool {
return len (types ) == 1 && s .g .FunctionalVia (types [0 ], dir )
}
func (s *SnapshotGraph ) SubstringCandidates (label , field , needle string ) (*nodeset .Set , bool ) {
return nil , false
}
func (s *SnapshotGraph ) FullTextSearch (label , field , query string ) (*nodeset .Set , bool ) {
return s .g .FullTextSearch (label , field , query ), true
}
func (s *SnapshotGraph ) GeoWithinRadius (label , latField , lonField string , lat , lon , km float64 ) (*nodeset .Set , bool ) {
return s .g .GeoWithinRadius (label , latField , lonField , lat , lon , km ), true
}
func (s *SnapshotGraph ) GeoWithinBBox (label , latField , lonField string , minLat , minLon , maxLat , maxLon float64 ) (*nodeset .Set , bool ) {
return s .g .GeoWithinBBox (label , latField , lonField , minLat , minLon , maxLat , maxLon ), true
}
func (s *SnapshotGraph ) RelWeightReader (key string ) func (pos uint32 ) float64 {
col , ok := s .g .RelColIndexed (key )
if !ok {
return nil
}
switch col .Dtype () {
case chickpeas .DtypeF64 :
r := col .F64 ()
return func (pos uint32 ) float64 {
if v , ok := r .Get (pos ); ok {
return v
}
return 1.0
}
case chickpeas .DtypeI64 :
r := col .I64 ()
return func (pos uint32 ) float64 {
if v , ok := r .Get (pos ); ok {
return float64 (v )
}
return 1.0
}
default :
return func (uint32 ) float64 { return 1.0 }
}
}
func propValue(p chickpeas .Prop ) (value .Value , bool ) {
v , ok := p .Value ()
if !ok {
return value .Value {}, false
}
switch v .Kind () {
case chickpeas .KindI64 :
i , _ := v .I64 ()
return value .Int (i ), true
case chickpeas .KindF64 :
f , _ := v .F64 ()
return value .Float (f ), true
case chickpeas .KindBool :
b , _ := v .Bool ()
return value .Bool (b ), true
default :
s , ok := p .Str ()
if !ok {
return value .Value {}, false
}
return value .Str (s ), true
}
}
func engineValue(g *chickpeas .Snapshot , v value .Value ) (chickpeas .Value , bool ) {
switch v .Kind () {
case value .KindInt :
i , _ := v .AsInt ()
return chickpeas .I64Value (i ), true
case value .KindFloat :
f , _ := v .AsFloat ()
return chickpeas .F64Value (f ), true
case value .KindBool :
b , _ := v .AsBool ()
return chickpeas .BoolValue (b ), true
case value .KindStr :
s , _ := v .AsStr ()
return g .ValueFromString (s )
default :
return chickpeas .Value {}, false
}
}
var (
_ Graph = (*SnapshotGraph )(nil )
_ Native = (*SnapshotGraph )(nil )
)
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .