예제 #1
0
 @Test
 public void shouldReturnMinusOneAfterAllKeysShiftingTest() {
   LRUCache cache = new LRUCache(4);
   cache.set(1, 1);
   cache.set(2, 2);
   cache.set(3, 3);
   cache.set(4, 4);
   cache.get(1);
   cache.get(2);
   cache.get(3);
   cache.set(5, 5);
   int expected = -1;
   int actual = cache.get(4);
   assertEquals(expected, actual);
 }
예제 #2
0
  private void waitForBlockStored(Sha256Hash hash) {
    if (hash.toString().equals("0000000000000000000000000000000000000000000000000000000000000000"))
      return;

    try {
      if (file_db.getBlockMap().containsKey(hash)) return;
    } finally {

    }

    Semaphore block_wait_sem = null;
    synchronized (in_progress) {
      block_wait_sem = in_progress.get(hash);
      if (block_wait_sem == null) {
        block_wait_sem = new Semaphore(0);
        in_progress.put(hash, block_wait_sem);
      }
    }

    try {
      // System.out.println("Waiting for " + hash);
      block_wait_sem.acquire(1);

    } catch (java.lang.InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
예제 #3
0
  public MethodInvoker getMethod(Class<?> clazz, String name, Class<?>[] parameters) {
    MethodDescriptor mDescriptor = new MethodDescriptor(name, clazz, parameters);
    MethodInvoker mInvoker = null;
    List<Method> acceptableMethods = null;
    LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get();

    mInvoker = cache.get(mDescriptor);

    if (mInvoker == null) {
      acceptableMethods = getMethodsByNameAndLength(clazz, name, parameters.length);

      if (acceptableMethods.size() == 1) {
        mInvoker = MethodInvoker.buildInvoker(acceptableMethods.get(0), parameters);
      } else {
        mInvoker = getBestMethod(acceptableMethods, parameters);
      }

      if (mInvoker != null && mInvoker.getCost() != -1) {
        cache.put(mDescriptor, mInvoker);
      } else {
        String errorMessage =
            "Method " + name + "(" + Arrays.toString(parameters) + ") does not exist";
        logger.log(Level.WARNING, errorMessage);
        throw new Py4JException(errorMessage);
      }
    }

    return mInvoker;
  }
예제 #4
0
  /**
   * Method to remove the user profile from cache.
   *
   * @param userId
   */
  protected void removeProfileDataFromCache(String userId) throws ProfileServiceException {
    if (logger.isLoggable(Level.FINEST)) {
      logger.entering(sourceClass, "removeProfileDataFromCache", userId);
    }
    if (isEmail(userId)) {
      String key;
      Set<String> keys = lruCache.getKeySet();
      Iterator<String> itr = keys.iterator();
      while (itr.hasNext()) {
        key = itr.next();
        Document data = lruCache.get(key);
        // check if email in profile object is same as input userId
        String email = "";
        try {
          email = DOMUtil.value(data, Profile.xpathMap.get("email"));
        } catch (XMLException e) {
          continue;
        }

        // cache hit
        if (StringUtil.equalsIgnoreCase(email, userId)) {
          lruCache.remove(key);
        }
      }
      // Cache miss

    } else {
      lruCache.remove(userId);
    }
    if (logger.isLoggable(Level.FINEST)) {
      logger.exiting(sourceClass, "removeProfileDataFromCache");
    }
  }
예제 #5
0
 public Pattern getPatternForRegex(String regex) {
   Pattern pattern = cache.get(regex);
   if (pattern == null) {
     pattern = Pattern.compile(regex);
     cache.put(regex, pattern);
   }
   return pattern;
 }
예제 #6
0
 @Test
 public void shouldReturnMinusOneWhenNotFoundTest() {
   LRUCache cache = new LRUCache(5);
   cache.set(1, 1);
   int expected = -1;
   int actual = cache.get(2);
   assertEquals(expected, actual);
 }
예제 #7
0
 /** @throws Exception On test failure. */
 @Test(
     groups = {"cache"},
     threadPoolSize = 5,
     invocationCount = 100,
     timeOut = 60000)
 public void get() throws Exception {
   SearchResult result =
       cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=3")));
   AssertJUnit.assertEquals(
       new SearchResult(new LdapEntry("uid=3,ou=test,dc=ldaptive,dc=org")), result);
   result = cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=4")));
   AssertJUnit.assertEquals(
       new SearchResult(new LdapEntry("uid=4,ou=test,dc=ldaptive,dc=org")), result);
   result = cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=5")));
   AssertJUnit.assertEquals(
       new SearchResult(new LdapEntry("uid=5,ou=test,dc=ldaptive,dc=org")), result);
 }
예제 #8
0
 @Test
 public void shouldReturnValueWhenCallGetWithKeyTest() {
   LRUCache cacheOneElement = new LRUCache(5);
   cacheOneElement.set(1, 1);
   int expected = 1;
   int actual = cacheOneElement.get(1);
   assertEquals(expected, actual);
 }
예제 #9
0
 /** Fills the cache with data. */
 private void fillCache() {
   cache.put(
       new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=1")),
       new SearchResult(new LdapEntry("uid=1,ou=test,dc=ldaptive,dc=org")));
   cache.put(
       new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=2")),
       new SearchResult(new LdapEntry("uid=2,ou=test,dc=ldaptive,dc=org")));
   cache.put(
       new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=3")),
       new SearchResult(new LdapEntry("uid=3,ou=test,dc=ldaptive,dc=org")));
   cache.put(
       new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=4")),
       new SearchResult(new LdapEntry("uid=4,ou=test,dc=ldaptive,dc=org")));
   cache.put(
       new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=5")),
       new SearchResult(new LdapEntry("uid=5,ou=test,dc=ldaptive,dc=org")));
   // ensure uid=1 and uid=2 get evicted first
   cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=3")));
   cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=4")));
   cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=5")));
 }
예제 #10
0
  public void testLRUCache() {
    MyService service1 = new MyService();
    MyService service2 = new MyService();

    cache.put("A", service1);
    cache.put("B", service2);

    assertEquals(2, cache.size());

    assertSame(service1, cache.get("A"));
    assertSame(service2, cache.get("B"));
  }
예제 #11
0
  private String getNamespace(int id) throws IOException {
    Integer cacheID = new Integer(id);
    String namespace = namespaceCache.get(cacheID);

    if (namespace == null) {
      byte[] namespaceData = dataStore.getData(id);
      namespace = data2namespace(namespaceData);

      namespaceCache.put(cacheID, namespace);
    }

    return namespace;
  }
예제 #12
0
 /**
  * Fetch cache value
  *
  * @param id ItemId
  * @return cached value or null when not in cache
  */
 public Boolean get(ItemId id) {
   if (maxCacheSize < 1) {
     return null;
   }
   accesses++;
   Boolean obj = readAccessCache.get(id);
   if (obj == null) {
     misses++;
   } else {
     hits++;
   }
   return obj;
 }
예제 #13
0
 @Test
 public void shouldReturnMinusOneAfterLatestKeyGetTest() {
   LRUCache cache = new LRUCache(5);
   cache.set(1, 1);
   cache.set(2, 2);
   cache.set(3, 3);
   cache.set(4, 4);
   cache.set(5, 5);
   cache.set(6, 6);
   int expected = -1;
   int actual = cache.get(1);
   assertEquals(expected, actual);
 }
예제 #14
0
 /*
  * Method to check if the Profile is cached. Calls findInCache to find for the profile in Cache.
  *
  * @param userId
  * @return Document
  */
 private Document getProfileDataFromCache(String userId) {
   // this should return just the content ..xmldoc
   // should a have a common caching framework for all services
   Document data = null;
   if (isEmail(userId)) {
     data = findInCache(userId);
   } else {
     if (lruCache.hasKey(userId)) {
       data = lruCache.get(userId);
     }
   }
   return data;
 }
예제 #15
0
 /**
  * Resolve a fully-qualified module name (as a reference to the {@linkplain ModuleName#localName()
  * local name} made from within the {@linkplain ModuleName#packageName() package}).
  *
  * @param qualifiedName A fully-qualified {@linkplain ModuleName module name}.
  * @param dependent The name of the module that requires this resolution, if any.
  * @return A {@linkplain ResolvedModuleName resolved module name} if the resolution was
  *     successful.
  * @throws UnresolvedDependencyException If resolution fails.
  */
 public ResolvedModuleName resolve(
     final ModuleName qualifiedName, final @Nullable ResolvedModuleName dependent)
     throws UnresolvedDependencyException {
   final ModuleNameResolutionResult result = resolutionCache.get(qualifiedName);
   assert result != null;
   if (!result.isResolved()) {
     // The resolution failed.
     if (dependent != null) {
       result.exception().setReferringModuleName(dependent);
     }
     throw result.exception();
   }
   return result.resolvedModule();
 }
예제 #16
0
  /** @throws Exception On test failure. */
  @Test(groups = {"cache"})
  public void put() throws Exception {
    AssertJUnit.assertEquals(5, cache.size());
    cache.put(
        new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=%s", new Object[] {"101"})),
        new SearchResult(new LdapEntry("uid=101,ou=test,dc=ldaptive,dc=org")));
    cache.put(
        new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=102")),
        new SearchResult(new LdapEntry("uid=102,ou=test,dc=ldaptive,dc=org")));
    AssertJUnit.assertEquals(5, cache.size());

    SearchResult result =
        cache.get(
            new SearchRequest(
                "dc=ldaptive,dc=org", new SearchFilter("uid=%s", new Object[] {"101"})));
    AssertJUnit.assertEquals(
        new SearchResult(new LdapEntry("uid=101,ou=test,dc=ldaptive,dc=org")), result);
    result = cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=102")));
    AssertJUnit.assertEquals(
        new SearchResult(new LdapEntry("uid=102,ou=test,dc=ldaptive,dc=org")), result);
    AssertJUnit.assertNull(
        cache.get(new SearchRequest("dc=ldaptive,dc=org", new SearchFilter("uid=1"))));
  }
예제 #17
0
  /**
   * Stores the supplied value and returns the ID that has been assigned to it. In case the value
   * was already present, the value will not be stored again and the ID of the existing value is
   * returned.
   *
   * @param value The Value to store.
   * @return The ID that has been assigned to the value.
   * @exception IOException If an I/O error occurred.
   */
  public int storeValue(Value value) throws IOException {
    // Try to get the internal ID from the value itself
    boolean isOwnValue = isOwnValue(value);

    if (isOwnValue) {
      NativeValue nativeValue = (NativeValue) value;

      if (revisionIsCurrent(nativeValue)) {
        // Value's ID is still current
        int id = nativeValue.getInternalID();

        if (id != NativeValue.UNKNOWN_ID) {
          return id;
        }
      }
    }

    // ID not stored in value itself, try the ID cache
    Integer cachedID = valueIDCache.get(value);

    if (cachedID != null) {
      int id = cachedID.intValue();

      if (isOwnValue) {
        // Store id in value for fast access in any consecutive calls
        ((NativeValue) value).setInternalID(id, revision);
      }

      return id;
    }

    // Unable to get internal ID in a cheap way, just store it in the data
    // store which will handle duplicates
    byte[] valueData = value2data(value, true);

    int id = dataStore.storeData(valueData);

    NativeValue nv = isOwnValue ? (NativeValue) value : getNativeValue(value);

    // Store id in value for fast access in any consecutive calls
    nv.setInternalID(id, revision);

    // Update cache
    valueIDCache.put(nv, new Integer(id));

    return id;
  }
예제 #18
0
 /** Gets the lock on a document. */
 public Lock getLock(final Serializable id) throws StorageException {
   serializationLock.lock();
   try {
     Lock lock;
     if (caching && (lock = lockCache.get(id)) != null) {
       return lock == NULL_LOCK ? null : lock;
     }
     // no transaction needed, single operation
     lock = mapper.getLock(id);
     if (caching) {
       lockCache.put(id, lock == null ? NULL_LOCK : lock);
     }
     return lock;
   } finally {
     serializationLock.unlock();
   }
 }
예제 #19
0
  public Transaction getTransaction(Sha256Hash hash) {
    Transaction tx = null;
    synchronized (transaction_cache) {
      tx = transaction_cache.get(hash);
    }
    if (tx == null) {

      SerializedTransaction s_tx = file_db.getTransactionMap().get(hash);

      if (s_tx != null) {
        tx = s_tx.getTx(params);
        synchronized (transaction_cache) {
          transaction_cache.put(hash, SerializedTransaction.scrubTransaction(params, tx));
        }
      }
    }
    return tx;
  }
예제 #20
0
    public String format(double value) {
      String f = cache.get(value);
      if (f == null) {
        f = String.format(format, value);
        cache.put(value, f);
        //                if ( cache.usedEntries() < maxCacheSize ) {
        //                    System.out.printf("CACHE size %d%n", cache.usedEntries());
        //                } else {
        //                    System.out.printf("CACHE is full %f%n", value);
        //                }
        //            }
        //            } else {
        //                System.out.printf("CACHE hit %f%n", value);
        //            }
      }

      return f;
    }
예제 #21
0
  /**
   * Gets the value for the specified ID.
   *
   * @param id A value ID.
   * @return The value for the ID, or <tt>null</tt> no such value could be found.
   * @exception IOException If an I/O error occurred.
   */
  public NativeValue getValue(int id) throws IOException {
    // Check value cache
    Integer cacheID = new Integer(id);
    NativeValue resultValue = valueCache.get(cacheID);

    if (resultValue == null) {
      // Value not in cache, fetch it from file
      byte[] data = dataStore.getData(id);

      if (data != null) {
        resultValue = data2value(id, data);

        // Store value in cache
        valueCache.put(cacheID, resultValue);
      }
    }

    return resultValue;
  }
예제 #22
0
  private int getNamespaceID(String namespace, boolean create) throws IOException {
    Integer cacheID = namespaceIDCache.get(namespace);
    if (cacheID != null) {
      return cacheID.intValue();
    }

    byte[] namespaceData = namespace.getBytes("UTF-8");

    int id;
    if (create) {
      id = dataStore.storeData(namespaceData);
    } else {
      id = dataStore.getID(namespaceData);
    }

    if (id != -1) {
      namespaceIDCache.put(namespace, new Integer(id));
    }

    return id;
  }
예제 #23
0
 /** Unlocks a document. */
 public Lock removeLock(final Serializable id, final String owner) throws StorageException {
   serializationLock.lock();
   try {
     Lock oldLock = null;
     if (caching && (oldLock = lockCache.get(id)) == NULL_LOCK) {
       return null;
     }
     if (oldLock != null && !canLockBeRemoved(oldLock, owner)) {
       // existing mismatched lock, flag failure
       oldLock = new Lock(oldLock, true);
     } else {
       if (oldLock == null) {
         oldLock =
             callInTransaction(
                 new Callable<Lock>() {
                   @Override
                   public Lock call() throws Exception {
                     return mapper.removeLock(id, owner, false);
                   }
                 });
       } else {
         // we know the previous lock, we can force
         // no transaction needed, single operation
         mapper.removeLock(id, owner, true);
       }
     }
     if (caching) {
       if (oldLock != null && oldLock.getFailed()) {
         // failed, but we now know the existing lock
         lockCache.put(id, new Lock(oldLock, false));
       } else {
         lockCache.put(id, NULL_LOCK);
       }
     }
     return oldLock;
   } finally {
     serializationLock.unlock();
   }
 }
예제 #24
0
 /*
  * Method to search the cache
  *
  * @param userId
  * @return Document
  */
 private Document findInCache(String userId) {
   String key;
   Set<String> keys = lruCache.getKeySet();
   Iterator<String> itr = keys.iterator();
   while (itr.hasNext()) {
     key = itr.next();
     Document data = lruCache.get(key);
     // check if email in profile object is same as input userId
     String email = "";
     try {
       email = DOMUtil.value(data, Profile.xpathMap.get("email"), Profile.nameSpaceCtx);
     } catch (XMLException e) {
       continue;
     }
     // cache hit
     if (StringUtil.equalsIgnoreCase(email, userId)) {
       return data;
     }
   }
   // Cache miss
   return null;
 }
예제 #25
0
 protected Lock setLockInternal(final Serializable id, final Lock lock) throws StorageException {
   serializationLock.lock();
   try {
     Lock oldLock;
     if (caching && (oldLock = lockCache.get(id)) != null && oldLock != NULL_LOCK) {
       return oldLock;
     }
     oldLock =
         callInTransaction(
             new Callable<Lock>() {
               @Override
               public Lock call() throws Exception {
                 return mapper.setLock(id, lock);
               }
             });
     if (caching && oldLock == null) {
       lockCache.put(id, lock == null ? NULL_LOCK : lock);
     }
     return oldLock;
   } finally {
     serializationLock.unlock();
   }
 }
예제 #26
0
  private void putInternal(Block block, StatusContext ctx) {
    long t1 = System.currentTimeMillis();
    Sha256Hash hash = block.getHash();

    ctx.setStatus("BLOCK_CHECK_EXIST");
    if (file_db.getBlockMap().containsKey(hash)) {
      imported_blocks.incrementAndGet();
      return;
    }
    // Mark block as in progress

    Semaphore block_wait_sem;
    synchronized (in_progress) {
      block_wait_sem = in_progress.get(hash);
      if (block_wait_sem == null) {
        block_wait_sem = new Semaphore(0);
        in_progress.put(hash, block_wait_sem);
      }
    }

    // Kick off threaded storage of transactions
    int size = 0;

    ctx.setStatus("BLOCK_TX_CACHE_INSERT");
    synchronized (transaction_cache) {
      for (Transaction tx : block.getTransactions()) {
        transaction_cache.put(tx.getHash(), SerializedTransaction.scrubTransaction(params, tx));
      }
    }

    ctx.setStatus("BLOCK_TX_ENQUE");
    LinkedList<Sha256Hash> tx_list = new LinkedList<Sha256Hash>();

    Collection<Map.Entry<String, Sha256Hash>> addrTxLst =
        new LinkedList<Map.Entry<String, Sha256Hash>>();
    Map<Sha256Hash, SerializedTransaction> txs_map =
        new HashMap<Sha256Hash, SerializedTransaction>();

    for (Transaction tx : block.getTransactions()) {
      imported_transactions.incrementAndGet();
      Collection<String> addrs = getAllAddresses(tx, true);

      for (String addr : addrs) {
        addrTxLst.add(
            new java.util.AbstractMap.SimpleEntry<String, Sha256Hash>(addr, tx.getHash()));
      }

      txs_map.put(tx.getHash(), new SerializedTransaction(tx));

      tx_list.add(tx.getHash());
      size++;
    }

    ctx.setStatus("TX_SAVEALL");
    file_db.getTransactionMap().putAll(txs_map);

    ctx.setStatus("BLOCK_TX_MAP_ADD");
    file_db.addTxsToBlockMap(tx_list, hash);

    ctx.setStatus("ADDR_SAVEALL");
    file_db.addAddressesToTxMap(addrTxLst);

    int h = block_store.getHeight(hash);

    for (Transaction tx : block.getTransactions()) {
      Collection<String> addrs = getAllAddresses(tx, true);
      ctx.setStatus("TX_NOTIFY");
      jelly.getElectrumNotifier().notifyNewTransaction(tx, addrs, h);
      ctx.setStatus("TX_DONE");
    }

    // Once all transactions are in, check for prev block in this store

    ctx.setStatus("BLOCK_WAIT_PREV");
    Sha256Hash prev_hash = block.getPrevBlockHash();

    waitForBlockStored(prev_hash);

    // System.out.println("Block " + hash + " " + Util.measureSerialization(new
    // SerializedBlock(block)));

    ctx.setStatus("BLOCK_SAVE");
    file_db.getBlockMap().put(hash, new SerializedBlock(block));

    block_wait_sem.release(1024);
    boolean wait_for_utxo = false;
    if (jelly.isUpToDate() && jelly.getUtxoTrieMgr().isUpToDate()) {
      wait_for_utxo = true;
    }

    jelly.getUtxoTrieMgr().notifyBlock(wait_for_utxo);
    if (wait_for_utxo) {
      jelly.getEventLog().alarm("UTXO root hash: " + jelly.getUtxoTrieMgr().getRootHash());
    }
    jelly.getElectrumNotifier().notifyNewBlock(block);

    long t2 = System.currentTimeMillis();
    DecimalFormat df = new DecimalFormat("0.000");
    double sec = (t2 - t1) / 1000.0;

    if (h % block_print_every == 0) {
      jelly
          .getEventLog()
          .alarm(
              "Saved block: "
                  + hash
                  + " - "
                  + h
                  + " - "
                  + size
                  + " ("
                  + df.format(sec)
                  + " seconds)");
    }
    jelly
        .getEventLog()
        .log(
            "Saved block: "
                + hash
                + " - "
                + h
                + " - "
                + size
                + " ("
                + df.format(sec)
                + " seconds)");

    imported_blocks.incrementAndGet();
  }
예제 #27
0
  /**
   * Gets the ID for the specified value.
   *
   * @param value A value.
   * @return The ID for the specified value, or {@link NativeValue#UNKNOWN_ID} if no such ID could
   *     be found.
   * @exception IOException If an I/O error occurred.
   */
  public int getID(Value value) throws IOException {
    // Try to get the internal ID from the value itself
    boolean isOwnValue = isOwnValue(value);

    if (isOwnValue) {
      NativeValue nativeValue = (NativeValue) value;

      if (revisionIsCurrent(nativeValue)) {
        int id = nativeValue.getInternalID();

        if (id != NativeValue.UNKNOWN_ID) {
          return id;
        }
      }
    }

    // Check cache
    Integer cachedID = valueIDCache.get(value);

    if (cachedID != null) {
      int id = cachedID.intValue();

      if (isOwnValue) {
        // Store id in value for fast access in any consecutive calls
        ((NativeValue) value).setInternalID(id, revision);
      }

      return id;
    }

    // ID not cached, search in file
    byte[] data = value2data(value, false);

    if (data == null && value instanceof Literal) {
      data = literal2legacy((Literal) value);
    }

    if (data != null) {
      int id = dataStore.getID(data);

      if (id == NativeValue.UNKNOWN_ID && value instanceof Literal) {
        id = dataStore.getID(literal2legacy((Literal) value));
      }

      if (id != NativeValue.UNKNOWN_ID) {
        if (isOwnValue) {
          // Store id in value for fast access in any consecutive calls
          ((NativeValue) value).setInternalID(id, revision);
        } else {
          // Store id in cache
          NativeValue nv = getNativeValue(value);
          nv.setInternalID(id, revision);
          valueIDCache.put(nv, new Integer(id));
        }
      }

      return id;
    }

    return NativeValue.UNKNOWN_ID;
  }