github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/sqlutil/single_partition.go (about)

     1  // Copyright 2020 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 sqlutil
    16  
    17  import (
    18  	"io"
    19  	"sync"
    20  
    21  	"github.com/dolthub/dolt/go/store/types"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  )
    25  
    26  var _ sql.Partition = SinglePartition{}
    27  
    28  type SinglePartition struct {
    29  	RowData types.Map
    30  }
    31  
    32  // Key returns the key for this partition, which must uniquely identity the partition. We have only a single partition
    33  // per table, so we use a constant.
    34  func (sp SinglePartition) Key() []byte {
    35  	return []byte("single")
    36  }
    37  
    38  var _ sql.PartitionIter = SinglePartitionIter{}
    39  
    40  type SinglePartitionIter struct {
    41  	once    *sync.Once
    42  	RowData types.Map
    43  }
    44  
    45  func NewSinglePartitionIter(rowData types.Map) SinglePartitionIter {
    46  	return SinglePartitionIter{&sync.Once{}, rowData}
    47  }
    48  
    49  // Close is required by the sql.PartitionIter interface. Does nothing.
    50  func (itr SinglePartitionIter) Close(*sql.Context) error {
    51  	return nil
    52  }
    53  
    54  // Next returns the next partition if there is one, or io.EOF if there isn't.
    55  func (itr SinglePartitionIter) Next() (sql.Partition, error) {
    56  	first := false
    57  	itr.once.Do(func() {
    58  		first = true
    59  	})
    60  
    61  	if !first {
    62  		return nil, io.EOF
    63  	}
    64  
    65  	return SinglePartition{itr.RowData}, nil
    66  }