-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e8cd0f
commit be9ec89
Showing
26 changed files
with
601 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
fluss-client/src/main/java/com/alibaba/fluss/client/lookup/FlussPrefixLookuper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright (c) 2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.fluss.client.lookup; | ||
|
||
import com.alibaba.fluss.annotation.PublicEvolving; | ||
import com.alibaba.fluss.client.lakehouse.LakeTableBucketAssigner; | ||
import com.alibaba.fluss.client.metadata.MetadataUpdater; | ||
import com.alibaba.fluss.client.table.getter.PartitionGetter; | ||
import com.alibaba.fluss.client.write.HashBucketAssigner; | ||
import com.alibaba.fluss.metadata.PhysicalTablePath; | ||
import com.alibaba.fluss.metadata.TableBucket; | ||
import com.alibaba.fluss.metadata.TableInfo; | ||
import com.alibaba.fluss.row.InternalRow; | ||
import com.alibaba.fluss.row.encode.KeyEncoder; | ||
import com.alibaba.fluss.row.encode.ValueDecoder; | ||
import com.alibaba.fluss.types.RowType; | ||
import com.alibaba.fluss.utils.Preconditions; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* The default impl of {@link PrefixLookuper}. | ||
* | ||
* @since 0.6 | ||
*/ | ||
@PublicEvolving | ||
public class FlussPrefixLookuper implements PrefixLookuper { | ||
|
||
private final TableInfo tableInfo; | ||
|
||
private final MetadataUpdater metadataUpdater; | ||
|
||
private final RowType primaryKeyRowType; | ||
|
||
private final LookupClient lookupClient; | ||
|
||
/** Extract bucket key from prefix lookup key row. */ | ||
private final KeyEncoder bucketKeyEncoder; | ||
|
||
private final boolean isDataLakeEnable; | ||
|
||
private final int numBuckets; | ||
|
||
private @Nullable LakeTableBucketAssigner lakeTableBucketAssigner; | ||
|
||
/** | ||
* a getter to extract partition from prefix lookup key row, null when it's not a partitioned. | ||
*/ | ||
private @Nullable final PartitionGetter prefixKeyRowPartitionGetter; | ||
|
||
/** Decode the lookup bytes to result row. */ | ||
private final ValueDecoder kvValueDecoder; | ||
|
||
public FlussPrefixLookuper( | ||
TableInfo tableInfo, | ||
int numBuckets, | ||
MetadataUpdater metadataUpdater, | ||
LookupClient lookupClient, | ||
RowType primaryKeyRowType, | ||
KeyEncoder bucketKeyEncoder, | ||
boolean isDataLakeEnable, | ||
@Nullable LakeTableBucketAssigner lakeTableBucketAssigner, | ||
@Nullable PartitionGetter prefixKeyRowPartitionGetter, | ||
ValueDecoder kvValueDecoder) { | ||
this.tableInfo = tableInfo; | ||
this.numBuckets = numBuckets; | ||
this.metadataUpdater = metadataUpdater; | ||
this.lookupClient = lookupClient; | ||
this.primaryKeyRowType = primaryKeyRowType; | ||
this.isDataLakeEnable = isDataLakeEnable; | ||
this.lakeTableBucketAssigner = lakeTableBucketAssigner; | ||
this.bucketKeyEncoder = bucketKeyEncoder; | ||
this.prefixKeyRowPartitionGetter = prefixKeyRowPartitionGetter; | ||
this.kvValueDecoder = kvValueDecoder; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<PrefixLookupResult> prefixLookup(InternalRow prefixKey) { | ||
byte[] prefixKeyBytes = bucketKeyEncoder.encode(prefixKey); | ||
int bucketId = getBucketId(prefixKeyBytes, prefixKey); | ||
|
||
Long partitionId = null; | ||
if (prefixKeyRowPartitionGetter != null) { | ||
partitionId = getPartitionId(prefixKey); | ||
} | ||
|
||
TableBucket tableBucket = new TableBucket(tableInfo.getTableId(), partitionId, bucketId); | ||
return lookupClient | ||
.prefixLookup(tableBucket, prefixKeyBytes) | ||
.thenApply( | ||
result -> { | ||
List<InternalRow> rowList = new ArrayList<>(); | ||
for (byte[] valueBytes : result) { | ||
rowList.add( | ||
valueBytes == null | ||
? null | ||
: kvValueDecoder.decodeValue(valueBytes).row); | ||
} | ||
return new PrefixLookupResult(rowList); | ||
}); | ||
} | ||
|
||
private Long getPartitionId(InternalRow row) { | ||
Preconditions.checkNotNull( | ||
prefixKeyRowPartitionGetter, "partitionGetter shouldn't be null."); | ||
String partitionName = prefixKeyRowPartitionGetter.getPartition(row); | ||
PhysicalTablePath physicalTablePath = | ||
PhysicalTablePath.of(tableInfo.getTablePath(), partitionName); | ||
metadataUpdater.checkAndUpdatePartitionMetadata(physicalTablePath); | ||
return metadataUpdater.getCluster().getPartitionIdOrElseThrow(physicalTablePath); | ||
} | ||
|
||
private int getBucketId(byte[] keyBytes, InternalRow key) { | ||
if (!isDataLakeEnable) { | ||
return HashBucketAssigner.bucketForRowKey(keyBytes, numBuckets); | ||
} else { | ||
if (lakeTableBucketAssigner == null) { | ||
lakeTableBucketAssigner = | ||
new LakeTableBucketAssigner( | ||
primaryKeyRowType, | ||
tableInfo.getTableDescriptor().getBucketKey(), | ||
numBuckets); | ||
} | ||
return lakeTableBucketAssigner.assignBucket( | ||
keyBytes, key, metadataUpdater.getCluster()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.