Search in sources :

Example 1 with GetResult

use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.

the class Level0MergerTest method testCase02.

@Test
public void testCase02() throws IOException, ClassNotFoundException {
    HashMapTable[] sourceTables = new HashMapTable[4];
    LevelQueue lq0 = new LevelQueue();
    for (int i = 0; i < 4; i++) {
        sourceTables[i] = new HashMapTable(testDir, SDB.LEVEL0, System.nanoTime() + i);
        sourceTables[i].setCompressionEnabled(true);
        lq0.addFirst(sourceTables[i]);
        assertTrue(sourceTables[i].isImmutable());
    }
    // delete
    int max = AbstractMapTable.INIT_INDEX_ITEMS_PER_TABLE;
    HashMapTable table4 = sourceTables[3];
    int start = 0;
    while (start < max) {
        table4.delete(String.valueOf(start).getBytes());
        start = start + 4;
    }
    // expiration
    HashMapTable table3 = sourceTables[2];
    start = 1;
    while (start < max) {
        table3.put(String.valueOf(start).getBytes(), String.valueOf(start).getBytes(), 200, System.currentTimeMillis());
        start = start + 4;
    }
    // expire table3
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    // ignore
    }
    // time to live 60 sec
    HashMapTable table2 = sourceTables[1];
    start = 2;
    while (start < max) {
        table2.put(String.valueOf(start).getBytes(), String.valueOf(start).getBytes(), 60 * 1000, System.currentTimeMillis());
        start = start + 4;
    }
    // time to live 120 sec with items that have been updated(table 2), deleted(table 4) and expired(table 3)
    HashMapTable table1 = sourceTables[0];
    start = 0;
    while (start < max) {
        table1.put(String.valueOf(start).getBytes(), String.valueOf(start).getBytes(), 120 * 1000, System.currentTimeMillis());
        start = start + 1;
    }
    LevelQueue lq1 = new LevelQueue();
    Level0Merger.mergeSort(lq0, lq1, 4, testDir, (short) 0);
    for (int i = 0; i < 4; i++) {
        assertTrue(sourceTables[i].isImmutable());
    }
    assertTrue(lq1.size() == 1);
    MMFMapTable targetTable = (MMFMapTable) lq1.poll();
    assertTrue(targetTable.getLevel() == SDB.LEVEL1);
    assertTrue(targetTable.getAppendedSize() == max);
    // validate delete
    start = 0;
    while (start < max) {
        GetResult result = targetTable.get(String.valueOf(start).getBytes());
        assertTrue(result.isFound());
        assertTrue(result.isDeleted());
        start += 4;
    }
    // validate expiration
    start = 1;
    while (start < max) {
        GetResult result = targetTable.get(String.valueOf(start).getBytes());
        assertTrue(result.isFound());
        assertTrue(result.isExpired());
        start += 4;
    }
    // validate ttl 60s
    start = 2;
    while (start < max) {
        GetResult result = targetTable.get(String.valueOf(start).getBytes());
        assertTrue(result.isFound());
        assertTrue(result.getTimeToLive() == 60 * 1000);
        start += 4;
    }
    // validate ttl 120s
    start = 3;
    while (start < max) {
        GetResult result = targetTable.get(String.valueOf(start).getBytes());
        assertTrue(result.isFound());
        assertTrue(result.getTimeToLive() == 120 * 1000);
        start += 4;
    }
    List<String> keyList = new ArrayList<String>();
    for (long i = 0; i < max; i++) {
        keyList.add(String.valueOf(i));
    }
    Collections.sort(keyList, new Comparator<String>() {

        @Override
        public int compare(String arg0, String arg1) {
            int hash0 = Arrays.hashCode(arg0.getBytes());
            int hash1 = Arrays.hashCode(arg1.getBytes());
            if (hash0 < hash1)
                return -1;
            else if (hash0 > hash1)
                return 1;
            else
                return 0;
        }
    });
    for (int i = 0; i < max; i++) {
        IMapEntry mapEntry = targetTable.getMapEntry(i);
        assertTrue(mapEntry.getIndex() == i);
        assertTrue(new String(mapEntry.getKey()).equals(keyList.get(i)));
        int intKey = Integer.parseInt(new String(mapEntry.getKey()));
        // validate disk space optimization
        if (// deleted
        intKey % 4 == 0)
            assertTrue(Arrays.equals(new byte[] { 0 }, mapEntry.getValue()));
        else if (// expired
        intKey % 4 == 1)
            assertTrue(Arrays.equals(new byte[] { 0 }, mapEntry.getValue()));
        else {
            assertTrue(new String(Snappy.uncompress(mapEntry.getValue())).equals(keyList.get(i)));
        //assertTrue(new String(mapEntry.getValue()).equals(keyList.get(i)));
        }
    }
    Random random = new Random();
    for (int i = 0; i < 1024; i++) {
        long key = random.nextInt(max);
        GetResult result = targetTable.get(String.valueOf(key).getBytes());
        assertTrue(result.isFound());
    }
    targetTable.close();
    targetTable.delete();
}
Also used : MMFMapTable(com.ctriposs.sdb.table.MMFMapTable) GetResult(com.ctriposs.sdb.table.GetResult) IMapEntry(com.ctriposs.sdb.table.IMapEntry) ArrayList(java.util.ArrayList) LevelQueue(com.ctriposs.sdb.LevelQueue) HashMapTable(com.ctriposs.sdb.table.HashMapTable) Random(java.util.Random) Test(org.junit.Test)

Example 2 with GetResult

use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.

the class FCMapTableTest method testEmtpy.

@Test
public void testEmtpy() throws IOException, ClassNotFoundException {
    long createdTime = System.nanoTime();
    mapTable = new FCMapTable(testDir, 1, createdTime, 1000);
    assertTrue(mapTable.getLevel() == 1);
    assertTrue(mapTable.getCreatedTime() == createdTime);
    assertTrue(mapTable.getAppendedSize() == 0);
    assertTrue(mapTable.isEmpty());
    assertTrue(mapTable.getBackFileSize() == HashMapTable.INIT_INDEX_FILE_SIZE + HashMapTable.INIT_DATA_FILE_SIZE + HashMapTable.INDEX_ITEM_LENGTH);
    assertFalse(mapTable.isUsable());
    try {
        @SuppressWarnings("unused") GetResult result = mapTable.get("empty".getBytes());
        fail();
    } catch (IllegalArgumentException iae) {
    }
    try {
        mapTable.getMapEntry(-1);
        fail();
    } catch (IllegalArgumentException iae) {
    }
    try {
        mapTable.getMapEntry(0);
    } catch (IllegalArgumentException iae) {
    }
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) FCMapTable(com.ctriposs.sdb.table.FCMapTable) Test(org.junit.Test)

Example 3 with GetResult

use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.

the class FCMapTableTest method testLoopAndReopen.

@Test
public void testLoopAndReopen() throws IOException, ClassNotFoundException {
    long createdTime = System.nanoTime();
    int loop = 32 * 1024;
    mapTable = new FCMapTable(testDir, 1, createdTime, loop);
    assertTrue(mapTable.getLevel() == 1);
    assertTrue(mapTable.getCreatedTime() == createdTime);
    assertTrue(mapTable.getAppendedSize() == 0);
    assertTrue(mapTable.isEmpty());
    assertTrue(mapTable.getBackFileSize() == HashMapTable.INIT_INDEX_FILE_SIZE + HashMapTable.INIT_DATA_FILE_SIZE + HashMapTable.INDEX_ITEM_LENGTH);
    assertFalse(mapTable.isUsable());
    List<byte[]> list = new ArrayList<byte[]>();
    for (int i = 0; i < loop; i++) {
        list.add(("key" + i).getBytes());
    }
    Collections.sort(list, new Comparator<byte[]>() {

        @Override
        public int compare(byte[] arg0, byte[] arg1) {
            int hash0 = Arrays.hashCode(arg0);
            int hash1 = Arrays.hashCode(arg1);
            if (hash0 < hash1)
                return -1;
            else if (hash0 > hash1)
                return 1;
            else
                return 0;
        }
    });
    for (int i = 0; i < loop; i++) {
        mapTable.appendNew(list.get(i), ("value" + i).getBytes(), -1);
    }
    assertTrue(mapTable.getAppendedSize() == loop);
    long start = System.currentTimeMillis();
    for (int i = 0; i < loop; i++) {
        GetResult result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound());
    }
    long time = System.currentTimeMillis() - start;
    System.out.printf("Get %,d K ops per second%n", (int) (loop / time));
    GetResult result = mapTable.get(("key" + loop).getBytes());
    assertFalse(result.isFound());
    assertFalse(result.isDeleted() || result.isExpired());
    mapTable.markUsable(true);
    mapTable.saveMetadata();
    mapTable.close();
    mapTable = new FCMapTable(testDir, mapTable.getFileName());
    assertTrue(mapTable.isUsable());
    assertTrue(mapTable.getAppendedSize() == loop);
    for (int i = 0; i < loop; i++) {
        result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound());
    }
    result = mapTable.get(("key" + loop).getBytes());
    assertFalse(result.isFound());
    assertFalse(result.isDeleted() || result.isExpired());
    mapTable.markUsable(false);
    mapTable.saveMetadata();
    mapTable.close();
    mapTable = new FCMapTable(testDir, mapTable.getFileName());
    assertFalse(mapTable.isUsable());
    assertTrue(mapTable.getAppendedSize() == loop);
    for (int i = 0; i < loop; i++) {
        result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound());
    }
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) ArrayList(java.util.ArrayList) FCMapTable(com.ctriposs.sdb.table.FCMapTable) Test(org.junit.Test)

Example 4 with GetResult

use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.

the class HashMapTablePerfTest method testPut.

@Test
public void testPut() throws IOException, ClassNotFoundException {
    int count = 40000;
    map = new HashMapTable(testDir, 0, System.nanoTime());
    long start = System.nanoTime();
    final SampleValue value = new SampleValue();
    StringBuilder user = new StringBuilder();
    System.out.println(value.toBytes().length);
    for (int i = 0; i < count; i++) {
        value.ee = i;
        value.gg = i;
        value.ii = i;
        map.put(users(user, i).getBytes(), value.toBytes(), AbstractMapTable.NO_TIMEOUT, System.currentTimeMillis());
    }
    assertTrue(map.getAppendedSize() == count);
    for (int i = 0; i < count; i++) {
        GetResult result = map.get(users(user, i).getBytes());
        assertTrue(result.isFound() && !result.isDeleted() && !result.isExpired());
        assertNotNull(result.getValue());
        SampleValue value2 = SampleValue.fromBytes(result.getValue());
        assertEquals(i, value2.ee);
        assertEquals(i, value2.gg, 0.0);
        assertEquals(i, value2.ii);
    }
    for (int i = 0; i < count; i++) {
        GetResult result = map.get(users(user, i).getBytes());
        assertNotNull(result.getValue());
    }
    for (int i = 0; i < count; i++) {
        map.delete(users(user, i).getBytes());
    }
    long time = System.nanoTime() - start;
    System.out.printf("Put/get %,d K operations per second%n", (int) (count * 4 * 1e6 / time));
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) HashMapTable(com.ctriposs.sdb.table.HashMapTable) Test(org.junit.Test)

Example 5 with GetResult

use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.

the class HashMapTablePerfTest method testPutPerf.

@Test
public void testPutPerf() throws ExecutionException, InterruptedException, IOException {
    ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    // use a simple pseudo-random distribution over 64-bits
    System.out.println("Starting test");
    map = new HashMapTable(testDir, 0, System.nanoTime());
    final int COUNT = 50000;
    final String[] users = new String[COUNT];
    for (int i = 0; i < COUNT; i++) users[i] = "user:" + i;
    long start = System.nanoTime();
    List<Future<?>> futures = new ArrayList<Future<?>>();
    for (int t = 0; t < N_THREADS; t++) {
        final int finalT = t;
        futures.add(es.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    final SampleValue value = new SampleValue();
                    StringBuilder user = new StringBuilder();
                    for (int i = finalT; i < COUNT; i += N_THREADS) {
                        value.ee = i;
                        value.gg = i;
                        value.ii = i;
                        map.put(users(user, i).getBytes(), value.toBytes(), AbstractMapTable.NO_TIMEOUT, System.currentTimeMillis());
                    }
                    for (int i = finalT; i < COUNT; i += N_THREADS) {
                        GetResult result = map.get(users(user, i).getBytes());
                        assertTrue(result.isFound() && !result.isDeleted() && !result.isExpired());
                        assertNotNull(result.getValue());
                        SampleValue value2 = SampleValue.fromBytes(result.getValue());
                        assertEquals(i, value2.ee);
                        assertEquals(i, value2.gg, 0.0);
                        assertEquals(i, value2.ii);
                    }
                    for (int i = finalT; i < COUNT; i += N_THREADS) assertTrue(map.get(users(user, i).getBytes()).isFound());
                    for (int i = finalT; i < COUNT; i += N_THREADS) map.delete(users(user, i).getBytes());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }));
    }
    for (Future<?> future : futures) future.get();
    long time = System.nanoTime() - start;
    System.out.printf("Put/get %,d K operations per second%n", (int) (COUNT * 4 * 1e6 / time));
    es.shutdown();
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) ArrayList(java.util.ArrayList) HashMapTable(com.ctriposs.sdb.table.HashMapTable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Aggregations

GetResult (com.ctriposs.sdb.table.GetResult)17 Test (org.junit.Test)16 HashMapTable (com.ctriposs.sdb.table.HashMapTable)8 MMFMapTable (com.ctriposs.sdb.table.MMFMapTable)7 ArrayList (java.util.ArrayList)7 IMapEntry (com.ctriposs.sdb.table.IMapEntry)6 FCMapTable (com.ctriposs.sdb.table.FCMapTable)5 LevelQueue (com.ctriposs.sdb.LevelQueue)4 Random (java.util.Random)4 IOException (java.io.IOException)2 AbstractMapTable (com.ctriposs.sdb.table.AbstractMapTable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1