From 9b2fb1245476a1250d1d35733107d9c6fb654073 Mon Sep 17 00:00:00 2001 From: Manish R Jain <manishrjain@gmail.com> Date: Wed, 14 Oct 2015 18:43:51 +1100 Subject: [PATCH] add store package to allow switch to Rocksdb later. Add license to every file. --- README.md | 5 +++ plist/main.go | 50 +++++++++++++++------------- query/query.go | 28 ++++++++++++++++ store/store.go | 79 +++++++++++++++++++++++++++++++++++++++++++++ store/store_test.go | 59 +++++++++++++++++++++++++++++++++ x/x.go | 7 ++++ 6 files changed, 205 insertions(+), 23 deletions(-) create mode 100644 query/query.go create mode 100644 store/store.go create mode 100644 store/store_test.go diff --git a/README.md b/README.md index 1ddc513c..60df999c 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,8 @@ Distributed Graph Serving System Welcome! + +## Dependencies +``` +sudo apt-get install libsnappy-dev liblz4-dev +``` diff --git a/plist/main.go b/plist/main.go index 3c4aacdd..959ba470 100644 --- a/plist/main.go +++ b/plist/main.go @@ -1,9 +1,24 @@ -package main +/* + * Copyright 2015 Manish R Jain <manishrjain@gmail.com> + * + * 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 triple import ( "fmt" "io/ioutil" - "time" "github.com/Sirupsen/logrus" "github.com/google/flatbuffers/go" @@ -16,29 +31,20 @@ import ( var log = logrus.WithField("package", "plist") type Triple struct { - Entity string + Entity uint64 + EntityEid string Attribute string Value interface{} - ValueId string - Source string - Timestamp time.Time + ValueId uint64 + // Source string + // Timestamp time.Time } -/* -func addTriple(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - x.SetStatus(w, x.E_INVALID_METHOD, "Should be POST") - return - } +func AddToList(t Triple) { - var t Triple - if ok := x.ParseRequest(w, r, &t); !ok { - return - } - - log.Debug(t) } -*/ + +var ldb *leveldb.DB func main() { path, err := ioutil.TempDir("", "dgraphldb_") @@ -49,7 +55,8 @@ func main() { opt := &opt.Options{ Filter: filter.NewBloomFilter(10), } - db, err := leveldb.OpenFile(path, opt) + var err error + ldb, err := leveldb.OpenFile(path, opt) if err != nil { log.Fatal(err) return @@ -100,7 +107,4 @@ func main() { for i := 0; i < plist.IdsLength(); i++ { fmt.Printf("[%d] [%d]\n", i, plist.Ids(i)) } - // http.HandleFunc("/add", addTriple) - // http.ListenAndServe(":8080", nil) - } diff --git a/query/query.go b/query/query.go new file mode 100644 index 00000000..5461a859 --- /dev/null +++ b/query/query.go @@ -0,0 +1,28 @@ +/* + * Copyright 2015 Manish R Jain <manishrjain@gmail.com> + * + * 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 query + +type QAttribute struct { + Attr string + Query *Query +} + +type Query struct { + Id uint64 // Dgraph Id + Eid string // External Id + Attrs []QAttribute +} diff --git a/store/store.go b/store/store.go new file mode 100644 index 00000000..7a7a2454 --- /dev/null +++ b/store/store.go @@ -0,0 +1,79 @@ +/* + * Copyright 2015 Manish R Jain <manishrjain@gmail.com> + * + * 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 store + +import ( + "bytes" + "encoding/binary" + + "github.com/manishrjain/gocrud/x" + "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/opt" +) + +var log = x.Log("store") + +type Store struct { + db *leveldb.DB + opt *opt.Options +} + +func (s *Store) Init(filepath string) { + var err error + s.db, err = leveldb.OpenFile(filepath, s.opt) + if err != nil { + x.LogErr(log, err).WithField("filepath", filepath). + Fatal("While opening store") + return + } +} + +func (s *Store) IsNew(id uint64) bool { + return false +} + +// key = (attribute, entity id) +func key(attr string, eid uint64) (ret []byte, rerr error) { + buf := new(bytes.Buffer) + buf.WriteString(attr) + if err := binary.Write(buf, binary.LittleEndian, eid); err != nil { + return ret, err + } + return buf.Bytes(), nil +} + +func (s *Store) Get(attr string, eid uint64) (val []byte, rerr error) { + k, err := key(attr, eid) + if err != nil { + return val, err + } + return s.db.Get(k, nil) +} + +func (s *Store) SetOne(attr string, eid uint64, val []byte) error { + k, err := key(attr, eid) + if err != nil { + return err + } + wb := new(leveldb.Batch) + wb.Put(k, val) + return s.db.Write(wb, nil) +} + +func (s *Store) Close() error { + return s.db.Close() +} diff --git a/store/store_test.go b/store/store_test.go new file mode 100644 index 00000000..76f02481 --- /dev/null +++ b/store/store_test.go @@ -0,0 +1,59 @@ +/* + * Copyright 2015 Manish R Jain <manishrjain@gmail.com> + * + * 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 store + +import ( + "io/ioutil" + "os" + "testing" +) + +func TestGet(t *testing.T) { + path, err := ioutil.TempDir("", "storetest_") + if err != nil { + t.Error(err) + t.Fail() + return + } + defer os.RemoveAll(path) + + var s Store + s.Init(path) + if err := s.SetOne("name", 1, []byte("neo")); err != nil { + t.Error(err) + t.Fail() + } + + if val, err := s.Get("name", 1); err != nil { + t.Error(err) + t.Fail() + } else if string(val) != "neo" { + t.Errorf("Expected 'neo'. Found: %s", string(val)) + } + + if err := s.SetOne("name", 1, []byte("the one")); err != nil { + t.Error(err) + t.Fail() + } + + if val, err := s.Get("name", 1); err != nil { + t.Error(err) + t.Fail() + } else if string(val) != "the one" { + t.Errorf("Expected 'the one'. Found: %s", string(val)) + } +} diff --git a/x/x.go b/x/x.go index 3ea34bb2..c757edf4 100644 --- a/x/x.go +++ b/x/x.go @@ -27,6 +27,13 @@ type Status struct { Message string `json:"message"` } +func Log(p string) *logrus.Entry { + l := logrus.WithFields(logrus.Fields{ + "package": p, + }) + return l +} + func Err(entry *logrus.Entry, err error) *logrus.Entry { return entry.WithField("error", err) } -- GitLab