@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Transactionals.execute(transactional, context -> { getRouteTable(context).write(ServiceDiscoverable.getName(serviceId), GSON.toJson(routeConfig.getRoutes())); }); }
@Test public void testRouteStorage() throws Exception { RouteStore routeStore = new LocalRouteStore(datasetFramework, new InMemoryTxSystemClient(txManager)); ApplicationId appId = new ApplicationId("n1", "a1"); ProgramId service1 = appId.service("s1"); RouteConfig routeConfig = new RouteConfig(ImmutableMap.of("v1", 100)); routeStore.store(service1, routeConfig); Assert.assertEquals(routeConfig.getRoutes(), routeStore.fetch(service1).getRoutes()); routeStore.delete(service1); Assert.assertNotNull(routeStore.fetch(service1)); try { routeStore.delete(service1); Assert.fail("Config should have been deleted and thus a NotFoundException must have been thrown."); } catch (NotFoundException e) { // expected } } }
@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Transactionals.execute(transactional, context -> { getRouteTable(context).write(ServiceDiscoverable.getName(serviceId), GSON.toJson(routeConfig.getRoutes())); }); }
@Override public RouteConfig fetch(final ProgramId serviceId) { return Transactionals.execute(transactional, context -> { byte[] value = getRouteTable(context).read(ServiceDiscoverable.getName(serviceId)); if (value == null) { return new RouteConfig(Collections.emptyMap()); } return new RouteConfig(GSON.fromJson(Bytes.toString(value), MAP_STRING_INTEGER_TYPE)); }); }
@Override public RouteConfig fetch(final ProgramId serviceId) { return Transactionals.execute(transactional, context -> { byte[] value = getRouteTable(context).read(ServiceDiscoverable.getName(serviceId)); if (value == null) { return new RouteConfig(Collections.emptyMap()); } return new RouteConfig(GSON.fromJson(Bytes.toString(value), MAP_STRING_INTEGER_TYPE)); }); }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { Transactionals.execute(transactional, context -> { byte[] key = Bytes.toBytes(ServiceDiscoverable.getName(serviceId)); KeyValueTable kvTable = getRouteTable(context); if (kvTable.read(key) == null) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } kvTable.delete(key); }, NotFoundException.class); }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { Transactionals.execute(transactional, context -> { byte[] key = Bytes.toBytes(ServiceDiscoverable.getName(serviceId)); KeyValueTable kvTable = getRouteTable(context); if (kvTable.read(key) == null) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } kvTable.delete(key); }, NotFoundException.class); }