EditableArray children,
boolean isFirst,
String nextBlock ) {
// The children in the next block should be added to the children in this block, even if the size would be too large
// as any too-large blocks will eventually be optimized later ...
EditableDocument info = document.getDocument(CHILDREN_INFO);
if (info == null) {
info = document.setDocument(CHILDREN_INFO);
}
// First, find the next block that we can use ...
Set<String> toBeDeleted = new HashSet<String>();
SchematicEntry nextEntry = null;
String nextBlocksNext = null;
while (nextBlock != null) {
nextEntry = documentStore.get(nextBlock);
Document nextDoc = nextEntry.getContent();
List<?> nextChildren = nextDoc.getArray(CHILDREN);
Document nextInfo = nextDoc.getDocument(CHILDREN_INFO);
if (nextChildren == null || nextChildren.isEmpty()) {
// Delete this empty block ...
toBeDeleted.add(nextBlock);
nextEntry = null;
// And figure out the next block ...
nextBlock = nextInfo != null ? nextInfo.getString(NEXT_BLOCK) : null;
} else {
// We can use this block, so copy the children into it ...
children.addAll(nextChildren);
// Figure out the key for the next block ...
nextBlocksNext = nextInfo != null ? nextInfo.getString(NEXT_BLOCK) : null;
if (isFirst && nextBlocksNext == null) {
// This is the first block and there is no more, so set the count and remove the block-related fields ...
info.setNumber(COUNT, children.size());
info.remove(NEXT_BLOCK);
info.remove(LAST_BLOCK);
} else {
// Just update the block size and the next block ...
info.setNumber(BLOCK_SIZE, children.size());
info.setString(NEXT_BLOCK, nextBlocksNext);
}
// And then mark it for deletion ...
toBeDeleted.add(nextBlock);
nextBlock = null;