This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,534 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,6 @@ build | |
|
||
# Mock files | ||
*.mock_test.go | ||
|
||
# BoltDB default database file | ||
.jackal.db |
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,79 @@ | ||
// Copyright 2022 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 boltdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
type boltDBBlockListRep struct { | ||
tx *bolt.Tx | ||
} | ||
|
||
func (r *boltDBBlockListRep) UpsertBlockListItem(_ context.Context, item *blocklistmodel.Item) error { | ||
op := upsertKeyOp{ | ||
tx: r.tx, | ||
bucket: blockListBucket(item.Username), | ||
key: item.Jid, | ||
obj: item, | ||
} | ||
return op.do() | ||
} | ||
|
||
func (r *boltDBBlockListRep) DeleteBlockListItem(_ context.Context, item *blocklistmodel.Item) error { | ||
op := delKeyOp{ | ||
tx: r.tx, | ||
bucket: blockListBucket(item.Username), | ||
key: item.Jid, | ||
} | ||
return op.do() | ||
} | ||
|
||
func (r *boltDBBlockListRep) FetchBlockListItems(_ context.Context, username string) ([]*blocklistmodel.Item, error) { | ||
var retVal []*blocklistmodel.Item | ||
|
||
op := iterKeysOp{ | ||
tx: r.tx, | ||
bucket: blockListBucket(username), | ||
iterFn: func(_, b []byte) error { | ||
var item blocklistmodel.Item | ||
if err := item.UnmarshalBinary(b); err != nil { | ||
return err | ||
} | ||
retVal = append(retVal, &item) | ||
return nil | ||
}, | ||
} | ||
if err := op.do(); err != nil { | ||
return nil, err | ||
} | ||
return retVal, nil | ||
} | ||
|
||
func (r *boltDBBlockListRep) DeleteBlockListItems(_ context.Context, username string) error { | ||
op := delBucketOp{ | ||
tx: r.tx, | ||
bucket: blockListBucket(username), | ||
} | ||
return op.do() | ||
} | ||
|
||
func blockListBucket(username string) string { | ||
return fmt.Sprintf("blocklist:%s", username) | ||
} |
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,130 @@ | ||
// Copyright 2022 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 boltdb | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist" | ||
"github.com/stretchr/testify/require" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
func TestBoltDB_UpsertAndFetchBlockListItems(t *testing.T) { | ||
t.Parallel() | ||
|
||
db := setupDB(t) | ||
t.Cleanup(func() { cleanUp(db) }) | ||
|
||
err := db.Update(func(tx *bolt.Tx) error { | ||
rep := boltDBBlockListRep{tx: tx} | ||
|
||
err := rep.UpsertBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = rep.UpsertBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
items, err := rep.FetchBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
require.Len(t, items, 2) | ||
|
||
require.Equal(t, "[email protected]", items[0].Jid) | ||
require.Equal(t, "[email protected]", items[1].Jid) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestBoltDB_DeleteBlockListItem(t *testing.T) { | ||
t.Parallel() | ||
|
||
db := setupDB(t) | ||
t.Cleanup(func() { cleanUp(db) }) | ||
|
||
err := db.Update(func(tx *bolt.Tx) error { | ||
rep := boltDBBlockListRep{tx: tx} | ||
|
||
err := rep.UpsertBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
items, err := rep.FetchBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
require.Len(t, items, 1) | ||
|
||
err = rep.DeleteBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
items, err = rep.FetchBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
require.Len(t, items, 0) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestBoltDB_DeleteBlockListItems(t *testing.T) { | ||
t.Parallel() | ||
|
||
db := setupDB(t) | ||
t.Cleanup(func() { cleanUp(db) }) | ||
|
||
err := db.Update(func(tx *bolt.Tx) error { | ||
rep := boltDBBlockListRep{tx: tx} | ||
|
||
err := rep.UpsertBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = rep.UpsertBlockListItem(context.Background(), &blocklistmodel.Item{ | ||
Username: "ortuman", | ||
Jid: "[email protected]", | ||
}) | ||
require.NoError(t, err) | ||
|
||
items, err := rep.FetchBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
require.Len(t, items, 2) | ||
|
||
err = rep.DeleteBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
items, err = rep.FetchBlockListItems(context.Background(), "ortuman") | ||
require.NoError(t, err) | ||
|
||
require.Len(t, items, 0) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
} |
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,70 @@ | ||
// Copyright 2022 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 boltdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
capsmodel "github.com/ortuman/jackal/pkg/model/caps" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
const capsKey = "caps" | ||
|
||
type boltDBCapsRep struct { | ||
tx *bolt.Tx | ||
} | ||
|
||
func (r *boltDBCapsRep) UpsertCapabilities(_ context.Context, caps *capsmodel.Capabilities) error { | ||
op := upsertKeyOp{ | ||
tx: r.tx, | ||
bucket: capsBucketKey(caps.Node, caps.Ver), | ||
key: capsKey, | ||
obj: caps, | ||
} | ||
return op.do() | ||
} | ||
|
||
func (r *boltDBCapsRep) CapabilitiesExist(_ context.Context, node, ver string) (bool, error) { | ||
op := bucketExistsOp{ | ||
tx: r.tx, | ||
bucket: capsBucketKey(node, ver), | ||
} | ||
return op.do(), nil | ||
} | ||
|
||
func (r *boltDBCapsRep) FetchCapabilities(_ context.Context, node, ver string) (*capsmodel.Capabilities, error) { | ||
op := fetchKeyOp{ | ||
tx: r.tx, | ||
bucket: capsBucketKey(node, ver), | ||
key: capsKey, | ||
obj: &capsmodel.Capabilities{}, | ||
} | ||
obj, err := op.do() | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch { | ||
case obj != nil: | ||
return obj.(*capsmodel.Capabilities), nil | ||
default: | ||
return nil, nil | ||
} | ||
} | ||
|
||
func capsBucketKey(node, ver string) string { | ||
return fmt.Sprintf("caps:%s:%s", node, ver) | ||
} |
Oops, something went wrong.