use of com.ctriposs.sdb.table.GetResult 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) {
}
}
use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.
the class MMFMapTableTest method testAppendAndGet.
@Test
public void testAppendAndGet() throws IOException, ClassNotFoundException {
long createdTime = System.nanoTime();
mapTable = new MMFMapTable(testDir, 1, createdTime, 1000, 4);
assertTrue(mapTable.getLevel() == 1);
assertTrue(mapTable.getCreatedTime() == createdTime);
assertTrue(mapTable.getAppendedSize() == 0);
assertTrue(mapTable.isEmpty());
//assertTrue(mapTable.getBackFileSize() == (MMFMapTable.INIT_INDEX_FILE_SIZE + MMFMapTable.INIT_DATA_FILE_SIZE) * Level0Merger.DEFAULT_MERGE_WAYS);
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());
}
use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.
the class MMFMapTableTest method testLoopAndReopen.
@Test
public void testLoopAndReopen() throws IOException, ClassNotFoundException {
long createdTime = System.nanoTime();
int loop = 32 * 1024;
mapTable = new MMFMapTable(testDir, 1, createdTime, loop, 4);
assertTrue(mapTable.getLevel() == 1);
assertTrue(mapTable.getCreatedTime() == createdTime);
assertTrue(mapTable.getAppendedSize() == 0);
assertTrue(mapTable.isEmpty());
//assertTrue(mapTable.getBackFileSize() == (MMFMapTable.INIT_INDEX_FILE_SIZE + MMFMapTable.INIT_DATA_FILE_SIZE) * Level0Merger.DEFAULT_MERGE_WAYS);
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);
mapTable.reMap();
assertTrue(mapTable.getBackFileSize() < (MMFMapTable.INIT_INDEX_FILE_SIZE + MMFMapTable.INIT_DATA_FILE_SIZE) * Level0Merger.DEFAULT_MERGE_WAYS);
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 MMFMapTable(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 MMFMapTable(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());
}
}
use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.
the class SDB method get.
/**
* Get value in the DB with specific key
*
* @param key map entry key
* @return non-null value if the entry exists, not deleted or expired.
* null value if the entry does not exist, or exists but deleted or expired.
*/
public byte[] get(byte[] key) {
Preconditions.checkArgument(key != null && key.length > 0, "key is empty");
ensureNotClosed();
long start = System.nanoTime();
int reachedLevel = INMEM_LEVEL;
try {
short shard = this.getShard(key);
// check active hashmap table first
GetResult result = this.activeInMemTables[shard].get(key);
if (result.isFound()) {
if (!result.isDeleted() && !result.isExpired()) {
return result.getValue();
} else {
// deleted or expired
return null;
}
} else {
// check level0 hashmap tables
reachedLevel = LEVEL0;
LevelQueue lq0 = levelQueueLists[shard].get(LEVEL0);
lq0.getReadLock().lock();
try {
if (lq0 != null && lq0.size() > 0) {
for (AbstractMapTable table : lq0) {
result = table.get(key);
if (result.isFound())
break;
}
}
} finally {
lq0.getReadLock().unlock();
}
if (result.isFound()) {
if (!result.isDeleted() && !result.isExpired()) {
if (result.getLevel() == SDB.LEVEL2 && this.config.isLocalityEnabled()) {
// keep locality
this.put(key, result.getValue(), result.getTimeToLive(), result.getCreatedTime(), false);
}
return result.getValue();
} else {
// deleted or expired
return null;
}
}
// check level 1-2 on disk sorted tables
searchLevel12: {
for (int level = 1; level <= MAX_LEVEL; level++) {
reachedLevel = level;
LevelQueue lq = levelQueueLists[shard].get(level);
lq.getReadLock().lock();
try {
if (lq.size() > 0) {
for (AbstractMapTable table : lq) {
result = table.get(key);
if (result.isFound())
break searchLevel12;
}
}
} finally {
lq.getReadLock().unlock();
}
}
}
if (result.isFound()) {
if (!result.isDeleted() && !result.isExpired()) {
if (result.getLevel() == SDB.LEVEL2 && this.config.isLocalityEnabled()) {
// keep locality
this.put(key, result.getValue(), result.getTimeToLive(), result.getCreatedTime(), false);
}
return result.getValue();
} else {
// deleted or expired
return null;
}
}
}
} catch (IOException ioe) {
stats.recordDBError(Operations.GET);
throw new RuntimeException("Fail to get value by key, IOException occurr", ioe);
} finally {
stats.recordDBOperation(Operations.GET, reachedLevel, System.nanoTime() - start);
}
// no luck
return null;
}
use of com.ctriposs.sdb.table.GetResult in project sessdb by ppdai.
the class Level0MergerTest method testCase01.
@Test
public void testCase01() throws IOException, ClassNotFoundException {
String value = TestUtil.randomString(128);
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());
}
int totalCount = 0;
int max = 0;
for (int i = 0; i < 4; i++) {
int start = i;
HashMapTable table = sourceTables[i];
while (table.put(String.valueOf(start).getBytes(), value.getBytes(), AbstractMapTable.NO_TIMEOUT, System.currentTimeMillis(), false)) {
totalCount++;
if (start > max)
max = start;
start = start + 4;
}
}
LevelQueue lq1 = new LevelQueue();
long start = System.currentTimeMillis();
Level0Merger.mergeSort(lq0, lq1, 4, testDir, (short) 1);
long end = System.currentTimeMillis();
System.out.println("Time spent to merge " + totalCount + " items in 4 ways is " + (end - start) / 1000 + "s");
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() == totalCount);
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(Snappy.uncompress(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();
}
Aggregations