Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
added BoltDB repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman committed Feb 27, 2022
1 parent 414d2eb commit 3da1fbf
Show file tree
Hide file tree
Showing 21 changed files with 1,533 additions and 6 deletions.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ require (
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/jackal-xmpp/runqueue/v2 v2.0.0
github.com/jackal-xmpp/stravaganza v1.1.1
github.com/jackal-xmpp/stravaganza v1.2.1
github.com/kkyr/fig v0.2.0
github.com/lib/pq v1.8.0
github.com/mattn/go-sqlite3 v1.14.5 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.etcd.io/bbolt v1.3.5
go.etcd.io/etcd/client/v3 v3.5.1
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
)

require github.com/gogo/protobuf v1.3.2

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
Expand All @@ -42,6 +41,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down Expand Up @@ -69,3 +69,5 @@ require (
)

replace go.etcd.io/etcd/v3 => github.com/etcd-io/etcd/v3 v3.5.1

replace go.etcd.io/bbolt => github.com/etcd-io/bbolt v1.3.5
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/etcd-io/bbolt v1.3.5 h1:3Uslx5o2Ds0IBbRW/L7/kiV5oR/kcG85nSnLsvEdKQI=
github.com/etcd-io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
Expand Down Expand Up @@ -223,8 +225,8 @@ github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/jackal-xmpp/runqueue/v2 v2.0.0 h1:QfvOfL6zF5yK1LN5TKabpj+VBuELMwtR8Xpkz0CrjoI=
github.com/jackal-xmpp/runqueue/v2 v2.0.0/go.mod h1:tXZARVqBMGeV8BTc/qDPg0qXILTUWmER7wlYbN9Xcac=
github.com/jackal-xmpp/stravaganza v1.1.1 h1:P7mpUNc+B5d8TF+PErmiK9ayCo+AC+xIxQBEV1dD6Eo=
github.com/jackal-xmpp/stravaganza v1.1.1/go.mod h1:C2sH3I3kQQWsOs6+Cg+il0mzGPLnyy+QgVwTxbyJ5kk=
github.com/jackal-xmpp/stravaganza v1.2.1 h1:FDCWRhBmrKAxIBr4ix7Bugd557f18Lg+lTmPWom0g9A=
github.com/jackal-xmpp/stravaganza v1.2.1/go.mod h1:C2sH3I3kQQWsOs6+Cg+il0mzGPLnyy+QgVwTxbyJ5kk=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -414,7 +416,6 @@ github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZ
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd/api/v3 v3.5.1 h1:v28cktvBq+7vGyJXF8G+rWJmj+1XUmMtqcLnH8hDocM=
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.1 h1:XIQcHCFSG53bJETYeRJtIxdLv2EWRGxcfzR8lSnTH4E=
Expand Down Expand Up @@ -529,6 +530,7 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
79 changes: 79 additions & 0 deletions pkg/storage/boltdb/blocklist.go
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)
}
130 changes: 130 additions & 0 deletions pkg/storage/boltdb/blocklist_test.go
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)
}
70 changes: 70 additions & 0 deletions pkg/storage/boltdb/capabilities.go
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)
}
Loading

0 comments on commit 3da1fbf

Please sign in to comment.