Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Jul 23, 2024
1 parent 59934d1 commit c403c73
Show file tree
Hide file tree
Showing 33 changed files with 11,302 additions and 12,051 deletions.
14,866 changes: 7,433 additions & 7,433 deletions cmd/hidden_lake/_test/result/coverage.out

Large diffs are not rendered by default.

595 changes: 301 additions & 294 deletions cmd/hidden_lake/_test/result/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ func (p *sApp) Run(pCtx context.Context) error {
adapted.NewAdaptedConsumer(
p.fPostID,
p.fSettings,
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: (1 << 10),
}),
),
lru.NewLRUCache(1<<10),
),
p.fStdfLogger,
hlt_client.NewClient(
Expand Down
6 changes: 1 addition & 5 deletions cmd/hidden_lake/adapters/common/cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ var (

func initDB() database.IKVDatabase {
var err error
db, err = database.NewKVDatabase(
database.NewSettings(&database.SSettings{
FPath: databasePath,
}),
)
db, err = database.NewKVDatabase(databasePath)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ func (p *sApp) Run(pCtx context.Context) error {
}
defer func() { _ = p.fState.Disable(nil) }()

kvDB, err := database.NewKVDatabase(
database.NewSettings(&database.SSettings{
FPath: settings.CPathDB,
}),
)
kvDB, err := database.NewKVDatabase(settings.CPathDB)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type sKeyValueDB struct {
fDB database.IKVDatabase
}

func NewKeyValueDB(pSettings database.ISettings) (IKVDatabase, error) {
db, err := database.NewKVDatabase(pSettings)
func NewKeyValueDB(pPath string) (IKVDatabase, error) {
db, err := database.NewKVDatabase(pPath)
if err != nil {
return nil, utils.MergeErrors(ErrCreateDB, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/database"
testutils "github.com/number571/go-peer/test/utils"
)

Expand All @@ -31,9 +30,7 @@ func TestDatabase(t *testing.T) {
os.RemoveAll(tcPath)
defer os.RemoveAll(tcPath)

db, err := NewKeyValueDB(database.NewSettings(&database.SSettings{
FPath: tcPath,
}))
db, err := NewKeyValueDB(tcPath)
if err != nil {
t.Error(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import (

hlm_database "github.com/number571/go-peer/cmd/hidden_lake/applications/messenger/internal/database"
hlm_settings "github.com/number571/go-peer/cmd/hidden_lake/applications/messenger/pkg/settings"
"github.com/number571/go-peer/pkg/database"
)

func (p *sApp) initDatabase() error {
sett := database.NewSettings(&database.SSettings{
FPath: filepath.Join(p.fPathTo, hlm_settings.CPathDB),
})
db, err := hlm_database.NewKeyValueDB(sett)
path := filepath.Join(p.fPathTo, hlm_settings.CPathDB)
db, err := hlm_database.NewKeyValueDB(path)
if err != nil {
return fmt.Errorf("open KV database: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ type sDatabase struct {
}

func NewDatabase(pSett ISettings) (IDatabase, error) {
kvDB, err := database.NewKVDatabase(
database.NewSettings(&database.SSettings{
FPath: pSett.GetPath(),
}),
)
kvDB, err := database.NewKVDatabase(pSett.GetPath())
if err != nil {
return nil, utils.MergeErrors(ErrCreateDB, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ type sInMemoryDatabase struct {
func NewInMemoryDatabase(pSett ISettings) (IDatabase, error) {
return &sInMemoryDatabase{
fSettings: pSett,
fLRUCache: lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: pSett.GetMessagesCapacity(),
}),
),
fLRUCache: lru.NewLRUCache(pSett.GetMessagesCapacity()),
}, nil
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/hidden_lake/helpers/traffic/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ func testRunService(db database.IDatabase, addr string, addrNode string) (*http.
conn.NewVSettings(&conn.SVSettings{
FNetworkKey: testutils.TCNetworkKey,
}),
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: testutils.TCCapacity,
}),
),
lru.NewLRUCache(testutils.TCCapacity),
)

logger := logger.NewLogger(
Expand Down Expand Up @@ -202,10 +198,6 @@ func testNewNetworkNode(addr string) network.INode {
conn.NewVSettings(&conn.SVSettings{
FNetworkKey: testutils.TCNetworkKey,
}),
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: testutils.TCCapacity,
}),
),
lru.NewLRUCache(testutils.TCCapacity),
)
}
6 changes: 1 addition & 5 deletions cmd/hidden_lake/helpers/traffic/pkg/app/init_network_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func (p *sApp) initNetworkNode(pDatabase database.IDatabase) {
conn.NewVSettings(&conn.SVSettings{
FNetworkKey: cfgSettings.GetNetworkKey(),
}),
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: hls_settings.CNetworkQueueCapacity,
}),
),
lru.NewLRUCache(hls_settings.CNetworkQueueCapacity),
).HandleFunc(
hls_settings.CNetworkMask,
handler.HandleServiceTCP(p.fConfig, pDatabase, p.fAnonLogger),
Expand Down
12 changes: 2 additions & 10 deletions cmd/hidden_lake/service/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,7 @@ func testRunNewNode(dbPath, addr string) (anonymity.INode, context.Context, cont
}

func testNewNode(dbPath, addr string) anonymity.INode {
db, err := database.NewKVDatabase(
database.NewSettings(&database.SSettings{
FPath: dbPath,
}),
)
db, err := database.NewKVDatabase(dbPath)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -243,10 +239,6 @@ func testNewNetworkNode(addr string) network.INode {
}),
}),
conn.NewVSettings(&conn.SVSettings{}),
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: testutils.TCCapacity,
}),
),
lru.NewLRUCache(testutils.TCCapacity),
)
}
13 changes: 3 additions & 10 deletions cmd/hidden_lake/service/pkg/app/init_anon_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ func (p *sApp) initAnonNode() error {
cfgSettings = cfg.GetSettings()
)

kvDatabase, err := database.NewKVDatabase(
database.NewSettings(&database.SSettings{
FPath: filepath.Join(p.fPathTo, hls_settings.CPathDB),
}),
)
dbPath := filepath.Join(p.fPathTo, hls_settings.CPathDB)
kvDatabase, err := database.NewKVDatabase(dbPath)
if err != nil {
return utils.MergeErrors(ErrOpenKVDatabase, err)
}
Expand Down Expand Up @@ -75,11 +72,7 @@ func (p *sApp) initAnonNode() error {
conn.NewVSettings(&conn.SVSettings{
FNetworkKey: cfgSettings.GetNetworkKey(),
}),
lru.NewLRUCache(
lru.NewSettings(&lru.SSettings{
FCapacity: hls_settings.CNetworkQueueCapacity,
}),
),
lru.NewLRUCache(hls_settings.CNetworkQueueCapacity),
),
queue.NewMessageQueueProcessor(
queue.NewSettings(&queue.SSettings{
Expand Down
20 changes: 7 additions & 13 deletions pkg/cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@ var (
)

type sLRUCache struct {
fSettings ISettings
fMutex sync.RWMutex
fMap map[string][]byte
fQueue []string
fIndex uint64
fMutex sync.RWMutex
fMap map[string][]byte
fQueue []string
fIndex uint64
}

func NewLRUCache(pSettings ISettings) ILRUCache {
func NewLRUCache(pCapacity uint64) ILRUCache {
return &sLRUCache{
fSettings: pSettings,
fQueue: make([]string, pSettings.GetCapacity()),
fMap: make(map[string][]byte, pSettings.GetCapacity()),
fQueue: make([]string, pCapacity),
fMap: make(map[string][]byte, pCapacity),
}
}

func (p *sLRUCache) GetSettings() ISettings {
return p.fSettings
}

func (p *sLRUCache) GetIndex() uint64 {
p.fMutex.RLock()
defer p.fMutex.RUnlock()
Expand Down
34 changes: 1 addition & 33 deletions pkg/cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,10 @@ import (
"github.com/number571/go-peer/pkg/encoding"
)

func TestSettings(t *testing.T) {
t.Parallel()

for i := 0; i < 1; i++ {
testSettings(t, i)
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{})
default:
}
}

func TestLRUCache(t *testing.T) {
t.Parallel()

lruCache := NewLRUCache(
NewSettings(&SSettings{
FCapacity: 3,
}),
)

sett := lruCache.GetSettings()
if sett.GetCapacity() != 3 {
t.Error("got invalid value from settings")
return
}
lruCache := NewLRUCache(3)

if _, ok := lruCache.Get([]byte("unknown-key")); ok {
t.Error("success load unknown key")
Expand Down
27 changes: 0 additions & 27 deletions pkg/cache/lru/settings.go

This file was deleted.

5 changes: 0 additions & 5 deletions pkg/cache/lru/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import "github.com/number571/go-peer/pkg/cache"

type ILRUCache interface {
cache.ICache
GetSettings() ISettings

GetIndex() uint64
GetKey(i uint64) ([]byte, bool)
}

type ISettings interface {
GetCapacity() uint64
}
16 changes: 4 additions & 12 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@ const (
)

type sKVDatabase struct {
fSettings ISettings
fDB *bbolt.DB
fDB *bbolt.DB
}

func NewKVDatabase(pSett ISettings) (IKVDatabase, error) {
db, err := bbolt.Open(pSett.GetPath(), 0600, &bbolt.Options{})
func NewKVDatabase(pPath string) (IKVDatabase, error) {
db, err := bbolt.Open(pPath, 0600, &bbolt.Options{})
if err != nil {
return nil, utils.MergeErrors(ErrOpenDB, err)
}
return &sKVDatabase{
fSettings: pSett,
fDB: db,
}, nil
}

func (p *sKVDatabase) GetSettings() ISettings {
return p.fSettings
return &sKVDatabase{db}, nil
}

func (p *sKVDatabase) Set(pKey []byte, pValue []byte) error {
Expand Down
Loading

0 comments on commit c403c73

Please sign in to comment.