Search in sources :

Example 1 with HashMapTable

use of com.ctriposs.sdb.table.HashMapTable 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 HashMapTable

use of com.ctriposs.sdb.table.HashMapTable 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 3 with HashMapTable

use of com.ctriposs.sdb.table.HashMapTable 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)

Example 4 with HashMapTable

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

the class HashMapTableTest method operationAfterClosedTest.

@Test
public void operationAfterClosedTest() throws Exception {
    long createdTime = System.nanoTime();
    mapTable = new HashMapTable(testDir, 1, createdTime);
    mapTable.close();
    final byte[] testKey = new byte[] { 1 }, testValue = new byte[] { 1 };
    try {
        mapTable.appendNew(testKey, testValue, 1000, System.currentTimeMillis());
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.getMapEntry(0);
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.getEntrySet();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.put(testKey, testValue, 1000, System.currentTimeMillis());
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.put(testKey, testValue, 1000, System.currentTimeMillis(), true);
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.delete(testKey);
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
}
Also used : HashMapTable(com.ctriposs.sdb.table.HashMapTable) Test(org.junit.Test)

Example 5 with HashMapTable

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

the class HashMapTableTest method testEmtpy.

@Test
public void testEmtpy() throws IOException {
    long createdTime = System.nanoTime();
    mapTable = new HashMapTable(testDir, 0, createdTime);
    assertTrue(mapTable.getLevel() == 0);
    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);
    GetResult result = mapTable.get("empty".getBytes());
    assertFalse(result.isFound());
    assertFalse(result.isDeleted() || result.isExpired());
    try {
        mapTable.getMapEntry(-1);
        fail();
    } catch (IllegalArgumentException iae) {
    }
    try {
        mapTable.getMapEntry(0);
    } catch (IllegalArgumentException iae) {
    }
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) HashMapTable(com.ctriposs.sdb.table.HashMapTable) Test(org.junit.Test)

Aggregations

HashMapTable (com.ctriposs.sdb.table.HashMapTable)14 Test (org.junit.Test)11 GetResult (com.ctriposs.sdb.table.GetResult)8 IMapEntry (com.ctriposs.sdb.table.IMapEntry)7 ArrayList (java.util.ArrayList)5 MMFMapTable (com.ctriposs.sdb.table.MMFMapTable)4 IOException (java.io.IOException)4 LevelQueue (com.ctriposs.sdb.LevelQueue)2 AbstractMapTable (com.ctriposs.sdb.table.AbstractMapTable)2 PriorityQueue (java.util.PriorityQueue)2 Random (java.util.Random)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ByteArrayWrapper (com.ctriposs.sdb.table.ByteArrayWrapper)1 FCMapTable (com.ctriposs.sdb.table.FCMapTable)1 InMemIndex (com.ctriposs.sdb.table.InMemIndex)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1