@Test public void fetchTop() throws FailStoreException { List<Pair<String, Job>> pairs = failStore.fetchTop(5, Job.class); if (CollectionUtils.isNotEmpty(pairs)) { for (Pair<String, Job> pair : pairs) { System.out.println(JSON.toJSONString(pair)); } } }
@Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Cursor cursor = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(); cursor = db.openCursor(null, CursorConfig.DEFAULT); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); while (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { String key = new String(foundKey.getData(), "UTF-8"); String valueString = new String(foundValue.getData(), "UTF-8"); T value = JSON.parse(valueString, type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (cursor != null) { try { cursor.close(); } catch (DatabaseException e) { LOGGER.warn("close cursor failed! ", e); } } } }
BerkeleydbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Cursor cursor = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(); cursor = db.openCursor(null, CursorConfig.DEFAULT); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); while (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { String key = new String(foundKey.getData(), "UTF-8"); String valueString = new String(foundValue.getData(), "UTF-8"); T value = JSON.parse(valueString, type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (cursor != null) { try { cursor.close(); } catch (DatabaseException e) { LOGGER.warn("close cursor failed! ", e); } } } } }
BerkeleydbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Cursor cursor = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(); cursor = db.openCursor(null, CursorConfig.DEFAULT); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); while (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { String key = new String(foundKey.getData(), "UTF-8"); String valueString = new String(foundValue.getData(), "UTF-8"); T value = JSON.parse(valueString, type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (cursor != null) { try { cursor.close(); } catch (DatabaseException e) { LOGGER.warn("close cursor failed! ", e); } } } } BerkeleydbFailStore(File dbPath, boolean needLock); }
BerkeleydbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Cursor cursor = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(); cursor = db.openCursor(null, CursorConfig.DEFAULT); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); while (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { String key = new String(foundKey.getData(), "UTF-8"); String valueString = new String(foundValue.getData(), "UTF-8"); T value = JSON.parse(valueString, type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (cursor != null) { try { cursor.close(); } catch (DatabaseException e) { LOGGER.warn("close cursor failed! ", e); } } } } BerkeleydbFailStore(File dbPath, boolean needLock); @Override void open(); @Override void put(String key, Object value); @Override void delete(String key); @Override void delete(List<String> keys); @Override List<Pair<String, T>> fetchTop(int size, Type type); @Override void close(); @Override void destroy(); }
BerkeleydbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Cursor cursor = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(); cursor = db.openCursor(null, CursorConfig.DEFAULT); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); while (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { String key = new String(foundKey.getData(), "UTF-8"); String valueString = new String(foundValue.getData(), "UTF-8"); T value = JSON.parse(valueString, type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (cursor != null) { try { cursor.close(); } catch (DatabaseException e) { LOGGER.warn("close cursor failed! ", e); } } } } BerkeleydbFailStore(File dbPath, boolean needLock); @Override void open(); @Override void put(String key, Object value); @Override void delete(String key); @Override void delete(List<String> keys); @Override List<Pair<String, T>> fetchTop(int size, Type type); @Override void close(); @Override void destroy(); static final String name; }