Skip to content
Snippets Groups Projects
Commit 72d70527 authored by Pawan Rawal's avatar Pawan Rawal
Browse files

Did some renaming and formatting changes

parent f551b017
No related branches found
No related tags found
No related merge requests found
/* /*
* Copyright 2015 DGraph Labs, Inc. * Copyright 2016 DGraph Labs, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -28,11 +28,9 @@ import ( ...@@ -28,11 +28,9 @@ import (
) )
var glog = x.Log("client") var glog = x.Log("client")
var port = flag.String("port", "8090", "Port to communicate with server") var port = flag.String("port", "8090", "Port to communicate with server")
func main() { func main() {
// TODO(pawan) - Remove hardcoded query. Give helper methods to user for building query. // TODO(pawan) - Remove hardcoded query. Give helper methods to user for building query.
var q0 = `{ var q0 = `{
me(_xid_: m.0f4vbz) { me(_xid_: m.0f4vbz) {
......
// Code generated by protoc-gen-go. // Code generated by protoc-gen-go.
// source: subgraph.proto // source: graphresponse.proto
// DO NOT EDIT! // DO NOT EDIT!
/* /*
Package protocolbuffer is a generated protocol buffer package. Package pb is a generated protocol buffer package.
It is generated from these files: It is generated from these files:
subgraph.proto graphresponse.proto
It has these top-level messages: It has these top-level messages:
UidList UidList
Result Result
SubGraph GraphResponse
*/ */
package protocolbuffer package pb
import proto "github.com/golang/protobuf/proto" import proto "github.com/golang/protobuf/proto"
import fmt "fmt" import fmt "fmt"
...@@ -29,7 +29,7 @@ var _ = math.Inf ...@@ -29,7 +29,7 @@ var _ = math.Inf
const _ = proto.ProtoPackageIsVersion1 const _ = proto.ProtoPackageIsVersion1
type UidList struct { type UidList struct {
Uids []uint64 `protobuf:"fixed64,1,rep,packed,name=uids" json:"uids,omitempty"` Uids []uint64 `protobuf:"fixed64,1,rep,name=uids" json:"uids,omitempty"`
} }
func (m *UidList) Reset() { *m = UidList{} } func (m *UidList) Reset() { *m = UidList{} }
...@@ -54,25 +54,25 @@ func (m *Result) GetUidmatrix() []*UidList { ...@@ -54,25 +54,25 @@ func (m *Result) GetUidmatrix() []*UidList {
return nil return nil
} }
type SubGraph struct { type GraphResponse struct {
Attr string `protobuf:"bytes,1,opt,name=attr" json:"attr,omitempty"` Attribute string `protobuf:"bytes,1,opt,name=attribute" json:"attribute,omitempty"`
Children []*SubGraph `protobuf:"bytes,2,rep,name=children" json:"children,omitempty"` Children []*GraphResponse `protobuf:"bytes,2,rep,name=children" json:"children,omitempty"`
Result *Result `protobuf:"bytes,3,opt,name=result" json:"result,omitempty"` Result *Result `protobuf:"bytes,3,opt,name=result" json:"result,omitempty"`
} }
func (m *SubGraph) Reset() { *m = SubGraph{} } func (m *GraphResponse) Reset() { *m = GraphResponse{} }
func (m *SubGraph) String() string { return proto.CompactTextString(m) } func (m *GraphResponse) String() string { return proto.CompactTextString(m) }
func (*SubGraph) ProtoMessage() {} func (*GraphResponse) ProtoMessage() {}
func (*SubGraph) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } func (*GraphResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
func (m *SubGraph) GetChildren() []*SubGraph { func (m *GraphResponse) GetChildren() []*GraphResponse {
if m != nil { if m != nil {
return m.Children return m.Children
} }
return nil return nil
} }
func (m *SubGraph) GetResult() *Result { func (m *GraphResponse) GetResult() *Result {
if m != nil { if m != nil {
return m.Result return m.Result
} }
...@@ -80,25 +80,24 @@ func (m *SubGraph) GetResult() *Result { ...@@ -80,25 +80,24 @@ func (m *SubGraph) GetResult() *Result {
} }
func init() { func init() {
proto.RegisterType((*UidList)(nil), "protocolbuffer.UidList") proto.RegisterType((*UidList)(nil), "pb.UidList")
proto.RegisterType((*Result)(nil), "protocolbuffer.Result") proto.RegisterType((*Result)(nil), "pb.Result")
proto.RegisterType((*SubGraph)(nil), "protocolbuffer.SubGraph") proto.RegisterType((*GraphResponse)(nil), "pb.GraphResponse")
} }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 212 bytes of a gzipped FileDescriptorProto // 207 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x2e, 0x4d, 0x4a, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x54, 0x8f, 0xcd, 0x4a, 0xc4, 0x30,
0x2f, 0x4a, 0x2c, 0xc8, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x03, 0x53, 0xc9, 0xf9, 0x14, 0x46, 0x69, 0x2b, 0xd1, 0xde, 0xea, 0xc2, 0x2b, 0x48, 0x17, 0x0a, 0x92, 0x55, 0x5d, 0xd8,
0x39, 0x49, 0xa5, 0x69, 0x69, 0xa9, 0x45, 0x4a, 0x8a, 0x5c, 0xec, 0xa1, 0x99, 0x29, 0x3e, 0x99, 0x45, 0x7d, 0x08, 0x17, 0xba, 0xba, 0xe0, 0x03, 0x24, 0x36, 0xd8, 0x40, 0x6d, 0x43, 0x7e, 0x64,
0xc5, 0x25, 0x42, 0x62, 0x5c, 0x2c, 0xa5, 0x99, 0x29, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0x6c, 0x96, 0xf3, 0xe8, 0x93, 0xb6, 0x61, 0xca, 0xec, 0x92, 0x9c, 0x8f, 0xc3, 0x09, 0x3c, 0xfc, 0x5a,
0x4e, 0x4c, 0x02, 0x8c, 0x41, 0x60, 0xbe, 0x52, 0x38, 0x17, 0x5b, 0x50, 0x6a, 0x71, 0x69, 0x0e, 0x61, 0x06, 0xab, 0x9c, 0x99, 0x27, 0xa7, 0x5a, 0x63, 0x67, 0x3f, 0x63, 0x6e, 0x24, 0x7f, 0x86,
0x48, 0x05, 0x5b, 0x59, 0x62, 0x4e, 0x69, 0x2a, 0x44, 0x0d, 0x4f, 0x10, 0x94, 0x27, 0x64, 0xca, 0xeb, 0x6f, 0xdd, 0x7f, 0x69, 0xe7, 0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0x50, 0xcc, 0xae, 0xad, 0x75, 0x11, 0x4d, 0x55, 0x07, 0xcb, 0x78, 0xab, 0xa7, 0x44, 0x24, 0x5b, 0x7f,
0x55, 0x49, 0x6c, 0x60, 0x69, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfd, 0x1f, 0xc8, 0xb1, 0xfe, 0x7e, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x26, 0xe5, 0x36, 0x59, 0x10, 0x01, 0x00, 0x00,
0x2b, 0x01, 0x00, 0x00,
} }
syntax="proto3"; syntax="proto3";
package protocolbuffer; package pb;
message UidList { message UidList {
repeated fixed64 uids = 1 [packed=true];; repeated fixed64 uids = 1;
} }
message Result { message Result {
...@@ -10,8 +10,8 @@ message Result { ...@@ -10,8 +10,8 @@ message Result {
repeated UidList uidmatrix = 2; repeated UidList uidmatrix = 2;
} }
message SubGraph { message GraphResponse {
string attr = 1; string attribute = 1;
repeated SubGraph children = 2; //Each node can have multiple children repeated GraphResponse children = 2; //Each node can have multiple children
Result result = 3; Result result = 3;
} }
\ No newline at end of file
...@@ -26,7 +26,7 @@ import ( ...@@ -26,7 +26,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/dgraph-io/dgraph/gql" "github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/posting" "github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/query/protocolbuffer" "github.com/dgraph-io/dgraph/query/pb"
"github.com/dgraph-io/dgraph/task" "github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/worker" "github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x" "github.com/dgraph-io/dgraph/x"
...@@ -253,24 +253,25 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) { ...@@ -253,24 +253,25 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) {
return json.Marshal(r) return json.Marshal(r)
} }
func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) { func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) {
sg = &protocolbuffer.SubGraph{} gr = &pb.GraphResponse{}
if len(g.query) == 0 { if len(g.query) == 0 {
return sg, nil return gr, nil
} }
sg.Attr = g.Attr
gr.Attribute = g.Attr
ro := flatbuffers.GetUOffsetT(g.result) ro := flatbuffers.GetUOffsetT(g.result)
r := new(task.Result) r := new(task.Result)
r.Init(g.result, ro) r.Init(g.result, ro)
var ul task.UidList var ul task.UidList
result := &protocolbuffer.Result{} result := &pb.Result{}
for i := 0; i < r.UidmatrixLength(); i++ { for i := 0; i < r.UidmatrixLength(); i++ {
if ok := r.Uidmatrix(&ul, i); !ok { if ok := r.Uidmatrix(&ul, i); !ok {
return sg, fmt.Errorf("While parsing UidList") return gr, fmt.Errorf("While parsing UidList")
} }
uidList := &protocolbuffer.UidList{}
uidList := &pb.UidList{}
for j := 0; j < ul.UidsLength(); j++ { for j := 0; j < ul.UidsLength(); j++ {
uid := ul.Uids(j) uid := ul.Uids(j)
uidList.Uids = append(uidList.Uids, uid) uidList.Uids = append(uidList.Uids, uid)
...@@ -281,43 +282,48 @@ func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) { ...@@ -281,43 +282,48 @@ func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) {
var tv task.Value var tv task.Value
for i := 0; i < r.ValuesLength(); i++ { for i := 0; i < r.ValuesLength(); i++ {
if ok := r.Values(&tv, i); !ok { if ok := r.Values(&tv, i); !ok {
return sg, fmt.Errorf("While parsing value") return gr, fmt.Errorf("While parsing value")
} }
var ival interface{} var ival interface{}
if err := posting.ParseValue(&ival, tv.ValBytes()); err != nil { if err := posting.ParseValue(&ival, tv.ValBytes()); err != nil {
return sg, err return gr, err
} }
if ival == nil { if ival == nil {
continue ival = ""
} }
result.Values = append(result.Values, []byte(ival.(string))) result.Values = append(result.Values, []byte(ival.(string)))
} }
sg.Result = result gr.Result = result
for _, child := range g.Children { for _, child := range g.Children {
childSg, err := preTraverse(child) childPb, err := preTraverse(child)
if err != nil { if err != nil {
x.Err(glog, err).Error("Error while traversal") x.Err(glog, err).Error("Error while traversal")
return sg, err return gr, err
} }
sg.Children = append(sg.Children, childSg)
gr.Children = append(gr.Children, childPb)
} }
return sg, nil return gr, nil
} }
func (g *SubGraph) ToProtocolBuffer() (pb []byte, rerr error) { func (g *SubGraph) ToProtocolBuffer() (pbuffer []byte, rerr error) {
sg, err := preTraverse(g) gr, err := preTraverse(g)
if err != nil { if err != nil {
x.Err(glog, err).Error("Error while traversal") x.Err(glog, err).Error("Error while traversal")
return pb, err return pbuffer, err
} }
pb, err = proto.Marshal(sg)
pbuffer, err = proto.Marshal(gr)
if err != nil { if err != nil {
x.Err(glog, err).Error("Error while marshalling to protocol buffer") x.Err(glog, err).Error("Error while marshalling to protocol buffer")
return pb, err return pbuffer, err
} }
return pb, nil
return pbuffer, nil
} }
func treeCopy(gq *gql.GraphQuery, sg *SubGraph) { func treeCopy(gq *gql.GraphQuery, sg *SubGraph) {
......
...@@ -26,7 +26,7 @@ import ( ...@@ -26,7 +26,7 @@ import (
"github.com/dgraph-io/dgraph/commit" "github.com/dgraph-io/dgraph/commit"
"github.com/dgraph-io/dgraph/gql" "github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/posting" "github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/query/protocolbuffer" "github.com/dgraph-io/dgraph/query/pb"
"github.com/dgraph-io/dgraph/store" "github.com/dgraph-io/dgraph/store"
"github.com/dgraph-io/dgraph/task" "github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/worker" "github.com/dgraph-io/dgraph/worker"
...@@ -355,45 +355,47 @@ func TestToProtocolBuffer(t *testing.T) { ...@@ -355,45 +355,47 @@ func TestToProtocolBuffer(t *testing.T) {
t.Error(err) t.Error(err)
} }
pb, err := sg.ToProtocolBuffer() pbuffer, err := sg.ToProtocolBuffer()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
// Unmarshalling to a protocol buffer subgraph for testing // Unmarshalling to a protocol buffer graph response for testing
usg := &protocolbuffer.SubGraph{} ugr := &pb.GraphResponse{}
err = proto.Unmarshal(pb, usg) err = proto.Unmarshal(pbuffer, ugr)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if len(usg.Children) != 4 { if len(ugr.Children) != 4 {
t.Errorf("Expected len 4. Got: %v", usg.Children) t.Errorf("Expected len 4. Got: %v", ugr.Children)
} }
child := usg.Children[0] child := ugr.Children[0]
if child.Attr != "name" { if child.Attribute != "name" {
t.Errorf("Expected attr name. Got: %v", child.Attr) t.Errorf("Expected attr name. Got: %v", child.Attribute)
} }
if string(child.Result.Values[0]) != "Michonne" { if string(child.Result.Values[0]) != "Michonne" {
t.Errorf("Expected value Michonne. Got %v", string(child.Result.Values[0])) t.Errorf("Expected value Michonne. Got %v", string(child.Result.Values[0]))
} }
child = usg.Children[3] child = ugr.Children[3]
if child.Attr != "friend" { if child.Attribute != "friend" {
t.Errorf("Expected attr friend. Got: %v", child.Attr) t.Errorf("Expected attr friend. Got: %v", child.Attribute)
} }
uids := child.Result.Uidmatrix[0].Uids uids := child.Result.Uidmatrix[0].Uids
if uids[0] != 23 || uids[1] != 24 || uids[2] != 25 || uids[3] != 31 || uids[4] != 101 { if uids[0] != 23 || uids[1] != 24 || uids[2] != 25 || uids[3] != 31 ||
uids[4] != 101 {
t.Errorf("Friend ids don't match") t.Errorf("Friend ids don't match")
} }
// To check for name of friends // To check for name of friends
child = child.Children[0] child = child.Children[0]
if child.Attr != "name" { if child.Attribute != "name" {
t.Errorf("Expected attr friend. Got: %v", child.Attr) t.Errorf("Expected attr friend. Got: %v", child.Attribute)
} }
names := child.Result.Values names := child.Result.Values
if string(names[0]) != "Rick Grimes" || string(names[1]) != "Glenn Rhee" || string(names[2]) != "Daryl Dixon" || string(names[3]) != "Andrea" { if string(names[0]) != "Rick Grimes" || string(names[1]) != "Glenn Rhee" ||
string(names[2]) != "Daryl Dixon" || string(names[3]) != "Andrea" {
t.Errorf("Names don't match") t.Errorf("Names don't match")
} }
} }
...@@ -199,9 +199,12 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { ...@@ -199,9 +199,12 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
func pbQueryHandler(q []byte) (pb []byte, rerr error) { func pbQueryHandler(q []byte) (pb []byte, rerr error) {
if len(q) == 0 { if len(q) == 0 {
return glog.Error("While reading query")
return pb, fmt.Errorf("Empty query")
} }
// TODO(pawan): Refactor query parsing and graph processing code to a common
// function used by pbQueryHandler and queryHandler
glog.WithField("q", string(q)).Debug("Query received.") glog.WithField("q", string(q)).Debug("Query received.")
gq, _, err := gql.Parse(string(q)) gq, _, err := gql.Parse(string(q))
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment