[HUDI-232] Implement sealing/unsealing for HoodieRecord class (#938)
This commit is contained in:
@@ -63,17 +63,24 @@ public class HoodieRecord<T extends HoodieRecordPayload> implements Serializable
|
||||
*/
|
||||
private HoodieRecordLocation newLocation;
|
||||
|
||||
/**
|
||||
* Indicates whether the object is sealed.
|
||||
*/
|
||||
private boolean sealed;
|
||||
|
||||
public HoodieRecord(HoodieKey key, T data) {
|
||||
this.key = key;
|
||||
this.data = data;
|
||||
this.currentLocation = null;
|
||||
this.newLocation = null;
|
||||
this.sealed = false;
|
||||
}
|
||||
|
||||
public HoodieRecord(HoodieRecord<T> record) {
|
||||
this(record.key, record.data);
|
||||
this.currentLocation = record.currentLocation;
|
||||
this.newLocation = record.newLocation;
|
||||
this.sealed = record.sealed;
|
||||
}
|
||||
|
||||
public HoodieKey getKey() {
|
||||
@@ -100,6 +107,7 @@ public class HoodieRecord<T extends HoodieRecordPayload> implements Serializable
|
||||
* Sets the current currentLocation of the record. This should happen exactly-once
|
||||
*/
|
||||
public HoodieRecord setCurrentLocation(HoodieRecordLocation location) {
|
||||
checkState();
|
||||
assert currentLocation == null;
|
||||
this.currentLocation = location;
|
||||
return this;
|
||||
@@ -114,6 +122,7 @@ public class HoodieRecord<T extends HoodieRecordPayload> implements Serializable
|
||||
* exactly-once.
|
||||
*/
|
||||
public HoodieRecord setNewLocation(HoodieRecordLocation location) {
|
||||
checkState();
|
||||
assert newLocation == null;
|
||||
this.newLocation = location;
|
||||
return this;
|
||||
@@ -170,4 +179,18 @@ public class HoodieRecord<T extends HoodieRecordPayload> implements Serializable
|
||||
assert key != null;
|
||||
return key.getRecordKey();
|
||||
}
|
||||
|
||||
public void seal() {
|
||||
this.sealed = true;
|
||||
}
|
||||
|
||||
public void unseal() {
|
||||
this.sealed = false;
|
||||
}
|
||||
|
||||
public void checkState() {
|
||||
if (sealed) {
|
||||
throw new UnsupportedOperationException("Not allowed to modify after sealed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.hudi.common.model;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.avro.generic.GenericRecord;
|
||||
import org.apache.avro.generic.IndexedRecord;
|
||||
import org.apache.hudi.common.util.Option;
|
||||
import org.apache.hudi.common.util.SchemaTestUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link HoodieRecord}.
|
||||
*/
|
||||
public class TestHoodieRecord {
|
||||
|
||||
private HoodieRecord hoodieRecord;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
final List<IndexedRecord> indexedRecords = SchemaTestUtil.generateHoodieTestRecords(0, 1);
|
||||
final List<HoodieRecord> hoodieRecords = indexedRecords.stream()
|
||||
.map(r -> new HoodieRecord(new HoodieKey(UUID.randomUUID().toString(), "0000/00/00"),
|
||||
new AvroBinaryTestPayload(Option.of((GenericRecord) r)))).collect(Collectors.toList());
|
||||
hoodieRecord = hoodieRecords.get(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModificationAfterSeal() {
|
||||
hoodieRecord.seal();
|
||||
final HoodieRecordLocation location = new HoodieRecordLocation("100", "0");
|
||||
try {
|
||||
hoodieRecord.setCurrentLocation(location);
|
||||
fail("should fail since modification after sealed is not allowed");
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof UnsupportedOperationException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalModification() {
|
||||
hoodieRecord.unseal();
|
||||
final HoodieRecordLocation location = new HoodieRecordLocation("100", "0");
|
||||
hoodieRecord.setCurrentLocation(location);
|
||||
hoodieRecord.seal();
|
||||
|
||||
hoodieRecord.unseal();
|
||||
hoodieRecord.setNewLocation(location);
|
||||
hoodieRecord.seal();
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,9 @@ public class SpillableMapTestUtils {
|
||||
recordKeys.add(key);
|
||||
HoodieRecord record = new HoodieRecord<>(new HoodieKey(key, partitionPath),
|
||||
new HoodieAvroPayload(Option.of((GenericRecord) r)));
|
||||
record.unseal();
|
||||
record.setCurrentLocation(new HoodieRecordLocation("DUMMY_COMMIT_TIME", "DUMMY_FILE_ID"));
|
||||
record.seal();
|
||||
records.put(key, record);
|
||||
});
|
||||
return recordKeys;
|
||||
|
||||
Reference in New Issue
Block a user