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 @@
Distributed Graph Serving System
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 (
"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)
}
/*
* 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 {
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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment