github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dsess/doc.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     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 dsess
    16  
    17  /*
    18  
    19  The dsess package is responsible for storing the state of every database in each session.
    20  
    21  The major players in this process are:
    22  
    23  * sqle.Database: The database implementation we provide to integrate with go-mysql-server's interface is mostly a
    24  	wrapper to provide access to the actual storage of tables and rows that are held by dsess.Session.
    25  * sqle.DatabaseProvider: Responsible for creating a new sqle.Database for each database name asked for by the engine,
    26  	as well as for managing the details of replication on the databases it returns.
    27  * dsess.Session: Responsible for maintaining the state of each session, including the data access for any row data.
    28  	Each physical dolt database in the provider can have the state of multiple branch heads managed by a session. This
    29  	state is loaded on demand from the provider as the client asks for different databases by name, as `dolt_checkout`
    30    is called, etc.
    31  * dsess.DoltTransaction: Records a start state (noms root) for each database managed by the transaction. Responsible
    32  	for committing new data as the result of a COMMIT or dolt_commit() by merging this start state with session changes
    33  	as appropriate.
    34  
    35  The rough flow of data between the engine and this package:
    36  
    37  1) START TRANSACTION calls dsess.Session.StartTransaction() to create a new dsess.DoltTransaction. This transaction
    38  	takes a snapshot of the current noms root for each database known to the provider and records these as part of the
    39  	transaction. This method clears out all cached state.
    40  2) The engine calls DatabaseProvider.Database() to get a sqle.Database for each database name included in a query,
    41  	including statements like `USE db`.
    42  3) Databases have access to tables, views, and other schema elements that they provide to the engine upon request as
    43  	part of query analysis, row iteration, etc. As a rule, this data is loaded from the session when asked for. Databases,
    44  	tables, views, and other structures in the sqle package are best understood as pass-through entities that always
    45  	defer to the session for their actual data.
    46  4) When actual data is required, a table or other schema element asks the session for the data. The primary interface
    47  	for this exchange is Session.LookupDbState(), which takes a database name.
    48  5) Eventually, the client session issues a COMMIT or DOLT_COMMIT() statement. This calls Session.CommitTransaction(),
    49  	which enforces business logic rules around the commit and then calls DoltTransaction.Commit() to persist the changes.
    50  
    51  Databases managed by the provider and the session can be referred to by either a base name (myDb) or a fully qualified
    52  name (myDb/myBranch). The details of this are a little bit subtle:
    53  
    54  * Database names that aren't qualified with a revision specifier resolve to either a) the default branch head, or
    55  	b) whatever branch head was last checked out with dolt_checkout(). Changing the branch head referred to by an
    56  	unqualified database name is the primary purpose of dolt_checkout().
    57  * `mydb/branch` always resolves to that branch head, as it existed at transaction start
    58  * Database names exposed to the engine are always `mydb`, never `mydb/branch`. This includes the result of
    59  	`select database()`. This is because the engine expects base database names when e.g. checking GRANTs, returning
    60  	information the information schema table, etc.
    61  * sqle.Database has an external name it exposes to the engine via Name(), as well as an internal name that includes a
    62  	revision qualifier, RevisionQualifiedName(). The latter should always be used internally when accessing session data,
    63  	including rows and all other table data. It's only appropriate to use an unqualified database name when you want
    64    the current checked out HEAD.
    65  
    66  It's possible to alter the data on multiple HEADS in a single session, but we currently restrict the users to
    67  committing a single one. It doesn't need to be the checked out head -- we simply look for a single dirty branch head
    68  state and commit that one. If there is more than one, it's an error. We may allow multiple branch heads to be updated
    69  in a single transaction in the future.
    70  
    71  */