diff --git a/cluster/cluster.go b/cluster/cluster.go new file mode 100644 index 0000000000000000000000000000000000000000..26e45ace703ac7d8d93562ccca9d80a8041e1d50 --- /dev/null +++ b/cluster/cluster.go @@ -0,0 +1,26 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package cluster + +func Init() { + //TODO::Code to start a RAFT group +} + +func Join() { + // TODO::Code when a new node joins the cluster +} diff --git a/cluster/doc.go b/cluster/doc.go new file mode 100644 index 0000000000000000000000000000000000000000..916b1b53b9f0d4dcc23f2acdd15d61f9a243674f --- /dev/null +++ b/cluster/doc.go @@ -0,0 +1 @@ +package cluster diff --git a/cluster/predicateList.go b/cluster/predicateList.go new file mode 100644 index 0000000000000000000000000000000000000000..fa57677895389b7ee4b9a78982fa7b2f3efd22a5 --- /dev/null +++ b/cluster/predicateList.go @@ -0,0 +1,46 @@ +/* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* + */ + +package cluster + +import ( + "bytes" + + "github.com/dgraph-io/dgraph/store" +) + +func getPredicate(b []byte) string { + buf := bytes.NewBuffer(b) + a, _ := buf.ReadString('|') + str := string(a[:len(a)-1]) // omit the trailing '|' + return str +} + +func GetPredicateList(ps *store.Store) []string { + var predicateList []string + var lastPredicate, predicate string + + it := ps.GetIterator() + for it.SeekToFirst(); it.Valid(); it.Next() { + predicate = getPredicate(it.Key()) + if predicate != lastPredicate { + predicateList = append(predicateList, predicate) + lastPredicate = predicate + } + } + return predicateList +} diff --git a/cluster/predicateList_test.go b/cluster/predicateList_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d7f74e569aa883b8ce78087a690a10fa9dcd11cb --- /dev/null +++ b/cluster/predicateList_test.go @@ -0,0 +1,75 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package cluster + +import ( + "bytes" + "encoding/binary" + "io/ioutil" + "os" + "testing" + + "github.com/Sirupsen/logrus" + "github.com/dgraph-io/dgraph/posting" + "github.com/dgraph-io/dgraph/store" +) + +func TestgetPredicate(t *testing.T) { + attr := "friends" + var uid uint64 = 12345 + + buf := bytes.NewBufferString(attr) + if err := binary.Write(buf, binary.LittleEndian, uid); err != nil { + t.Fatalf("Error while creating key with attr: %v uid: %v\n", attr, uid) + } + + str := getPredicate(buf.Bytes()) + if str != "friends" { + t.Error("Wrong predicate obtained") + } +} + +func TestGetPredicateList(t *testing.T) { + logrus.SetLevel(logrus.DebugLevel) + dir1, err := ioutil.TempDir("", "dir_") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(dir1) + ps1 := new(store.Store) + ps1.Init(dir1) + defer ps1.Close() + + k1 := posting.Key(1000, "friend") + k2 := posting.Key(1010, "friend") + k3 := posting.Key(1020, "friend") + k4 := posting.Key(1030, "follow") + k5 := posting.Key(1040, "follow") + ps1.SetOne(k1, []byte("alice")) + ps1.SetOne(k2, []byte("bob")) + ps1.SetOne(k3, []byte("ram")) + ps1.SetOne(k4, []byte("ash")) + ps1.SetOne(k5, []byte("mallory")) + + list := GetPredicateList(ps1) + + if len(list) != 2 { + t.Errorf("Predicate List incorrect %v", len(list)) + } +} diff --git a/posting/list.go b/posting/list.go index 41cfe53488bf021a11cba8f475cf1f8c9d0c2b3b..07a0887638e6286004fd8908743cbaaa545e83cc 100644 --- a/posting/list.go +++ b/posting/list.go @@ -105,7 +105,7 @@ func samePosting(a *types.Posting, b *types.Posting) bool { // key = (entity uid, attribute) func Key(uid uint64, attr string) []byte { - buf := bytes.NewBufferString(attr) + buf := bytes.NewBufferString(attr + "|") if err := binary.Write(buf, binary.LittleEndian, uid); err != nil { glog.Fatalf("Error while creating key with attr: %v uid: %v\n", attr, uid) } diff --git a/store/store.go b/store/store.go index eabd85848237fc934bd945bb29f291dfb01281fb..90ef7ee83c77769aa3a2bfbc785854b99a8a4a36 100644 --- a/store/store.go +++ b/store/store.go @@ -67,6 +67,10 @@ func (s *Store) Delete(k []byte) error { return s.db.Delete(s.wopt, k) } +func (s *Store) GetIterator() *rocksdb.Iterator { + return s.db.NewIterator(s.ropt) +} + func (s *Store) Close() { s.db.Close() }