Skip to content
Snippets Groups Projects
Commit 9b2fb124 authored by Manish R Jain's avatar Manish R Jain
Browse files

add store package to allow switch to Rocksdb later. Add license to every file.

parent 508d0e96
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,8 @@ ...@@ -2,3 +2,8 @@
Distributed Graph Serving System Distributed Graph Serving System
Welcome! Welcome!
## Dependencies
```
sudo apt-get install libsnappy-dev liblz4-dev
```
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 ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/google/flatbuffers/go" "github.com/google/flatbuffers/go"
...@@ -16,29 +31,20 @@ import ( ...@@ -16,29 +31,20 @@ import (
var log = logrus.WithField("package", "plist") var log = logrus.WithField("package", "plist")
type Triple struct { type Triple struct {
Entity string Entity uint64
EntityEid string
Attribute string Attribute string
Value interface{} Value interface{}
ValueId string ValueId uint64
Source string // Source string
Timestamp time.Time // Timestamp time.Time
} }
/* func AddToList(t Triple) {
func addTriple(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
x.SetStatus(w, x.E_INVALID_METHOD, "Should be POST")
return
}
var t Triple
if ok := x.ParseRequest(w, r, &t); !ok {
return
}
log.Debug(t)
} }
*/
var ldb *leveldb.DB
func main() { func main() {
path, err := ioutil.TempDir("", "dgraphldb_") path, err := ioutil.TempDir("", "dgraphldb_")
...@@ -49,7 +55,8 @@ func main() { ...@@ -49,7 +55,8 @@ func main() {
opt := &opt.Options{ opt := &opt.Options{
Filter: filter.NewBloomFilter(10), Filter: filter.NewBloomFilter(10),
} }
db, err := leveldb.OpenFile(path, opt) var err error
ldb, err := leveldb.OpenFile(path, opt)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
...@@ -100,7 +107,4 @@ func main() { ...@@ -100,7 +107,4 @@ func main() {
for i := 0; i < plist.IdsLength(); i++ { for i := 0; i < plist.IdsLength(); i++ {
fmt.Printf("[%d] [%d]\n", i, plist.Ids(i)) fmt.Printf("[%d] [%d]\n", i, plist.Ids(i))
} }
// http.HandleFunc("/add", addTriple)
// http.ListenAndServe(":8080", nil)
} }
/*
* 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
}
/*
* 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()
}
/*
* 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))
}
}
...@@ -27,6 +27,13 @@ type Status struct { ...@@ -27,6 +27,13 @@ type Status struct {
Message string `json:"message"` 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 { func Err(entry *logrus.Entry, err error) *logrus.Entry {
return entry.WithField("error", err) return entry.WithField("error", err)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment