This repository was archived by the owner on Aug 23, 2020. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 371
Ability to toggle cache on/off #1706
          
     Open
      
      
            acha-bill
  wants to merge
  2
  commits into
  iotaledger-archive:cache
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
acha-bill:cache-benchmark
  
      
      
   
  
    
  
  
  
 
  
      
    base: cache
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -236,7 +236,11 @@ public static boolean mightExist(Tangle tangle, Hash hash) throws Exception { | |
| * @throws Exception Thrown if there is an error determining if the transaction exists or not | ||
| */ | ||
| public static boolean exists(Tangle tangle, Hash hash) throws Exception { | ||
| return tangle.getCache(TransactionViewModel.class).get(hash) != null || tangle.exists(Transaction.class, hash); | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache != null) { | ||
| return cache.get(hash) != null || tangle.exists(Transaction.class, hash); | ||
| } | ||
| return tangle.exists(Transaction.class, hash); | ||
| } | ||
|  | ||
| /** | ||
|  | @@ -310,7 +314,13 @@ public void update(Tangle tangle, Snapshot initialSnapshot, String item) throws | |
| return; | ||
| } | ||
|  | ||
| TransactionViewModel cachedTvm = tangle.getCache(TransactionViewModel.class).get(hash); | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache == null) { | ||
| updateDB(tangle, transaction, hash, item); | ||
| return; | ||
| } | ||
|  | ||
| TransactionViewModel cachedTvm = cache.get(hash); | ||
| if (cachedTvm != null) { | ||
| this.isCacheEntryFresh = false; | ||
| } | ||
|  | @@ -442,8 +452,12 @@ public boolean store(Tangle tangle, Snapshot initialSnapshot) throws Exception { | |
| // non-cached operations. | ||
| List<Pair<Indexable, Persistable>> batch = getSaveBatch(); | ||
| cacheApprovees(tangle); | ||
| if (tangle.getCache(TransactionViewModel.class).get(hash) == null) { | ||
| cachePut(tangle, this, hash); | ||
|  | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache != null) { | ||
| if (cache.get(hash) == null) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| cachePut(tangle, this, hash); | ||
| } | ||
| } | ||
|  | ||
| if (tangle.exists(Transaction.class, hash)) { | ||
|  | @@ -455,6 +469,9 @@ public boolean store(Tangle tangle, Snapshot initialSnapshot) throws Exception { | |
|  | ||
| private void cacheApprovees(Tangle tangle) throws Exception { | ||
| Cache<Indexable, ApproveeViewModel> approveeViewModelCache = tangle.getCache(ApproveeViewModel.class); | ||
| if (approveeViewModelCache == null) { | ||
| return; | ||
| } | ||
| ApproveeViewModel branchViewModel = ApproveeViewModel.load(tangle, getBranchTransactionHash()); | ||
| branchViewModel.addHash(hash); | ||
| approveeViewModelCache.put(getBranchTransactionHash(), branchViewModel); | ||
|  | @@ -976,6 +993,9 @@ public String toString() { | |
| */ | ||
| private static void cachePut(Tangle tangle, TransactionViewModel transactionViewModel, Hash hash) throws Exception { | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache == null) { | ||
| return; | ||
| } | ||
| if (cache.getSize() == cache.getConfiguration().getMaxSize()) { | ||
| cacheEvict(tangle); | ||
| } | ||
|  | @@ -989,6 +1009,9 @@ private static void cachePut(Tangle tangle, TransactionViewModel transactionView | |
| */ | ||
| public static void cacheEvict(Tangle tangle) throws Exception { | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache == null) { | ||
| return; | ||
| } | ||
| for (int i = 0; i < cache.getConfiguration().getEvictionCount(); i++) { | ||
| Indexable hash = cache.nextEvictionKey(); | ||
| if (hash != null) { | ||
|  | @@ -1009,7 +1032,10 @@ public static void cacheEvict(Tangle tangle) throws Exception { | |
| * @param hash hash to evict | ||
| */ | ||
| private static void cacheDelete(Tangle tangle, Hash hash) { | ||
| tangle.getCache(TransactionViewModel.class).evict(hash); | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache != null) { | ||
| cache.evict(hash); | ||
| } | ||
| } | ||
|  | ||
| /** | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -8,26 +8,13 @@ | |
| import com.iota.iri.controllers.TransactionViewModel; | ||
| import com.iota.iri.model.Hash; | ||
| import com.iota.iri.model.StateDiff; | ||
| import com.iota.iri.model.persistables.Address; | ||
| import com.iota.iri.model.persistables.Approvee; | ||
| import com.iota.iri.model.persistables.Bundle; | ||
| import com.iota.iri.model.persistables.Milestone; | ||
| import com.iota.iri.model.persistables.ObsoleteTag; | ||
| import com.iota.iri.model.persistables.Tag; | ||
| import com.iota.iri.model.persistables.Transaction; | ||
| import com.iota.iri.model.persistables.*; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| import com.iota.iri.utils.Pair; | ||
| import com.iota.iri.zmq.MessageQueueProvider; | ||
|  | ||
| import javax.naming.OperationNotSupportedException; | ||
|  | ||
| import java.util.AbstractMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.*; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|  | ||
|  | @@ -58,6 +45,7 @@ public class Tangle { | |
| private final List<PersistenceProvider> persistenceProviders = new ArrayList<>(); | ||
| private final List<MessageQueueProvider> messageQueueProviders = new ArrayList<>(); | ||
| private CacheManager cacheManager; | ||
| private boolean isCacheEnabled = true; | ||
|  | ||
| public void addPersistenceProvider(PersistenceProvider provider) { | ||
| this.persistenceProviders.add(provider); | ||
|  | @@ -398,7 +386,28 @@ public void clearMetadata(Class<?> column) throws Exception { | |
| * @return The cache with the specified type | ||
| */ | ||
| public <T> Cache<Indexable, T> getCache(Class<T> type){ | ||
| return getCacheManager().getCache(type); | ||
| if (!isCacheEnabled) { | ||
| return null; | ||
| } | ||
| return getCacheManager().getCache(type); | ||
| } | ||
|  | ||
| /** | ||
| * Gets if caching is enabled or not. | ||
| * | ||
| * @return True if caching is enabled. Else, returns false. | ||
| */ | ||
| public boolean isCacheEnabled() { | ||
| return isCacheEnabled; | ||
| } | ||
|  | ||
| /** | ||
| * Sets caching on or off. | ||
| * | ||
| * @param cacheEnabled The value to set. | ||
| */ | ||
| public void setCacheEnabled(boolean cacheEnabled) { | ||
| isCacheEnabled = cacheEnabled; | ||
| } | ||
|  | ||
| /** | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,33 +1,30 @@ | ||
| package com.iota.iri.service.tipselection.impl; | ||
|  | ||
| import com.iota.iri.TransactionTestUtils; | ||
| import com.iota.iri.cache.Cache; | ||
| import com.iota.iri.conf.MainnetConfig; | ||
| import com.iota.iri.conf.TipSelConfig; | ||
| import com.iota.iri.controllers.TransactionViewModel; | ||
| import com.iota.iri.model.Hash; | ||
| import com.iota.iri.service.ledger.LedgerService; | ||
| import com.iota.iri.service.snapshot.SnapshotProvider; | ||
| import com.iota.iri.service.snapshot.impl.SnapshotMockUtils; | ||
| import com.iota.iri.storage.Indexable; | ||
| import com.iota.iri.storage.Tangle; | ||
| import com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider; | ||
| import org.junit.After; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|  | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
|  | ||
| import org.junit.*; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.junit.MockitoJUnit; | ||
| import org.mockito.junit.MockitoRule; | ||
|  | ||
| import static com.iota.iri.TransactionTestUtils.getTransactionTritsWithTrunkAndBranch; | ||
| import static com.iota.iri.TransactionTestUtils.getTransactionHash; | ||
|  | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import static com.iota.iri.TransactionTestUtils.getTransactionTritsWithTrunkAndBranch; | ||
|  | ||
| public class WalkValidatorImplTest { | ||
|  | ||
|  | @@ -54,7 +51,10 @@ public static void tearDown() throws Exception { | |
|  | ||
| @After | ||
| public void clearCache(){ | ||
| tangle.getCache(TransactionViewModel.class).clear(); | ||
| Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class); | ||
| if (cache != null) { | ||
| cache.clear(); | ||
| } | ||
| } | ||
|  | ||
| @BeforeClass | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
storedoesn't do anything?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes