99import com .couchbase .lite .Query ;
1010import com .couchbase .lite .QueryEnumerator ;
1111import com .couchbase .lite .QueryRow ;
12+ import com .couchbase .lite .TransactionalTask ;
1213import com .couchbase .lite .View ;
1314import com .couchbase .lite .android .AndroidContext ;
1415import de .greenrobot .performance .BasePerfTestCase ;
2122import java .util .Map ;
2223
2324/**
24- * http://developer.couchbase.com/documentation/mobile/1.1.0 /develop/training/build-first-android-app/index.html
25+ * http://developer.couchbase.com/documentation/mobile/1.2 /develop/training/build-first-android-app/index.html
2526 * https://github.com/couchbaselabs/ToDoLite-Android
2627 */
27- public class PerformanceTestCouchbase extends BasePerfTestCase {
28+ public class PerfTestCouchbase extends BasePerfTestCase {
2829
2930 private static final String DB_NAME = "couchbase-test" ;
3031 private static final String DOC_TYPE = "simpleentity" ;
@@ -33,7 +34,7 @@ public class PerformanceTestCouchbase extends BasePerfTestCase {
3334
3435 @ Override
3536 protected String getLogTag () {
36- return "PerfTestCouchbase" ;
37+ return getClass (). getSimpleName () ;
3738 }
3839
3940 @ Override
@@ -77,18 +78,30 @@ public void map(Map<String, Object> document, Emitter emitter) {
7778 }
7879 }
7980
80- private void indexedStringEntityQueriesRun (View indexedStringView , int count )
81+ private void indexedStringEntityQueriesRun (View indexedStringView , final int count )
8182 throws CouchbaseLiteException {
8283 // create entities
83- String [] fixedRandomStrings = StringGenerator .createFixedRandomStrings (count );
84- database .beginTransaction ();
85- for (int i = 0 ; i < count ; i ++) {
86- Document entity = database .getDocument (String .valueOf (i ));
87- Map <String , Object > properties = new HashMap <>();
88- properties .put ("indexedString" , fixedRandomStrings [i ]);
89- entity .putProperties (properties );
84+ final String [] fixedRandomStrings = StringGenerator .createFixedRandomStrings (count );
85+ boolean successful = database .runInTransaction (new TransactionalTask () {
86+ @ Override
87+ public boolean run () {
88+ for (int i = 0 ; i < count ; i ++) {
89+ Document entity = database .getDocument (String .valueOf (i ));
90+ Map <String , Object > properties = new HashMap <>();
91+ properties .put ("indexedString" , fixedRandomStrings [i ]);
92+ try {
93+ entity .putProperties (properties );
94+ } catch (CouchbaseLiteException e ) {
95+ log (e .toString ());
96+ return false ;
97+ }
98+ }
99+ return true ;
100+ }
101+ });
102+ if (!successful ) {
103+ throw new RuntimeException ("Exception while running transaction" );
90104 }
91- database .endTransaction (true );
92105 log ("Built and inserted entities." );
93106
94107 // query for entities by indexed string at random
@@ -156,36 +169,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException {
156169 deleteAll ();
157170 }
158171
159- private void batchCrudRun (int count ) throws Exception {
172+ private void batchCrudRun (final int count ) throws Exception {
160173 // precreate property maps for documents
161- List <Map <String , Object >> maps = new ArrayList <>(count );
174+ final List <Map <String , Object >> maps = new ArrayList <>(count );
162175 for (int i = 0 ; i < count ; i ++) {
163176 maps .add (createDocumentMap (i ));
164177 }
165178
166179 startClock ();
167- List <Document > documents = new ArrayList <>(count );
168- database .beginTransaction ();
169- for (int i = 0 ; i < count ; i ++) {
170- // use our own ids (use .createDocument() for random UUIDs)
171- Document document = database .getDocument (String .valueOf (i ));
172- document .putProperties (maps .get (i ));
173- documents .add (document );
180+ final List <Document > documents = new ArrayList <>(count );
181+ boolean successful = database .runInTransaction (new TransactionalTask () {
182+ @ Override
183+ public boolean run () {
184+ for (int i = 0 ; i < count ; i ++) {
185+ // use our own ids (use .createDocument() for random UUIDs)
186+ Document document = database .getDocument (String .valueOf (i ));
187+ try {
188+ // TODO ut: exception with 409 (HTTP 409 conflict)
189+ document .putProperties (maps .get (i ));
190+ } catch (CouchbaseLiteException e ) {
191+ log (e .toString ());
192+ return false ;
193+ }
194+ documents .add (document );
195+ }
196+ return true ;
197+ }
198+ });
199+ if (!successful ) {
200+ throw new RuntimeException ("Exception while running transaction" );
174201 }
175- database .endTransaction (true );
176202 stopClock (LogMessage .BATCH_CREATE );
177203
178204 startClock ();
179- database .beginTransaction ();
180- for (int i = 0 ; i < count ; i ++) {
181- Document document = documents .get (i );
182- Map <String , Object > updatedProperties = new HashMap <>();
183- // copy existing properties to get _rev property
184- updatedProperties .putAll (document .getProperties ());
185- updatedProperties .putAll (maps .get (i ));
186- document .putProperties (updatedProperties );
205+ successful = database .runInTransaction (new TransactionalTask () {
206+ @ Override
207+ public boolean run () {
208+ for (int i = 0 ; i < count ; i ++) {
209+ Document document = documents .get (i );
210+ Map <String , Object > updatedProperties = new HashMap <>();
211+ // copy existing properties to get _rev property
212+ updatedProperties .putAll (document .getProperties ());
213+ updatedProperties .putAll (maps .get (i ));
214+ try {
215+ document .putProperties (updatedProperties );
216+ } catch (CouchbaseLiteException e ) {
217+ log (e .toString ());
218+ return false ;
219+ }
220+ }
221+ return true ;
222+ }
223+ });
224+ if (!successful ) {
225+ throw new RuntimeException ("Exception while running transaction" );
187226 }
188- database .endTransaction (true );
189227 stopClock (LogMessage .BATCH_UPDATE );
190228
191229 startClock ();
@@ -221,13 +259,23 @@ private void batchCrudRun(int count) throws Exception {
221259 private void deleteAll () throws CouchbaseLiteException {
222260 // query all documents, mark them as deleted
223261 Query query = database .createAllDocumentsQuery ();
224- QueryEnumerator result = query .run ();
225- database .beginTransaction ();
226- while (result .hasNext ()) {
227- QueryRow row = result .next ();
228- row .getDocument ().purge ();
262+ final QueryEnumerator result = query .run ();
263+ boolean successful = database .runInTransaction (new TransactionalTask () {
264+ @ Override
265+ public boolean run () {
266+ QueryRow row = result .next ();
267+ try {
268+ row .getDocument ().purge ();
269+ } catch (CouchbaseLiteException e ) {
270+ log (e .toString ());
271+ return false ;
272+ }
273+ return true ;
274+ }
275+ });
276+ if (!successful ) {
277+ throw new RuntimeException ("Exception while running transaction" );
229278 }
230- database .endTransaction (true );
231279 }
232280
233281 private Map <String , Object > createDocumentMap (int seed ) throws CouchbaseLiteException {
0 commit comments