(Configuration conf)
| 68 | private AtomicLong lastFlush; |
| 69 | |
| 70 | public HostDb(Configuration conf) throws GoraException { |
| 71 | try { |
| 72 | hostStore = StorageUtils.createWebStore(conf, String.class, Host.class); |
| 73 | } catch (ClassNotFoundException e) { |
| 74 | throw new RuntimeException(e); |
| 75 | } |
| 76 | |
| 77 | // Create a cache. |
| 78 | // We add a removal listener to see if we need to flush the store, |
| 79 | // in order to adhere to the put-flush-get semantic |
| 80 | // ("read your own write") of DataStore. |
| 81 | |
| 82 | long lruSize = conf.getLong(HOSTDB_LRU_SIZE, DEFAULT_LRU_SIZE); |
| 83 | int concurrencyLevel = conf.getInt(HOSTDB_CONCURRENCY_LEVEL, |
| 84 | DEFAULT_HOSTDB_CONCURRENCY_LEVEL); |
| 85 | RemovalListener<String, CacheHost> listener = new RemovalListener<String, CacheHost>() { |
| 86 | @Override |
| 87 | public void onRemoval(RemovalNotification<String, CacheHost> notification) { |
| 88 | CacheHost removeFromCacheHost = notification.getValue(); |
| 89 | if (removeFromCacheHost != NULL_HOST) { |
| 90 | if (removeFromCacheHost.timestamp < lastFlush.get()) { |
| 91 | try { |
| 92 | hostStore.flush(); |
| 93 | } catch (Exception e) { |
| 94 | throw new RuntimeException(e); |
| 95 | } |
| 96 | lastFlush.set(System.currentTimeMillis()); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | cache = CacheBuilder.newBuilder().maximumSize(lruSize) |
| 103 | .removalListener(listener).concurrencyLevel(concurrencyLevel).build(); |
| 104 | lastFlush = new AtomicLong(System.currentTimeMillis()); |
| 105 | } |
| 106 | |
| 107 | public Host get(final String key) throws IOException { |
| 108 | Callable<CacheHost> valueLoader = new Callable<CacheHost>() { |
nothing calls this directly
no test coverage detected