cache: add saveTree/loadTree to save/load cache to/from a normal filesystem

This commit is contained in:
Adam
2017-03-14 14:08:36 -04:00
parent 963d82c903
commit b2327d7e8e
5 changed files with 350 additions and 98 deletions

View File

@@ -22,14 +22,13 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.cache.fs;
import com.google.common.io.Files;
import java.io.FileOutputStream;
import java.io.IOException;
import net.runelite.cache.StoreLocation;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -55,7 +54,9 @@ public class StoreLoadTest
java.io.File testStoreFile = folder.newFolder();
for (java.io.File f : StoreLocation.LOCATION.listFiles())
{
Files.copy(f, new java.io.File(testStoreFile, f.getName()));
}
Store testStore = new Store(testStoreFile);
testStore.load();
@@ -68,7 +69,8 @@ public class StoreLoadTest
Assert.assertTrue(store.equals(testStore));
}
//@Test
@Test
@Ignore
public void unpackStore() throws IOException
{
java.io.File base = StoreLocation.LOCATION;
@@ -76,29 +78,22 @@ public class StoreLoadTest
{
store.load();
for (Index i : store.getIndexes())
{
java.io.File ifile = new java.io.File(folder.newFolder(), "" + i.getId());
ifile.mkdir();
store.saveTree(folder.newFolder());
}
}
for (Archive a : i.getArchives())
{
java.io.File afile = new java.io.File(ifile, "" + a.getArchiveId());
afile.mkdir();
@Test
@Ignore
public void loadTree() throws IOException
{
Store store = new Store(folder.newFolder());
store.loadTree(new java.io.File("C:\\rs\\temp\\tree"));
for (File f : a.getFiles())
{
java.io.File ffile = new java.io.File(afile, "" + f.getFileId());
try (FileOutputStream fout = new FileOutputStream(ffile))
{
if (f.getContents() != null)
{
fout.write(f.getContents());
}
}
}
}
}
try (Store store2 = new Store(StoreLocation.LOCATION))
{
store2.load();
Assert.assertEquals(store, store2);
}
}
}

View File

@@ -22,7 +22,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.cache.fs;
import java.io.IOException;
@@ -35,9 +34,11 @@ import org.junit.rules.TemporaryFolder;
public class StoreTest
{
private static final int NUMBER_OF_FILES = 1024;
@Rule
public TemporaryFolder folder = StoreLocation.getTemporaryFolder();
@Test
public void testOneFile() throws IOException
{
@@ -50,29 +51,28 @@ public class StoreTest
file.setContents("test".getBytes());
store.save();
try (Store store2 = new Store(folder.getRoot()))
{
store2.load();
Assert.assertEquals(store, store2);
}
}
}
private static final int NUMBER_OF_FILES = 1024;
@Test
public void testManyFiles() throws IOException
{
Random random = new Random(42L);
java.io.File root = folder.newFolder();
try (Store store = new Store(folder.getRoot()))
try (Store store = new Store(root))
{
Index index = store.addIndex(0);
Archive archive = index.addArchive(0);
archive.setNameHash(random.nextInt());
for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
File file = archive.addFile(i);
@@ -81,70 +81,80 @@ public class StoreTest
random.nextBytes(data);
file.setContents(data);
}
store.save();
try (Store store2 = new Store(folder.getRoot()))
try (Store store2 = new Store(root))
{
store2.load();
Assert.assertEquals(store, store2);
}
}
}
@Test
public void testMultipleArchives() throws IOException
{
Random random = new Random(43L);
java.io.File root = folder.newFolder();
try (Store store = new Store(folder.getRoot()))
try (Store store = new Store(root))
{
Index index = store.addIndex(0);
Index index2 = store.addIndex(1);
Archive archive = index.addArchive(0);
archive.setNameHash(random.nextInt());
archive.setNameHash(random.nextInt(Integer.MAX_VALUE));
Archive archive2 = index.addArchive(1);
Archive archive3 = index2.addArchive(0);
for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
File file = archive.addFile(i);
file.setNameHash(random.nextInt());
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
byte[] data = new byte[random.nextInt(1024)];
random.nextBytes(data);
file.setContents(data);
}
for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
File file = archive2.addFile(i);
file.setNameHash(random.nextInt());
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
byte[] data = new byte[random.nextInt(1024)];
random.nextBytes(data);
file.setContents(data);
}
for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
File file = archive3.addFile(i);
file.setNameHash(random.nextInt());
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
byte[] data = new byte[random.nextInt(1024)];
random.nextBytes(data);
file.setContents(data);
}
store.save();
try (Store store2 = new Store(folder.getRoot()))
try (Store store2 = new Store(root))
{
store2.load();
Assert.assertEquals(store, store2);
}
// Test tree save/load
java.io.File tree = folder.newFolder();
store.saveTree(tree);
try (Store store2 = new Store(folder.newFolder()))
{
store2.loadTree(tree);
}
}
}
}