gofmt peernet (#103)

This commit is contained in:
Akilan Selvacoumar
2023-02-12 09:59:31 +00:00
committed by GitHub
parent 3a1eece580
commit 9775525d35
95 changed files with 15798 additions and 15794 deletions

View File

@@ -1,94 +1,94 @@
/*
File Name: Memory.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package store
import (
"sync"
"time"
)
// MemoryStore is a simple in-memory key-value store for testing purposes.
type MemoryStore struct {
mutex *sync.Mutex
data map[string][]byte
expireMap map[string]time.Time
}
// NewMemoryStore create a properly initialized memory store.
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
data: make(map[string][]byte),
mutex: &sync.Mutex{},
expireMap: make(map[string]time.Time),
}
}
// ExpireKeys is called to delete all keys that are marked for expiration.
func (ms *MemoryStore) ExpireKeys() {
ms.mutex.Lock()
defer ms.mutex.Unlock()
for k, v := range ms.expireMap {
if time.Now().After(v) {
delete(ms.expireMap, k)
delete(ms.data, k)
}
}
}
// Set stores the key-value pair.
func (ms *MemoryStore) Set(key []byte, data []byte) error {
ms.mutex.Lock()
ms.data[string(key)] = data
ms.mutex.Unlock()
return nil
}
// StoreExpire stores the key-value pair and deletes it after the expiration time.
func (ms *MemoryStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
ms.mutex.Lock()
ms.expireMap[string(key)] = expiration
ms.data[string(key)] = data
ms.mutex.Unlock()
return nil
}
// Get returns the value for the key if present.
func (ms *MemoryStore) Get(key []byte) (data []byte, found bool) {
ms.mutex.Lock()
data, found = ms.data[string(key)]
ms.mutex.Unlock()
return data, found
}
// Delete deletes a key-value pair.
func (ms *MemoryStore) Delete(key []byte) {
ms.mutex.Lock()
delete(ms.expireMap, string(key))
delete(ms.data, string(key))
ms.mutex.Unlock()
}
// Count returns the count of records stored.
func (ms *MemoryStore) Count() uint64 {
ms.mutex.Lock()
defer ms.mutex.Unlock()
return uint64(len(ms.data))
}
// Iterate iterates over all records.
func (ms *MemoryStore) Iterate(callback func(key, value []byte)) {
ms.mutex.Lock()
defer ms.mutex.Unlock()
for key, value := range ms.data {
ms.mutex.Unlock() // allow access to the map while calling the callback
callback([]byte(key), value)
ms.mutex.Lock()
}
}
/*
File Name: Memory.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package store
import (
"sync"
"time"
)
// MemoryStore is a simple in-memory key-value store for testing purposes.
type MemoryStore struct {
mutex *sync.Mutex
data map[string][]byte
expireMap map[string]time.Time
}
// NewMemoryStore create a properly initialized memory store.
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
data: make(map[string][]byte),
mutex: &sync.Mutex{},
expireMap: make(map[string]time.Time),
}
}
// ExpireKeys is called to delete all keys that are marked for expiration.
func (ms *MemoryStore) ExpireKeys() {
ms.mutex.Lock()
defer ms.mutex.Unlock()
for k, v := range ms.expireMap {
if time.Now().After(v) {
delete(ms.expireMap, k)
delete(ms.data, k)
}
}
}
// Set stores the key-value pair.
func (ms *MemoryStore) Set(key []byte, data []byte) error {
ms.mutex.Lock()
ms.data[string(key)] = data
ms.mutex.Unlock()
return nil
}
// StoreExpire stores the key-value pair and deletes it after the expiration time.
func (ms *MemoryStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
ms.mutex.Lock()
ms.expireMap[string(key)] = expiration
ms.data[string(key)] = data
ms.mutex.Unlock()
return nil
}
// Get returns the value for the key if present.
func (ms *MemoryStore) Get(key []byte) (data []byte, found bool) {
ms.mutex.Lock()
data, found = ms.data[string(key)]
ms.mutex.Unlock()
return data, found
}
// Delete deletes a key-value pair.
func (ms *MemoryStore) Delete(key []byte) {
ms.mutex.Lock()
delete(ms.expireMap, string(key))
delete(ms.data, string(key))
ms.mutex.Unlock()
}
// Count returns the count of records stored.
func (ms *MemoryStore) Count() uint64 {
ms.mutex.Lock()
defer ms.mutex.Unlock()
return uint64(len(ms.data))
}
// Iterate iterates over all records.
func (ms *MemoryStore) Iterate(callback func(key, value []byte)) {
ms.mutex.Lock()
defer ms.mutex.Unlock()
for key, value := range ms.data {
ms.mutex.Unlock() // allow access to the map while calling the callback
callback([]byte(key), value)
ms.mutex.Lock()
}
}

View File

@@ -1,72 +1,72 @@
/*
File Name: Pebble.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
Note: It turned out that pebble has many dependencies and increases the binary file size by ~6 MB.
*/
package store
/*
import (
"errors"
"sync"
"time"
"github.com/cockroachdb/pebble"
)
// PebbleStore is a key/value store using Pebble from CockroachDB.
// Expiration is currently not supported.
type PebbleStore struct {
mutex *sync.Mutex
filename string
db *pebble.DB
}
// NewPebbleStore create a properly initialized pebble store.
func NewPebbleStore(filename string) (store *PebbleStore, err error) {
// if the database does not exist, it will be created
db, err := pebble.Open(filename, &pebble.Options{})
if err != nil {
return nil, err
}
return &PebbleStore{
mutex: &sync.Mutex{},
filename: filename,
db: db,
}, nil
}
func (store *PebbleStore) ExpireKeys() {
// Not yet implemented
}
// Store stores the key/value pair.
func (store *PebbleStore) Set(key []byte, data []byte) error {
return store.db.Set(key, data, pebble.Sync)
}
// StoreExpire stores the key/value pair and deletes it after the expiration time.
func (store *PebbleStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
// Not yet implemented
return errors.New("not yet implemented")
}
// Get returns the value for the key if present.
func (store *PebbleStore) Get(key []byte) (data []byte, found bool) {
value, closer, err := store.db.Get(key)
if err != nil {
return nil, false
}
closer.Close()
return value, true
}
// Delete deletes a key/value pair.
func (store *PebbleStore) Delete(key []byte) {
store.db.Delete(key, pebble.Sync)
}
*/
/*
File Name: Pebble.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
Note: It turned out that pebble has many dependencies and increases the binary file size by ~6 MB.
*/
package store
/*
import (
"errors"
"sync"
"time"
"github.com/cockroachdb/pebble"
)
// PebbleStore is a key/value store using Pebble from CockroachDB.
// Expiration is currently not supported.
type PebbleStore struct {
mutex *sync.Mutex
filename string
db *pebble.DB
}
// NewPebbleStore create a properly initialized pebble store.
func NewPebbleStore(filename string) (store *PebbleStore, err error) {
// if the database does not exist, it will be created
db, err := pebble.Open(filename, &pebble.Options{})
if err != nil {
return nil, err
}
return &PebbleStore{
mutex: &sync.Mutex{},
filename: filename,
db: db,
}, nil
}
func (store *PebbleStore) ExpireKeys() {
// Not yet implemented
}
// Store stores the key/value pair.
func (store *PebbleStore) Set(key []byte, data []byte) error {
return store.db.Set(key, data, pebble.Sync)
}
// StoreExpire stores the key/value pair and deletes it after the expiration time.
func (store *PebbleStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
// Not yet implemented
return errors.New("not yet implemented")
}
// Get returns the value for the key if present.
func (store *PebbleStore) Get(key []byte) (data []byte, found bool) {
value, closer, err := store.db.Get(key)
if err != nil {
return nil, false
}
closer.Close()
return value, true
}
// Delete deletes a key/value pair.
func (store *PebbleStore) Delete(key []byte) {
store.db.Delete(key, pebble.Sync)
}
*/

View File

@@ -1,89 +1,89 @@
/*
File Name: Pogreb.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package store
import (
"errors"
"io"
"log"
"sync"
"time"
"github.com/akrylysov/pogreb"
)
// PogrebStore is a key-value store using Pogreb.
// Expiration is currently not supported.
type PogrebStore struct {
mutex *sync.Mutex
filename string
db *pogreb.DB
}
// NewPogrebStore create a properly initialized Pogreb store.
func NewPogrebStore(filename string) (store *PogrebStore, err error) {
pogreb.SetLogger(log.New(io.Discard, "", 0))
// if the database does not exist, it will be created
db, err := pogreb.Open(filename, nil)
if err != nil {
return nil, err
}
return &PogrebStore{
mutex: &sync.Mutex{},
filename: filename,
db: db,
}, nil
}
func (store *PogrebStore) ExpireKeys() {
// Not yet implemented
}
// Store stores the key-value pair.
func (store *PogrebStore) Set(key []byte, data []byte) error {
return store.db.Put(key, data)
}
// StoreExpire stores the key-value pair and deletes it after the expiration time.
func (store *PogrebStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
// Not yet implemented
return errors.New("not yet implemented")
}
// Get returns the value for the key if present.
func (store *PogrebStore) Get(key []byte) (data []byte, found bool) {
value, err := store.db.Get(key)
if err != nil || value == nil {
return nil, false
}
return value, true
}
// Delete deletes a key-value pair.
func (store *PogrebStore) Delete(key []byte) {
store.db.Delete(key)
}
// Count returns the count of records stored.
func (store *PogrebStore) Count() uint64 {
return uint64(store.db.Count())
}
// Iterate iterates over all records.
func (store *PogrebStore) Iterate(callback func(key, value []byte)) {
iterator := store.db.Items()
for {
key, value, err := iterator.Next()
if err != nil {
break
}
callback(key, value)
}
}
/*
File Name: Pogreb.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package store
import (
"errors"
"io"
"log"
"sync"
"time"
"github.com/akrylysov/pogreb"
)
// PogrebStore is a key-value store using Pogreb.
// Expiration is currently not supported.
type PogrebStore struct {
mutex *sync.Mutex
filename string
db *pogreb.DB
}
// NewPogrebStore create a properly initialized Pogreb store.
func NewPogrebStore(filename string) (store *PogrebStore, err error) {
pogreb.SetLogger(log.New(io.Discard, "", 0))
// if the database does not exist, it will be created
db, err := pogreb.Open(filename, nil)
if err != nil {
return nil, err
}
return &PogrebStore{
mutex: &sync.Mutex{},
filename: filename,
db: db,
}, nil
}
func (store *PogrebStore) ExpireKeys() {
// Not yet implemented
}
// Store stores the key-value pair.
func (store *PogrebStore) Set(key []byte, data []byte) error {
return store.db.Put(key, data)
}
// StoreExpire stores the key-value pair and deletes it after the expiration time.
func (store *PogrebStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
// Not yet implemented
return errors.New("not yet implemented")
}
// Get returns the value for the key if present.
func (store *PogrebStore) Get(key []byte) (data []byte, found bool) {
value, err := store.db.Get(key)
if err != nil || value == nil {
return nil, false
}
return value, true
}
// Delete deletes a key-value pair.
func (store *PogrebStore) Delete(key []byte) {
store.db.Delete(key)
}
// Count returns the count of records stored.
func (store *PogrebStore) Count() uint64 {
return uint64(store.db.Count())
}
// Iterate iterates over all records.
func (store *PogrebStore) Iterate(callback func(key, value []byte)) {
iterator := store.db.Items()
for {
key, value, err := iterator.Next()
if err != nil {
break
}
callback(key, value)
}
}

View File

@@ -1,38 +1,38 @@
/*
File Name: Store.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
Simple key-value store interface.
*/
package store
import (
"time"
)
// Store is the interface for implementing the storage mechanism for the DHT.
type Store interface {
// Set stores the key-value pair.
Set(key []byte, data []byte) error
// StoreExpire stores the key-value pair and deletes it after the expiration time.
// If key-value already exists, it will be overwritten and the new expiration time applies.
StoreExpire(key []byte, data []byte, expiration time.Time) error
// Get returns the value for the key if present.
Get(key []byte) (data []byte, found bool)
// Delete deletes a key-value pair.
Delete(key []byte)
// ExpireKeys is called to delete all keys that are marked for expiration.
ExpireKeys()
// Count returns the total number of records stored.
Count() uint64
// Iterate iterates over all records.
Iterate(callback func(key, value []byte))
}
/*
File Name: Store.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
Simple key-value store interface.
*/
package store
import (
"time"
)
// Store is the interface for implementing the storage mechanism for the DHT.
type Store interface {
// Set stores the key-value pair.
Set(key []byte, data []byte) error
// StoreExpire stores the key-value pair and deletes it after the expiration time.
// If key-value already exists, it will be overwritten and the new expiration time applies.
StoreExpire(key []byte, data []byte, expiration time.Time) error
// Get returns the value for the key if present.
Get(key []byte) (data []byte, found bool)
// Delete deletes a key-value pair.
Delete(key []byte)
// ExpireKeys is called to delete all keys that are marked for expiration.
ExpireKeys()
// Count returns the total number of records stored.
Count() uint64
// Iterate iterates over all records.
Iterate(callback func(key, value []byte))
}