InstanceConfig should be used at most once

This commit is contained in:
leventov
2014-11-10 16:14:17 +03:00
parent 878a19479e
commit 780a6ae2ee
3 changed files with 21 additions and 4 deletions
@@ -501,8 +501,6 @@ final class ChannelProvider implements Closeable {
@Override
public void close() throws IOException {
// synchronized on parent instance to prevent possible (but very unlikely) contention
// between close() calls on different channels
channelDataLock.writeLock().lock();
try {
chronicleChannelList.remove(chronicleChannels[chronicleChannel]);
@@ -32,6 +32,8 @@ final class InstanceConfig<K, V>
final ReplicationChannel channel;
final File file;
private boolean used = false;
InstanceConfig(AbstractChronicleMapBuilder<K, V, ?> mapBuilder,
SingleChronicleHashReplication singleHashReplication,
ReplicationChannel channel,
@@ -67,7 +69,15 @@ final class InstanceConfig<K, V>
}
@Override
public ChronicleMap<K, V> create() throws IOException {
public synchronized ChronicleMap<K, V> create() throws IOException {
if (used)
throw new IllegalStateException(
"A ChronicleMap has already been created using this instance config. " +
"Create a new instance config (builder.instance() or call any " +
"configuration method on this instance config) to create a new " +
"ChronicleMap instance");
used = true;
return mapBuilder.create(this);
}
}
@@ -29,6 +29,8 @@ final class InstanceConfig<E> implements ChronicleHashInstanceConfig<ChronicleSe
private final ChronicleHashInstanceConfig<ChronicleMap<E, DummyValue>> mapConfig;
private boolean used;
InstanceConfig(ChronicleHashInstanceConfig<ChronicleMap<E, DummyValue>> mapConfig) {
this.mapConfig = mapConfig;
}
@@ -56,7 +58,14 @@ final class InstanceConfig<E> implements ChronicleHashInstanceConfig<ChronicleSe
}
@Override
public ChronicleSet<E> create() throws IOException {
public synchronized ChronicleSet<E> create() throws IOException {
if (used)
throw new IllegalStateException(
"A ChronicleSet has already been created using this instance config. " +
"Create a new instance config (builder.instance() or call any " +
"configuration method on this instance config) to create a new " +
"ChronicleSet instance");
used = true;
return new SetFromMap<>(mapConfig.create());
}
}