(self, pageindex, callback, ready)
| 729 | } |
| 730 | |
| 731 | function findDocumentFree(self, pageindex, callback, ready) { |
| 732 | |
| 733 | var offset = offsetPage(self, pageindex); |
| 734 | var buf = U.createBufferSize(self.header.pagesize); |
| 735 | |
| 736 | Fs.read(self.fd, buf, 0, buf.length, offset, function() { |
| 737 | |
| 738 | // ==== DB:PAGE (20b) |
| 739 | // type (1b) = from: 0 |
| 740 | // index (1b) = from: 1 |
| 741 | // documents (2b) = from: 2 |
| 742 | // freeslots (1b) = from: 4 |
| 743 | // parentindex (4b) = from: 5 |
| 744 | |
| 745 | var relid = buf.readUInt32LE(5); |
| 746 | if (!relid) { |
| 747 | if (!ready) { |
| 748 | callback(null, 0); |
| 749 | return; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // First page is the last page saved in meta therefore is needed to perform recursive with "ready" |
| 754 | if (!ready) { |
| 755 | findDocumentFree(self, relid, callback, true); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | var documents = buf.readUInt16LE(2); |
| 760 | if (documents >= self.header.pagelimit) { |
| 761 | // Finds in parent if exists |
| 762 | if (relid) |
| 763 | findDocumentFree(self, relid, callback, true); |
| 764 | else |
| 765 | callback(null, 0); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | // Finds a free document slot |
| 770 | var index = getDocumentIndex(self, pageindex) - 1; |
| 771 | var buffer = U.createBufferSize(self.header.pagelimit * self.header.documentsize); |
| 772 | |
| 773 | Fs.read(self.fd, buffer, 0, buffer.length, offset + self.header.pagesize, function() { |
| 774 | while (true) { |
| 775 | index++; |
| 776 | var data = buffer.slice(0, self.header.documentsize); |
| 777 | if (!data.length) |
| 778 | break; |
| 779 | |
| 780 | if (data[2] === STATE_REMOVED) { |
| 781 | |
| 782 | if (F.isKilled) |
| 783 | return; |
| 784 | |
| 785 | updPageMeta(self, pageindex, function(err, buf) { |
| 786 | buf.writeUInt16LE(documents + 1, 2); |
| 787 | setImmediate(callback, null, index, pageindex); |
| 788 | }); |
no test coverage detected