forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for caps cached repository (ortuman#205)
* added support for caps cached repository * updated CHANGELOG.md
- Loading branch information
Showing
5 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2021 The jackal 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 cachedrepository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/golang/protobuf/proto" | ||
capsmodel "github.com/ortuman/jackal/pkg/model/caps" | ||
"github.com/ortuman/jackal/pkg/storage/repository" | ||
) | ||
|
||
type capsCodec struct { | ||
val *capsmodel.Capabilities | ||
} | ||
|
||
func (c *capsCodec) encode(i interface{}) ([]byte, error) { | ||
return proto.Marshal(i.(*capsmodel.Capabilities)) | ||
} | ||
|
||
func (c *capsCodec) decode(b []byte) error { | ||
var caps capsmodel.Capabilities | ||
if err := proto.Unmarshal(b, &caps); err != nil { | ||
return err | ||
} | ||
c.val = &caps | ||
return nil | ||
} | ||
|
||
func (c *capsCodec) value() interface{} { | ||
return c.val | ||
} | ||
|
||
type cachedCapsRep struct { | ||
c Cache | ||
rep repository.Capabilities | ||
} | ||
|
||
func (c *cachedCapsRep) UpsertCapabilities(ctx context.Context, caps *capsmodel.Capabilities) error { | ||
op := updateOp{ | ||
c: c.c, | ||
key: capsKey(caps.Node, caps.Ver), | ||
updateFn: func(ctx context.Context) error { | ||
return c.rep.UpsertCapabilities(ctx, caps) | ||
}, | ||
} | ||
return op.do(ctx) | ||
} | ||
|
||
func (c *cachedCapsRep) CapabilitiesExist(ctx context.Context, node, ver string) (bool, error) { | ||
op := existsOp{ | ||
c: c.c, | ||
key: capsKey(node, ver), | ||
missFn: func(ctx context.Context) (bool, error) { | ||
return c.rep.CapabilitiesExist(ctx, node, ver) | ||
}, | ||
} | ||
return op.do(ctx) | ||
} | ||
|
||
func (c *cachedCapsRep) FetchCapabilities(ctx context.Context, node, ver string) (*capsmodel.Capabilities, error) { | ||
op := fetchOp{ | ||
c: c.c, | ||
key: capsKey(node, ver), | ||
codec: &capsCodec{}, | ||
missFn: func(ctx context.Context) (interface{}, error) { | ||
return c.rep.FetchCapabilities(ctx, node, ver) | ||
}, | ||
} | ||
v, err := op.do(ctx) | ||
switch { | ||
case err != nil: | ||
return nil, err | ||
case v != nil: | ||
return v.(*capsmodel.Capabilities), nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func capsKey(node, ver string) string { | ||
return fmt.Sprintf("caps:%s:%s", node, ver) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2021 The jackal 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 cachedrepository | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
capsmodel "github.com/ortuman/jackal/pkg/model/caps" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCachedCapsRep_UpsertCaps(t *testing.T) { | ||
// given | ||
var cacheKey string | ||
|
||
cacheMock := &cacheMock{} | ||
cacheMock.DelFunc = func(ctx context.Context, k string) error { | ||
cacheKey = k | ||
return nil | ||
} | ||
|
||
repMock := &repositoryMock{} | ||
repMock.UpsertCapabilitiesFunc = func(ctx context.Context, caps *capsmodel.Capabilities) error { | ||
return nil | ||
} | ||
|
||
// when | ||
rep := cachedCapsRep{ | ||
c: cacheMock, | ||
rep: repMock, | ||
} | ||
err := rep.UpsertCapabilities(context.Background(), &capsmodel.Capabilities{ | ||
Node: "n1", | ||
Ver: "v1", | ||
Features: []string{"f0"}, | ||
}) | ||
|
||
// then | ||
require.NoError(t, err) | ||
require.Equal(t, capsKey("n1", "v1"), cacheKey) | ||
require.Len(t, repMock.UpsertCapabilitiesCalls(), 1) | ||
} | ||
|
||
func TestCachedCapsRep_CapsExist(t *testing.T) { | ||
// given | ||
cacheMock := &cacheMock{} | ||
cacheMock.HasKeyFunc = func(ctx context.Context, k string) (bool, error) { | ||
if k == capsKey("n1", "v1") { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
repMock := &repositoryMock{} | ||
repMock.CapabilitiesExistFunc = func(ctx context.Context, node, ver string) (bool, error) { | ||
return node == "n2" && ver == "v2", nil | ||
} | ||
|
||
// when | ||
rep := cachedCapsRep{ | ||
c: cacheMock, | ||
rep: repMock, | ||
} | ||
ok1, err1 := rep.CapabilitiesExist(context.Background(), "n1", "v1") | ||
ok2, err2 := rep.CapabilitiesExist(context.Background(), "n2", "v2") | ||
ok3, err3 := rep.CapabilitiesExist(context.Background(), "n3", "v3") | ||
|
||
// then | ||
require.True(t, ok1) | ||
require.NoError(t, err1) | ||
|
||
require.True(t, ok2) | ||
require.NoError(t, err2) | ||
|
||
require.False(t, ok3) | ||
require.NoError(t, err3) | ||
|
||
require.Len(t, repMock.CapabilitiesExistCalls(), 2) | ||
} | ||
|
||
func TestCachedCapsRep_FetchCaps(t *testing.T) { | ||
// given | ||
cacheMock := &cacheMock{} | ||
cacheMock.GetFunc = func(ctx context.Context, k string) ([]byte, error) { | ||
return nil, nil | ||
} | ||
cacheMock.PutFunc = func(ctx context.Context, k string, val []byte) error { | ||
return nil | ||
} | ||
|
||
repMock := &repositoryMock{} | ||
repMock.FetchCapabilitiesFunc = func(ctx context.Context, node, ver string) (*capsmodel.Capabilities, error) { | ||
return &capsmodel.Capabilities{ | ||
Node: "n1", | ||
Ver: "v1", | ||
}, nil | ||
} | ||
|
||
// when | ||
rep := cachedCapsRep{ | ||
c: cacheMock, | ||
rep: repMock, | ||
} | ||
caps, err := rep.FetchCapabilities(context.Background(), "n1", "v1") | ||
|
||
// then | ||
require.NotNil(t, caps) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "n1", caps.Node) | ||
require.Equal(t, "v1", caps.Ver) | ||
|
||
require.Len(t, cacheMock.GetCalls(), 1) | ||
require.Len(t, cacheMock.PutCalls(), 1) | ||
require.Len(t, repMock.FetchCapabilitiesCalls(), 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters