private LmdbPayload createPayload(final WriteTxn writeTxn,
                                      final LmdbDb db,
                                      final boolean complete) {
        SearchProgressLog.increment(queryKey, SearchPhase.LMDB_DATA_STORE_CREATE_PAYLOAD);
        return SimpleMetrics.measure("createPayload", () -> {
            if (maxPayloadSize > 0) {
                final PayloadOutput payloadOutput = new PayloadOutput(minPayloadSize);
                final AtomicBoolean finalPayload = new AtomicBoolean(complete);
                db.iterate(writeTxn, cursorIterable -> {
                    long size = 0;
                    long count = 0;

                    for (final KeyVal<ByteBuffer> kv : cursorIterable) {
                        final ByteBuffer keyBuffer = kv.key();
                        final ByteBuffer valBuffer = kv.val();

                        // Make sure we don't add a state key to the payload.
                        if (LmdbRowKeyFactoryFactory.isNotStateKey(keyBuffer)) {
                            // Add to the size of the current payload.
                            size += 4;
                            size += keyBuffer.remaining();
                            size += 4;
                            size += valBuffer.remaining();
                            count++;

                            if (size < maxPayloadSize || count == 1) {
                                payloadOutput.writeInt(keyBuffer.remaining());
                                payloadOutput.writeByteBuffer(keyBuffer);
                                payloadOutput.writeInt(valBuffer.remaining());
                                payloadOutput.writeByteBuffer(valBuffer);

                                db.delete(writeTxn, keyBuffer.flip());

                            } else {
                                // We have reached the maximum payload size so stop adding.
                                // We also can't be complete as more payloads will be needed.
                                finalPayload.set(false);
                                break;
                            }
                        }
                    }
                });

                writeTxn.commit();
                payloadOutput.close();
                return new LmdbPayload(finalPayload.get(), payloadOutput.toBytes());

            } else {
                final PayloadOutput payloadOutput = new PayloadOutput(minPayloadSize);
                db.iterate(writeTxn, cursorIterable -> {
                    for (final KeyVal<ByteBuffer> kv : cursorIterable) {
                        final ByteBuffer keyBuffer = kv.key();
                        final ByteBuffer valBuffer = kv.val();

                        payloadOutput.writeInt(keyBuffer.remaining());
                        payloadOutput.writeByteBuffer(keyBuffer);
                        payloadOutput.writeInt(valBuffer.remaining());
                        payloadOutput.writeByteBuffer(valBuffer);
                    }
                });

                db.drop(writeTxn);
                writeTxn.commit();
                payloadOutput.close();
                return new LmdbPayload(complete, payloadOutput.toBytes());
            }
        });
    }
Write data from the data store to an output removing them from the datastore as we go as they will be transferred
to another store.

@param output The output to write to.
stroom-query/stroom-query-common/src/main/java/stroom/query/common/v2/LmdbPayloadCreator.java