Reactive extensions for SimpleNoSQL. Manipulate entities using Observables
and Completables.
Suppose we have the following entity we want to manipulate:
classSampleBeanimplementsEntity {
privateStringname;
privateStringid;
privateMap<String, String> mapping;
publicStringgetName() {
returnname;
}
publicvoidsetName(Stringname) {
this.name = name;
}
@OverridepublicStringgetId() {
returnid;
}
publicvoidsetId(Stringid) {
this.id = id;
}
publicMap<String, String> getMapping() {
returnmapping;
}
publicvoidsetMapping(Map<String, String> mapping) {
this.mapping = mapping;
}
}We first start creating a bucket:
Bucket<SampleBean> bucket = newBucket<>(context, SampleBean.class, "bucketId");SampleBeanentity = newSampleBean();
entity.setId("1");
entity.setName("Colin");
Map<String, Integer> birthday = newHashMap<String, Integer>();
birthday.put("day", 17);
birthday.put("month", 2);
birthday.put("year", 1982);
entity.setMapping(birthday);
bucket.newQuery()
.save(entity)
.subscribe();SampleBeanentity2 = newSampleBean();
entity2.setId("2");
entity2.setName("Santiago");
SampleBeanentity3 = newSampleBean();
entity3.setId("3");
entity3.setName("Xmartlabs");
bucket.newQuery()
.save(Arrays.asList(entity2, entity3))
.subscribe();bucket.newQuery()
.entityId("entityId")
.retrieve()
.subscribe(sampleBean -> System.out.println("Name: %s", sampleBean.getName()));bucket.newQuery()
.filter(sampleBean -> sampleBean.getName().startsWith("S"))
.retrieve()
.subscribe(sampleBean -> System.out.println("Name: %s", sampleBean.getName()));bucket.newQuery()
.retrieve()
.subscribe(sampleBean -> System.out.println("Name: %s", sampleBean.getName()));bucket.newQuery()
.entityId("entityId")
.delete()
.subscribe();Currently, SimpleNoSQL does not support the usage of filter for the delete operation.
There's an issue opened.
Nevertheless, this functionality can be achieved by first retrieving the entities to be deleted and then performing the actual delete operation individually:
Observable<SampleBean> itemsToDelete = bucket.newQuery()
.filter(sampleBean -> sampleBean.getName().startsWith("S"))
.retrieve()
CompletabledeleteCompletable = Completable.concat(
itemsToDelete
.map(item -> bucket.newQuery()
.entityId(item.getId()))
.delete());bucket.newQuery()
.delete()
.subscribe();[As SimpleNoSQL sorts the results in memory]
(https://github.com/Jearil/SimpleNoSQL/blob/master/SimpleNoSQL/src/main/java/com/colintmiller/simplenosql/threading/DataDispatcher.java#L140),
you can carry this out the same way with Observable#toSortedList.
As SimpleNoSQL, this project still isn't stable (the API can change at any time). You can use it with Gradle and JitPack:
repositories {
maven { url "https://jitpack.io" }
}Then adding the dependency:
implementation 'com.github.xmartlabs:RxSimpleNoSQL:-SNAPSHOT'RxSimpleNoSQL requires at minimum Java 7 or Android 2.2.
To build:
git clone https://github.com/xmartlabs/RxSimpleNoSQL.git
cd RxSimpleNoSQL/
./gradlew buildCopyright 2016 Xmartlabs SRL.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.