Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

33
vendor/github.com/go-ego/riot/store/BUILD.bazel generated vendored Normal file
View File

@@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"badger_store.go",
"bolt_store.go",
"ldb_store.go",
"store.go",
],
importmap = "go-common/vendor/github.com/go-ego/riot/store",
importpath = "github.com/go-ego/riot/store",
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/coreos/bbolt:go_default_library",
"//vendor/github.com/dgraph-io/badger:go_default_library",
"//vendor/github.com/syndtr/goleveldb/leveldb:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

140
vendor/github.com/go-ego/riot/store/badger_store.go generated vendored Normal file
View File

@@ -0,0 +1,140 @@
// Copyright 2016 ego authors
//
// 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 (
"log"
"github.com/dgraph-io/badger"
)
// Badger badger.KV db store
type Badger struct {
db *badger.DB
}
// OpenBadger open Badger store
func OpenBadger(dbPath string) (Store, error) {
// err := os.MkdirAll(dbPath, 0777)
// if err != nil {
// log.Fatal("os.MkdirAll: ", err)
// os.Exit(1)
// }
// os.MkdirAll(path.Dir(dbPath), os.ModePerm)
opt := badger.DefaultOptions
opt.Dir = dbPath
opt.ValueDir = dbPath
opt.SyncWrites = true
kv, err := badger.Open(opt)
if err != nil {
log.Fatal("badger NewKV: ", err)
}
return &Badger{kv}, err
}
// WALName is useless for this kv database
func (s *Badger) WALName() string {
return "" // 对于此数据库,本函数没用~
}
// Set sets the provided value for a given key.
// If key is not present, it is created. If it is present,
// the existing value is overwritten with the one provided.
func (s *Badger) Set(k, v []byte) error {
err := s.db.Update(func(txn *badger.Txn) error {
// return txn.Set(k, v, 0x00)
return txn.Set(k, v)
})
return err
}
// Get looks for key and returns a value.
// If key is not found, value is nil.
func (s *Badger) Get(k []byte) ([]byte, error) {
var ival []byte
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(k)
if err != nil {
return err
}
ival, err = item.Value()
return err
})
return ival, err
}
// Delete deletes a key. Exposing this so that user does not
// have to specify the Entry directly. For example, BitDelete
// seems internal to badger.
func (s *Badger) Delete(k []byte) error {
err := s.db.Update(func(txn *badger.Txn) error {
return txn.Delete(k)
})
return err
}
// Has returns true if the DB does contains the given key.
func (s *Badger) Has(k []byte) (bool, error) {
// return s.db.Exists(k)
val, err := s.Get(k)
if string(val) == "" && err != nil {
return false, err
}
return true, err
}
// Len returns the size of lsm and value log files in bytes.
// It can be used to decide how often to call RunValueLogGC.
func (s *Badger) Len() (int64, int64) {
return s.db.Size()
}
// ForEach get all key and value
func (s *Badger) ForEach(fn func(k, v []byte) error) error {
err := s.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 1000
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
key := item.Key()
val, err := item.Value()
if err != nil {
return err
}
if err := fn(key, val); err != nil {
return err
}
}
return nil
})
return err
}
// Close closes a KV. It's crucial to call it to ensure
// all the pending updates make their way to disk.
func (s *Badger) Close() error {
return s.db.Close()
}

118
vendor/github.com/go-ego/riot/store/bolt_store.go generated vendored Normal file
View File

@@ -0,0 +1,118 @@
// Copyright 2016 ego authors
//
// 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 (
"time"
"github.com/coreos/bbolt"
// "github.com/boltdb/bolt"
)
var gdocs = []byte("gdocs")
// Bolt bolt store struct
type Bolt struct {
db *bolt.DB
}
// OpenBolt open Bolt store
func OpenBolt(dbPath string) (Store, error) {
db, err := bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 3600 * time.Second})
// db, err := bolt.Open(dbPath, 0600, &bolt.Options{})
if err != nil {
return nil, err
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(gdocs)
return err
})
if err != nil {
db.Close()
return nil, err
}
return &Bolt{db}, nil
}
// WALName returns the path to currently open database file.
func (s *Bolt) WALName() string {
return s.db.Path()
}
// Set executes a function within the context of a read-write managed
// transaction. If no error is returned from the function then the transaction
// is committed. If an error is returned then the entire transaction is rolled back.
// Any error that is returned from the function or returned from the commit is returned
// from the Update() method.
func (s *Bolt) Set(k []byte, v []byte) error {
return s.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(gdocs).Put(k, v)
})
}
// Get executes a function within the context of a managed read-only transaction.
// Any error that is returned from the function is returned from the View() method.
func (s *Bolt) Get(k []byte) (b []byte, err error) {
err = s.db.View(func(tx *bolt.Tx) error {
b = tx.Bucket(gdocs).Get(k)
return nil
})
return
}
// Delete deletes a key. Exposing this so that user does not
// have to specify the Entry directly.
func (s *Bolt) Delete(k []byte) error {
return s.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(gdocs).Delete(k)
})
}
// Has returns true if the DB does contains the given key.
func (s *Bolt) Has(k []byte) (bool, error) {
// return s.db.Exists(k)
var b []byte
err := s.db.View(func(tx *bolt.Tx) error {
b = tx.Bucket(gdocs).Get(k)
return nil
})
// b == nil
if err != nil || string(b) == "" {
return false, err
}
return true, nil
}
// ForEach get all key and value
func (s *Bolt) ForEach(fn func(k, v []byte) error) error {
return s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(gdocs)
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if err := fn(k, v); err != nil {
return err
}
}
return nil
})
}
// Close releases all database resources. All transactions
// must be closed before closing the database.
func (s *Bolt) Close() error {
return s.db.Close()
}

107
vendor/github.com/go-ego/riot/store/ldb_store.go generated vendored Normal file
View File

@@ -0,0 +1,107 @@
// Copyright 2016 ego authors
//
// 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 (
"github.com/syndtr/goleveldb/leveldb"
)
// Leveldb leveldb store
type Leveldb struct {
db *leveldb.DB
}
// OpenLeveldb opens or creates a DB for the given store. The DB
// will be created if not exist, unless ErrorIfMissing is true.
// Also, if ErrorIfExist is true and the DB exist Open will
// returns os.ErrExist error.
func OpenLeveldb(dbPath string) (Store, error) {
db, err := leveldb.OpenFile(dbPath, nil)
if err != nil {
return nil, err
}
return &Leveldb{db}, nil
}
// WALName is useless for this kv database
func (s *Leveldb) WALName() string {
return "" // 对于此数据库,本函数没用~
}
// Set sets the provided value for a given key.
// If key is not present, it is created. If it is present,
// the existing value is overwritten with the one provided.
func (s *Leveldb) Set(k, v []byte) error {
return s.db.Put(k, v, nil)
}
// Get gets the value for the given key. It returns
// ErrNotFound if the DB does not contains the key.
//
// The returned slice is its own copy, it is safe to modify
// the contents of the returned slice. It is safe to modify the contents
// of the argument after Get returns.
func (s *Leveldb) Get(k []byte) ([]byte, error) {
return s.db.Get(k, nil)
}
// Delete deletes the value for the given key. Delete will not
// returns error if key doesn't exist. Write merge also applies
// for Delete, see Write.
//
// It is safe to modify the contents of the arguments after Delete
// returns but not before.
func (s *Leveldb) Delete(k []byte) error {
return s.db.Delete(k, nil)
}
// Has returns true if the DB does contains the given key.
// It is safe to modify the contents of the argument after Has returns.
func (s *Leveldb) Has(k []byte) (bool, error) {
return s.db.Has(k, nil)
}
// Len calculates approximate sizes of the given key ranges.
// The length of the returned sizes are equal with the length of
// the given ranges. The returned sizes measure store space usage,
// so if the user data compresses by a factor of ten, the returned
// sizes will be one-tenth the size of the corresponding user data size.
// The results may not include the sizes of recently written data.
func (s *Leveldb) Len() (leveldb.Sizes, error) {
return s.db.SizeOf(nil)
}
// ForEach get all key and value
func (s *Leveldb) ForEach(fn func(k, v []byte) error) error {
iter := s.db.NewIterator(nil, nil)
for iter.Next() {
// Remember that the contents of the returned slice should not be modified, and
// only valid until the next call to Next.
key := iter.Key()
val := iter.Value()
if err := fn(key, val); err != nil {
return err
}
}
iter.Release()
return iter.Error()
}
// Close closes the DB. This will also releases any outstanding snapshot,
// abort any in-flight compaction and discard open transaction.
func (s *Leveldb) Close() error {
return s.db.Close()
}

72
vendor/github.com/go-ego/riot/store/store.go generated vendored Normal file
View File

@@ -0,0 +1,72 @@
// Copyright 2016 ego authors
//
// 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 (
"fmt"
"os"
)
const (
// DefaultStore default store engine
DefaultStore = "ldb"
// DefaultStore = "bad"
// DefaultStore = "bolt"
)
var supportedStore = map[string]func(path string) (Store, error){
"ldb": OpenLeveldb,
"bg": OpenBadger, // bad to bg
"bolt": OpenBolt,
// "kv": OpenKV,
// "ledisdb": Open,
}
// RegisterStore register store engine
func RegisterStore(name string, fn func(path string) (Store, error)) {
supportedStore[name] = fn
}
// Store is store interface
type Store interface {
// type KVBatch interface {
Set(k, v []byte) error
Get(k []byte) ([]byte, error)
Delete(k []byte) error
Has(k []byte) (bool, error)
ForEach(fn func(k, v []byte) error) error
Close() error
WALName() string
}
// OpenStore open store engine
func OpenStore(path string, args ...string) (Store, error) {
storeName := DefaultStore
if len(args) > 0 && args[0] != "" {
storeName = args[0]
} else {
storeEnv := os.Getenv("Riot_Store_Engine")
if storeEnv != "" {
storeName = storeEnv
}
}
if fn, has := supportedStore[storeName]; has {
return fn(path)
}
return nil, fmt.Errorf("unsupported store engine: %v", storeName)
}