use of com.amplifyframework.datastore.DataStoreConfiguration in project amplify-android by aws-amplify.
the class QueryPredicateTest method setup.
/**
* Setup the queryPredicateProvider.
* @throws DataStoreException on failure to build {@link DataStoreConfiguration}
*/
@Before
public void setup() throws DataStoreException {
expectedPredicate = BlogOwner.NAME.beginsWith(RandomString.string());
blogSyncExpression = () -> expectedPredicate;
DataStoreConfiguration dataStoreConfiguration = DataStoreConfiguration.builder().syncExpression(BlogOwner.class, blogSyncExpression).build();
queryPredicateProvider = new QueryPredicateProvider(() -> dataStoreConfiguration);
}
use of com.amplifyframework.datastore.DataStoreConfiguration in project amplify-android by aws-amplify.
the class SubscriptionProcessorTest method setup.
/**
* Sets up an {@link SubscriptionProcessor} and associated test dependencies.
* @throws DataStoreException on error building the {@link DataStoreConfiguration}
*/
@Before
public void setup() throws DataStoreException {
ModelProvider modelProvider = AmplifyModelProvider.getInstance();
schemaRegistry = SchemaRegistry.instance();
schemaRegistry.register(modelProvider.modelSchemas());
this.modelSchemas = sortedModels(modelProvider);
this.appSync = mock(AppSync.class);
this.merger = mock(Merger.class);
DataStoreConfiguration dataStoreConfiguration = DataStoreConfiguration.builder().syncExpression(BlogOwner.class, () -> BlogOwner.NAME.beginsWith("John")).build();
QueryPredicateProvider queryPredicateProvider = new QueryPredicateProvider(() -> dataStoreConfiguration);
queryPredicateProvider.resolvePredicates();
this.subscriptionProcessor = SubscriptionProcessor.builder().appSync(appSync).modelProvider(modelProvider).schemaRegistry(schemaRegistry).merger(merger).queryPredicateProvider(queryPredicateProvider).onFailure(throwable -> {
}).build();
}
use of com.amplifyframework.datastore.DataStoreConfiguration in project amplify-android by aws-amplify.
the class SyncStatusTest method syncStatusGetReturnSyncedStatus.
/**
* Get returns sync status.
*/
@Test
public void syncStatusGetReturnSyncedStatus() {
final LastSyncMetadata lastSyncMetadata = LastSyncMetadata.baseSyncedAt(BlogOwner.class.getName(), Time.now());
List<LastSyncMetadata> resultList = new ArrayList<>();
resultList.add(lastSyncMetadata);
Consumer<DataStoreException> onObservationError = value -> {
};
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(LastSyncMetadata.class), any(), any())).thenReturn(resultList);
DataStoreConfiguration mockDataStoreConfig = mock(DataStoreConfiguration.class);
when(mockDataStoreConfig.getSyncIntervalInMinutes()).thenReturn(5L);
SyncStatus subject = new SyncStatus(mockSqlQueryProcessor, mockDataStoreConfig);
boolean result = subject.get(LastSyncMetadata.class.getName(), onObservationError);
Assert.assertTrue(result);
}
use of com.amplifyframework.datastore.DataStoreConfiguration in project amplify-android by aws-amplify.
the class SyncStatusTest method syncStatusGetReturnNotSyncedStatus.
/**
* Get returns false for sync status.
*/
@Test
public void syncStatusGetReturnNotSyncedStatus() {
final LastSyncMetadata lastSyncMetadata = LastSyncMetadata.baseSyncedAt(BlogOwner.class.getName(), Time.now());
List<LastSyncMetadata> resultList = new ArrayList<>();
resultList.add(lastSyncMetadata);
Consumer<DataStoreException> onObservationError = value -> {
};
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(LastSyncMetadata.class), any(), any())).thenReturn(resultList);
DataStoreConfiguration mockDataStoreConfig = mock(DataStoreConfiguration.class);
when(mockDataStoreConfig.getSyncIntervalInMinutes()).thenReturn(0L);
SyncStatus subject = new SyncStatus(mockSqlQueryProcessor, mockDataStoreConfig);
boolean result = subject.get(LastSyncMetadata.class.getName(), onObservationError);
Assert.assertFalse(result);
}
use of com.amplifyframework.datastore.DataStoreConfiguration in project amplify-android by aws-amplify.
the class SQLiteStorageAdapter method initialize.
/**
* {@inheritDoc}
*/
@Override
public synchronized void initialize(@NonNull Context context, @NonNull Consumer<List<ModelSchema>> onSuccess, @NonNull Consumer<DataStoreException> onError, @NonNull DataStoreConfiguration dataStoreConfiguration) {
Objects.requireNonNull(context);
Objects.requireNonNull(onSuccess);
Objects.requireNonNull(onError);
// Create a thread pool large enough to take advantage of parallelization, but small enough to avoid
// OutOfMemoryError and CursorWindowAllocationException issues.
this.threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * THREAD_POOL_SIZE_MULTIPLIER);
this.context = context;
this.dataStoreConfiguration = dataStoreConfiguration;
threadPool.submit(() -> {
try {
/*
* Start with a fresh registry.
*/
schemaRegistry.clear();
/*
* Create {@link ModelSchema} objects for the corresponding {@link Model}.
* Any exception raised during this when inspecting the Model classes
* through reflection will be notified via the `onError` callback.
*/
schemaRegistry.register(modelsProvider.modelSchemas(), modelsProvider.customTypeSchemas());
/*
* Create the CREATE TABLE and CREATE INDEX commands for each of the
* Models. Instantiate {@link SQLiteStorageHelper} to execute those
* create commands.
*/
this.sqlCommandFactory = new SQLiteCommandFactory(schemaRegistry, gson);
CreateSqlCommands createSqlCommands = getCreateCommands(modelsProvider.modelNames());
sqliteStorageHelper = SQLiteStorageHelper.getInstance(context, databaseName, DATABASE_VERSION, createSqlCommands);
/*
* Create and/or open a database. This also invokes
* {@link SQLiteStorageHelper#onCreate(SQLiteDatabase)} which executes the tasks
* to create tables and indexes. When the function returns without any exception
* being thrown, invoke the `onError` callback.
*
* Errors are thrown when there is no write permission to the database, no space
* left in the database for any write operation and other errors thrown while
* creating and opening a database. All errors are passed through the
* `onError` callback.
*
* databaseConnectionHandle represents a connection handle to the database.
* All database operations will happen through this handle.
*/
databaseConnectionHandle = sqliteStorageHelper.getWritableDatabase();
/*
* Create helper instance that can traverse through model relations.
*/
this.sqliteModelTree = new SQLiteModelTree(schemaRegistry, databaseConnectionHandle);
/*
* Create a command processor which runs the actual SQL transactions.
*/
this.sqlCommandProcessor = new SQLCommandProcessor(databaseConnectionHandle);
sqlQueryProcessor = new SqlQueryProcessor(sqlCommandProcessor, sqlCommandFactory, schemaRegistry);
syncStatus = new SyncStatus(sqlQueryProcessor, dataStoreConfiguration);
/*
* Detect if the version of the models stored in SQLite is different
* from the version passed in through {@link ModelProvider#version()}.
* Delete the database if there is a version change.
*/
toBeDisposed.add(updateModels().subscribe(() -> onSuccess.accept(Immutable.of(new ArrayList<>(schemaRegistry.getModelSchemaMap().values()))), throwable -> onError.accept(new DataStoreException("Error in initializing the SQLiteStorageAdapter", throwable, AmplifyException.TODO_RECOVERY_SUGGESTION))));
} catch (Exception exception) {
onError.accept(new DataStoreException("Error in initializing the SQLiteStorageAdapter", exception, "See attached exception"));
}
});
}
Aggregations