Search in sources :

Example 11 with HashMapTable

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

the class HashMapTableTest method testMapOps.

@Test
public void testMapOps() 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);
    for (int i = 0; i < 100; i++) {
        mapTable.put(("key" + i).getBytes(), ("value" + i).getBytes(), AbstractMapTable.NO_TIMEOUT, System.currentTimeMillis());
    }
    for (int i = 0; i < 100; i++) {
        if (i % 2 == 0) {
            mapTable.delete(("key" + i).getBytes());
        }
    }
    mapTable.delete(("key" + 100).getBytes());
    for (int i = 0; i < 100; i++) {
        GetResult result = mapTable.get(("key" + i).getBytes());
        if (i % 2 == 0) {
            assertTrue(result.isFound() && result.isDeleted());
        } else {
            assertTrue(result.isFound() && !result.isDeleted() && !result.isExpired());
            assertTrue(Arrays.equals(("value" + i).getBytes(), result.getValue()));
        }
    }
    GetResult result = mapTable.get(("key" + 100).getBytes());
    assertTrue(result.isFound() && result.isDeleted());
    result = mapTable.get(("key" + 101).getBytes());
    assertTrue(!result.isFound() && !result.isDeleted());
    mapTable.close();
    // test expiration
    createdTime = System.nanoTime();
    mapTable = new HashMapTable(testDir, 0, createdTime);
    for (int i = 0; i < 100; i++) {
        mapTable.put(("key" + i).getBytes(), ("value" + i).getBytes(), 200, System.currentTimeMillis());
    }
    try {
        Thread.sleep(600);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 100; i++) {
        result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound() && !result.isDeleted() && result.isExpired());
    }
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) HashMapTable(com.ctriposs.sdb.table.HashMapTable) Test(org.junit.Test)

Example 12 with HashMapTable

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

the class HashMapTableTest method testLoop.

@Test
public void testLoop() 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);
    int loop = 6 * 1024;
    for (int i = 0; i < loop; i++) {
        mapTable.appendNew(("key" + i).getBytes(), ("value" + i).getBytes(), -1, System.currentTimeMillis());
    }
    assertTrue(mapTable.getAppendedSize() == loop);
    for (int i = 0; i < loop; i++) {
        IMapEntry mapEntry = mapTable.getMapEntry(i);
        assertTrue(Arrays.equals(("key" + i).getBytes(), mapEntry.getKey()));
        assertTrue(Arrays.equals(("value" + i).getBytes(), mapEntry.getValue()));
        assertTrue(-1 == mapEntry.getTimeToLive());
        assertTrue(System.currentTimeMillis() >= mapEntry.getCreatedTime());
        assertFalse(mapEntry.isDeleted());
        assertTrue(mapEntry.isInUse());
    }
    for (int i = 0; i < loop; i++) {
        GetResult result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound());
        assertFalse(result.isDeleted() || result.isExpired());
        assertTrue(Arrays.equals(("value" + i).getBytes(), result.getValue()));
    }
    GetResult result = mapTable.get(("key" + loop).getBytes());
    assertFalse(result.isFound());
    assertFalse(result.isDeleted() || result.isExpired());
    try {
        mapTable.getMapEntry(loop);
    } catch (IllegalArgumentException iae) {
    }
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) IMapEntry(com.ctriposs.sdb.table.IMapEntry) HashMapTable(com.ctriposs.sdb.table.HashMapTable) Test(org.junit.Test)

Example 13 with HashMapTable

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

the class HashMapTableTest method testAppendConcurrency.

@Test
public void testAppendConcurrency() throws IOException, InterruptedException, ExecutionException {
    ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    long createdTime = System.nanoTime();
    mapTable = new HashMapTable(testDir, 1, createdTime);
    List<Future<?>> futures = new ArrayList<Future<?>>();
    int N_THREADS = 128;
    final int LOOP = 512;
    final AtomicInteger failedCount = new AtomicInteger(0);
    for (int t = 0; t < N_THREADS; t++) {
        final int finalT = t;
        futures.add(es.submit(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < LOOP; i++) {
                    try {
                        mapTable.appendNew(("" + finalT).getBytes(), ("" + i).getBytes(), -1, System.currentTimeMillis());
                    } catch (IOException e) {
                        e.printStackTrace();
                        failedCount.incrementAndGet();
                    }
                }
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertTrue(failedCount.get() == 0);
    assertTrue(mapTable.getAppendedSize() == N_THREADS * LOOP);
    Map<Integer, Set<Integer>> resultMap = new HashMap<Integer, Set<Integer>>();
    for (int i = 0; i < mapTable.getAppendedSize(); i++) {
        IMapEntry mapEntry = mapTable.getMapEntry(i);
        String key = new String(mapEntry.getKey());
        String value = new String(mapEntry.getValue());
        if (!resultMap.containsKey(Integer.parseInt(key))) {
            resultMap.put(Integer.parseInt(key), new HashSet<Integer>());
        }
        resultMap.get(Integer.parseInt(key)).add(Integer.parseInt(value));
    }
    assertTrue(resultMap.size() == N_THREADS);
    Set<Integer> keySet = resultMap.keySet();
    for (int t = 0; t < N_THREADS; t++) {
        keySet.contains(t);
    }
    for (Integer key : keySet) {
        Set<Integer> valueSet = resultMap.get(key);
        assertTrue(valueSet.size() == LOOP);
        for (int l = 0; l < LOOP; l++) {
            valueSet.contains(l);
        }
    }
    es.shutdown();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) IMapEntry(com.ctriposs.sdb.table.IMapEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HashMapTable(com.ctriposs.sdb.table.HashMapTable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 14 with HashMapTable

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

the class HashMapTableTest method testReOpen.

@Test
public void testReOpen() 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);
    int loop = 1024;
    for (int i = 0; i < loop; i++) {
        mapTable.appendNew(("key" + i).getBytes(), ("value" + i).getBytes(), -1, System.currentTimeMillis());
    }
    assertTrue(mapTable.getAppendedSize() == loop);
    mapTable.close();
    mapTable = new HashMapTable(testDir, 0, createdTime);
    for (int i = 0; i < loop; i++) {
        IMapEntry mapEntry = mapTable.getMapEntry(i);
        assertTrue(Arrays.equals(("key" + i).getBytes(), mapEntry.getKey()));
        assertTrue(Arrays.equals(("value" + i).getBytes(), mapEntry.getValue()));
        assertTrue(-1 == mapEntry.getTimeToLive());
        assertTrue(System.currentTimeMillis() >= mapEntry.getCreatedTime());
        assertFalse(mapEntry.isDeleted());
        assertTrue(mapEntry.isInUse());
    }
    try {
        mapTable.getMapEntry(loop);
    } catch (IllegalArgumentException iae) {
    }
    for (int i = 0; i < loop; i++) {
        GetResult result = mapTable.get(("key" + i).getBytes());
        assertTrue(result.isFound());
        assertFalse(result.isDeleted() || result.isExpired());
        assertTrue(Arrays.equals(("value" + i).getBytes(), result.getValue()));
    }
    GetResult result = mapTable.get(("key" + loop).getBytes());
    assertFalse(result.isFound());
    assertFalse(result.isDeleted() || result.isExpired());
}
Also used : GetResult(com.ctriposs.sdb.table.GetResult) IMapEntry(com.ctriposs.sdb.table.IMapEntry) 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