@@ -10,15 +10,31 @@ const { gen_expression } = require('./expressions')
1010
1111
1212
13- class CyclicCollection {
13+ class CyclicCollection {
14+ /*
15+ * Constructs a CyclicCollection that can be used to interact with collection.
16+ * @arg {string} collection - name of the collection
17+ * @arg {object} props - optional
18+ */
1419 constructor ( collection , props = { } ) {
1520 validate_strings ( collection , 'Collection Name' )
16-
17- this . collection = collection
21+ this . collection = collection
1822 }
23+
24+ /*
25+ * Constructs a new CyclicItem addressed by key inside of this collection
26+ * @arg {string} key - the key to assign to the new item
27+ * @returns {CyclicItem } - new item for the key in this collection
28+ */
1929 item ( key ) {
2030 return new CyclicItem ( this . collection , key )
21- }
31+ }
32+
33+ /*
34+ * Retrieve data from dynamodb associated to key instead of this collection.
35+ * @arg {string} key - the key to lookup the item
36+ * @returns {CyclicItem } - retrieves the data associated with key from dynamodb
37+ */
2238 async get ( key ) {
2339 let item = new CyclicItem ( this . collection , key )
2440 return item . get ( )
@@ -27,7 +43,7 @@ class CyclicCollection{
2743 let item = new CyclicItem ( this . collection , key )
2844 return item . set ( props , opts )
2945 }
30-
46+
3147 async delete ( key , props , opts ) {
3248 let item = new CyclicItem ( this . collection , key )
3349 return item . delete ( )
@@ -40,36 +56,36 @@ class CyclicCollection{
4056 }
4157
4258 let scans = Array . from ( { length : segments } , ( _ , index ) => index + 1 ) ;
43-
59+
4460 let filter = gen_expression ( q )
4561 filter . expression = `${ filter . expression } AND cy_meta.#kc = :vcol`
4662 filter . attr_names [ `#kc` ] = 'c'
4763 filter . attr_vals [ `:vcol` ] = this . collection
48-
64+
4965 let r = {
5066 results : [ ]
5167 }
5268
5369 let segment_results = await Promise . all ( scans . map ( s => {
5470 return this . parallel_scan ( filter , s - 1 , segments )
55-
71+
5672 } ) )
5773
5874 segment_results . forEach ( s => {
5975 s . results . forEach ( sr => { r . results . push ( sr ) } )
6076 } )
61-
77+
6278 return r
6379 }
6480
65-
81+
6682
6783 async parallel_scan ( filter , segment , total_segments , limit = 50000 , next = undefined ) {
6884 let results = [ ]
6985 do {
7086 var params = {
7187 TableName : process . env . CYCLIC_DB ,
72- Limit : limit ,
88+ Limit : limit ,
7389 ScanIndexForward :false ,
7490 Segment : segment ,
7591 TotalSegments :total_segments ,
@@ -175,4 +191,4 @@ class CyclicCollection{
175191}
176192
177193
178- module . exports = CyclicCollection
194+ module . exports = CyclicCollection
0 commit comments