Skip to content

Commit

Permalink
Make sure to use the checked possibly-nil pointer in memdb
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Schorr <josephschorr@users.noreply.github.com>
  • Loading branch information
josephschorr committed Oct 14, 2021
1 parent d926ca4 commit df88351
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
20 changes: 12 additions & 8 deletions internal/datastore/memdb/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const (
)

func (mds *memdbDatastore) WriteNamespace(ctx context.Context, newConfig *v0.NamespaceDefinition) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(true)
txn := db.Txn(true)
defer txn.Abort()

time.Sleep(mds.simulatedLatency)
Expand Down Expand Up @@ -79,11 +80,12 @@ func (mds *memdbDatastore) WriteNamespace(ctx context.Context, newConfig *v0.Nam

// ReadNamespace reads a namespace definition and version and returns it if found.
func (mds *memdbDatastore) ReadNamespace(ctx context.Context, nsName string) (*v0.NamespaceDefinition, datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return nil, datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(false)
txn := db.Txn(false)
defer txn.Abort()

time.Sleep(mds.simulatedLatency)
Expand All @@ -107,11 +109,12 @@ func (mds *memdbDatastore) ReadNamespace(ctx context.Context, nsName string) (*v
}

func (mds *memdbDatastore) DeleteNamespace(ctx context.Context, nsName string) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(true)
txn := db.Txn(true)
defer txn.Abort()

time.Sleep(mds.simulatedLatency)
Expand Down Expand Up @@ -165,13 +168,14 @@ func (mds *memdbDatastore) DeleteNamespace(ctx context.Context, nsName string) (
}

func (mds *memdbDatastore) ListNamespaces(ctx context.Context) ([]*v0.NamespaceDefinition, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return nil, fmt.Errorf("memdb closed")
}

var nsDefs []*v0.NamespaceDefinition

txn := mds.db.Txn(false)
txn := db.Txn(false)
defer txn.Abort()

it, err := txn.Get(tableNamespaceConfig, indexID)
Expand Down
5 changes: 3 additions & 2 deletions internal/datastore/memdb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ func iteratorForFilter(txn *memdb.Txn, filter *v1.RelationshipFilter) (memdb.Res
}

func (mtq memdbTupleQuery) Execute(ctx context.Context) (datastore.TupleIterator, error) {
if mtq.db == nil {
db := mtq.db
if db == nil {
return nil, fmt.Errorf("memdb closed")
}

txn := mtq.db.Txn(false)
txn := db.Txn(false)

time.Sleep(mtq.simulatedLatency)

Expand Down
7 changes: 6 additions & 1 deletion internal/datastore/memdb/reverse_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func (mtq memdbReverseTupleQuery) WithObjectRelation(namespaceName string, relat
}

func (mtq memdbReverseTupleQuery) Execute(ctx context.Context) (datastore.TupleIterator, error) {
txn := mtq.db.Txn(false)
db := mtq.db
if db == nil {
return nil, fmt.Errorf("memdb closed")
}

txn := db.Txn(false)

time.Sleep(mtq.simulatedLatency)

Expand Down
27 changes: 18 additions & 9 deletions internal/datastore/memdb/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ func (mds *memdbDatastore) checkPrecondition(txn *memdb.Txn, preconditions []*v1
}

func (mds *memdbDatastore) WriteTuples(ctx context.Context, preconditions []*v1.Precondition, mutations []*v1.RelationshipUpdate) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(true)
txn := db.Txn(true)
defer txn.Abort()

if err := mds.checkPrecondition(txn, preconditions); err != nil {
Expand Down Expand Up @@ -137,11 +138,12 @@ func (mds *memdbDatastore) write(ctx context.Context, txn *memdb.Txn, mutations
}

func (mds *memdbDatastore) DeleteRelationships(ctx context.Context, preconditions []*v1.Precondition, filter *v1.RelationshipFilter) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(true)
txn := db.Txn(true)
defer txn.Abort()

if err := mds.checkPrecondition(txn, preconditions); err != nil {
Expand Down Expand Up @@ -221,12 +223,13 @@ func (mds *memdbDatastore) ReverseQueryTuplesFromSubjectNamespace(subjectNamespa
}

func (mds *memdbDatastore) SyncRevision(ctx context.Context) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

// Compute the current revision
txn := mds.db.Txn(false)
txn := db.Txn(false)
defer txn.Abort()

lastRaw, err := txn.Last(tableChangelog, indexID)
Expand All @@ -240,11 +243,12 @@ func (mds *memdbDatastore) SyncRevision(ctx context.Context) (datastore.Revision
}

func (mds *memdbDatastore) Revision(ctx context.Context) (datastore.Revision, error) {
if mds.db == nil {
db := mds.db
if db == nil {
return datastore.NoRevision, fmt.Errorf("memdb closed")
}

txn := mds.db.Txn(false)
txn := db.Txn(false)
defer txn.Abort()

lowerBound := uint64(time.Now().Add(-1 * mds.revisionFuzzingTimedelta).UnixNano())
Expand All @@ -267,7 +271,12 @@ func (mds *memdbDatastore) Revision(ctx context.Context) (datastore.Revision, er
}

func (mds *memdbDatastore) CheckRevision(ctx context.Context, revision datastore.Revision) error {
txn := mds.db.Txn(false)
db := mds.db
if db == nil {
return fmt.Errorf("memdb closed")
}

txn := db.Txn(false)
defer txn.Abort()

// We need to know the highest possible revision
Expand Down

0 comments on commit df88351

Please sign in to comment.