lindong28 commented on code in PR #203: URL: https://github.com/apache/flink-ml/pull/203#discussion_r1099814163
########## flink-ml-lib/src/main/java/org/apache/flink/ml/common/typeinfo/PriorityQueueSerializer.java: ########## @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.ml.common.typeinfo; + +import org.apache.flink.api.common.typeutils.CompositeTypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.base.ListSerializer; +import org.apache.flink.api.java.typeutils.runtime.DataInputViewStream; +import org.apache.flink.api.java.typeutils.runtime.DataOutputViewStream; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputView; +import org.apache.flink.util.InstantiationUtil; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.PriorityQueue; + +/** + * TypeSerializer for {@link java.util.PriorityQueue}. + * + * @param <T> The type of inner objects. + */ +public class PriorityQueueSerializer<T> extends TypeSerializer<PriorityQueue<T>> { + + private final Comparator<? super T> comparator; + + private final TypeSerializer<T> innerSerializer; Review Comment: nits: can we rename it to `elementSerializer` for consistency with `LinkedListSerializer#elementSerializer` and `ListSerializer#elementSerializer`? ########## flink-ml-lib/src/main/java/org/apache/flink/ml/common/typeinfo/PriorityQueueSerializer.java: ########## @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.ml.common.typeinfo; + +import org.apache.flink.api.common.typeutils.CompositeTypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.base.ListSerializer; +import org.apache.flink.api.java.typeutils.runtime.DataInputViewStream; +import org.apache.flink.api.java.typeutils.runtime.DataOutputViewStream; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputView; +import org.apache.flink.util.InstantiationUtil; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.PriorityQueue; + +/** + * TypeSerializer for {@link java.util.PriorityQueue}. + * + * @param <T> The type of inner objects. + */ +public class PriorityQueueSerializer<T> extends TypeSerializer<PriorityQueue<T>> { + + private final Comparator<? super T> comparator; + + private final TypeSerializer<T> innerSerializer; + + public PriorityQueueSerializer( + Comparator<? super T> comparator, TypeSerializer<T> innerSerializer) { + this.comparator = comparator; + this.innerSerializer = innerSerializer; + } + + @Override + public boolean isImmutableType() { + return false; + } + + @Override + public TypeSerializer<PriorityQueue<T>> duplicate() { + return new PriorityQueueSerializer<>(comparator, innerSerializer.duplicate()); + } + + @Override + public PriorityQueue<T> createInstance() { + return new PriorityQueue<>(comparator); + } + + @Override + public PriorityQueue<T> copy(PriorityQueue<T> from) { + return new PriorityQueue<>(from); + } + + @Override + public PriorityQueue<T> copy(PriorityQueue<T> from, PriorityQueue<T> reuse) { + return new PriorityQueue<>(from); + } + + @Override + public int getLength() { + return -1; + } + + @Override + public void serialize(PriorityQueue<T> queue, DataOutputView target) throws IOException { + List<T> tmpList = new ArrayList<>(queue); + ListSerializer<T> listSerializer = new ListSerializer<>(innerSerializer); + listSerializer.serialize(tmpList, target); + } + + @Override + public PriorityQueue<T> deserialize(DataInputView source) throws IOException { + ListSerializer<T> listSerializer = new ListSerializer<>(innerSerializer); + List<T> tmpList = listSerializer.deserialize(source); + PriorityQueue<T> queue = new PriorityQueue<>(comparator); + queue.addAll(tmpList); + return queue; + } + + @Override + public PriorityQueue<T> deserialize(PriorityQueue<T> reuse, DataInputView source) + throws IOException { + return deserialize(source); + } + + @Override + public void copy(DataInputView source, DataOutputView target) throws IOException { + PriorityQueue<T> queue = deserialize(source); Review Comment: Would it be simpler to do the following without having to convert from/to PriorityQueue? ``` ListSerializer<T> listSerializer = new ListSerializer<>(innerSerializer); listSerializer.copy(source, target); ``` ########## flink-ml-lib/src/main/java/org/apache/flink/ml/common/util/QuantileSummary.java: ########## @@ -367,6 +373,26 @@ public double getRelativeError() { return relativeError; } + public int getCompressThreshold() { Review Comment: Is this needed to make this class POJO? Would it be simpler to still keep these member variables `public`? ########## flink-ml-lib/src/main/java/org/apache/flink/ml/common/typeinfo/QuantileSummaryTypeInfoFactory.java: ########## @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.ml.common.typeinfo; + +import org.apache.flink.api.common.typeinfo.TypeInfoFactory; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.api.java.typeutils.TypeExtractor; +import org.apache.flink.ml.common.util.QuantileSummary; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +/** + * Used by {@link TypeExtractor} to create a {@link TypeInformation} for implementations of {@link + * QuantileSummary}. + */ +public class QuantileSummaryTypeInfoFactory extends TypeInfoFactory<QuantileSummary> { + + private static final Map<String, TypeInformation<?>> fields; + + static { + fields = new HashMap<>(); + fields.put("relativeError", Types.DOUBLE); + fields.put("compressThreshold", Types.INT); + fields.put("count", Types.LONG); + fields.put("sampled", Types.LIST(TypeInformation.of(QuantileSummary.StatsTuple.class))); + fields.put("headBuffer", Types.LIST(Types.DOUBLE)); + fields.put("compressed", Types.BOOLEAN); + } + + private static final TypeInformation<QuantileSummary> TYPE_INFO = + Types.POJO(QuantileSummary.class, fields); Review Comment: Can we just use `TypeInformation.of(QuantileSummary.class)` here after making `QuantileSummary` POJO, instead of having to explicitly specifying the `fields` above? And do we still need `QuantileSummaryTypeInfoFactory` if `QuantileSummary` is POJO? ########## flink-ml-lib/src/main/java/org/apache/flink/ml/common/typeinfo/PriorityQueueTypeInfo.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.ml.common.typeinfo; + +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; + +import java.util.Comparator; +import java.util.Objects; +import java.util.PriorityQueue; + +/** + * TypeInformation for {@link java.util.PriorityQueue}. + * + * @param <T> The type of inner objects. + */ +public class PriorityQueueTypeInfo<T> extends TypeInformation<PriorityQueue<T>> { + + private final Comparator<? super T> comparator; + + private final TypeInformation<T> innerTypeInfo; Review Comment: nits: can we rename it to `elementTypeInfo` for consistency with `ListTypeInfo#elementTypeInfo`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
