예제 #1
0
  /**
   * Method to remove a value from the Map.
   *
   * @param key The key for the value.
   * @return The value removed.
   */
  public synchronized Object remove(Object key) {
    makeDirty();

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    Object removed = null;
    Object delegateRemoved = delegate.remove(key);
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(
                new MapRemoveOperation(ownerOP, backingStore, key, delegateRemoved));
        removed = delegateRemoved;
      } else {
        removed = backingStore.remove(ownerOP, key);
      }
    } else {
      removed = delegateRemoved;
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return removed;
  }
예제 #2
0
  /**
   * Method to add a Map of values to this map.
   *
   * @param m The Map to add
   */
  public synchronized void putAll(java.util.Map m) {
    makeDirty();

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        Iterator iter = m.entrySet().iterator();
        while (iter.hasNext()) {
          java.util.Map.Entry entry = (java.util.Map.Entry) iter.next();
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new MapPutOperation(ownerOP, backingStore, entry.getKey(), entry.getValue()));
        }
      } else {
        backingStore.putAll(ownerOP, m);
      }
    }
    delegate.putAll(m);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
  }
예제 #3
0
  /**
   * Accessor for the set of keys in the Map.
   *
   * @return Set of keys.
   */
  public synchronized java.util.Set keySet() {
    if (useCache) {
      loadFromStore();
    } else if (backingStore != null) {
      return new Set(ownerOP, ownerMmd, false, backingStore.keySetStore());
    }

    return delegate.keySet();
  }
예제 #4
0
  /**
   * Accessor for the value stored against a key.
   *
   * @param key The key
   * @return The value.
   */
  public synchronized Object get(Object key) {
    if (useCache) {
      loadFromStore();
    } else if (backingStore != null) {
      return backingStore.get(ownerOP, key);
    }

    return delegate.get(key);
  }
예제 #5
0
  /**
   * Accessor for the set of values in the Map.
   *
   * @return Set of values.
   */
  public synchronized Collection values() {
    if (useCache) {
      loadFromStore();
    } else if (backingStore != null) {
      return new org.datanucleus.store.types.wrappers.backed.Collection(
          ownerOP, ownerMmd, true, backingStore.valueCollectionStore());
    }

    return delegate.values();
  }
예제 #6
0
  /**
   * Method to return the size of the Map.
   *
   * @return The size
   */
  public synchronized int size() {
    if (useCache && isCacheLoaded) {
      // If the "delegate" is already loaded, use it
      return delegate.size();
    } else if (backingStore != null) {
      return backingStore.entrySetStore().size(ownerOP);
    }

    return delegate.size();
  }
예제 #7
0
  /**
   * Utility to check if a value is contained in the Map.
   *
   * @param value The value to check
   * @return Whether it is contained
   */
  public synchronized boolean containsValue(Object value) {
    if (useCache && isCacheLoaded) {
      // If the "delegate" is already loaded, use it
      return delegate.containsValue(value);
    } else if (backingStore != null) {
      return backingStore.containsValue(ownerOP, value);
    }

    return delegate.containsValue(value);
  }
예제 #8
0
  /** Method to clear the Map. */
  public synchronized void clear() {
    makeDirty();
    delegate.clear();

    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new MapClearOperation(ownerOP, backingStore));
      } else {
        backingStore.clear(ownerOP);
      }
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
  }
예제 #9
0
  /**
   * Method to add a value to the Map.
   *
   * @param key The key for the value.
   * @param value The value
   * @return The previous value against this key (if any).
   */
  public synchronized Object put(Object key, Object value) {
    // Reject inappropriate values
    if (!allowNulls) {
      if (value == null) {
        throw new NullPointerException(
            "Nulls not allowed for map at field " + ownerMmd.getName() + " but value is null");
      }
      if (key == null) {
        throw new NullPointerException(
            "Nulls not allowed for map at field " + ownerMmd.getName() + " but key is null");
      }
    }

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    makeDirty();

    Object oldValue = null;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new MapPutOperation(ownerOP, backingStore, key, value));
      } else {
        oldValue = backingStore.put(ownerOP, key, value);
      }
    }
    Object delegateOldValue = delegate.put(key, value);
    if (backingStore == null) {
      oldValue = delegateOldValue;
    } else if (SCOUtils.useQueuedUpdate(ownerOP)) {
      oldValue = delegateOldValue;
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return oldValue;
  }
예제 #10
0
 /**
  * Method to update an embedded value in this map.
  *
  * @param value The value
  * @param fieldNumber Number of field in the value
  * @param newValue New value for this field
  */
 public void updateEmbeddedValue(Object value, int fieldNumber, Object newValue) {
   if (backingStore != null) {
     backingStore.updateEmbeddedValue(ownerOP, value, fieldNumber, newValue);
   }
 }
예제 #11
0
 /**
  * Method to update an embedded key in this map.
  *
  * @param key The key
  * @param fieldNumber Number of field in the key
  * @param newValue New value for this field
  */
 public void updateEmbeddedKey(Object key, int fieldNumber, Object newValue) {
   if (backingStore != null) {
     backingStore.updateEmbeddedKey(ownerOP, key, fieldNumber, newValue);
   }
 }
예제 #12
0
  /**
   * Method to initialise the SCO from an existing value.
   *
   * @param m The object to set from
   * @param forInsert Whether the object needs inserting in the datastore with this value
   * @param forUpdate Whether to update the datastore with this value
   */
  public synchronized void initialise(java.util.Map m, boolean forInsert, boolean forUpdate) {
    if (m != null) {
      // Check for the case of serialised maps, and assign ObjectProviders to any PC keys/values
      // without
      if (SCOUtils.mapHasSerialisedKeysAndValues(ownerMmd)
          && (ownerMmd.getMap().keyIsPersistent() || ownerMmd.getMap().valueIsPersistent())) {
        ExecutionContext ec = ownerOP.getExecutionContext();
        Iterator iter = m.entrySet().iterator();
        while (iter.hasNext()) {
          Map.Entry entry = (Map.Entry) iter.next();
          Object key = entry.getKey();
          Object value = entry.getValue();
          if (ownerMmd.getMap().keyIsPersistent()) {
            ObjectProvider objSM = ec.findObjectProvider(key);
            if (objSM == null) {
              objSM =
                  ec.getNucleusContext()
                      .getObjectProviderFactory()
                      .newForEmbedded(ec, key, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
            }
          }
          if (ownerMmd.getMap().valueIsPersistent()) {
            ObjectProvider objSM = ec.findObjectProvider(value);
            if (objSM == null) {
              objSM =
                  ec.getNucleusContext()
                      .getObjectProviderFactory()
                      .newForEmbedded(ec, value, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
            }
          }
        }
      }

      if (backingStore != null && useCache && !isCacheLoaded) {
        // Mark as loaded
        isCacheLoaded = true;
      }

      if (forInsert) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }

        makeDirty();
        if (useCache) {
          // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
          loadFromStore();
        }
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            Iterator iter = m.entrySet().iterator();
            while (iter.hasNext()) {
              Map.Entry entry = (Map.Entry) iter.next();
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(
                      new MapPutOperation(ownerOP, backingStore, entry.getKey(), entry.getValue()));
            }
          } else {
            backingStore.putAll(ownerOP, m);
          }
        }
        delegate.putAll(m);
      } else if (forUpdate) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }

        // TODO This is clear+putAll. Improve it to work out what is changed
        delegate.clear();
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // If not yet flushed to store then no need to add to queue (since will be handled via
            // insert)
            if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(new MapClearOperation(ownerOP, backingStore));
            }
          } else {
            backingStore.clear(ownerOP);
          }
        }
        if (useCache) {
          // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
          loadFromStore();
        }

        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // If not yet flushed to store then no need to add to queue (since will be handled via
            // insert)
            if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
              Iterator iter = m.entrySet().iterator();
              while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                ownerOP
                    .getExecutionContext()
                    .addOperationToQueue(
                        new MapPutOperation(
                            ownerOP, backingStore, entry.getKey(), entry.getValue()));
              }
            }
          } else {
            backingStore.putAll(ownerOP, m);
          }
        }
        delegate.putAll(m);
        makeDirty();
      } else {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }
        delegate.clear();
        delegate.putAll(m);
      }
    }
  }