public V put(K key, V value) {
    memcachedClient.set(key.toString(), NOT_EXPIRE, value);

    // Key index update
    if (!keySet().contains(key)) {
      String index = (String) get(name + KEYS_LOCATION);
      index = index + key.toString() + KEY_SEPARATOR;
      memcachedClient.set(name + KEYS_LOCATION, NOT_EXPIRE, index);
    }

    return value;
  }
 synchronized boolean remove(long hash, K key) {
   int h = smallMap.startSearch(hash);
   boolean found = false;
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       break;
     }
     bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
     K key2 = getKey();
     if (equals(key, key2)) {
       usedSet.clear(pos);
       smallMap.remove(h, pos);
       found = true;
       this.size--;
       break;
     }
   }
   K key2 = key instanceof CharSequence ? (K) key.toString() : key;
   DirectStore remove = map.remove(key2);
   if (remove == null) return found;
   offHeapUsed -= remove.size();
   remove.free();
   this.size--;
   return true;
 }
 synchronized V get(long hash, K key, V value) {
   smallMap.startSearch(hash);
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       K key2 = key instanceof CharSequence ? (K) key.toString() : key;
       final DirectStore store = map.get(key2);
       if (store == null) return null;
       bytes.storePositionAndSize(store, 0, store.size());
       break;
     } else {
       bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
       K key2 = getKey();
       if (equals(key, key2)) break;
     }
   }
   if (bytesMarshallable) {
     try {
       V v = value == null ? (V) NativeBytes.UNSAFE.allocateInstance(vClass) : value;
       ((BytesMarshallable) v).readMarshallable(bytes);
       return v;
     } catch (InstantiationException e) {
       throw new AssertionError(e);
     }
   }
   return (V) bytes.readObject();
 }
Exemple #4
0
  /**
   * Returns the string representation of this {@code Hashtable}.
   *
   * @return the string representation of this {@code Hashtable}.
   */
  @Override
  public synchronized String toString() {
    StringBuilder result = new StringBuilder(CHARS_PER_ENTRY * size);
    result.append('{');
    Iterator<Entry<K, V>> i = entrySet().iterator();
    boolean hasMore = i.hasNext();
    while (hasMore) {
      Entry<K, V> entry = i.next();

      K key = entry.getKey();
      result.append(key == this ? "(this Map)" : key.toString());

      result.append('=');

      V value = entry.getValue();
      result.append(value == this ? "(this Map)" : value.toString());

      if (hasMore = i.hasNext()) {
        result.append(", ");
      }
    }

    result.append('}');
    return result.toString();
  }
 public synchronized V put(K key, V value) {
   V v = get(key);
   if (value != null) {
     V retVal = super.put(key, value);
     firePropertyChange(new PropertyChangeEvent(this, key.toString(), v, value));
     return retVal;
   }
   return null;
 }
Exemple #6
0
 /*@ ensures \result != "";*/
 public String toString() {
   return leftNode.toString(Node.Placement.RIGHT)
       + "("
       + key.toString()
       + ","
       + obj.toString()
       + ")"
       + rightNode.toString(Node.Placement.LEFT);
 }
 @Override
 public void write(K key, V value) throws IOException {
   try {
     out.writeBytes(key.toString() + "," + value.toString());
     out.writeBytes("\r\n");
   } catch (Exception ex) {
     out.close();
   }
 }
 @SuppressWarnings("unchecked")
 @Override
 protected <K> K onSetProperty(String[] taPath, K toValue) {
   return (K)
       m_oProperties.setProperty(
           denormalise(taPath),
           toValue != null
               ? Java.isPrimitive(toValue) ? toValue.toString() : Utilities.toJSONString(toValue)
               : null);
 }
 @Override
 public String toString() {
   return new StringBuilder()
       .append('{')
       .append(singleKey.toString())
       .append('=')
       .append(singleValue.toString())
       .append('}')
       .toString();
 }
  public int hashFunction(K dkey) {
    String hashstr = dkey.toString();
    int hashCode = 0;
    for (int i = 0; i < hashstr.length(); i++) {
      int code = (int) hashstr.charAt(i) * 32 ^ i;
      hashCode = hashCode + code;
    }

    hashCode = hashCode % size;
    // System.out.println(hashCode);
    return hashCode;
  }
Exemple #11
0
 /**
  * Using the key's {@link Object#toString() toString()} method, generates a string suitable for
  * using as a filename.
  *
  * @param key
  * @return a string uniquely representing the the key.
  */
 public String hash(K key) {
   final byte[] ba;
   synchronized (hash) {
     hash.update(key.toString().getBytes());
     ba = hash.digest();
   }
   final BigInteger bi = new BigInteger(1, ba);
   final String result = bi.toString(16);
   if (result.length() % 2 != 0) {
     return "0" + result;
   }
   return result;
 }
  public String getStratsAndStatesStringForKey(final int key) {
    if (keyStrings.get(key) == null) {
      StringBuilder b = new StringBuilder();
      for (int i = 0; i < stratifiers.size(); i++) {
        final K strat = stratifiers.get(i);
        final Object stratValue = stratifierValuesByKey.get(key).get(i);
        b.append(strat.toString()).append(":").append(stratValue.toString());
      }
      keyStrings.set(key, b.toString());
    }

    return keyStrings.get(key);
  }
Exemple #13
0
 /**
  * toString methode: creates a String representation of the object
  *
  * @return the String representation
  * @author info.vancauwenberge.tostring plugin
  */
 public String toString() {
   StringBuffer buffer = new StringBuffer();
   ArrayList keys = new ArrayList(hashtable.keySet());
   Collections.sort(keys);
   for (K key : (List<K>) keys) {
     ArrayList<O> vals = get(key);
     buffer.append(key.toString()).append("(").append(vals.size()).append(")").append(":\n");
     for (O val : vals) {
       buffer.append("\t").append(val.toString()).append("\n");
     }
   }
   return buffer.toString();
 }
 private void notifyEventToChild(@NotNull MapEvent<K, V> changeEvent) {
   K key = changeEvent.getKey();
   if (asset.hasChildren() && key instanceof CharSequence) {
     String keyStr = key.toString();
     Asset child = asset.getChild(keyStr);
     if (child != null) {
       SubscriptionCollection subscription = child.subscription(false);
       if (subscription instanceof MapSimpleSubscription) {
         //                    System.out.println(changeEvent.toString().substring(0, 100));
         ((SimpleSubscription) subscription).notifyMessage(changeEvent.getValue());
       }
     }
   }
 }
 /**
  * Inserts a key,value pair into the table using the specified values. The index of insertion is
  * calculated by adding all the characters in the string representation of the key.
  *
  * @param key the key of the element to be inserted
  * @param value the value of the element to be inserted
  */
 public boolean put(K key, V value) {
   try {
     MyHashEntry<K, V> entry = new MyHashEntry<K, V>(key, value);
     String keyString = key.toString();
     int code = 0;
     for (int i = 0; i < keyString.length(); i++) code += (int) keyString.charAt(i);
     if (code > maxSize) code = code % maxSize;
     if (this.size() / (double) (this.maxSize) > .75) this.reHash();
     table[code].add(entry);
     return true;
   } catch (NullPointerException e) {
     return false;
   }
 }
 @Override
 public <K> boolean addMultiField(final String name, final K[] values) {
   if (values == null) {
     return false;
   } else {
     boolean wasAdded = false;
     for (K value : values) {
       if (value == null) continue;
       super.addField(name, value.toString());
       wasAdded = true;
     }
     return wasAdded;
   }
 }
  public String describe() {
    StringBuilder result = new StringBuilder();

    result.append("List: ");
    for (K k : this) {
      result.append(String.format("%s ", k.toString()));
    }
    result.append("\n");

    result.append(String.format("Length: %d\n", this.length));
    result.append(String.format("Head: %s\n", this.head.getElem().toString()));
    result.append(String.format("Tail: %s\n", this.tail.getElem().toString()));

    return result.toString();
  }
  private int getIndex(K key) {
    // Digit Folding method

    int sum = 0;

    String strKey = key.toString();

    for (int i = 0; i < strKey.length(); i++) {
      Character ch = strKey.charAt(i);
      sum += Character.getNumericValue(ch);
    }

    int hash = sum % table.length;
    return hash;
  }
 synchronized boolean containsKey(long hash, K key) {
   smallMap.startSearch(hash);
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       K key2 = key instanceof CharSequence ? (K) key.toString() : key;
       return map.containsKey(key2);
     }
     bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
     K key2 = getKey();
     if (equals(key, key2)) {
       return true;
     }
   }
 }
  public synchronized Closeable addResource(final K key, final boolean isShared)
      throws IOException {
    boolean isInUse = true;
    final Map<K, Set<Closeable>> unexpected = (isShared) ? exclusive : shared;
    final Map<K, Set<Closeable>> expected = (isShared) ? shared : exclusive;

    final Set<Closeable> unexpectedCounter = unexpected.get(key);
    Set<Closeable> expectedCounter = expected.get(key);

    for (int i = 0; i < 10 && isInUse; i++) {
      isInUse = unexpectedCounter != null && unexpectedCounter.size() > 0;

      if (!isInUse) {
        if (expectedCounter == null) {
          expectedCounter = new WeakSet<Closeable>();
          expected.put(key, expectedCounter);
        }
        isInUse = !isShared && expectedCounter.size() > 0;
      }

      if (isInUse) {
        try {
          wait(200);
        } catch (InterruptedException e) {
          break;
        }
      }
    }

    if (isInUse) {
      try {
        FSException.io(
            isShared ? "EXC_CannotGetSharedAccess" : "EXC_CannotGetExclusiveAccess",
            key.toString()); // NOI18N
      } catch (IOException x) {
        assert addStack(x, unexpectedCounter, expectedCounter);
        throw x;
      }
    }

    final Closeable retVal = new Closeable(key, isShared);
    expectedCounter.add(retVal);
    return retVal;
  }
  /**
   * Returns a string representation of this {@code Hashtable} object in the form of a set of
   * entries, enclosed in braces and separated by the ASCII characters "<code> ,&nbsp;</code>"
   * (comma and space). Each entry is rendered as the key, an equals sign {@code =}, and the
   * associated element, where the {@code toString} method is used to convert the key and element to
   * strings.
   *
   * @return a string representation of this hashtable
   */
  public synchronized String toString() {
    int max = size() - 1;
    if (max == -1) return "{}";

    StringBuilder sb = new StringBuilder();
    Iterator<Map.Entry<K, V>> it = entrySet().iterator();

    sb.append('{');
    for (int i = 0; ; i++) {
      Map.Entry<K, V> e = it.next();
      K key = e.getKey();
      V value = e.getValue();
      sb.append(key == this ? "(this Map)" : key.toString());
      sb.append('=');
      sb.append(value == this ? "(this Map)" : value.toString());

      if (i == max) return sb.append('}').toString();
      sb.append(", ");
    }
  }
  @Override
  public void acquireAndApplyMutation(Function<MutationBatch, Boolean> callback)
      throws NotUniqueException, Exception {
    try {
      // Phase 1: Write a unique column
      MutationBatch m = keyspace.prepareMutationBatch().setConsistencyLevel(consistencyLevel);
      if (data == null) {
        m.withRow(columnFamily, key).putEmptyColumn(uniqueColumn, ttl);
      } else {
        m.withRow(columnFamily, key).putColumn(uniqueColumn, data, ttl);
      }
      m.execute();

      // Phase 2: Read back all columns. There should be only 1
      ColumnList<C> result =
          keyspace
              .prepareQuery(columnFamily)
              .setConsistencyLevel(consistencyLevel)
              .getKey(key)
              .execute()
              .getResult();

      if (result.size() != 1) {
        throw new NotUniqueException(key.toString());
      }

      // Phase 3: Persist the uniqueness with
      m = keyspace.prepareMutationBatch().setConsistencyLevel(consistencyLevel);
      if (callback != null) callback.apply(m);

      if (data == null) {
        m.withRow(columnFamily, key).putEmptyColumn(uniqueColumn, null);
      } else {
        m.withRow(columnFamily, key).putColumn(uniqueColumn, data, null);
      }
      m.execute();
    } catch (Exception e) {
      release();
      throw e;
    }
  }
Exemple #23
0
 @Override
 public String toString() {
   /* Cast: netbeans/compiler workaround */
   return _klass.toString();
 }
Exemple #24
0
 @SuppressWarnings("unchecked")
 public V put(K key, V value) {
   if (key instanceof String) key = (K) key.toString().toLowerCase();
   return super.put(key, value);
 }
 public String toString() {
   return key.toString() + "=" + value.toString();
 }
 protected final void syncRemote(K key, Broadcaster.EVENT syncAction) {
   Broadcaster.getInstance().queue(syncType.getType(), syncAction.getType(), key.toString());
 }
Exemple #27
0
    // O: 1-{4,5,6,7} 0-{0,1,2,3}
    // P: 1-{2,3,6,7} 0-{0,1,4,5}
    // S: 1-{1,3,5,7} 0-{0,2,4,6}
    public int getPartition(K key, V value, int numReduceTasks) {

      String line = key.toString();

      if (line.startsWith("O")) // order
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }
        if (keyId % 2 == 1) {
          // 4,5,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,2,3
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 3;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("P")) // part
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 2,3,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,4,5
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 5;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("S")) // supplier
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 1,3,5,7
          if (placeId.equalsIgnoreCase("A")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,2,4,6
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 6;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("L")) // lineitem
      {
        String[] keyIdS = line.substring(1).split("[+]");

        Integer keyId0 = 0;
        Integer keyId1 = 0;
        Integer keyId2 = 0;

        try {
          keyId0 = Integer.parseInt(keyIdS[0].substring(1)) % 2; // Order
          keyId1 = Integer.parseInt(keyIdS[1].substring(1)) % 2; // Part
          keyId2 = Integer.parseInt(keyIdS[2].substring(1)) % 2; // Supplier
        } catch (Exception ex) {
          return 0;
        }
        return 4 * keyId0 + 2 * keyId1 + 1 * keyId2;

      } else {
        return 0;
      }
    }
 public String toString() {
   return key.toString();
 }
 @Override
 public String toString() {
   if (matcher != null)
     return clazz.getSimpleName() + "#" + key.toString() + "; matcher=" + matcher;
   else return clazz.getSimpleName() + "#" + key.toString();
 }
    synchronized void put(long hash, K key, V value, boolean ifPresent, boolean ifAbsent) {
      // search for the previous entry
      int h = smallMap.startSearch(hash);
      boolean foundSmall = false, foundLarge = false;
      while (true) {
        int pos = smallMap.nextPos();
        if (pos < 0) {
          K key2 = key instanceof CharSequence ? (K) key.toString() : key;
          final DirectStore store = map.get(key2);
          if (store == null) {
            if (ifPresent && !ifAbsent) return;
            break;
          }
          if (ifAbsent) return;
          bytes.storePositionAndSize(store, 0, store.size());
          foundLarge = true;
          break;
        } else {
          bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
          K key2 = getKey();
          if (equals(key, key2)) {
            if (ifAbsent && !ifPresent) return;
            foundSmall = true;
            break;
          }
        }
      }

      tmpBytes.clear();
      if (csKey)
        //noinspection ConstantConditions
        tmpBytes.writeUTFΔ((CharSequence) key);
      else tmpBytes.writeObject(key);
      long startOfValuePos = tmpBytes.position();
      if (bytesMarshallable) ((BytesMarshallable) value).writeMarshallable(tmpBytes);
      else tmpBytes.writeObject(value);
      long size = tmpBytes.position();
      if (size <= smallEntrySize) {
        if (foundSmall) {
          bytes.position(0);
          bytes.write(tmpBytes, 0, size);
          return;
        } else if (foundLarge) {
          remove(hash, key);
        }
        // look for a free spot.
        int position = h & (entriesPerSegment - 1);
        int free = usedSet.nextClearBit(position);
        if (free >= entriesPerSegment) free = usedSet.nextClearBit(0);
        if (free < entriesPerSegment) {
          bytes.storePositionAndSize(store, free * smallEntrySize, smallEntrySize);
          bytes.write(tmpBytes, 0, size);
          smallMap.put(h, free);
          usedSet.set(free);
          this.size++;
          return;
        }
      }
      if (foundSmall) {
        remove(hash, key);
      } else if (foundLarge) {
        // can it be reused.
        if (bytes.capacity() <= size || bytes.capacity() - size < (size >> 3)) {
          bytes.write(tmpBytes, startOfValuePos, size);
          return;
        }
        remove(hash, key);
      }
      size = size - startOfValuePos;
      DirectStore store = new DirectStore(bmf, size);
      bytes.storePositionAndSize(store, 0, size);
      bytes.write(tmpBytes, startOfValuePos, size);
      K key2 = key instanceof CharSequence ? (K) key.toString() : key;
      map.put(key2, store);
      offHeapUsed += size;
      this.size++;
    }