Search in sources :

Example 1 with SQLiteTypeMapping

use of com.pushtorefresh.storio.sqlite.SQLiteTypeMapping in project storio by pushtorefresh.

the class PreparedPutCollectionOfObjects method executeAsBlocking.

/**
     * 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 results of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResults<T> executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, PutResolver<T>>> objectsAndPutResolvers;
        if (explicitPutResolver != null) {
            objectsAndPutResolvers = null;
        } else {
            objectsAndPutResolvers = new ArrayList<SimpleImmutableEntry<T, PutResolver<T>>>(objects.size());
            for (final T object : objects) {
                final SQLiteTypeMapping<T> typeMapping = (SQLiteTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection 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");
                }
                objectsAndPutResolvers.add(new SimpleImmutableEntry<T, PutResolver<T>>(object, typeMapping.putResolver()));
            }
        }
        if (useTransaction) {
            lowLevel.beginTransaction();
        }
        final Map<T, PutResult> results = new HashMap<T, PutResult>(objects.size());
        boolean transactionSuccessful = false;
        try {
            if (explicitPutResolver != null) {
                for (final T object : objects) {
                    final PutResult putResult = explicitPutResolver.performPut(storIOSQLite, object);
                    results.put(object, putResult);
                    if (!useTransaction && (putResult.wasInserted() || putResult.wasUpdated())) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
                    }
                }
            } else {
                for (final SimpleImmutableEntry<T, PutResolver<T>> objectAndPutResolver : objectsAndPutResolvers) {
                    final T object = objectAndPutResolver.getKey();
                    final PutResolver<T> putResolver = objectAndPutResolver.getValue();
                    final PutResult putResult = putResolver.performPut(storIOSQLite, object);
                    results.put(object, putResult);
                    if (!useTransaction && (putResult.wasInserted() || putResult.wasUpdated())) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
                    }
                }
            }
            if (useTransaction) {
                lowLevel.setTransactionSuccessful();
                transactionSuccessful = true;
            }
        } finally {
            if (useTransaction) {
                lowLevel.endTransaction();
                // if put was in transaction and it was successful -> notify about changes
                if (transactionSuccessful) {
                    // in most cases it will be 1 table
                    final Set<String> affectedTables = new HashSet<String>(1);
                    for (final T object : results.keySet()) {
                        final PutResult putResult = results.get(object);
                        if (putResult.wasInserted() || putResult.wasUpdated()) {
                            affectedTables.addAll(putResult.affectedTables());
                        }
                    }
                    // It'll reduce number of possible deadlock situations
                    if (!affectedTables.isEmpty()) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
                    }
                }
            }
        }
        return PutResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) SQLiteTypeMapping(com.pushtorefresh.storio.sqlite.SQLiteTypeMapping) HashSet(java.util.HashSet) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 2 with SQLiteTypeMapping

use of com.pushtorefresh.storio.sqlite.SQLiteTypeMapping in project storio by pushtorefresh.

the class PreparedDeleteCollectionOfObjects method executeAsBlocking.

/**
     * Executes Delete 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 results of Delete Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResults<T> executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, DeleteResolver<T>>> objectsAndDeleteResolvers;
        if (explicitDeleteResolver != null) {
            objectsAndDeleteResolvers = null;
        } else {
            objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry<T, DeleteResolver<T>>>(objects.size());
            for (final T object : objects) {
                final SQLiteTypeMapping<T> typeMapping = (SQLiteTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection 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");
                }
                objectsAndDeleteResolvers.add(new SimpleImmutableEntry<T, DeleteResolver<T>>(object, typeMapping.deleteResolver()));
            }
        }
        if (useTransaction) {
            lowLevel.beginTransaction();
        }
        final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
        boolean transactionSuccessful = false;
        try {
            if (explicitDeleteResolver != null) {
                for (final T object : objects) {
                    final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOSQLite, object);
                    results.put(object, deleteResult);
                    if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
                    }
                }
            } else {
                for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
                    final T object = objectAndDeleteResolver.getKey();
                    final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();
                    final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object);
                    results.put(object, deleteResult);
                    if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
                    }
                }
            }
            if (useTransaction) {
                lowLevel.setTransactionSuccessful();
                transactionSuccessful = true;
            }
        } finally {
            if (useTransaction) {
                lowLevel.endTransaction();
                // if delete was in transaction and it was successful -> notify about changes
                if (transactionSuccessful) {
                    // in most cases it will be one table
                    final Set<String> affectedTables = new HashSet<String>(1);
                    for (final T object : results.keySet()) {
                        final DeleteResult deleteResult = results.get(object);
                        if (deleteResult.numberOfRowsDeleted() > 0) {
                            affectedTables.addAll(results.get(object).affectedTables());
                        }
                    }
                    // It'll reduce number of possible deadlock situations
                    if (!affectedTables.isEmpty()) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
                    }
                }
            }
        }
        return DeleteResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) SQLiteTypeMapping(com.pushtorefresh.storio.sqlite.SQLiteTypeMapping) HashSet(java.util.HashSet) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

NonNull (android.support.annotation.NonNull)2 WorkerThread (android.support.annotation.WorkerThread)2 StorIOException (com.pushtorefresh.storio.StorIOException)2 SQLiteTypeMapping (com.pushtorefresh.storio.sqlite.SQLiteTypeMapping)2 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2