Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take()/limit() not available? #73

Closed
zoul0813 opened this issue Aug 10, 2017 · 2 comments
Closed

take()/limit() not available? #73

zoul0813 opened this issue Aug 10, 2017 · 2 comments

Comments

@zoul0813
Copy link
Contributor

Looks like the Query Builder's "take" and "limit" methods are not available, preventing you from limiting the results returned.

// Should return up to 50 results, no more
$results = Model::take(50)->get();

The "getAll" method has a $limit parameter, but this also does not appear to work due to the "use_iterator" parameter being defaulted to true.

Changing the default to false for $user_iterator, and adding the following code to the DynamoDbBuilder class seems to allow "take()/limit()" to work as expected.

    /**
     * @var int
     */
    protected $limit = -1;

    /**
     * Alias to set the "limit" value of the query.
     *
     * @param  int  $value
     * @return \Illuminate\Database\Query\Builder|static
     */
    public function take($value)
    {
        return $this->limit($value);
    }

    /**
     * Set the "limit" value of the query.
     *
     * @param  int  $value
     * @return $this
     */
    public function limit($value) {
        $this->limit = $value;
        return $this;
    }

    protected function getAll($columns = [], $limit = -1, $use_iterator = true)
    {
        if($limit === -1) $limit = $this->limit; // set the limit to the 
        // clipped remaining method code as it's unchanged
    }
@baopham
Copy link
Owner

baopham commented Aug 11, 2017

Does it work when you have where conditions?

It is quite inefficient to do a get all in dynamodb. That is doing a limit of 1000 might not really give you 1000 because of the scan limit.

@zoul0813
Copy link
Contributor Author

Because of how Limit works in DynamoDb, ... it does work with where conditions ... but if your table has 1000 items which might be returned, and a total of 5000 items ... Model::where($condition)->take(1000)->get() may return less than 1000 items ...

To get it to return the 1000 results that would match, we'd have to call the query multiple times using the LastEvaluatedKey until we have >= Limit items to return ... then we only return Limit items in the Collection, discarding any extras. Perhaps taking advantage of 'chunk' ... and changing the while(true) {} or the break; condition to account for the Limit

At the moment, I just needed a way to reduce the number of items being returned as my table has thousands ... and for testing, I only wanted a handful of them.

getAll() is also protected, so I can't call it directly... and chunk still gets all the records, it just processes them in smaller batches using the Limit parameter. So there's no way to just get "X" Items back as one would expect when they call "take/limit".

baopham added a commit that referenced this issue Aug 12, 2017
baopham added a commit that referenced this issue Aug 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants