Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Keysets to to table dto / info #452

Merged
merged 7 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip2
  • Loading branch information
zcxcasd committed Aug 19, 2021
commit 46218ebdb18b73d078e9e6a3682028e7c99a24ef
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Key Info.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class KeyInfo extends BaseDto {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use BaseInfo

public String name;
public List<String> fields;
private static final long serialVersionUID = 7254898853779135216L;

private String name;
private List<String> fields;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.netflix.metacat.common.server.connectors.model;

import com.netflix.metacat.common.dto.FieldDto;
import com.netflix.metacat.common.dto.KeyDto;
import com.netflix.metacat.common.dto.KeySetDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -11,32 +8,49 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
* KeySet Info.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@EqualsAndHashCode(callSuper = false)
public class KeySetInfo implements Serializable {
private static final long serialVersionUID = 3659843901964058788L;

private static final String partitionKey_defaultName = "partition";
private static final String primaryKey_defaultName = "primary";
private static final String sortKey_defaultName = "sort";
private static final String indexKey_defaultName = "index";
private static final String PARTITION_KEY_DEFAULT_NAME = "partition";
private static final String PRIMARY_KEY_DEFAULT_NAME = "primary";
private static final String SORT_KEY_DEFAULT_NAME = "sort";
private static final String INDEX_KEY_DEFAULT_NAME = "index";

public List<KeyInfo> partition;
public List<KeyInfo> primary;
public List<KeyInfo> sort;
public List<KeyInfo> index;
private List<KeyInfo> partition;
private List<KeyInfo> primary;
private List<KeyInfo> sort;
private List<KeyInfo> index;

public static KeySetInfo buildKeySet(List<FieldInfo> fields) {
/**
* builds a keyset from fieldInfo list.
*
* @param fields list of fieldInfo
* @return keyset
*/
public static KeySetInfo buildKeySet(final List<FieldInfo> fields) {
return buildKeySet(fields, null);
}

public static KeySetInfo buildKeySet(List<FieldInfo> fields, List<KeyInfo> primary)
{
/**
* builds a keyset from fieldInfo list and primary key list.
*
* @param fields list of fieldInfo
* @param primary list of primary keys
* @return keyset
*/
public static KeySetInfo buildKeySet(final List<FieldInfo> fields, final List<KeyInfo> primary) {
if (fields == null) {
return null;
} else if (fields.isEmpty()) {
Expand All @@ -50,33 +64,25 @@ public static KeySetInfo buildKeySet(List<FieldInfo> fields, List<KeyInfo> prima
if (field.isPartitionKey()) {
partitionKeys.add(field.getName());
}
if (field.getIsSortKey()) {
if (field.getIsSortKey() != null && field.getIsSortKey()) {
sortKeys.add(field.getName());
}
if (field.getIsIndexKey()){
if (field.getIsIndexKey() != null && field.getIsIndexKey()) {
indexKeys.add(field.getName());
}
}

final KeySetInfo keySetInfo = new KeySetInfo();
keySetInfo.setDefaultPartitionKeys(partitionKeys);
keySetInfo.setDefaultSortKeys(sortKeys);
keySetInfo.setDefaultIndexKeys(indexKeys);
keySetInfo.primary = primary;
keySetInfo.partition = partitionKeys.isEmpty() ? Collections.emptyList()
: Arrays.asList(
KeyInfo.builder().name(PARTITION_KEY_DEFAULT_NAME).fields(partitionKeys).build());
keySetInfo.sort = sortKeys.isEmpty() ? Collections.emptyList()
: Arrays.asList(KeyInfo.builder().name(SORT_KEY_DEFAULT_NAME).fields(sortKeys).build());
keySetInfo.index = indexKeys.isEmpty() ? Collections.emptyList()
: Arrays.asList(KeyInfo.builder().name(INDEX_KEY_DEFAULT_NAME).fields(indexKeys).build());
keySetInfo.primary = (primary == null || primary.isEmpty()) ? Collections.emptyList()
: primary;

return keySetInfo;
}

private void setDefaultPartitionKeys(List<String> fieldNames) {
this.partition = Arrays.asList(KeyInfo.builder().name(partitionKey_defaultName).fields(fieldNames).build());
}

private void setDefaultSortKeys(List<String> fieldNames) {
this.sort = Arrays.asList(KeyInfo.builder().name(sortKey_defaultName).fields(fieldNames).build());
}

private void setDefaultIndexKeys(List<String> fieldNames) {
this.index = Arrays.asList(KeyInfo.builder().name(indexKey_defaultName).fields(fieldNames).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ public class TableInfo extends BaseInfo {
private ViewInfo view;
private KeySetInfo keys;

private KeySetInfo getKeys()
{
if (this.keys != null) {
return keys;
}
return KeySetInfo.buildKeySet(this.fields);
}

/**
* Constructor.
*
Expand All @@ -75,4 +67,16 @@ private TableInfo(
this.view = view;
this.keys = keys;
}

private KeySetInfo getKeys() {
if (this.keys != null) {
return keys;
}
keys = KeySetInfo.buildKeySet(this.fields);
return keys;
}

public void setKeys(final KeySetInfo keys) {
this.keys = keys;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ class ConverterUtilSpec extends Specification {
given:
def dto = new TableDto(name: QualifiedName.ofTable('prodhive', 'amajumdar', 'part'),
audit: new AuditDto('test', new Date(), 'test', new Date()),
fields: [FieldDto.builder().name('esn').type('string').source_type('string').jsonType(new TextNode('string')).pos(0).build()] ,
serde: new StorageDto(owner: 'test'))
fields: [FieldDto.builder().name('esn').type('string').source_type('string')
.jsonType(new TextNode('string')).pos(0).build()] ,
serde: new StorageDto(owner: 'test'),
keys: KeySetDto.builder()
.partition(Arrays.asList(KeyDto.builder().name('partition').fields(Arrays.asList('esn')).build()))
.build()
)
when:
def info = converter.fromTableDto(dto)
def resultDto = converter.toTableDto(info)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright 2016 Netflix, Inc.
*
* 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:https://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.netflix.metacat.common.dto;

import io.swagger.annotations.ApiModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
/*
*
* Copyright 2016 Netflix, Inc.
*
* 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:https://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.netflix.metacat.common.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Key DTO.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class KeyDto extends BaseDto {
public String name;
public List<String> fields;
private static final long serialVersionUID = 5511551575484406779L;

private String name;
private List<String> fields;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
/*
*
* Copyright 2016 Netflix, Inc.
*
* 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:https://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.netflix.metacat.common.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* KeySet DTO.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class KeySetDto extends BaseDto {
public List<KeyDto> partition;
public List<KeyDto> primary;
public List<KeyDto> sort;
public List<KeyDto> index;
private static final long serialVersionUID = 6250093794701822334L;

private List<KeyDto> partition;
private List<KeyDto> primary;
private List<KeyDto> sort;
private List<KeyDto> index;
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,25 @@ public QualifiedName getDefinitionName() {
@JsonProperty
@SuppressWarnings("checkstyle:methodname")
public List<String> getPartition_keys() {
return fields == null
? null
: (this.keys.partition.isEmpty() ? Collections.EMPTY_LIST : this.keys.partition.get(0).fields);
if (this.keys == null) {
if (fields == null) {
return null;
} else if (fields.isEmpty()) {
return Collections.emptyList();
}

final List<String> partitionKeys = new LinkedList<>();
for (FieldDto field : fields) {
if (field.isPartition_key()) {
partitionKeys.add(field.getName());
}
}
return partitionKeys;
}

return this.keys.getPartition().isEmpty()
? Collections.EMPTY_LIST
: this.keys.getPartition().get(0).getFields();
}

/**
Expand All @@ -114,6 +130,19 @@ public List<String> getPartition_keys() {
public void setPartition_keys(final List<String> ignored) {
}

@JsonIgnore
public KeySetDto getKeys() {
return keys;
}

/**
* Sets the keyset.
* @param keys keyset
*/
public void setKeys(final KeySetDto keys) {
this.keys = keys;
}

@Override
@JsonProperty
public boolean isDataExternal() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.dto.KeyDto;
import com.netflix.metacat.common.dto.Pageable;
import com.netflix.metacat.common.dto.Sort;
import com.netflix.metacat.common.server.connectors.ConnectorRequestContext;
Expand Down Expand Up @@ -147,17 +146,19 @@ public TableInfo get(@Nonnull final ConnectorRequestContext context, @Nonnull fi
throw new TableNotFoundException(name);
}

Map<String, List<String>> pkMap = new HashMap<>();
try (ResultSet primaryKeys = this.getPrimaryKeys(connection, name)){
while(primaryKeys.next())
{
final Map<String, List<String>> pkMap = new HashMap<>();
try (ResultSet primaryKeys = this.getPrimaryKeys(connection, name)) {
while (primaryKeys.next()) {
final String tableName = primaryKeys.getString("TABLE_NAME");
if (!tableName.equals(name.getTableName())) {
continue;
}
final String pkName = primaryKeys.getString("PK_NAME");
final String columnName = primaryKeys.getString("COLUMN_NAME");
final int keySeq = Integer.parseInt(primaryKeys.getString("KEY_SEQ"));
if(pkMap.containsKey(pkName)) {
if (pkMap.containsKey(pkName)) {
pkMap.get(pkName).add(keySeq, columnName);
}
else {
} else {
final List<String> columnArray = new ArrayList<>();
columnArray.add(columnName);
pkMap.put(pkName, columnArray);
Expand Down
Loading