forked from oleg-vyalyh/BackendlessDataCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendlessDataCollection.java
More file actions
630 lines (515 loc) · 17.6 KB
/
BackendlessDataCollection.java
File metadata and controls
630 lines (515 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package com.examples.backendless.datacollection;
import com.backendless.Backendless;
import com.backendless.IDataStore;
import com.backendless.persistence.DataQueryBuilder;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
/**
* <p>This is an implementation of the Java Collection interface enabling to retrieve and iterate over a collection of objects stored in a Backendless data table.</p>
* <p>Interface methods returning data are mapped to various Backendless APIs.</p>
* <p>The Iterator returned by the implementation lets you access either all objects from the data table or a subset determined by a where clause. Additionally, the implementation can work with data streams.</p>
*
* <ul><u>The collection has <i>two modes of operation</i><u>:
* <li><b>persisted</b> - all retrieved objects are saved locally to enable faster access in future iterations. The persisted data is shared between all iterators returned by the collection. To enable this mode use the "preservedData" parameter.
* <li><b>transient</b> - every iterator returned by the collection works with a fresh data collection returned from the server.
* </ul>
*
* @param <T> the type of your entity. Be sure it properly mapped with {@code Backendless.Data.mapTableToClass( String tableName, Class<T> entityClass )}
*/
public class BackendlessDataCollection<T extends BackendlessDataCollection.Identifiable<T>> implements Collection<T>
{
public static interface Identifiable<T>
{
String getObjectId();
void setObjectId( String id );
}
private Class<T> entityType;
private IDataStore<T> iDataStore;
private String slice;
private LinkedHashMap<String, T> preservedData; // if null, the mode is 'transient'
private int size;
private boolean isLoaded = false;
public BackendlessDataCollection( Class<T> entityType )
{
this( entityType, false );
}
public BackendlessDataCollection( Class<T> entityType, String slice )
{
this( entityType, false, slice );
}
public BackendlessDataCollection( Class<T> entityType, boolean preserveIteratedData )
{
this( entityType, preserveIteratedData, "" );
}
public BackendlessDataCollection( Class<T> entityType, boolean preserveIteratedData, String slice )
{
this.entityType = entityType;
this.slice = (slice == null) ? "" : slice;
this.iDataStore = Backendless.Data.of( this.entityType );
if( preserveIteratedData )
this.preservedData = new LinkedHashMap<>();
this.size = getRealSize();
}
/**
* @return <b>query</b>, which limits the data set, retrieved from the table.
*/
public String getSlice()
{
return slice;
}
/**
* @return true, if this collection was created with paramer <b>preserveIteratedData</b>
*/
public boolean isPersisted()
{
return this.preservedData != null;
}
/**
* Only for <b>persisted</b> mode.
*
* @return the number of elements that preserved locally.
*/
public int getPersistedSize()
{
if( this.preservedData == null )
throw new IllegalStateException( "This collection is not persisted." );
return this.preservedData.size();
}
/**
* Only for <b>persisted</b> mode.
*
* @return true, if the current collection is fully loaded from remote server.
*/
public boolean isLoaded()
{
if( this.preservedData == null )
throw new IllegalStateException( "This collection is not persisted." );
return this.isLoaded;
}
/**
* If this collections in <b>persisted</b> mode, this operation delete all locally saved data and refresh the size.
* The operation has only local effect.
*/
public void invalidateState()
{
if( this.preservedData != null )
{
this.preservedData.clear();
this.isLoaded = false;
}
this.size = getRealSize();
}
/**
* Only for <b>persisted</b> mode.
* Fills up this collection with the values from the Backendless table.
*/
public void populate()
{
if( this.preservedData == null )
throw new IllegalStateException( "This collection is not persisted." );
Iterator<T> iter = this.iterator();
while( iter.hasNext() )
iter.next();
}
private int getRealSize()
{
return this.iDataStore.getObjectCount( DataQueryBuilder.create().setWhereClause( this.slice ) );
}
private void checkObjectType( Object o )
{
if( this.entityType != o.getClass() )
throw new IllegalArgumentException( o.getClass() + " is not a type objects of which are contained in this collection." );
}
private void checkObjectTypeAndId( Object o )
{
if( this.entityType != o.getClass() )
throw new IllegalArgumentException( o.getClass() + " is not a type objects of which are contained in this collection." );
String objectId = ((T) o).getObjectId();
if( objectId == null )
throw new IllegalArgumentException( "'objectId' is null." );
}
private String getQuery( String id )
{
String query = "objectId='" + id + "'";
query = (this.slice.isEmpty()) ? query : this.slice + " and " + query;
return query;
}
private String getQuery( Collection<T> objs, boolean exclude )
{
String firstPart = exclude ? "objectId not in (" : "objectId in (";
StringBuilder sb = new StringBuilder( firstPart );
for( T obj : objs )
sb.append( '\'' ).append( obj.getObjectId() ).append( '\'' ).append( ',' );
sb.replace( sb.length() - 1, sb.length(), ")" );
String query = sb.toString();
query = (this.slice.isEmpty()) ? query : this.slice + " and " + query;
return query;
}
private String getQueryByIds( Collection<String> ids, boolean exclude )
{
String firstPart = exclude ? "objectId not in (" : "objectId in (";
StringBuilder sb = new StringBuilder( firstPart );
for( String id : ids )
sb.append( '\'' ).append( id ).append( '\'' ).append( ',' );
sb.replace( sb.length() - 1, sb.length(), ")" );
String query = sb.toString();
query = (this.slice.isEmpty()) ? query : this.slice + " and " + query;
return query;
}
@Override
public Iterator<T> iterator()
{
return new BackendlessDataCollectionIterator();
}
@Override
public int size()
{
return this.size;
}
@Override
public boolean remove( Object o )
{
this.checkObjectTypeAndId( o );
boolean result = false;
if( this.preservedData != null )
result = this.preservedData.remove( ((T) o).getObjectId() ) != null;
result |= this.iDataStore.remove( this.getQuery( ((T) o).getObjectId() ) ) != 0;
return result;
}
@Override
public boolean removeAll( Collection<?> c )
{
c.forEach( this::checkObjectTypeAndId );
boolean result = false;
if( this.preservedData != null )
{
for( T entity : (Collection<T>) c )
result = this.preservedData.remove( entity.getObjectId() ) != null;
}
result |= this.iDataStore.remove( this.getQuery( (Collection<T>) c, false ) ) != 0;
return result;
}
@Override
public boolean isEmpty()
{
return this.size == 0;
}
/**
* If this collection is <i>persisted</i> and fully loaded, than no api-calls will be performed.
*
* @param o
* @return
*/
@Override
public boolean contains( Object o )
{
this.checkObjectTypeAndId( o );
boolean result = false;
if( this.preservedData != null )
result = this.preservedData.containsKey( ((T) o).getObjectId() );
if( this.isLoaded )
return result;
DataQueryBuilder queryBuilder = DataQueryBuilder.create().setWhereClause( this.getQuery( ((T) o).getObjectId() ) );
result |= this.iDataStore.getObjectCount( queryBuilder ) != 0;
return result;
}
/**
* If this collection is <i>persisted</i> and fully loaded, than no api-calls will be performed.
*
* @return
*/
@Override
public T[] toArray()
{
if( this.preservedData != null && this.isLoaded )
return this.preservedData.values().toArray( (T[]) Array.newInstance( this.entityType, this.preservedData.size() ) );
ArrayList<T> list = new ArrayList<>();
Iterator<T> iter = this.iterator();
while( iter.hasNext() )
list.add( iter.next() );
return (T[]) list.toArray();
}
/**
* If this collection is <i>persisted</i>, than no api-calls will be performed.
*
* @return
*/
@Override
public <T1> T1[] toArray( T1[] a )
{
Class arrayType = a.getClass().getComponentType();
if( this.entityType != arrayType )
throw new IllegalArgumentException( arrayType + " is not a type objects of which are contained in this collection." );
return (T1[]) this.toArray();
}
/**
* If this collection is a 'slice' of data from Backendless table, then after every <i>add</i> operation another api-call will be performed to check that the new saved object doesn't violate the 'slice' condition.
*
* @param t
* @return
*/
@Override
public boolean add( T t )
{
this.checkObjectType( t );
T savedEntity = this.iDataStore.save( t );
if( this.slice != null )
{
DataQueryBuilder queryBuilder = DataQueryBuilder.create().setWhereClause( this.getQuery( savedEntity.getObjectId() ) );
if( this.iDataStore.getObjectCount( queryBuilder ) == 0 )
{
this.iDataStore.remove( this.getQuery( savedEntity.getObjectId() ) );
return false;
}
}
if( this.preservedData != null )
preservedData.put( savedEntity.getObjectId(), savedEntity );
this.size++;
return true;
}
/**
* If this collection is <i>persisted</i> and fully loaded, than no api-calls will be performed.
*
* @param c
* @return
*/
@Override
public boolean containsAll( Collection<?> c )
{
c.forEach( this::checkObjectTypeAndId );
Collection<T> collection = (Collection<T>) c;
if( this.preservedData != null && this.isLoaded )
{
Set<String> listId = new HashSet<>();
for( T obj : collection )
listId.add( obj.getObjectId() );
return this.preservedData.keySet().containsAll( listId );
}
DataQueryBuilder queryBuilder = DataQueryBuilder.create().setWhereClause( this.getQuery( collection, false ) );
return this.iDataStore.getObjectCount( queryBuilder ) == c.size();
}
@Override
public boolean addAll( Collection<? extends T> c )
{
c.forEach( this::checkObjectType );
List<String> listId = this.iDataStore.create( (List<T>) c );
if( this.slice != null )
{
DataQueryBuilder queryBuilder = DataQueryBuilder.create().setWhereClause( this.getQueryByIds( listId, false ) );
if( this.iDataStore.getObjectCount( queryBuilder ) != listId.size() )
{
this.iDataStore.remove( this.getQueryByIds( listId, false ) );
return false;
}
}
if( this.preservedData != null )
{
for( int i = 0; i < c.size(); i++ )
{
T obj = ((List<T>) c).get( i );
obj.setObjectId( listId.get( i ) );
preservedData.put( obj.getObjectId(), obj );
}
}
this.size += listId.size();
return true;
}
@Override
public boolean retainAll( Collection<?> c )
{
c.forEach( this::checkObjectTypeAndId );
Set<String> listId = new HashSet<>();
for( T obj : (Collection<T>) c )
listId.add( obj.getObjectId() );
boolean result = this.iDataStore.remove( this.getQueryByIds( listId, true ) ) != 0;
if( this.preservedData != null )
{
Iterator<String> idIterator = this.preservedData.keySet().iterator();
while( idIterator.hasNext() )
{
if( !listId.contains( idIterator.next() ) )
{
result = true;
idIterator.remove();
}
}
}
return result;
}
/**
* Affects on the current local collection (if mode is <i>persisted</i>) and remote Backendless data table.
*/
@Override
public void clear()
{
this.iDataStore.remove( this.slice );
invalidateState();
}
/**
* Takes into account only 'entityType' and 'slice'.
*
* @param o
* @return
*/
@Override
public boolean equals( Object o )
{
if( this == o )
return true;
if( !(o instanceof BackendlessDataCollection) )
return false;
BackendlessDataCollection<?> that = (BackendlessDataCollection<?>) o;
return Objects.equals( entityType, that.entityType ) && Objects.equals( slice, that.slice );
}
/**
* Takes into account only 'entityType' and 'slice'.
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hash( entityType, slice );
}
public class BackendlessDataCollectionIterator implements Iterator<T>
{
private static final int pageSize = 100;
private DataQueryBuilder queryBuilder;
private int currentPosition;
private List<T> currentPageData;
private List<T> nextPageData;
private Iterator<T> persistedIterator;
private T[] loadedData;
private BackendlessDataCollectionIterator()
{
if( BackendlessDataCollection.this.size() < 1 )
return;
if( BackendlessDataCollection.this.isLoaded )
{
persistedIterator = BackendlessDataCollection.this.preservedData.values().iterator();
return;
}
this.currentPosition = 0;
this.queryBuilder = DataQueryBuilder.create().setWhereClause( BackendlessDataCollection.this.slice ).setPageSize( pageSize );
if( BackendlessDataCollection.this.preservedData == null )
{
this.currentPageData = BackendlessDataCollection.this.iDataStore.find( this.queryBuilder );
this.nextPageData = BackendlessDataCollection.this.iDataStore.find( this.queryBuilder.prepareNextPage() );
}
else
{
T[] array = (T[]) Array.newInstance( BackendlessDataCollection.this.entityType, BackendlessDataCollection.this.preservedData.size() );
loadedData = BackendlessDataCollection.this.preservedData.values().toArray( array );
// load first page
this.currentPageData = new ArrayList<>();
boolean fullPage = this.loadNextPageUsingLocalDataIfPresent( 0, this.currentPageData );
if( !fullPage )
return;
// load second page
this.nextPageData = new ArrayList<>();
this.loadNextPageUsingLocalDataIfPresent( 1, this.nextPageData );
}
}
@Override
public boolean hasNext()
{
boolean hasNext;
if( this.persistedIterator != null )
hasNext = this.persistedIterator.hasNext();
else
hasNext = ((currentPageData != null && currentPosition % pageSize < currentPageData.size()) || (nextPageData != null && !nextPageData.isEmpty()));
if( !hasNext )
{
this.currentPageData = this.nextPageData = null;
this.persistedIterator = null;
this.queryBuilder = null;
}
return hasNext;
}
@Override
public T next()
{
if( this.persistedIterator != null )
return this.persistedIterator.next();
if( this.currentPageData == null )
throw new NoSuchElementException();
int indexOnPage = currentPosition++ % pageSize;
T result = null;
if( indexOnPage < currentPageData.size() )
result = currentPageData.get( indexOnPage );
if( indexOnPage == currentPageData.size() - 1 )
getNextPage();
return result;
}
private void getNextPage()
{
if( currentPageData == null || nextPageData == null || nextPageData.isEmpty() )
{
currentPageData = nextPageData = null;
return;
}
currentPageData = nextPageData;
if( currentPageData.size() < pageSize )
{
this.nextPageData = null;
return;
}
if( BackendlessDataCollection.this.preservedData == null )
{
this.nextPageData = BackendlessDataCollection.this.iDataStore.find( this.queryBuilder.prepareNextPage() );
if( this.nextPageData.size() < pageSize )
BackendlessDataCollection.this.size = currentPosition + this.currentPageData.size() + this.nextPageData.size();
}
else
{
// load next page
this.nextPageData = new ArrayList<>();
int nextPageNumber = currentPosition / pageSize + 1;
this.loadNextPageUsingLocalDataIfPresent( nextPageNumber, this.nextPageData );
}
}
private boolean loadNextPageUsingLocalDataIfPresent( int pageNumber, List<T> target )
{
int startLoadIndex = pageNumber * pageSize;
int lastLoadIndex = startLoadIndex + pageSize;
if( loadedData != null )
{
int tmpLastIndex = (loadedData.length < lastLoadIndex) ? loadedData.length : lastLoadIndex;
for( int i = startLoadIndex; i < tmpLastIndex; i++ )
target.add( loadedData[ i ] );
startLoadIndex = tmpLastIndex;
}
boolean fullPage = true;
if( target.size() < pageSize )
{
loadedData = null;
fullPage = loadPartialData( startLoadIndex, lastLoadIndex - startLoadIndex, target );
}
return fullPage;
}
private boolean loadPartialData( int pOffset, int pPageSize, List<T> target )
{
this.queryBuilder.setOffset( pOffset ).setPageSize( pPageSize );
List<T> lackingObjects = BackendlessDataCollection.this.iDataStore.find( this.queryBuilder );
this.queryBuilder.setPageSize( pageSize );
target.addAll( lackingObjects );
// save data to the main collection
lackingObjects.forEach( obj -> BackendlessDataCollection.this.preservedData.put( obj.getObjectId(), obj ) );
boolean fullPage = lackingObjects.size() == pPageSize;
if( !fullPage )
{
BackendlessDataCollection.this.size = pOffset + pPageSize - 1;
BackendlessDataCollection.this.isLoaded = true;
}
return fullPage;
}
}
}