Ejemplo n.º 1
0
  /**
   * Executes Put Operation immediately in current thread.
   *
   * <p>Notice: This is blocking I/O operation that should not be executed on the Main Thread, it
   * can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames. So
   * please, call this method on some background thread. See {@link WorkerThread}.
   *
   * @return non-null result of Put Operation.
   */
  @SuppressWarnings("unchecked")
  @WorkerThread
  @NonNull
  @Override
  public PutResult executeAsBlocking() {
    try {
      final StorIOSQLite.Internal internal = storIOSQLite.internal();

      final PutResolver<T> putResolver;

      if (explicitPutResolver != null) {
        putResolver = explicitPutResolver;
      } else {
        final SQLiteTypeMapping<T> typeMapping = internal.typeMapping((Class<T>) object.getClass());

        if (typeMapping == null) {
          throw new IllegalStateException(
              "Object does not have type mapping: "
                  + "object = "
                  + object
                  + ", object.class = "
                  + object.getClass()
                  + ", "
                  + "db was not affected by this operation, please add type mapping for this type");
        }

        putResolver = typeMapping.putResolver();
      }

      final PutResult putResult = putResolver.performPut(storIOSQLite, object);

      if (putResult.wasInserted() || putResult.wasUpdated()) {
        internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
      }

      return putResult;
    } catch (Exception exception) {
      throw new StorIOException(exception);
    }
  }
Ejemplo n.º 2
0
  /**
   * Returns number of updates from all {@link #results()}.
   *
   * @return number of updates from all {@link #results()}.
   */
  public int numberOfUpdates() {
    final Integer cachedValue = numberOfUpdatesCache;

    if (cachedValue != null) {
      return cachedValue;
    }

    int numberOfUpdates = 0;

    for (T object : results.keySet()) {
      final PutResult putResult = results.get(object);

      if (putResult.wasUpdated()) {
        //noinspection ConstantConditions
        numberOfUpdates += putResult.numberOfRowsUpdated();
      }
    }

    numberOfUpdatesCache = numberOfUpdates;

    return numberOfUpdates;
  }