com.gemstone.gemfire.internal.cache.OfflineSnapshotJUnitTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.internal.cache;
import java.io.File;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import junit.framework.TestCase;
import com.examples.snapshot.MyObject;
import com.examples.snapshot.MyPdxSerializer;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DiskStore;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.snapshot.RegionGenerator;
import com.gemstone.gemfire.cache.snapshot.RegionGenerator.RegionType;
import com.gemstone.gemfire.cache.snapshot.RegionGenerator.SerializationType;
import com.gemstone.gemfire.cache.snapshot.SnapshotIterator;
import com.gemstone.gemfire.cache.snapshot.SnapshotReader;
public class OfflineSnapshotJUnitTest extends TestCase {
private RegionGenerator rgen;
private Cache cache;
private DiskStore ds;
public void testExport() throws Exception {
int rcount = 0;
for (final RegionType rt : RegionType.persistentValues()) {
for (final SerializationType st : SerializationType.offlineValues()) {
Region region = rgen.createRegion(cache, ds.getName(), rt, "test" + rcount++);
final Map expected = createExpected(st, 1000);
region.putAll(expected);
cache.close();
DiskStoreImpl.exportOfflineSnapshot(ds.getName(), new File[] { new File(".") }, new File("."));
checkSnapshotEntries(expected, ds.getName(), region.getName());
reset();
}
}
}
public void testLargeFileExport() throws Exception {
int count = 10000;
Region region = rgen.createRegion(cache, ds.getName(), RegionType.PARTITION_PERSISTENT, "test");
System.out.println("Creating entries...");
final Map expected = createExpected(SerializationType.DATA_SERIALIZABLE, count);
region.putAll(expected);
cache.close();
System.out.println("Recovering entries...");
for (int i = 0; i < 10; i++) {
long start = System.currentTimeMillis();
DiskStoreImpl.exportOfflineSnapshot(ds.getName(), new File[] { new File(".") }, new File("."));
long elapsed = System.currentTimeMillis() - start;
double rate = 1.0 * count / elapsed;
System.out.println("Created snapshot with " + count + " entries in " + elapsed + " ms (" + rate + " entries/ms)");
checkSnapshotEntries(expected, ds.getName(), region.getName());
}
}
public Map createExpected(SerializationType type, int count) {
Map expected = new HashMap();
for (int i = 0; i < count; i++) {
expected.put(i, rgen.createData(type, i, "The number is " + i));
}
return expected;
}
public void setUp() throws Exception {
for (File f : new File(".").listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("BACKUP") || name.startsWith("snapshot-");
}
})) {
f.delete();
}
reset();
rgen = new RegionGenerator();
}
public void tearDown() throws Exception {
if (!cache.isClosed()) {
cache.close();
}
}
public void reset() {
CacheFactory cf = new CacheFactory()
.set("mcast-port", "0")
.set("log-level", "error")
.setPdxSerializer(new MyPdxSerializer())
.setPdxPersistent(true);
cache = cf.create();
ds = cache.createDiskStoreFactory().setMaxOplogSize(1).create("snapshotTest");
}
private void checkSnapshotEntries(Map expected, String diskStoreName, String regionName)
throws Exception {
final Map testData = new HashMap(expected);
String snapshot = "snapshot-" + diskStoreName + "-" + regionName;
SnapshotIterator iter = SnapshotReader.read(new File(snapshot));
try {
while (iter.hasNext()) {
Entry entry = iter.next();
Object expectedVal = testData.remove(entry.getKey());
assertEquals(expectedVal, entry.getValue());
}
assertTrue(testData.isEmpty());
} finally {
iter.close();
}
}
}