@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 { Snapshot snapshot = db.getSnapshot(); DBIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); ReadOptions options = new ReadOptions(); options.snapshot(snapshot); iterator = db.iterator(options); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { Map.Entry<byte[], byte[]> entry = iterator.peekNext(); String key = new String(entry.getKey(), "UTF-8"); T value = JSON.parse(new String(entry.getValue(), "UTF-8"), 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 (iterator != null) { try { iterator.close(); } catch (IOException ignored) { } } try { snapshot.close(); } catch (IOException ignored) { } } }
LeveldbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Snapshot snapshot = db.getSnapshot(); DBIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); ReadOptions options = new ReadOptions(); options.snapshot(snapshot); iterator = db.iterator(options); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { Map.Entry<byte[], byte[]> entry = iterator.peekNext(); String key = new String(entry.getKey(), "UTF-8"); T value = JSON.parse(new String(entry.getValue(), "UTF-8"), 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 (iterator != null) { try { iterator.close(); } catch (IOException ignored) { } } try { snapshot.close(); } catch (IOException ignored) { } } } }
LeveldbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Snapshot snapshot = db.getSnapshot(); DBIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); ReadOptions options = new ReadOptions(); options.snapshot(snapshot); iterator = db.iterator(options); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { Map.Entry<byte[], byte[]> entry = iterator.peekNext(); String key = new String(entry.getKey(), "UTF-8"); T value = JSON.parse(new String(entry.getValue(), "UTF-8"), 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 (iterator != null) { try { iterator.close(); } catch (IOException ignored) { } } try { snapshot.close(); } catch (IOException ignored) { } } } LeveldbFailStore(File dbPath, boolean needLock); }
LeveldbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Snapshot snapshot = db.getSnapshot(); DBIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); ReadOptions options = new ReadOptions(); options.snapshot(snapshot); iterator = db.iterator(options); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { Map.Entry<byte[], byte[]> entry = iterator.peekNext(); String key = new String(entry.getKey(), "UTF-8"); T value = JSON.parse(new String(entry.getValue(), "UTF-8"), 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 (iterator != null) { try { iterator.close(); } catch (IOException ignored) { } } try { snapshot.close(); } catch (IOException ignored) { } } } LeveldbFailStore(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(); void destroy(); }
LeveldbFailStore extends AbstractFailStore { @Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { Snapshot snapshot = db.getSnapshot(); DBIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); ReadOptions options = new ReadOptions(); options.snapshot(snapshot); iterator = db.iterator(options); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { Map.Entry<byte[], byte[]> entry = iterator.peekNext(); String key = new String(entry.getKey(), "UTF-8"); T value = JSON.parse(new String(entry.getValue(), "UTF-8"), 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 (iterator != null) { try { iterator.close(); } catch (IOException ignored) { } } try { snapshot.close(); } catch (IOException ignored) { } } } LeveldbFailStore(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(); void destroy(); static final String name; }