package org.terracotta.offheapstore.disk.persistent;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.terracotta.offheapstore.Segment;
import org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapCache;
import org.terracotta.offheapstore.pinning.PinnableSegment;
import org.terracotta.offheapstore.util.Factory;
public class AbstractPersistentConcurrentOffHeapCache<K, V> extends AbstractConcurrentOffHeapCache<K, V> implements Persistent {
private static final int MAGIC = 0x57415349;
public AbstractPersistentConcurrentOffHeapCache(Factory<? extends PinnableSegment<K, V>> segmentFactory) {
super(segmentFactory);
}
public AbstractPersistentConcurrentOffHeapCache(Factory<? extends PinnableSegment<K, V>> segmentFactory, int concurrency) {
super(segmentFactory, concurrency);
}
@Override
public void flush() throws IOException {
for (Segment<?, ?> s : segments) {
((Persistent) s).flush();
}
}
@Override
public void persist(ObjectOutput output) throws IOException {
output.writeInt(MAGIC);
output.writeInt(segments.length);
for (Segment<?, ?> s : segments) {
((Persistent) s).persist(output);
}
}
@Override
public void close() throws IOException {
for (Segment<?, ?> s : segments) {
((Persistent) s).close();
}
}
@Override
public void bootstrap(ObjectInput input) throws IOException {
for (Segment<?, ?> s : segments) {
((Persistent) s).bootstrap(input);
}
}
protected static int readSegmentCount(ObjectInput input) throws IOException {
if (input.readInt() != MAGIC) {
throw new IOException("Wrong magic number");
}
return input.readInt();
}
}