jerqi commented on code in PR #930: URL: https://github.com/apache/incubator-uniffle/pull/930#discussion_r1225023499
########## client-tez/src/main/java/org/apache/tez/runtime/library/common/sort/buffer/WriteBuffer.java: ########## @@ -0,0 +1,332 @@ +/* + * 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.tez.runtime.library.common.sort.buffer; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Comparator; +import java.util.List; + +import com.google.common.collect.Lists; +import org.apache.hadoop.io.RawComparator; +import org.apache.hadoop.io.WritableUtils; +import org.apache.hadoop.io.serializer.Serializer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + + +public class WriteBuffer<K,V> extends OutputStream { + + private static final Logger LOG = LoggerFactory.getLogger(WriteBuffer.class); + + private int partitionId; + private Serializer<K> keySerializer; + private Serializer<V> valSerializer; + private long maxSegmentSize; + private final RawComparator<K> comparator; + private int dataLength = 0; + private int currentOffset = 0; + private int currentIndex = 0; + private long sortTime = 0; + private long copyTime = 0; + private boolean isNeedSorted = false; + private final List<WrappedBuffer> buffers = Lists.newArrayList(); + private final List<Record<K>> records = Lists.newArrayList(); + + public WriteBuffer( + boolean isNeedSorted, + int partitionId, + RawComparator<K> comparator, + long maxSegmentSize, + Serializer<K> keySerializer, + Serializer<V> valueSerializer) { + this.partitionId = partitionId; + this.comparator = comparator; + this.maxSegmentSize = maxSegmentSize; + this.keySerializer = keySerializer; + this.valSerializer = valueSerializer; + this.isNeedSorted = isNeedSorted; + } + + /** + * add records + */ + public int addRecord(K key, V value) throws IOException { + keySerializer.open(this); + valSerializer.open(this); + int lastOffSet = currentOffset; + int lastIndex = currentIndex; + int lastDataLength = dataLength; + int keyIndex = lastIndex; + keySerializer.serialize(key); + int keyLength = dataLength - lastDataLength; + int keyOffset = lastOffSet; + if (compact(lastIndex, lastOffSet, keyLength)) { + keyOffset = lastOffSet; + keyIndex = lastIndex; + } + lastDataLength = dataLength; + valSerializer.serialize(value); + int valueLength = dataLength - lastDataLength; + records.add(new Record<K>(keyIndex, keyOffset, keyLength, valueLength)); + return keyLength + valueLength; + } + + public void clear() { + buffers.clear(); + records.clear(); + } + + /** + * get data + */ + public synchronized byte[] getData() { + int extraSize = 0; + for (Record<K> record : records) { + extraSize += WritableUtils.getVIntSize(record.getKeyLength()); + extraSize += WritableUtils.getVIntSize(record.getValueLength()); + } + extraSize += WritableUtils.getVIntSize(-1); Review Comment: Ok, let's merge this pr first. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
