Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
neverchanje committed Nov 21, 2018
1 parent 26ecae2 commit 3af031b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static byte[] decompress(byte[] src) throws PException {

/**
* A handle for Zstd dictionary.
* Dict can be created once and shared by multiple threads concurrently, since its usage is read-only.
*/
public static class Dict {
private Dict(byte[] dictBuffer) {
Expand Down Expand Up @@ -119,7 +120,7 @@ public void trainAndSaveResultToPegasus(PegasusTableInterface table) throws PExc
}

private void train() throws PException {
if (samples.size() < 100) {
if (samples.size() < 200) {
throw new PException("samples are not enough for training");
}

Expand Down
34 changes: 29 additions & 5 deletions src/test/java/com/xiaomi/infra/pegasus/tools/TestZstdWrapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.xiaomi.infra.pegasus.tools;


import com.xiaomi.infra.pegasus.client.PException;
import com.xiaomi.infra.pegasus.client.PegasusClientFactory;
import com.xiaomi.infra.pegasus.client.PegasusClientInterface;
import com.xiaomi.infra.pegasus.client.PegasusTableInterface;
import com.xiaomi.infra.pegasus.client.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -57,4 +54,31 @@ public void testCompression() throws Exception {
}
}
}

@Test
public void testCompressionUsingDict() throws Exception {
PegasusClientInterface client = PegasusClientFactory.getSingletonClient();
PegasusTableInterface table = client.openTable("wutao1_sample");

ZstdWrapper.DictTrainer trainer = ZstdWrapper.startTraining();
for (int i = 0; i < 200; i++) {
byte[] value = RandomStringUtils.randomAlphabetic(1000).getBytes();
trainer.addSample(value);
}

// start training
trainer.trainAndSaveResultToPegasus(table);

// get result
ZstdWrapper.Dict dict = ZstdWrapper.getDictFromPegasus(table);

for (int i = 0; i < 10000; i++) {
byte[] value = RandomStringUtils.randomAlphabetic(1000).getBytes();

byte[] compressed = ZstdWrapper.compress(value, dict);
byte[] original = ZstdWrapper.decompress(compressed, dict);

Assert.assertArrayEquals(value, original);
}
}
}

0 comments on commit 3af031b

Please sign in to comment.