io.datarouter.gcp.spanner.op.read.index.SpannerLookupUniqueOp Maven / Gradle / Ivy
/*
* Copyright © 2009 HotPads ([email protected])
*
* 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 io.datarouter.gcp.spanner.op.read.index;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.KeySet;
import com.google.cloud.spanner.Options;
import com.google.cloud.spanner.ReadOnlyTransaction;
import com.google.cloud.spanner.ResultSet;
import io.datarouter.gcp.spanner.field.SpannerBaseFieldCodec;
import io.datarouter.gcp.spanner.field.SpannerFieldCodecRegistry;
import io.datarouter.model.databean.Databean;
import io.datarouter.model.field.Field;
import io.datarouter.model.key.primary.PrimaryKey;
import io.datarouter.model.key.unique.UniqueKey;
import io.datarouter.model.serialize.fielder.DatabeanFielder;
import io.datarouter.scanner.Scanner;
import io.datarouter.storage.config.Config;
import io.datarouter.storage.serialize.fieldcache.PhysicalDatabeanFieldInfo;
import io.datarouter.util.collection.ListTool;
public class SpannerLookupUniqueOp<
PK extends PrimaryKey,
D extends Databean,
F extends DatabeanFielder>
extends SpannerBaseReadIndexOp{
private final Collection extends UniqueKey> keys;
private final PhysicalDatabeanFieldInfo fieldInfo;
public SpannerLookupUniqueOp(
DatabaseClient client,
PhysicalDatabeanFieldInfo fieldInfo,
Collection extends UniqueKey> keys,
Config config,
SpannerFieldCodecRegistry codecRegistry){
super(client, config, codecRegistry, fieldInfo.getTableName());
this.keys = keys;
this.fieldInfo = fieldInfo;
}
@Override
public List wrappedCall(){
if(keys == null || keys.isEmpty()){
return List.of();
}
ResultSet databeanRs;
String indexName = getIndexName(keys.iterator().next());
try(ReadOnlyTransaction txn = client.readOnlyTransaction()){
ResultSet rs;
if(config.getLimit() != null){
rs = txn.readUsingIndex(
tableName,
indexName,
buildKeySet(),
fieldInfo.getPrimaryKeyFieldColumnNames(),
Options.limit(config.getLimit()));
}else{
rs = txn.readUsingIndex(
tableName,
indexName,
buildKeySet(),
fieldInfo.getPrimaryKeyFieldColumnNames());
}
List keyList = createFromResultSet(
rs,
fieldInfo.getPrimaryKeySupplier(),
fieldInfo.getPrimaryKeyFields());
if(config.getLimit() != null){
databeanRs = txn.read(
tableName,
buildKeySet(keyList),
fieldInfo.getFieldColumnNames(),
Options.limit(config.getLimit()));
}else{
databeanRs = txn.read(tableName, buildKeySet(keyList), fieldInfo.getFieldColumnNames());
}
return createFromResultSet(databeanRs, fieldInfo.getDatabeanSupplier(), fieldInfo.getFields());
}
}
@Override
public KeySet buildKeySet(){
KeySet.Builder keySetBuilder = KeySet.newBuilder();
keys.stream()
.map(this::primaryKeyConversion)
.forEach(keySetBuilder::addKey);
return keySetBuilder.build();
}
private > String getIndexName(UK key){
List indexFields = key.getFieldNames();
for(Entry>> uniqueIndex : fieldInfo.getUniqueIndexes().entrySet()){
List fieldNames = Scanner.of(uniqueIndex.getValue())
.map(field -> field.getKey().getName())
.list();
if(ListTool.compare(indexFields, fieldNames) == 0){
return uniqueIndex.getKey();
}
}
throw new RuntimeException("Cannot find index Name for key");
}
private Key primaryKeyConversion(UniqueKey key){
Key.Builder mutationKey = Key.newBuilder();
for(SpannerBaseFieldCodec,?> codec : codecRegistry.createCodecs(key.getFields())){
mutationKey = codec.setKey(mutationKey);
}
return mutationKey.build();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy