1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org.rocksdb;
3 
4 
5 import org.junit.ClassRule;
6 import org.junit.Rule;
7 import org.junit.Test;
8 import org.junit.rules.TemporaryFolder;
9 
10 import static org.assertj.core.api.Assertions.assertThat;
11 
12 public class CheckPointTest {
13 
14   @ClassRule
15   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
16       new RocksNativeLibraryResource();
17 
18   @Rule
19   public TemporaryFolder dbFolder = new TemporaryFolder();
20 
21   @Rule
22   public TemporaryFolder checkpointFolder = new TemporaryFolder();
23 
24   @Test
checkPoint()25   public void checkPoint() throws RocksDBException {
26     try (final Options options = new Options().
27         setCreateIfMissing(true)) {
28 
29       try (final RocksDB db = RocksDB.open(options,
30           dbFolder.getRoot().getAbsolutePath())) {
31         db.put("key".getBytes(), "value".getBytes());
32         try (final Checkpoint checkpoint = Checkpoint.create(db)) {
33           checkpoint.createCheckpoint(checkpointFolder.
34               getRoot().getAbsolutePath() + "/snapshot1");
35           db.put("key2".getBytes(), "value2".getBytes());
36           checkpoint.createCheckpoint(checkpointFolder.
37               getRoot().getAbsolutePath() + "/snapshot2");
38         }
39       }
40 
41       try (final RocksDB db = RocksDB.open(options,
42           checkpointFolder.getRoot().getAbsolutePath() +
43               "/snapshot1")) {
44         assertThat(new String(db.get("key".getBytes()))).
45             isEqualTo("value");
46         assertThat(db.get("key2".getBytes())).isNull();
47       }
48 
49       try (final RocksDB db = RocksDB.open(options,
50           checkpointFolder.getRoot().getAbsolutePath() +
51               "/snapshot2")) {
52         assertThat(new String(db.get("key".getBytes()))).
53             isEqualTo("value");
54         assertThat(new String(db.get("key2".getBytes()))).
55             isEqualTo("value2");
56       }
57     }
58   }
59 
60   @Test(expected = IllegalArgumentException.class)
failIfDbIsNull()61   public void failIfDbIsNull() {
62     try (final Checkpoint checkpoint = Checkpoint.create(null)) {
63 
64     }
65   }
66 
67   @Test(expected = IllegalStateException.class)
failIfDbNotInitialized()68   public void failIfDbNotInitialized() throws RocksDBException {
69     try (final RocksDB db = RocksDB.open(
70         dbFolder.getRoot().getAbsolutePath())) {
71       db.close();
72       Checkpoint.create(db);
73     }
74   }
75 
76   @Test(expected = RocksDBException.class)
failWithIllegalPath()77   public void failWithIllegalPath() throws RocksDBException {
78     try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
79          final Checkpoint checkpoint = Checkpoint.create(db)) {
80       checkpoint.createCheckpoint("/Z:///:\\C:\\TZ/-");
81     }
82   }
83 }
84