buffer operations

This commit is contained in:
vss-devel 2019-02-26 20:33:42 +03:00
parent 477e4a657e
commit 3cb9420b45

View File

@ -639,27 +639,22 @@ class Item {
fatal( 'storeDirEntry error: clusterIdx == null', this ) fatal( 'storeDirEntry error: clusterIdx == null', this )
return return
} }
header.articleCount++ header.articleCount++
const mimeId = this.mimeId()
const isRedirect = redirectTarget != null // redirect dirEntry is shorter in 4 bytes
var buf = Buffer.alloc( isRedirect ? 12 : 16 )
var mimeId = this.mimeId()
log( 'storeDirEntry', mimeId, this ) log( 'storeDirEntry', mimeId, this )
writeUIntLE( buf, mimeId, 0, 2 ) const chunks = [
writeUIntLE( buf, 0, 2, 1 ) // parameters length [ mimeId, 2 ],
buf.write( this.nameSpace, 3, 1 ) [ 0, 1 ], // parameters length
buf.writeIntLE( this.revision, 4, 4 ) this.nameSpace,
writeUIntLE( buf, clusterIdx || redirectTarget || 0, 8, 4 ) // or redirect target article index [ this.revision, 4 ],
if ( ! isRedirect ) [ clusterIdx || redirectTarget || 0, 4 ], // or redirect target article index
writeUIntLE( buf, blobIdx, 12, 4 ) redirectTarget == null ? [ blobIdx, 4 ] : '', // if not a redirect
this.path + '\0',
this.title + '\0',
]
var urlBuf = Buffer.from( this.path + '\0' ) this.dirEntryOffset = await out.write( chunksToBuffer( chunks ))
var titleBuf = Buffer.from( this.title + '\0' ) log( 'storeDirEntry done', this.dirEntryOffset, this.path )
this.dirEntry = await out.write( Buffer.concat([ buf, urlBuf, titleBuf ]))
log( 'storeDirEntry done', this.dirEntry, buf.length, this.path )
return this.saveDirEntryIndex( this.dirEntry ) return this.saveDirEntryIndex( this.dirEntry )
} }
@ -1379,27 +1374,28 @@ function getHeader () {
//~ log( 'Header', 'articleCount', header.articleCount, 'clusterCount', header.clusterCount, 'mainPage', mainPage ) //~ log( 'Header', 'articleCount', header.articleCount, 'clusterCount', header.clusterCount, 'mainPage', mainPage )
log( 'Header', header ) log( 'Header', header )
var buf = Buffer.alloc( headerLength ) const chunks = [
writeUIntLE( buf, header.magicNumber, 0, 4 ) [ header.magicNumber, 4 ],
writeUIntLE( buf, header.versionMajor, 4, 2 ) [ header.versionMajor, 2 ],
writeUIntLE( buf, header.versionMinor, 6, 2 ) [ header.versionMinor, 2 ],
header.uuid.copy( buf, 8 ) header.uuid,
writeUIntLE( buf, header.articleCount, 24, 4 ) [ header.articleCount, 4 ],
writeUIntLE( buf, header.clusterCount, 28, 4 ) [ header.clusterCount, 4 ],
writeUIntLE( buf, header.urlPtrPos, 32, 8 ) [ header.urlPtrPos, 8 ],
writeUIntLE( buf, header.titlePtrPos, 40, 8 ) [ header.titlePtrPos, 8 ],
writeUIntLE( buf, header.clusterPtrPos, 48, 8 ) [ header.clusterPtrPos, 8 ],
writeUIntLE( buf, header.mimeListPos, 56, 8 ) [ header.mimeListPos, 8 ],
writeUIntLE( buf, header.mainPage, 64, 4 ) [ header.mainPage, 4 ],
writeUIntLE( buf, header.layoutPage, 68, 4 ) [ header.layoutPage, 4 ],
writeUIntLE( buf, header.checksumPos, 72, 8 ) [ header.checksumPos, 8 ],
]
return buf
return chunksToBuffer( chunks )
} }
async function storeHeader() { async function storeHeader() {