github.com/matrixorigin/matrixone@v1.2.0/pkg/container/hashtable/bucket.go (about)

     1  // Copyright 2021 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 hashtable
    16  
    17  import (
    18  	"unsafe"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  )
    22  
    23  func (ht *StringHashMap) InsertStringBatchInBucket(states [][3]uint64, keys [][]byte, values []uint64, ibucket, nbucket uint64, m *mpool.MPool) error {
    24  	if err := ht.ResizeOnDemand(uint64(len(keys)), m); err != nil {
    25  		return err
    26  	}
    27  
    28  	BytesBatchGenHashStates(&keys[0], &states[0], len(keys))
    29  
    30  	for i := range keys {
    31  		if states[i][1]%nbucket != ibucket {
    32  			values[i] = 0
    33  			continue
    34  		}
    35  		cell := ht.findCell(&states[i])
    36  		if cell.Mapped == 0 {
    37  			ht.elemCnt++
    38  			cell.HashState = states[i]
    39  			cell.Mapped = ht.elemCnt
    40  		}
    41  		values[i] = cell.Mapped
    42  	}
    43  	return nil
    44  }
    45  
    46  /*
    47  func (ht *StringHashMap) InsertString24BatchInBucket(states [][3]uint64, keys [][3]uint64, values []uint64, ibucket, nbucket uint64) {
    48  	ht.resizeOnDemand(uint64(len(keys)))
    49  
    50  	AesInt192BatchGenHashStates(&keys[0], &states[0], len(keys))
    51  
    52  	for i := range keys {
    53  		if states[i][0]%nbucket != ibucket {
    54  			continue
    55  		}
    56  		cell := ht.findCell(&states[i])
    57  		if cell.Mapped == 0 {
    58  			ht.elemCnt++
    59  			cell.HashState = states[i]
    60  			cell.Mapped = ht.elemCnt
    61  		}
    62  		values[i] = cell.Mapped
    63  	}
    64  }
    65  
    66  func (ht *StringHashMap) InsertString32BatchInBucket(states [][3]uint64, keys [][4]uint64, values []uint64, ibucket, nbucket uint64) {
    67  	ht.resizeOnDemand(uint64(len(keys)))
    68  
    69  	AesInt256BatchGenHashStates(&keys[0], &states[0], len(keys))
    70  
    71  	for i := range keys {
    72  		if states[i][0]%nbucket != ibucket {
    73  			continue
    74  		}
    75  		cell := ht.findCell(&states[i])
    76  		if cell.Mapped == 0 {
    77  			ht.elemCnt++
    78  			cell.HashState = states[i]
    79  			cell.Mapped = ht.elemCnt
    80  		}
    81  		values[i] = cell.Mapped
    82  	}
    83  }
    84  
    85  func (ht *StringHashMap) InsertString40BatchInBucket(states [][3]uint64, keys [][5]uint64, values []uint64, ibucket, nbucket uint64) {
    86  	ht.resizeOnDemand(uint64(len(keys)))
    87  
    88  	AesInt320BatchGenHashStates(&keys[0], &states[0], len(keys))
    89  
    90  	for i := range keys {
    91  		if states[i][0]%nbucket != ibucket {
    92  			continue
    93  		}
    94  		cell := ht.findCell(&states[i])
    95  		if cell.Mapped == 0 {
    96  			ht.elemCnt++
    97  			cell.HashState = states[i]
    98  			cell.Mapped = ht.elemCnt
    99  		}
   100  		values[i] = cell.Mapped
   101  	}
   102  }
   103  */
   104  
   105  func (ht *StringHashMap) InsertStringBatchWithRingInBucket(zValues []int64, states [][3]uint64, keys [][]byte, values []uint64, ibucket, nbucket uint64, m *mpool.MPool) error {
   106  	if err := ht.ResizeOnDemand(uint64(len(keys)), m); err != nil {
   107  		return err
   108  	}
   109  
   110  	BytesBatchGenHashStates(&keys[0], &states[0], len(keys))
   111  
   112  	for i := range keys {
   113  		if zValues[i] == 0 {
   114  			continue
   115  		}
   116  		if states[i][1]%nbucket != ibucket {
   117  			values[i] = 0
   118  			continue
   119  		}
   120  
   121  		cell := ht.findCell(&states[i])
   122  		if cell.Mapped == 0 {
   123  			ht.elemCnt++
   124  			cell.HashState = states[i]
   125  			cell.Mapped = ht.elemCnt
   126  		}
   127  		values[i] = cell.Mapped
   128  	}
   129  	return nil
   130  }
   131  
   132  /*
   133  func (ht *StringHashMap) InsertString24BatchWithRingInBucket(zValues []int64, states [][3]uint64, keys [][3]uint64, values []uint64, ibucket, nbucket uint64) {
   134  	ht.resizeOnDemand(uint64(len(keys)))
   135  
   136  	AesInt192BatchGenHashStates(&keys[0], &states[0], len(keys))
   137  
   138  	for i := range keys {
   139  		if zValues[i] == 0 {
   140  			continue
   141  		}
   142  		if states[i][0]%nbucket != ibucket {
   143  			continue
   144  		}
   145  
   146  		cell := ht.findCell(&states[i])
   147  		if cell.Mapped == 0 {
   148  			ht.elemCnt++
   149  			cell.HashState = states[i]
   150  			cell.Mapped = ht.elemCnt
   151  		}
   152  		values[i] = cell.Mapped
   153  	}
   154  }
   155  
   156  func (ht *StringHashMap) InsertString32BatchWithRingInBucket(zValues []int64, states [][3]uint64, keys [][4]uint64, values []uint64, ibucket, nbucket uint64) {
   157  	ht.resizeOnDemand(uint64(len(keys)))
   158  
   159  	AesInt256BatchGenHashStates(&keys[0], &states[0], len(keys))
   160  
   161  	for i := range keys {
   162  		if zValues[i] == 0 {
   163  			continue
   164  		}
   165  		if states[i][0]%nbucket != ibucket {
   166  			continue
   167  		}
   168  
   169  		cell := ht.findCell(&states[i])
   170  		if cell.Mapped == 0 {
   171  			ht.elemCnt++
   172  			cell.HashState = states[i]
   173  			cell.Mapped = ht.elemCnt
   174  		}
   175  		values[i] = cell.Mapped
   176  	}
   177  }
   178  
   179  func (ht *StringHashMap) InsertString40BatchWithRingInBucket(zValues []int64, states [][3]uint64, keys [][5]uint64, values []uint64, ibucket, nbucket uint64) {
   180  	ht.resizeOnDemand(uint64(len(keys)))
   181  
   182  	AesInt320BatchGenHashStates(&keys[0], &states[0], len(keys))
   183  
   184  	for i := range keys {
   185  		if zValues[i] == 0 {
   186  			continue
   187  		}
   188  		if states[i][0]%nbucket != ibucket {
   189  			continue
   190  		}
   191  
   192  		cell := ht.findCell(&states[i])
   193  		if cell.Mapped == 0 {
   194  			ht.elemCnt++
   195  			cell.HashState = states[i]
   196  			cell.Mapped = ht.elemCnt
   197  		}
   198  		values[i] = cell.Mapped
   199  	}
   200  }
   201  */
   202  
   203  func (ht *StringHashMap) FindStringBatchInBucket(states [][3]uint64, keys [][]byte, values []uint64, inBuckets []uint8, ibucket, nbucket uint64) {
   204  	BytesBatchGenHashStates(&keys[0], &states[0], len(keys))
   205  
   206  	for i := range keys {
   207  		if states[i][1]%nbucket != ibucket {
   208  			inBuckets[i] = 0 // mark of 0 means it is not in the processed bucket
   209  			continue
   210  		}
   211  		cell := ht.findCell(&states[i])
   212  		values[i] = cell.Mapped
   213  	}
   214  }
   215  
   216  func (ht *StringHashMap) FindStringBatchWithRingInBucket(states [][3]uint64, zValues []int64, keys [][]byte, values []uint64, inBuckets []uint8, ibucket, nbucket uint64) {
   217  	BytesBatchGenHashStates(&keys[0], &states[0], len(keys))
   218  
   219  	for i := range keys {
   220  		if states[i][1]%nbucket != ibucket {
   221  			inBuckets[i] = 0 // mark of 0 means it is not in the processed bucket
   222  			continue
   223  		}
   224  		if zValues[i] == 0 {
   225  			values[i] = 0
   226  			continue
   227  		}
   228  		cell := ht.findCell(&states[i])
   229  		values[i] = cell.Mapped
   230  	}
   231  }
   232  
   233  func (ht *Int64HashMap) InsertBatchInBucket(n int, hashes []uint64, keysPtr unsafe.Pointer, values []uint64, ibucket, nbucket uint64, m *mpool.MPool) error {
   234  	if err := ht.ResizeOnDemand(n, m); err != nil {
   235  		return err
   236  	}
   237  
   238  	if hashes[0] == 0 {
   239  		Int64BatchHash(keysPtr, &hashes[0], n)
   240  	}
   241  
   242  	for i, hash := range hashes {
   243  		if (hashes[i]>>16)%nbucket != ibucket {
   244  			values[i] = 0
   245  			continue
   246  		}
   247  		cell := ht.findCell(hash)
   248  		if cell.Mapped == 0 {
   249  			ht.elemCnt++
   250  			cell.Key = hash
   251  			cell.Mapped = ht.elemCnt
   252  		}
   253  		values[i] = cell.Mapped
   254  	}
   255  	return nil
   256  }
   257  
   258  func (ht *Int64HashMap) InsertBatchWithRingInBucket(n int, zValues []int64, hashes []uint64, keysPtr unsafe.Pointer, values []uint64, ibucket, nbucket uint64, m *mpool.MPool) error {
   259  	if err := ht.ResizeOnDemand(n, m); err != nil {
   260  		return err
   261  	}
   262  
   263  	if hashes[0] == 0 {
   264  		Int64BatchHash(keysPtr, &hashes[0], n)
   265  	}
   266  
   267  	for i, hash := range hashes {
   268  		if zValues[i] == 0 {
   269  			continue
   270  		}
   271  		if (hashes[i]>>16)%nbucket != ibucket {
   272  			values[i] = 0
   273  			continue
   274  		}
   275  		cell := ht.findCell(hash)
   276  		if cell.Mapped == 0 {
   277  			ht.elemCnt++
   278  			cell.Key = hash
   279  			cell.Mapped = ht.elemCnt
   280  		}
   281  		values[i] = cell.Mapped
   282  	}
   283  	return nil
   284  }
   285  
   286  func (ht *Int64HashMap) FindBatchInBucket(n int, hashes []uint64, keysPtr unsafe.Pointer, values []uint64, inBuckets []uint8, ibucket, nbucket uint64) {
   287  	if hashes[0] == 0 {
   288  		Int64BatchHash(keysPtr, &hashes[0], n)
   289  	}
   290  
   291  	for i, hash := range hashes {
   292  		if (hashes[i]>>16)%nbucket != ibucket {
   293  			inBuckets[i] = 0 // mark of 0 means it is not in the processed bucket
   294  			continue
   295  		}
   296  		cell := ht.findCell(hash)
   297  		values[i] = cell.Mapped
   298  	}
   299  }
   300  
   301  func (ht *Int64HashMap) FindBatchWithRingInBucket(n int, zValues []int64, hashes []uint64, keysPtr unsafe.Pointer, values []uint64, inBuckets []uint8, ibucket, nbucket uint64) {
   302  	if hashes[0] == 0 {
   303  		Int64BatchHash(keysPtr, &hashes[0], n)
   304  	}
   305  
   306  	for i, hash := range hashes {
   307  		if (hashes[i]>>16)%nbucket != ibucket {
   308  			inBuckets[i] = 0 // mark of 0 means it is not in the processed bucket
   309  			continue
   310  		}
   311  		if zValues[i] == 0 {
   312  			values[i] = 0
   313  			continue
   314  		}
   315  		cell := ht.findCell(hash)
   316  		values[i] = cell.Mapped
   317  	}
   318  }