StatementPatternIdCache { public Set<String> getStatementPatternIds(TransactionBase tx) { checkNotNull(tx); Optional<Bytes> hashBytes = Optional.ofNullable(tx.get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS_HASH)); if (hashBytes.isPresent()) { String hash = hashBytes.get().toString(); if ((HASH.isPresent() && HASH.get().equals(hash))) { return IDS; } lock.lock(); try { String ids = tx.get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS).toString(); IDS = Sets.newHashSet(ids.split(VAR_DELIM)); HASH = Optional.of(hash); return IDS; } finally { lock.unlock(); } } return IDS; }  Set<String> getStatementPatternIds(TransactionBase tx); void clear();  }
@Test public void testCache() { Transaction mockTx = Mockito.mock(Transaction.class); String nodeId = NodeType.generateNewFluoIdForType(NodeType.STATEMENT_PATTERN); when(mockTx.get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS)).thenReturn(Bytes.of(nodeId)); when(mockTx.get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS_HASH)).thenReturn(Bytes.of("123")); StatementPatternIdCache cache = new StatementPatternIdCache(); Set<String> ids1 = cache.getStatementPatternIds(mockTx); Set<String> ids2 = cache.getStatementPatternIds(mockTx); Assert.assertEquals(ids1, ids2); Assert.assertEquals(Sets.newHashSet(nodeId), ids1); Mockito.verify(mockTx, Mockito.times(1)).get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS); }