github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/schema_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package memex
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  )
    21  
    22  // generateKeys4Schema will generate keys for a given schemaReplicant. Used only in this file.
    23  func generateKeys4Schema(schemaReplicant *Schema) {
    24  	keyCount := len(schemaReplicant.DeferredCausets) - 1
    25  	keys := make([]KeyInfo, 0, keyCount)
    26  	for i := 0; i < keyCount; i++ {
    27  		keys = append(keys, []*DeferredCauset{schemaReplicant.DeferredCausets[i]})
    28  	}
    29  	schemaReplicant.Keys = keys
    30  }
    31  
    32  // generateSchema will generate a schemaReplicant for test. Used only in this file.
    33  func (s *testEvalSuite) generateSchema(defCausCount int) *Schema {
    34  	defcaus := make([]*DeferredCauset, 0, defCausCount)
    35  	for i := 0; i < defCausCount; i++ {
    36  		defcaus = append(defcaus, &DeferredCauset{
    37  			UniqueID: s.allocDefCausID(),
    38  		})
    39  	}
    40  	return NewSchema(defcaus...)
    41  }
    42  
    43  func (s *testEvalSuite) TestSchemaString(c *C) {
    44  	schemaReplicant := s.generateSchema(5)
    45  	c.Assert(schemaReplicant.String(), Equals, "DeferredCauset: [DeferredCauset#1,DeferredCauset#2,DeferredCauset#3,DeferredCauset#4,DeferredCauset#5] Unique key: []")
    46  	generateKeys4Schema(schemaReplicant)
    47  	c.Assert(schemaReplicant.String(), Equals, "DeferredCauset: [DeferredCauset#1,DeferredCauset#2,DeferredCauset#3,DeferredCauset#4,DeferredCauset#5] Unique key: [[DeferredCauset#1],[DeferredCauset#2],[DeferredCauset#3],[DeferredCauset#4]]")
    48  }
    49  
    50  func (s *testEvalSuite) TestSchemaRetrieveDeferredCauset(c *C) {
    51  	schemaReplicant := s.generateSchema(5)
    52  	defCausOutSchema := &DeferredCauset{
    53  		UniqueID: 100,
    54  	}
    55  	for _, defCaus := range schemaReplicant.DeferredCausets {
    56  		c.Assert(schemaReplicant.RetrieveDeferredCauset(defCaus), Equals, defCaus)
    57  	}
    58  	c.Assert(schemaReplicant.RetrieveDeferredCauset(defCausOutSchema), IsNil)
    59  }
    60  
    61  func (s *testEvalSuite) TestSchemaIsUniqueKey(c *C) {
    62  	schemaReplicant := s.generateSchema(5)
    63  	generateKeys4Schema(schemaReplicant)
    64  	defCausOutSchema := &DeferredCauset{
    65  		UniqueID: 100,
    66  	}
    67  	for i, defCaus := range schemaReplicant.DeferredCausets {
    68  		if i < len(schemaReplicant.DeferredCausets)-1 {
    69  			c.Assert(schemaReplicant.IsUniqueKey(defCaus), Equals, true)
    70  		} else {
    71  			c.Assert(schemaReplicant.IsUniqueKey(defCaus), Equals, false)
    72  		}
    73  	}
    74  	c.Assert(schemaReplicant.IsUniqueKey(defCausOutSchema), Equals, false)
    75  }
    76  
    77  func (s *testEvalSuite) TestSchemaContains(c *C) {
    78  	schemaReplicant := s.generateSchema(5)
    79  	defCausOutSchema := &DeferredCauset{
    80  		UniqueID: 100,
    81  	}
    82  	for _, defCaus := range schemaReplicant.DeferredCausets {
    83  		c.Assert(schemaReplicant.Contains(defCaus), Equals, true)
    84  	}
    85  	c.Assert(schemaReplicant.Contains(defCausOutSchema), Equals, false)
    86  }
    87  
    88  func (s *testEvalSuite) TestSchemaDeferredCausetsIndices(c *C) {
    89  	schemaReplicant := s.generateSchema(5)
    90  	defCausOutSchema := &DeferredCauset{
    91  		UniqueID: 100,
    92  	}
    93  	for i := 0; i < len(schemaReplicant.DeferredCausets)-1; i++ {
    94  		defCausIndices := schemaReplicant.DeferredCausetsIndices([]*DeferredCauset{schemaReplicant.DeferredCausets[i], schemaReplicant.DeferredCausets[i+1]})
    95  		for j, res := range defCausIndices {
    96  			c.Assert(res, Equals, i+j)
    97  		}
    98  	}
    99  	c.Assert(schemaReplicant.DeferredCausetsIndices([]*DeferredCauset{schemaReplicant.DeferredCausets[0], schemaReplicant.DeferredCausets[1], defCausOutSchema, schemaReplicant.DeferredCausets[2]}), IsNil)
   100  }
   101  
   102  func (s *testEvalSuite) TestSchemaDeferredCausetsByIndices(c *C) {
   103  	schemaReplicant := s.generateSchema(5)
   104  	indices := []int{0, 1, 2, 3}
   105  	retDefCauss := schemaReplicant.DeferredCausetsByIndices(indices)
   106  	for i, ret := range retDefCauss {
   107  		c.Assert(fmt.Sprintf("%p", schemaReplicant.DeferredCausets[i]), Equals, fmt.Sprintf("%p", ret))
   108  	}
   109  }
   110  
   111  func (s *testEvalSuite) TestSchemaMergeSchema(c *C) {
   112  	lSchema := s.generateSchema(5)
   113  	generateKeys4Schema(lSchema)
   114  
   115  	rSchema := s.generateSchema(5)
   116  	generateKeys4Schema(rSchema)
   117  
   118  	c.Assert(MergeSchema(nil, nil), IsNil)
   119  	c.Assert(MergeSchema(lSchema, nil).String(), Equals, lSchema.String())
   120  	c.Assert(MergeSchema(nil, rSchema).String(), Equals, rSchema.String())
   121  
   122  	schemaReplicant := MergeSchema(lSchema, rSchema)
   123  	for i := 0; i < len(lSchema.DeferredCausets); i++ {
   124  		c.Assert(schemaReplicant.DeferredCausets[i].UniqueID, Equals, lSchema.DeferredCausets[i].UniqueID)
   125  	}
   126  	for i := 0; i < len(rSchema.DeferredCausets); i++ {
   127  		c.Assert(schemaReplicant.DeferredCausets[i+len(lSchema.DeferredCausets)].UniqueID, Equals, rSchema.DeferredCausets[i].UniqueID)
   128  	}
   129  }
   130  
   131  func (s *testEvalSuite) TestGetUsedList(c *C) {
   132  	schemaReplicant := s.generateSchema(5)
   133  	var usedDefCauss []*DeferredCauset
   134  	usedDefCauss = append(usedDefCauss, schemaReplicant.DeferredCausets[3])
   135  	usedDefCauss = append(usedDefCauss, s.generateSchema(2).DeferredCausets...)
   136  	usedDefCauss = append(usedDefCauss, schemaReplicant.DeferredCausets[1])
   137  	usedDefCauss = append(usedDefCauss, schemaReplicant.DeferredCausets[3])
   138  
   139  	used := GetUsedList(usedDefCauss, schemaReplicant)
   140  	c.Assert(used, DeepEquals, []bool{false, true, false, true, false})
   141  }