org.apache.tephra.snapshot.SnapshotCodecV4 Maven / Gradle / Ivy
/*
* 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.tephra.snapshot;
import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import org.apache.tephra.TransactionManager;
import org.apache.tephra.persist.TransactionSnapshot;
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;
/**
* Handles serialization/deserialization of a {@link TransactionSnapshot}
* and its elements to {@code byte[]}.
*
*/
public class SnapshotCodecV4 extends SnapshotCodecV2 {
@Override
public int getVersion() {
return 4;
}
@Override
protected void encodeInProgress(BinaryEncoder encoder, Map inProgress)
throws IOException {
if (!inProgress.isEmpty()) {
encoder.writeInt(inProgress.size());
for (Map.Entry entry : inProgress.entrySet()) {
encoder.writeLong(entry.getKey()); // tx id
encoder.writeLong(entry.getValue().getExpiration());
encoder.writeLong(entry.getValue().getVisibilityUpperBound());
encoder.writeInt(entry.getValue().getType().ordinal());
// write checkpoint tx IDs
LongArrayList checkpointPointers = entry.getValue().getCheckpointWritePointers();
if (!checkpointPointers.isEmpty()) {
encoder.writeInt(checkpointPointers.size());
for (int i = 0; i < checkpointPointers.size(); i++) {
encoder.writeLong(checkpointPointers.getLong(i));
}
}
encoder.writeInt(0);
}
}
encoder.writeInt(0); // zero denotes end of list as per AVRO spec
}
@Override
protected NavigableMap decodeInProgress(BinaryDecoder decoder)
throws IOException {
int size = decoder.readInt();
NavigableMap inProgress = Maps.newTreeMap();
while (size != 0) { // zero denotes end of list as per AVRO spec
for (int remaining = size; remaining > 0; --remaining) {
long txId = decoder.readLong();
long expiration = decoder.readLong();
long visibilityUpperBound = decoder.readLong();
int txTypeIdx = decoder.readInt();
TransactionManager.InProgressType txType;
try {
txType = TransactionManager.InProgressType.values()[txTypeIdx];
} catch (ArrayIndexOutOfBoundsException e) {
throw new IOException("Type enum ordinal value is out of range: " + txTypeIdx);
}
// read checkpoint tx IDs
int checkpointPointerSize = decoder.readInt();
LongArrayList checkpointPointers = new LongArrayList(checkpointPointerSize);
while (checkpointPointerSize != 0) {
for (int checkpointRemaining = checkpointPointerSize; checkpointRemaining > 0; --checkpointRemaining) {
checkpointPointers.add(decoder.readLong());
}
checkpointPointerSize = decoder.readInt();
}
inProgress.put(txId,
new TransactionManager.InProgressTx(visibilityUpperBound, expiration, txType, checkpointPointers));
}
size = decoder.readInt();
}
return inProgress;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy