replace zxq.co/ripple/hanayo

This commit is contained in:
Alicia
2019-02-23 13:29:15 +00:00
commit c3d206c173
5871 changed files with 1353715 additions and 0 deletions

18
vendor/github.com/thehowl/cieca/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,18 @@
Copyright (c) 2016 Morgan Bazalgette
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

117
vendor/github.com/thehowl/cieca/cieca.go generated vendored Normal file
View File

@@ -0,0 +1,117 @@
// Package cieca is an in-memory Key-Value datastore for Go. The main purpose
// is to easily cache data in the RAM for a certain amount of time.
package cieca
import (
"sync"
"time"
)
type element struct {
data []byte
expire time.Time
cancel chan struct{}
}
// DataStore is the datastore containing all the data.
type DataStore struct {
data map[string]element
mutex *sync.RWMutex
}
// Get retrieves a value in the datastore. If it is not found, nil is returned.
func (d *DataStore) Get(key string) []byte {
v, _ := d.GetWithExist(key)
return v
}
// GetWithExist retrieves the value of a key, and a boolean indicating whether
// the value existed in the store.
func (d *DataStore) GetWithExist(key string) ([]byte, bool) {
d.setup()
d.mutex.RLock()
defer d.mutex.RUnlock()
el, ex := d.data[key]
return el.data, ex
}
// Expiration gets the time of expiration of a key.
func (d *DataStore) Expiration(key string) *time.Time {
d.setup()
d.mutex.RLock()
defer d.mutex.RUnlock()
el, exist := d.data[key]
if !exist || el.cancel == nil {
return nil
}
t := el.expire
return &t
}
// Delete removes an element from the datastore.
func (d *DataStore) Delete(key string) {
d.setup()
d.mutex.Lock()
defer d.mutex.Unlock()
exp := d.data[key].cancel
if exp != nil && time.Now().Sub(d.data[key].expire) <= 0 {
exp <- struct{}{}
close(exp)
}
delete(d.data, key)
}
// Set sets a key in the datastore with no expiration.
func (d *DataStore) Set(key string, value []byte) {
d.SetWithExpiration(key, value, -1)
}
// SetWithExpiration sets a key in the datastore with a certain value.
func (d *DataStore) SetWithExpiration(key string, value []byte, expiration time.Duration) {
d.setup()
var c chan struct{}
if expiration >= 0 {
c = make(chan struct{})
}
el := element{
data: value,
expire: time.Now().Add(expiration),
cancel: c,
}
d.Delete(key)
d.mutex.Lock()
defer d.mutex.Unlock()
d.data[key] = el
if c != nil {
go d.queueDeletion(expiration, c, key)
}
}
// Clean clears the datastore. Not so thread-safe. Use with care.
func (d *DataStore) Clean() {
if d == nil {
return
}
for el := range d.data {
d.Delete(el)
}
}
func (d *DataStore) queueDeletion(dur time.Duration, canc <-chan struct{}, key string) {
select {
case <-time.NewTimer(dur).C:
d.Delete(key)
case <-canc:
// useless, but explicits what we're doing
return
}
}
func (d *DataStore) setup() {
if d.data == nil {
d.data = make(map[string]element)
}
if d.mutex == nil {
d.mutex = &sync.RWMutex{}
}
}

84
vendor/github.com/thehowl/cieca/cieca_test.go generated vendored Normal file
View File

@@ -0,0 +1,84 @@
package cieca_test
import (
"testing"
"time"
"github.com/thehowl/cieca"
)
func TestSetGet(t *testing.T) {
x := []byte("carroponte")
s := &cieca.DataStore{}
s.Set("test", x)
if string(s.Get("test")) != "carroponte" {
t.Fatal("test != carroponte", string(s.Get("test")))
}
s.Clean()
}
func TestClean(t *testing.T) {
s := &cieca.DataStore{}
s.Set("test", []byte("a"))
s.Clean()
if _, ex := s.GetWithExist("test"); ex {
t.Fatal("value exists even after Clean!")
}
}
func TestExpire(t *testing.T) {
s := &cieca.DataStore{}
defer s.Clean()
s.SetWithExpiration("zxcvbn", []byte("why?"), time.Nanosecond*100)
if s.Get("zxcvbn") == nil {
t.Fatal("Early expiration?")
}
if s.Expiration("zxcvbn") == nil {
t.Fatal("key's expiration is nil")
}
time.Sleep(time.Nanosecond * 5000)
if s.Get("zxcvbn") != nil {
t.Fatal("Late expiration?")
}
}
func TestOverwrite(t *testing.T) {
s := &cieca.DataStore{}
defer s.Clean()
s.Set("meme", []byte("1451"))
s.Set("meme", []byte("1337"))
if string(s.Get("meme")) != "1337" {
t.Fatal("No overwrite?")
}
}
func TestOverwriteWithExpiration(t *testing.T) {
s := &cieca.DataStore{}
defer s.Clean()
s.SetWithExpiration("carroponte", []byte("19689168196"), time.Second)
s.Delete("carroponte")
if s.Get("carroponte") != nil {
t.Fatal("carroponte ain't nil")
}
}
// just for coverage
func TestCleanOnNil(t *testing.T) {
var s *cieca.DataStore
s.Clean()
}
func TestExpirationWhenTheresNoExpiration(t *testing.T) {
s := &cieca.DataStore{}
defer s.Clean()
s.Set("meme", []byte("x"))
if s.Expiration("NotExisting") != nil || s.Expiration("meme") != nil {
t.Fatal("WHAT EXPIRATION???")
}
}
func BenchmarkSetGetDelete(b *testing.B) {
s := &cieca.DataStore{}
s.Get("NotExisting")
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Set("test", []byte("x"))
s.Get("test")
s.Delete("test")
}
}