Created
August 20, 2015 22:51
-
-
Save davisp/78275a854c1f48293370 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| diff --git a/apps/couch/src/couch_btree.erl b/apps/couch/src/couch_btree.erl | |
| index 36c2553..e76a485 100644 | |
| --- a/apps/couch/src/couch_btree.erl | |
| +++ b/apps/couch/src/couch_btree.erl | |
| @@ -348,27 +348,34 @@ complete_root(Bt, KPs) -> | |
| % written. | |
| chunkify(InList) -> | |
| - BaseChunkSize = list_to_integer(config:get("couchdb", | |
| - "btree_chunk_size", "1279")), | |
| - case erlang:external_size(InList) of | |
| - Size when Size > BaseChunkSize -> | |
| - NumberOfChunksLikely = ((Size div BaseChunkSize) + 1), | |
| - ChunkThreshold = Size div NumberOfChunksLikely, | |
| - chunkify(InList, ChunkThreshold, [], 0, []); | |
| - _Else -> | |
| - [InList] | |
| + ChunkSizeStr = config:get("couchdb", "btree_chunk_size", "1279"), | |
| + ChunkSize = list_to_integer(ChunkSizeStr), | |
| + case erlang:external_size(InList) < ChunkSize of | |
| + true -> | |
| + % Return a single chunk | |
| + [InList]; | |
| + false -> | |
| + chunkify(InList, ChunkSize, 0, [], []) | |
| end. | |
| -chunkify([], _ChunkThreshold, [], 0, OutputChunks) -> | |
| - lists:reverse(OutputChunks); | |
| -chunkify([], _ChunkThreshold, OutList, _OutListSize, OutputChunks) -> | |
| - lists:reverse([lists:reverse(OutList) | OutputChunks]); | |
| -chunkify([InElement | RestInList], ChunkThreshold, OutList, OutListSize, OutputChunks) -> | |
| - case erlang:external_size(InElement) of | |
| - Size when (Size + OutListSize) > ChunkThreshold andalso OutList /= [] -> | |
| - chunkify(RestInList, ChunkThreshold, [], 0, [lists:reverse([InElement | OutList]) | OutputChunks]); | |
| - Size -> | |
| - chunkify(RestInList, ChunkThreshold, [InElement | OutList], OutListSize + Size, OutputChunks) | |
| +chunkify([], _ChunkSize, 0, [], ChunkAcc) -> | |
| + lists:reverse(ChunkAcc); | |
| +chunkify([], _ChunkSize, _CurrSize, [Single], [LastChunk | RestAcc]) -> | |
| + % Don't sent out chunks with a single element. Just push | |
| + % them onto the last chunk. | |
| + NewLastChunk = LastChunk ++ [Single], | |
| + lists:reverse(RestAcc, [NewLastChunk]); | |
| +chunkify([], _ChunkSize, _CurrSize, CurrChunk, ChunkAcc) -> | |
| + lists:reverse(ChunkAcc, [lists:reverse(CurrChunk)]); | |
| +chunkify([Next | Rest], ChunkSize, CurrSize, CurrChunk, ChunkAcc) -> | |
| + Size = erlang:external_size(Next), | |
| + case Size + CurrSize =< ChunkSize of | |
| + true -> | |
| + NewCurrChunk = [Next | CurrChunk], | |
| + chunkify(Rest, ChunkSize, Size + CurrSize, NewCurrChunk, ChunkAcc); | |
| + false -> | |
| + NewChunkAcc = [lists:reverse(CurrChunk, [Next]) | ChunkAcc], | |
| + chunkify(Rest, ChunkSize, 0, [], NewChunkAcc) | |
| end. | |
| modify_node(Bt, Node, Actions, QueryOutput) -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like a lot of gratuitous changes in here that obscure the actual fix to the function.