diff --git a/client/go/main.go b/client/go/main.go index 8814c0c0729a13f9b7e8ef32ff51af8ac5e8f05e..780ea53721e83c273680c47ffbf0f30118e6b58a 100644 --- a/client/go/main.go +++ b/client/go/main.go @@ -1,5 +1,5 @@ /* - * Copyright 2015 DGraph Labs, Inc. + * Copyright 2016 DGraph Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,11 +28,9 @@ import ( ) var glog = x.Log("client") - var port = flag.String("port", "8090", "Port to communicate with server") func main() { - // TODO(pawan) - Remove hardcoded query. Give helper methods to user for building query. var q0 = `{ me(_xid_: m.0f4vbz) { diff --git a/query/pb/graphresponse.pb.go b/query/pb/graphresponse.pb.go new file mode 100644 index 0000000000000000000000000000000000000000..f96a0a83e1e7292b991433abbdfb9615017b985d --- /dev/null +++ b/query/pb/graphresponse.pb.go @@ -0,0 +1,103 @@ +// Code generated by protoc-gen-go. +// source: graphresponse.proto +// DO NOT EDIT! + +/* +Package pb is a generated protocol buffer package. + +It is generated from these files: + graphresponse.proto + +It has these top-level messages: + UidList + Result + GraphResponse +*/ +package pb + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +const _ = proto.ProtoPackageIsVersion1 + +type UidList struct { + Uids []uint64 `protobuf:"fixed64,1,rep,name=uids" json:"uids,omitempty"` +} + +func (m *UidList) Reset() { *m = UidList{} } +func (m *UidList) String() string { return proto.CompactTextString(m) } +func (*UidList) ProtoMessage() {} +func (*UidList) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +type Result struct { + Values [][]byte `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"` + Uidmatrix []*UidList `protobuf:"bytes,2,rep,name=uidmatrix" json:"uidmatrix,omitempty"` +} + +func (m *Result) Reset() { *m = Result{} } +func (m *Result) String() string { return proto.CompactTextString(m) } +func (*Result) ProtoMessage() {} +func (*Result) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } + +func (m *Result) GetUidmatrix() []*UidList { + if m != nil { + return m.Uidmatrix + } + return nil +} + +type GraphResponse struct { + Attribute string `protobuf:"bytes,1,opt,name=attribute" json:"attribute,omitempty"` + Children []*GraphResponse `protobuf:"bytes,2,rep,name=children" json:"children,omitempty"` + Result *Result `protobuf:"bytes,3,opt,name=result" json:"result,omitempty"` +} + +func (m *GraphResponse) Reset() { *m = GraphResponse{} } +func (m *GraphResponse) String() string { return proto.CompactTextString(m) } +func (*GraphResponse) ProtoMessage() {} +func (*GraphResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } + +func (m *GraphResponse) GetChildren() []*GraphResponse { + if m != nil { + return m.Children + } + return nil +} + +func (m *GraphResponse) GetResult() *Result { + if m != nil { + return m.Result + } + return nil +} + +func init() { + proto.RegisterType((*UidList)(nil), "pb.UidList") + proto.RegisterType((*Result)(nil), "pb.Result") + proto.RegisterType((*GraphResponse)(nil), "pb.GraphResponse") +} + +var fileDescriptor0 = []byte{ + // 207 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x54, 0x8f, 0xcd, 0x4a, 0xc4, 0x30, + 0x14, 0x46, 0x69, 0x2b, 0xd1, 0xde, 0xea, 0xc2, 0x2b, 0x48, 0x17, 0x0a, 0x92, 0x55, 0x5d, 0xd8, + 0x45, 0x7d, 0x08, 0x17, 0xba, 0xba, 0xe0, 0x03, 0x24, 0x36, 0xd8, 0x40, 0x6d, 0x43, 0x7e, 0x64, + 0x96, 0xf3, 0xe8, 0x93, 0xb6, 0x61, 0xca, 0xec, 0x92, 0x9c, 0x8f, 0xc3, 0x09, 0x3c, 0xfc, 0x5a, + 0x61, 0x06, 0xab, 0x9c, 0x99, 0x27, 0xa7, 0x5a, 0x63, 0x67, 0x3f, 0x63, 0x6e, 0x24, 0x7f, 0x86, + 0xeb, 0x6f, 0xdd, 0x7f, 0x69, 0xe7, 0x11, 0xe1, 0x2a, 0xe8, 0xde, 0xd5, 0xd9, 0x4b, 0xd1, 0x30, + 0x5a, 0xcf, 0xfc, 0x13, 0x18, 0x29, 0x17, 0x46, 0x8f, 0x8f, 0xc0, 0xfe, 0xc5, 0x18, 0xd4, 0xc6, + 0x6f, 0x29, 0xdd, 0xf0, 0x15, 0xca, 0xb8, 0xfc, 0x13, 0xde, 0xea, 0x43, 0x9d, 0x47, 0x54, 0x75, + 0x55, 0x6b, 0x64, 0x9b, 0xac, 0xb4, 0x53, 0x7e, 0xcc, 0xe0, 0xee, 0x63, 0xe9, 0xa0, 0xd4, 0x81, + 0x4f, 0x50, 0x0a, 0x1f, 0x99, 0x0c, 0x5e, 0x45, 0x6f, 0xd6, 0x94, 0xb4, 0x3f, 0xe0, 0x1b, 0xdc, + 0xfc, 0x0c, 0x7a, 0xec, 0xad, 0x9a, 0x92, 0xf9, 0x7e, 0x31, 0x5f, 0x28, 0xe8, 0x3c, 0x41, 0x0e, + 0xcc, 0xae, 0xad, 0x75, 0x11, 0x4d, 0x55, 0x07, 0xcb, 0x78, 0xab, 0xa7, 0x44, 0x24, 0x5b, 0x7f, + 0xfe, 0x7e, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x26, 0xe5, 0x36, 0x59, 0x10, 0x01, 0x00, 0x00, +} diff --git a/query/pb/graphresponse.proto b/query/pb/graphresponse.proto new file mode 100644 index 0000000000000000000000000000000000000000..046bbb53157845c8af87b7b08d6ded188940ef23 --- /dev/null +++ b/query/pb/graphresponse.proto @@ -0,0 +1,17 @@ +syntax="proto3"; +package pb; + +message UidList { + repeated fixed64 uids = 1; +} + +message Result { + repeated bytes values = 1; + repeated UidList uidmatrix = 2; +} + +message GraphResponse { + string attribute = 1; + repeated GraphResponse children = 2; //Each node can have multiple children + Result result = 3; +} diff --git a/query/protocolbuffer/subgraph.pb.go b/query/protocolbuffer/subgraph.pb.go deleted file mode 100644 index 56032779f9d47f00110ece6820e117546b03db12..0000000000000000000000000000000000000000 --- a/query/protocolbuffer/subgraph.pb.go +++ /dev/null @@ -1,104 +0,0 @@ -// Code generated by protoc-gen-go. -// source: subgraph.proto -// DO NOT EDIT! - -/* -Package protocolbuffer is a generated protocol buffer package. - -It is generated from these files: - subgraph.proto - -It has these top-level messages: - UidList - Result - SubGraph -*/ -package protocolbuffer - -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -const _ = proto.ProtoPackageIsVersion1 - -type UidList struct { - Uids []uint64 `protobuf:"fixed64,1,rep,packed,name=uids" json:"uids,omitempty"` -} - -func (m *UidList) Reset() { *m = UidList{} } -func (m *UidList) String() string { return proto.CompactTextString(m) } -func (*UidList) ProtoMessage() {} -func (*UidList) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } - -type Result struct { - Values [][]byte `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"` - Uidmatrix []*UidList `protobuf:"bytes,2,rep,name=uidmatrix" json:"uidmatrix,omitempty"` -} - -func (m *Result) Reset() { *m = Result{} } -func (m *Result) String() string { return proto.CompactTextString(m) } -func (*Result) ProtoMessage() {} -func (*Result) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } - -func (m *Result) GetUidmatrix() []*UidList { - if m != nil { - return m.Uidmatrix - } - return nil -} - -type SubGraph struct { - Attr string `protobuf:"bytes,1,opt,name=attr" json:"attr,omitempty"` - Children []*SubGraph `protobuf:"bytes,2,rep,name=children" json:"children,omitempty"` - Result *Result `protobuf:"bytes,3,opt,name=result" json:"result,omitempty"` -} - -func (m *SubGraph) Reset() { *m = SubGraph{} } -func (m *SubGraph) String() string { return proto.CompactTextString(m) } -func (*SubGraph) ProtoMessage() {} -func (*SubGraph) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } - -func (m *SubGraph) GetChildren() []*SubGraph { - if m != nil { - return m.Children - } - return nil -} - -func (m *SubGraph) GetResult() *Result { - if m != nil { - return m.Result - } - return nil -} - -func init() { - proto.RegisterType((*UidList)(nil), "protocolbuffer.UidList") - proto.RegisterType((*Result)(nil), "protocolbuffer.Result") - proto.RegisterType((*SubGraph)(nil), "protocolbuffer.SubGraph") -} - -var fileDescriptor0 = []byte{ - // 212 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x2e, 0x4d, 0x4a, - 0x2f, 0x4a, 0x2c, 0xc8, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x03, 0x53, 0xc9, 0xf9, - 0x39, 0x49, 0xa5, 0x69, 0x69, 0xa9, 0x45, 0x4a, 0x8a, 0x5c, 0xec, 0xa1, 0x99, 0x29, 0x3e, 0x99, - 0xc5, 0x25, 0x42, 0x62, 0x5c, 0x2c, 0xa5, 0x99, 0x29, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0x6c, - 0x4e, 0x4c, 0x02, 0x8c, 0x41, 0x60, 0xbe, 0x52, 0x38, 0x17, 0x5b, 0x50, 0x6a, 0x71, 0x69, 0x0e, - 0x48, 0x05, 0x5b, 0x59, 0x62, 0x4e, 0x69, 0x2a, 0x44, 0x0d, 0x4f, 0x10, 0x94, 0x27, 0x64, 0xca, - 0xc5, 0x09, 0x54, 0x99, 0x9b, 0x58, 0x52, 0x94, 0x59, 0x21, 0xc1, 0x04, 0x94, 0xe2, 0x36, 0x12, - 0xd7, 0x43, 0xb5, 0x48, 0x0f, 0x6a, 0x4b, 0x10, 0x42, 0xa5, 0x52, 0x0b, 0x23, 0x17, 0x47, 0x70, - 0x69, 0x92, 0x3b, 0xc8, 0x79, 0x42, 0x42, 0x5c, 0x2c, 0x89, 0x25, 0x25, 0x45, 0x40, 0x93, 0x19, - 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x21, 0x13, 0x2e, 0x8e, 0xe4, 0x8c, 0xcc, 0x9c, 0x94, 0xa2, 0xd4, - 0x3c, 0xa8, 0xb1, 0x12, 0xe8, 0xc6, 0xc2, 0xf4, 0x07, 0xc1, 0x55, 0x0a, 0xe9, 0x71, 0xb1, 0x15, - 0x81, 0xdd, 0x2b, 0xc1, 0x0c, 0x34, 0x8b, 0xdb, 0x48, 0x0c, 0x5d, 0x0f, 0xc4, 0x37, 0x41, 0x50, - 0x55, 0x49, 0x6c, 0x60, 0x69, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfd, 0x1f, 0xc8, 0xb1, - 0x2b, 0x01, 0x00, 0x00, -} diff --git a/query/protocolbuffer/subgraph.proto b/query/protocolbuffer/subgraph.proto deleted file mode 100644 index d5b713b293d13d494c3e63582e89fd1c73987e8e..0000000000000000000000000000000000000000 --- a/query/protocolbuffer/subgraph.proto +++ /dev/null @@ -1,17 +0,0 @@ -syntax="proto3"; -package protocolbuffer; - -message UidList { - repeated fixed64 uids = 1 [packed=true];; -} - -message Result { - repeated bytes values = 1; - repeated UidList uidmatrix = 2; -} - -message SubGraph { - string attr = 1; - repeated SubGraph children = 2; //Each node can have multiple children - Result result = 3; -} \ No newline at end of file diff --git a/query/query.go b/query/query.go index 1022692214cd8772f7c813ffe18a59f4070f8048..27b71f737ff45777b00c856d9457e25c60378098 100644 --- a/query/query.go +++ b/query/query.go @@ -26,7 +26,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/dgraph-io/dgraph/gql" "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/worker" "github.com/dgraph-io/dgraph/x" @@ -253,24 +253,25 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) { return json.Marshal(r) } -func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) { - sg = &protocolbuffer.SubGraph{} +func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) { + gr = &pb.GraphResponse{} if len(g.query) == 0 { - return sg, nil + return gr, nil } - sg.Attr = g.Attr + gr.Attribute = g.Attr ro := flatbuffers.GetUOffsetT(g.result) r := new(task.Result) r.Init(g.result, ro) var ul task.UidList - result := &protocolbuffer.Result{} + result := &pb.Result{} for i := 0; i < r.UidmatrixLength(); i++ { 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++ { uid := ul.Uids(j) uidList.Uids = append(uidList.Uids, uid) @@ -281,43 +282,48 @@ func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) { var tv task.Value for i := 0; i < r.ValuesLength(); i++ { if ok := r.Values(&tv, i); !ok { - return sg, fmt.Errorf("While parsing value") + return gr, fmt.Errorf("While parsing value") } + var ival interface{} if err := posting.ParseValue(&ival, tv.ValBytes()); err != nil { - return sg, err + return gr, err } + if ival == nil { - continue + ival = "" } result.Values = append(result.Values, []byte(ival.(string))) } - sg.Result = result + gr.Result = result for _, child := range g.Children { - childSg, err := preTraverse(child) + childPb, err := preTraverse(child) if err != nil { 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) { - sg, err := preTraverse(g) +func (g *SubGraph) ToProtocolBuffer() (pbuffer []byte, rerr error) { + gr, err := preTraverse(g) if err != nil { 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 { 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) { diff --git a/query/query_test.go b/query/query_test.go index ed68c2513daf8ce3a3e29ca74ecc44754d6c8dd7..285d188185a1c564be1fe2a1e61bdd7456c4e22a 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -26,7 +26,7 @@ import ( "github.com/dgraph-io/dgraph/commit" "github.com/dgraph-io/dgraph/gql" "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/task" "github.com/dgraph-io/dgraph/worker" @@ -355,45 +355,47 @@ func TestToProtocolBuffer(t *testing.T) { t.Error(err) } - pb, err := sg.ToProtocolBuffer() + pbuffer, err := sg.ToProtocolBuffer() if err != nil { t.Error(err) } - // Unmarshalling to a protocol buffer subgraph for testing - usg := &protocolbuffer.SubGraph{} - err = proto.Unmarshal(pb, usg) + // Unmarshalling to a protocol buffer graph response for testing + ugr := &pb.GraphResponse{} + err = proto.Unmarshal(pbuffer, ugr) if err != nil { t.Error(err) } - if len(usg.Children) != 4 { - t.Errorf("Expected len 4. Got: %v", usg.Children) + if len(ugr.Children) != 4 { + t.Errorf("Expected len 4. Got: %v", ugr.Children) } - child := usg.Children[0] - if child.Attr != "name" { - t.Errorf("Expected attr name. Got: %v", child.Attr) + child := ugr.Children[0] + if child.Attribute != "name" { + t.Errorf("Expected attr name. Got: %v", child.Attribute) } if string(child.Result.Values[0]) != "Michonne" { t.Errorf("Expected value Michonne. Got %v", string(child.Result.Values[0])) } - child = usg.Children[3] - if child.Attr != "friend" { - t.Errorf("Expected attr friend. Got: %v", child.Attr) + child = ugr.Children[3] + if child.Attribute != "friend" { + t.Errorf("Expected attr friend. Got: %v", child.Attribute) } 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") } // To check for name of friends child = child.Children[0] - if child.Attr != "name" { - t.Errorf("Expected attr friend. Got: %v", child.Attr) + if child.Attribute != "name" { + t.Errorf("Expected attr friend. Got: %v", child.Attribute) } 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") } } diff --git a/server/main.go b/server/main.go index fb9b2a6d978c7f4cbc269a7174ec0bafbb90b1d8..76cd6471a634fb24e99437f9a3b4dd580433715c 100644 --- a/server/main.go +++ b/server/main.go @@ -199,9 +199,12 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { func pbQueryHandler(q []byte) (pb []byte, rerr error) { 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.") gq, _, err := gql.Parse(string(q)) if err != nil {