/** @return The hashing values for the user source that were stored in the backup */
    public Hashing getHashing() {
      prisms.util.LongList mults = new prisms.util.LongList();
      prisms.util.LongList mods = new prisms.util.LongList();
      try {
        if (!"hashing".equals(theJsonReader.getNextProperty())) return null;
        JsonSerialReader.StructState rootState = theJsonReader.startObject();
        try {
          if (!"multiples".equals(theJsonReader.getNextProperty())) return null;
          theJsonReader.startArray();
          Number num;
          do {
            num = theJsonReader.parseNumber();
            if (num != null) mults.add(num.longValue());
          } while (num != null);
          theJsonReader.endArray(null);

          if (!"modulos".equals(theJsonReader.getNextProperty())) return null;
          theJsonReader.startArray();
          do {
            num = theJsonReader.parseNumber();
            if (num != null) mods.add(num.longValue());
          } while (num != null);
          theJsonReader.endArray(null);
        } finally {
          theJsonReader.endObject(rootState);
        }
      } catch (Exception e) {
        log.error("Could not read JSON backup data", e);
        return null;
      }
      Hashing ret = new Hashing();
      ret.setPrimaryHashing(mults.toArray(), mods.toArray());
      return ret;
    }
Пример #2
0
  /** Constructor for RegularImmutableBiMap that makes no assumptions about the input entries. */
  RegularImmutableBiMap(Entry<?, ?>[] entriesToAdd) {
    int n = entriesToAdd.length;
    int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR);
    this.mask = tableSize - 1;
    ImmutableMapEntry<K, V>[] keyTable = createEntryArray(tableSize);
    ImmutableMapEntry<K, V>[] valueTable = createEntryArray(tableSize);
    ImmutableMapEntry<K, V>[] entries = createEntryArray(n);
    int hashCode = 0;

    for (int i = 0; i < n; i++) {
      @SuppressWarnings("unchecked")
      Entry<K, V> entry = (Entry<K, V>) entriesToAdd[i];
      K key = entry.getKey();
      V value = entry.getValue();
      checkEntryNotNull(key, value);
      int keyHash = key.hashCode();
      int valueHash = value.hashCode();
      int keyBucket = Hashing.smear(keyHash) & mask;
      int valueBucket = Hashing.smear(valueHash) & mask;

      ImmutableMapEntry<K, V> nextInKeyBucket = keyTable[keyBucket];
      for (ImmutableMapEntry<K, V> keyEntry = nextInKeyBucket;
          keyEntry != null;
          keyEntry = keyEntry.getNextInKeyBucket()) {
        checkNoConflict(!key.equals(keyEntry.getKey()), "key", entry, keyEntry);
      }
      ImmutableMapEntry<K, V> nextInValueBucket = valueTable[valueBucket];
      for (ImmutableMapEntry<K, V> valueEntry = nextInValueBucket;
          valueEntry != null;
          valueEntry = valueEntry.getNextInValueBucket()) {
        checkNoConflict(!value.equals(valueEntry.getValue()), "value", entry, valueEntry);
      }
      ImmutableMapEntry<K, V> newEntry =
          (nextInKeyBucket == null && nextInValueBucket == null)
              ? new TerminalEntry<K, V>(key, value)
              : new NonTerminalBiMapEntry<K, V>(key, value, nextInKeyBucket, nextInValueBucket);
      keyTable[keyBucket] = newEntry;
      valueTable[valueBucket] = newEntry;
      entries[i] = newEntry;
      hashCode += keyHash ^ valueHash;
    }

    this.keyTable = keyTable;
    this.valueTable = valueTable;
    this.entries = entries;
    this.hashCode = hashCode;
  }
 /**
  * Constructor for RegularImmutableMap that takes as input an array of {@code TerminalEntry}
  * entries. Assumes that these entries have already been checked for null.
  *
  * <p>This allows reuse of the entry objects from the array in the actual implementation.
  */
 RegularImmutableMap(int size, TerminalEntry<?, ?>[] theEntries) {
   entries = createEntryArray(size);
   int tableSize = Hashing.closedTableSize(size, MAX_LOAD_FACTOR);
   table = createEntryArray(tableSize);
   mask = tableSize - 1;
   for (int entryIndex = 0; entryIndex < size; entryIndex++) {
     @SuppressWarnings("unchecked")
     TerminalEntry<K, V> entry = (TerminalEntry<K, V>) theEntries[entryIndex];
     K key = entry.getKey();
     int tableIndex = Hashing.smear(key.hashCode()) & mask;
     @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex];
     // prepend, not append, so the entries can be immutable
     ImmutableMapEntry<K, V> newEntry =
         (existing == null) ? entry : new NonTerminalMapEntry<K, V>(entry, existing);
     table[tableIndex] = newEntry;
     entries[entryIndex] = newEntry;
     checkNoConflictInBucket(key, newEntry, existing);
   }
 }
 protected byte[] configureTokenBytes(
     byte[] token, DataGraphConfig graph, Hashing hashing, PreDefinedFieldName tokenName) {
   byte[] result = token;
   ColumnKeyFieldConfig tokenConfig = graph.getColumnKeyField(tokenName);
   if (tokenConfig != null) {
     if (tokenConfig.isHash()) {
       result = hashing.toStringBytes(result);
     }
   }
   return result;
 }
 /** Constructor for RegularImmutableMap that makes no assumptions about the input entries. */
 RegularImmutableMap(Entry<?, ?>[] theEntries) {
   int size = theEntries.length;
   entries = createEntryArray(size);
   int tableSize = Hashing.closedTableSize(size, MAX_LOAD_FACTOR);
   table = createEntryArray(tableSize);
   mask = tableSize - 1;
   for (int entryIndex = 0; entryIndex < size; entryIndex++) {
     @SuppressWarnings("unchecked") // all our callers carefully put in only Entry<K, V>s
     Entry<K, V> entry = (Entry<K, V>) theEntries[entryIndex];
     K key = entry.getKey();
     V value = entry.getValue();
     checkEntryNotNull(key, value);
     int tableIndex = Hashing.smear(key.hashCode()) & mask;
     @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex];
     // prepend, not append, so the entries can be immutable
     ImmutableMapEntry<K, V> newEntry =
         (existing == null)
             ? new TerminalEntry<K, V>(key, value)
             : new NonTerminalMapEntry<K, V>(key, value, existing);
     table[tableIndex] = newEntry;
     entries[entryIndex] = newEntry;
     checkNoConflictInBucket(key, newEntry, existing);
   }
 }
Пример #6
0
 @Override
 public boolean contains(Object target) {
   if (target == null) {
     return false;
   }
   for (int i = Hashing.smear(target.hashCode()); true; i++) {
     Object candidate = table[i & mask];
     if (candidate == null) {
       return false;
     }
     if (candidate.equals(target)) {
       return true;
     }
   }
 }
Пример #7
0
  /** {@inheritDoc} */
  public boolean contains(final int value) {
    final int[] values = this.values;
    @DoNotSub final int mask = values.length - 1;
    @DoNotSub int index = Hashing.hash(value, mask);

    while (values[index] != missingValue) {
      if (values[index] == value) {
        return true;
      }

      index = next(index, mask);
    }

    return false;
  }
Пример #8
0
 @Override
 public K get(@Nullable Object value) {
   if (value == null) {
     return null;
   }
   int bucket = Hashing.smear(value.hashCode()) & mask;
   for (ImmutableMapEntry<K, V> entry = valueTable[bucket];
       entry != null;
       entry = entry.getNextInValueBucket()) {
     if (value.equals(entry.getValue())) {
       return entry.getKey();
     }
   }
   return null;
 }
Пример #9
0
 @Override
 @Nullable
 public V get(@Nullable Object key) {
   if (key == null) {
     return null;
   }
   int bucket = Hashing.smear(key.hashCode()) & mask;
   for (ImmutableMapEntry<K, V> entry = keyTable[bucket];
       entry != null;
       entry = entry.getNextInKeyBucket()) {
     if (key.equals(entry.getKey())) {
       return entry.getValue();
     }
   }
   return null;
 }
Пример #10
0
  /**
   * An int specialised version of {this#remove(Object)}.
   *
   * @param value the value to remove
   * @return true if the value was present, false otherwise
   */
  public boolean remove(final int value) {
    final int[] values = this.values;
    @DoNotSub final int mask = values.length - 1;
    @DoNotSub int index = Hashing.hash(value, mask);

    while (values[index] != missingValue) {
      if (values[index] == value) {
        values[index] = missingValue;
        compactChain(index);
        size--;
        return true;
      }

      index = next(index, mask);
    }

    return false;
  }
Пример #11
0
  /**
   * Primitive specialised overload of {this#add(Integer)}
   *
   * @param value the value to add
   * @return true if the collection has changed, false otherwise
   */
  public boolean add(final int value) {
    final int[] values = this.values;
    @DoNotSub final int mask = values.length - 1;
    @DoNotSub int index = Hashing.hash(value, mask);

    while (values[index] != missingValue) {
      if (values[index] == value) {
        return false;
      }

      index = next(index, mask);
    }

    values[index] = value;
    size++;

    if (size > resizeThreshold) {
      increaseCapacity();
    }

    return true;
  }
Пример #12
0
  private void rehash(@DoNotSub final int newCapacity) {
    @DoNotSub final int capacity = newCapacity;
    @DoNotSub final int mask = newCapacity - 1;
    resizeThreshold = (int) (newCapacity * loadFactor); // @DoNotSub

    final int[] tempValues = new int[capacity];
    final int missingValue = this.missingValue;
    Arrays.fill(tempValues, missingValue);

    for (final int value : values) {
      if (value != missingValue) {
        @DoNotSub int newHash = Hashing.hash(value, mask);
        while (tempValues[newHash] != missingValue) {
          newHash = ++newHash & mask;
        }

        tempValues[newHash] = value;
      }
    }

    values = tempValues;
  }
  @Override
  public V get(@Nullable Object key) {
    if (key == null) {
      return null;
    }
    int index = Hashing.smear(key.hashCode()) & mask;
    for (ImmutableMapEntry<K, V> entry = table[index];
        entry != null;
        entry = entry.getNextInKeyBucket()) {
      K candidateKey = entry.getKey();

      /*
       * Assume that equals uses the == optimization when appropriate, and that
       * it would check hash codes as an optimization when appropriate. If we
       * did these things, it would just make things worse for the most
       * performance-conscious users.
       */
      if (key.equals(candidateKey)) {
        return entry.getValue();
      }
    }
    return null;
  }
Пример #14
0
  @DoNotSub
  private void compactChain(int deleteIndex) {
    final int[] values = this.values;
    @DoNotSub final int mask = values.length - 1;

    @DoNotSub int index = deleteIndex;
    while (true) {
      index = next(index, mask);
      if (values[index] == missingValue) {
        return;
      }

      @DoNotSub final int hash = Hashing.hash(values[index], mask);

      if ((index < hash && (hash <= deleteIndex || deleteIndex <= index))
          || (hash <= deleteIndex && deleteIndex <= index)) {
        values[deleteIndex] = values[index];

        values[index] = missingValue;
        deleteIndex = index;
      }
    }
  }
    boolean exportData(prisms.ui.UI ui, prisms.ui.UI.DefaultProgressInformer pi, boolean global) {
      java.io.File exportFile =
          new java.io.File(
              theApps[0].getEnvironment().getLogger().getExposedDir() + ".exportedData.dat");
      if (exportFile.exists() && !prisms.util.FileSegmentizerOutputStream.delete(exportFile)) {
        ui.error(
            "Could not delete data exported on "
                + prisms.util.PrismsUtils.print(exportFile.lastModified()));
        log.error(
            "Could not delete data exported on "
                + prisms.util.PrismsUtils.print(exportFile.lastModified()));
        return false;
      }
      prisms.util.FileSegmentizerOutputStream fileStream = null;
      prisms.util.ExportStream exportStream;
      java.io.OutputStreamWriter streamWriter;
      JsonStreamWriter jsw;
      try {
        fileStream = new prisms.util.FileSegmentizerOutputStream(exportFile);
        exportStream = new prisms.util.ExportStream(fileStream);
        streamWriter = new java.io.OutputStreamWriter(exportStream);
        // streamWriter = new java.io.OutputStreamWriter(fileStream);
        jsw = new JsonStreamWriter(streamWriter);
      } catch (java.io.IOException e) {
        ui.error("Could not write data for export: " + e);
        log.error("Could not write data for export", e);
        if (fileStream != null)
          try {
            fileStream.close();
          } catch (java.io.IOException e2) {
          }
        prisms.util.FileSegmentizerOutputStream.delete(exportFile);
        return false;
      }
      boolean success = false;
      try {
        jsw.startObject();

        jsw.startProperty("exportTime");
        jsw.writeNumber(Long.valueOf(System.currentTimeMillis()));

        jsw.startProperty("instance");
        jsw.writeNumber(Integer.valueOf(theApps[0].getEnvironment().getIDs().getCenterID()));

        jsw.startProperty("hashing");
        Hashing hashing = theApps[0].getEnvironment().getUserSource().getHashing();
        jsw.startObject();
        jsw.startProperty("multiples");
        jsw.startArray();
        for (long h : hashing.getPrimaryMultiples()) jsw.writeNumber(Long.valueOf(h));
        jsw.endArray();
        jsw.startProperty("modulos");
        jsw.startArray();
        for (long h : hashing.getPrimaryModulos()) jsw.writeNumber(Long.valueOf(h));
        jsw.endArray();
        jsw.endObject();

        jsw.startProperty("data");
        jsw.startArray();
        java.util.HashSet<String> namespaces = new java.util.HashSet<String>();
        for (PrismsApplication app : theApps) {
          for (prisms.arch.event.PrismsProperty<?> property : app.getGlobalProperties()) {
            if (PrismsSynchronizer.class.isAssignableFrom(property.getType())) {
              PrismsSynchronizer sync = (PrismsSynchronizer) app.getGlobalProperty(property);
              if (sync == null || !(sync.getKeeper() instanceof DBRecordKeeper)) continue;
              exportData(ui, sync, jsw, namespaces, pi, global);
            }
          }
        }
        jsw.endArray();
        jsw.startProperty("passwords");
        jsw.startArray();
        for (User user : theApps[0].getEnvironment().getUserSource().getActiveUsers()) {
          prisms.arch.ds.UserSource.Password pwd =
              theApps[0].getEnvironment().getUserSource().getPassword(user);
          if (pwd == null) continue;
          jsw.startObject();
          jsw.startProperty("userName");
          jsw.writeString(user.getName());
          jsw.startProperty("passwordData");
          jsw.startArray();
          for (long h : pwd.hash) jsw.writeNumber(Long.valueOf(h));
          jsw.endArray();
          jsw.endObject();
        }
        jsw.endArray();
        jsw.endObject();
        jsw.close();
        streamWriter.close();
        fileStream.close();
        success = true;
        ui.info(
            "Data has been exported. On server restart after rebuild,"
                + " local data will be imported.");
        log.info(
            "Instance "
                + theApps[0].getEnvironment().getIDs().getLocalInstance().location
                + ": Data has been exported to "
                + exportFile.getCanonicalPath());
      } catch (java.io.IOException e) {
        ui.error("Data export failed: " + e);
        log.error("Data export failed", e);
      } catch (prisms.records.PrismsRecordException e) {
        ui.error("Data export failed: " + e);
        log.error("Data export failed", e);
      } finally {
        if (!success) {
          try {
            fileStream.close();
          } catch (java.io.IOException e2) {
          }
          prisms.util.FileSegmentizerOutputStream.delete(exportFile);
        }
      }
      return success;
    }
Пример #16
0
 public static String generate() {
   final SecureRandom random = new SecureRandom();
   random.setSeed(System.nanoTime());
   return Hashing.sha512Hash(random.toString());
 }
Пример #17
0
 public final int hashCode() {
   return Hashing.getHashCodeFor(name, host, port);
 }