Skip to content

Commit

Permalink
Add context to storage arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpppk committed Jul 23, 2019
1 parent 8811c53 commit 57ac4e9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ var genCmd = &cobra.Command{
//panicIfErrorExists(err)
//memoryStorage := storage.NewMemoryWithOtherStorage(boltStorage)
//iroha := lib.NewIroha(words, memoryStorage, config.DepthOptions)
ctx := context.Background()
serviceAccountFilePath := "iroha-247312-5f9b1522fbd5.json"
firestoreStorage, err := storage.NewFireStore(context.Background(), serviceAccountFilePath, config.DBPath)
firestoreStorage, err := storage.NewFireStore(ctx, serviceAccountFilePath, config.DBPath)
panicIfErrorExists(err)
memoryStorage := storage.NewMemoryWithOtherStorage(firestoreStorage)
iroha := lib.NewIroha(words, memoryStorage, config.DepthOptions)
Expand Down
6 changes: 4 additions & 2 deletions lib/iroha.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"context"
"fmt"
"math/bits"

Expand Down Expand Up @@ -95,6 +96,7 @@ func (i *Iroha) gf(word *ktkn.Word, usedIndices []int, katakanaBitsAndWords []*k
}

func (i *Iroha) searchByBits(usedIndices []int, katakanaBitsAndWords []*ktkn.KatakanaBitsAndWords, remainKatakanaBits ktkn.WordBits) ([][]*ktkn.Word, bool, error) {
ctx := context.Background()
remainKatakanaNum := bits.OnesCount64(uint64(remainKatakanaBits))
if remainKatakanaNum == int(ktkn.KatakanaLen) {
return [][]*ktkn.Word{{}}, true, nil
Expand All @@ -112,7 +114,7 @@ func (i *Iroha) searchByBits(usedIndices []int, katakanaBitsAndWords []*ktkn.Kat
depth := int(ktkn.KatakanaLen) - len(katakanaBitsAndWords) - 1

if depth <= i.depths.MaxStorage {
if results, ok, err := i.storage.Get(usedIndices); err != nil {
if results, ok, err := i.storage.Get(ctx, usedIndices); err != nil {
return nil, false, err
} else if ok {
i.log.PrintProgressLog(depth, "cache used")
Expand Down Expand Up @@ -185,7 +187,7 @@ func (i *Iroha) searchByBits(usedIndices []int, katakanaBitsAndWords []*ktkn.Kat

msg := ""
if depth <= i.depths.MaxStorage {
if err := i.storage.Set(usedIndices, irohaWordLists); err != nil {
if err := i.storage.Set(ctx, usedIndices, irohaWordLists); err != nil {
return nil, false, err
}
msg += "cache saved"
Expand Down
7 changes: 3 additions & 4 deletions storage/firestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewFireStore(ctx context.Context, filePath string, rootCollectionName strin
StorageClass: "STANDARD",
Location: "asia-northeast1",
}
// FIXME projectId
bucketClient, err := newCloudStorageClient(ctx, app, "iroha-247312", rootCollectionName, bucketAttrs)
if err != nil {
return nil, errors.Wrap(err, baseErrMsg+"failed to create bucket client")
Expand All @@ -58,8 +59,7 @@ func NewFireStore(ctx context.Context, filePath string, rootCollectionName strin
}, errors.Wrap(err, baseErrMsg+"failed to close firestore client")
}

func (f *FireStore) Set(indices []int, wordsList [][]*ktkn.Word) error {
ctx := context.Background()
func (f *FireStore) Set(ctx context.Context, indices []int, wordsList [][]*ktkn.Word) error {
wl := wordsList
if wl == nil {
wl = make([][]*ktkn.Word, 0)
Expand All @@ -77,7 +77,6 @@ func (f *FireStore) Set(indices []int, wordsList [][]*ktkn.Word) error {
return nil
}

func (f *FireStore) Get(indices []int) ([][]*ktkn.Word, bool, error) {
ctx := context.Background()
func (f *FireStore) Get(ctx context.Context, indices []int) ([][]*ktkn.Word, bool, error) {
return f.cstorageClient.Get(ctx, indices)
}
9 changes: 5 additions & 4 deletions storage/memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"context"
"sync"

"github.com/mpppk/iroha/ktkn"
Expand Down Expand Up @@ -49,15 +50,15 @@ func NewMemoryWithOtherStorage(storage Storage) *Memory {
return m
}

func (m *Memory) Set(indices []int, wordsList [][]*ktkn.Word) error {
func (m *Memory) Set(ctx context.Context, indices []int, wordsList [][]*ktkn.Word) error {
m.cache.Set(toStorageStrKey(indices), wordsList)
return m.otherStorage.Set(indices, wordsList)
return m.otherStorage.Set(ctx, indices, wordsList)
}

func (m *Memory) Get(indices []int) ([][]*ktkn.Word, bool, error) {
func (m *Memory) Get(ctx context.Context, indices []int) ([][]*ktkn.Word, bool, error) {
wordsList, ok := m.cache.Get(toStorageStrKey(indices))
if ok {
return wordsList, true, nil
}
return m.otherStorage.Get(indices)
return m.otherStorage.Get(ctx, indices)
}
6 changes: 4 additions & 2 deletions storage/none.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package storage

import (
"context"

"github.com/mpppk/iroha/ktkn"
)

type None struct{}

func (e *None) Get(indices []int) ([][]*ktkn.Word, bool, error) {
func (e *None) Get(ctx context.Context, indices []int) ([][]*ktkn.Word, bool, error) {
return nil, false, nil
}

func (e *None) Set(indices []int, wordsList [][]*ktkn.Word) error {
func (e *None) Set(ctx context.Context, indices []int, wordsList [][]*ktkn.Word) error {
return nil
}
5 changes: 3 additions & 2 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"context"
"fmt"
"strconv"

Expand All @@ -9,8 +10,8 @@ import (
)

type Storage interface {
Set(indices []int, wordsList [][]*ktkn.Word) error
Get(indices []int) ([][]*ktkn.Word, bool, error)
Set(ctx context.Context, indices []int, wordsList [][]*ktkn.Word) error
Get(ctx context.Context, indices []int) ([][]*ktkn.Word, bool, error)
}

func toStorageStrKey(indices []int) string {
Expand Down

0 comments on commit 57ac4e9

Please sign in to comment.