go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/txnBuf/context.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package txnBuf
    16  
    17  import (
    18  	"context"
    19  
    20  	ds "go.chromium.org/luci/gae/service/datastore"
    21  )
    22  
    23  var (
    24  	dsTxnBufParent   = "holds a *txnBufState of the parent transaction"
    25  	dsTxnBufHaveLock = "a boolean indicating that this context has the lock for this level of the transaction"
    26  )
    27  
    28  // FilterRDS installs a transaction buffer datastore filter in the context.
    29  func FilterRDS(c context.Context) context.Context {
    30  	// TODO(riannucci): allow the specification of the set of roots to limit this
    31  	// transaction to, transitively.
    32  	return ds.AddRawFilters(c, func(c context.Context, rds ds.RawInterface) ds.RawInterface {
    33  		if par, _ := c.Value(&dsTxnBufParent).(*txnBufState); par != nil {
    34  			haveLock, _ := c.Value(&dsTxnBufHaveLock).(bool)
    35  			return &dsTxnBuf{c, par, haveLock, rds}
    36  		}
    37  		return &dsBuf{rds}
    38  	})
    39  }
    40  
    41  // impossible is a marker function to indicate that the given error is an
    42  // impossible state, due to conditions outside of the function.
    43  func impossible(err error) {
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  }
    48  
    49  // memoryCorruption is a marker function to indicate that given error is
    50  // actually due to corrupted memory to make it easier to read the code.
    51  func memoryCorruption(err error) {
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  }