-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZENKO-1124: mongo listing, avoid to loop
- Loading branch information
Jeremy Desanlis
committed
Sep 10, 2018
1 parent
7088812
commit b306e20
Showing
2 changed files
with
91 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const assert = require('assert'); | ||
|
||
const { FILTER_END, FILTER_SKIP, SKIP_NONE } = require('./tools'); | ||
|
||
|
||
const MAX_STREAK_LENGTH = 100; | ||
|
||
|
||
class Skip { | ||
constructor(params) { | ||
assert(params.extension); | ||
|
||
this.extension = params.extension; | ||
this.gteParams = params.gte; | ||
|
||
this.listingEndCb = null; | ||
this.skipRangeCb = null; | ||
this.streakLength = 0; | ||
} | ||
|
||
setListingEndCb(cb) { | ||
this.listingEndCb = cb; | ||
} | ||
|
||
setSkipRangeCb(cb) { | ||
this.skipRangeCb = cb; | ||
} | ||
|
||
filter(entry) { | ||
const filteringResult = this.extension.filter(entry); | ||
const skippingRange = this.extension.skipping(); | ||
|
||
if (filteringResult === FILTER_END) { | ||
if (this.listingEndCb) { | ||
this.listingEndCb(); | ||
} | ||
} else if (filteringResult === FILTER_SKIP | ||
&& skippingRange !== SKIP_NONE) { | ||
if (++this.streakLength >= MAX_STREAK_LENGTH) { | ||
/* Avoid to loop on the same range again and again. */ | ||
const newRange = this._inc(skippingRange); | ||
if (newRange === this.gteParams) { | ||
this.streakLength = 0; | ||
} else if (this.skipRangeCb) { | ||
this.skipRangeCb(newRange); | ||
} | ||
} | ||
} else { | ||
this.streakLength = 0; | ||
} | ||
} | ||
|
||
_inc(str) { | ||
if (!str) { | ||
return str; | ||
} | ||
|
||
const lastCharValue = str.charCodeAt(str.length - 1); | ||
const lastCharNewValue = String.fromCharCode(lastCharValue + 1); | ||
return `${str.slice(0, str.length - 1)}${lastCharNewValue}`; | ||
} | ||
} | ||
|
||
|
||
module.exports = { Skip }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters