Search in sources :

Example 6 with FCMapTable

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

the class Level1MergerTest method testCase01.

@Test
public void testCase01() throws IOException, ClassNotFoundException {
    int maxSize = AbstractMapTable.INIT_INDEX_ITEMS_PER_TABLE;
    String value = TestUtil.randomString(1024 * 3);
    MMFMapTable[] sourceTables = new MMFMapTable[4];
    LevelQueue lq1 = new LevelQueue();
    for (int i = 0; i < 4; i++) {
        sourceTables[i] = new MMFMapTable(testDir, SDB.LEVEL1, System.nanoTime() + i, maxSize, 4);
        lq1.addFirst(sourceTables[i]);
    }
    int totalCount = 0;
    for (int i = 0; i < 4; i++) {
        int start = i;
        MMFMapTable table = sourceTables[i];
        List<String> keyList = new ArrayList<String>();
        while (keyList.size() < maxSize) {
            keyList.add(String.valueOf(start));
            totalCount++;
            start = start + 4;
        }
        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 (String key : keyList) {
            table.appendNew(key.getBytes(), value.getBytes(), AbstractMapTable.NO_TIMEOUT);
        }
    }
    LevelQueue lq2 = new LevelQueue();
    long start = System.currentTimeMillis();
    Level1Merger.mergeSort(lq1, lq2, 4, testDir, (short) 3);
    long end = System.currentTimeMillis();
    System.out.println("Time spent to merge " + totalCount + " items in 4 ways  is " + (end - start) / 1000 + "s");
    assertTrue(lq1.size() == 0);
    assertTrue(lq2.size() == 1);
    FCMapTable targetTable = (FCMapTable) lq2.poll();
    assertTrue(targetTable.getLevel() == SDB.LEVEL2);
    assertTrue(targetTable.getAppendedSize() == totalCount);
    assertTrue(totalCount == maxSize * 4);
    List<String> keyList = new ArrayList<String>();
    for (long i = 0; i < totalCount; 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 < totalCount; i++) {
        IMapEntry mapEntry = targetTable.getMapEntry(i);
        assertTrue(mapEntry.getIndex() == i);
        assertTrue(new String(mapEntry.getKey()).equals(keyList.get(i)));
        assertTrue(new String(mapEntry.getValue()).equals(value));
    }
    start = System.currentTimeMillis();
    Random random = new Random();
    for (int i = 0; i < 1024; i++) {
        long key = random.nextInt(totalCount);
        GetResult result = targetTable.get(String.valueOf(key).getBytes());
        assertTrue(result.isFound());
    }
    end = System.currentTimeMillis();
    System.out.println("Time to lookup 1024 random key in the target table is " + (end - start) + "ms");
    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) FCMapTable(com.ctriposs.sdb.table.FCMapTable) Random(java.util.Random) Test(org.junit.Test)

Example 7 with FCMapTable

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

the class FCMapTableTest method testAppendAndGet.

@Test
public void testAppendAndGet() 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);
    mapTable.appendNew("key".getBytes(), "value".getBytes(), 500);
    assertTrue(mapTable.getLevel() == 1);
    assertTrue(mapTable.getCreatedTime() == createdTime);
    assertTrue(mapTable.getAppendedSize() == 1);
    assertFalse(mapTable.isEmpty());
    GetResult result = mapTable.get("key".getBytes());
    assertTrue(result.isFound());
    assertTrue(!result.isDeleted());
    assertTrue(!result.isExpired());
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    result = mapTable.get("key".getBytes());
    assertTrue(result.isFound());
    assertTrue(!result.isDeleted());
    assertTrue(result.isExpired());
    result = mapTable.get("key1".getBytes());
    assertFalse(result.isFound());
    assertFalse(mapTable.isUsable());
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) FCMapTable(com.ctriposs.sdb.table.FCMapTable) Test(org.junit.Test)

Example 8 with FCMapTable

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

the class FCMapTableTest method operationAfterClosedTest.

@Test
public void operationAfterClosedTest() throws Exception {
    long createdTime = System.nanoTime();
    mapTable = new FCMapTable(testDir, 1, createdTime, 1);
    mapTable.close();
    final byte[] testKey = new byte[] { 1 }, testValue = new byte[] { 1 };
    try {
        mapTable.appendNew(testKey, testValue, 1000);
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.appendNew(testKey, Arrays.hashCode(testKey), testValue, 1000, System.currentTimeMillis(), false, false);
        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.reMap();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.persistToAppendDataFileOffset();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.persistToAppendIndex();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.persistBloomFilter();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
    try {
        mapTable.saveMetadata();
        fail("Should not get here after the SDB is closed.");
    } catch (IllegalStateException e) {
    }
}
Also used : FCMapTable(com.ctriposs.sdb.table.FCMapTable) Test(org.junit.Test)

Aggregations

FCMapTable (com.ctriposs.sdb.table.FCMapTable)8 Test (org.junit.Test)6 GetResult (com.ctriposs.sdb.table.GetResult)5 ArrayList (java.util.ArrayList)4 IMapEntry (com.ctriposs.sdb.table.IMapEntry)3 MMFMapTable (com.ctriposs.sdb.table.MMFMapTable)3 LevelQueue (com.ctriposs.sdb.LevelQueue)2 AbstractMapTable (com.ctriposs.sdb.table.AbstractMapTable)2 PriorityQueue (java.util.PriorityQueue)2 Random (java.util.Random)2 AbstractSortedMapTable (com.ctriposs.sdb.table.AbstractSortedMapTable)1 HashMapTable (com.ctriposs.sdb.table.HashMapTable)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 LinkedList (java.util.LinkedList)1