github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/account.go (about)

     1  // Copyright 2022 Matrix Origin
     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 memorystorage
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/catalog"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine"
    22  )
    23  
    24  func (m *MemHandler) ensureAccount(
    25  	ctx context.Context,
    26  	tx *Transaction,
    27  	accessInfo memoryengine.AccessInfo,
    28  ) (
    29  	err error,
    30  ) {
    31  
    32  	// ensure catalog db exists for this account
    33  	keys, err := m.databases.Index(tx, Tuple{
    34  		index_AccountID_Name,
    35  		Uint(accessInfo.AccountID),
    36  		Text(catalog.MO_CATALOG),
    37  	})
    38  	if err != nil {
    39  		return err
    40  	}
    41  	if len(keys) == 0 {
    42  		// create one
    43  		id, err := m.idGenerator.NewID(ctx)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		db := &DatabaseRow{
    48  			ID:        id,
    49  			AccountID: accessInfo.AccountID,
    50  			Name:      []byte(catalog.MO_CATALOG),
    51  		}
    52  		err = m.databases.Insert(tx, db)
    53  		//TODO add a unique constraint on (AccountID, Name)
    54  		if err != nil {
    55  			return err
    56  		}
    57  	}
    58  
    59  	return
    60  }