1.0.4 • Published 2 years ago

nv-buf-read-byte v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

nv-buf-read-byte

  • nv-buf-read-byte
  • read just one-byte from file
  • this is for test nvlang's byte-stream interpreter(parse unknown code char-by-char from remote),normally USELESS

install

  • npm install nv-buf-read-byte

usage

   const creat = require("nv-buf-read-byte"); 

   var ctx  = creat(filename,buf_size=4096,max_element_size=4) ;  // buf_size must >= max_element_size * 3

   ctx.read_byte(index)
   ctx.read_byte(index)
   
   .....
   
   ctx.release() 

verify

    const fs = require("fs");

    function verify(fn="./index.js",bufsz=4096) {
        let begin = (new Date).getTime();
        var  buf    = fs.readFileSync(fn);
        var small   = creat(fn,bufsz);
        var i;
        for(i=0;i<buf.length;i++) {
            let orig = buf[i];
            let curr = small.read_byte(i);
            if(orig === curr) {
            } else {
                small.release()
                throw(i)
            }
        }
        let end = (new Date).getTime();
        return({
            sz:buf.length,
            small,
            i,
            cost: (end - begin)
        })
    }

small

        var r = verify("./index.js",4096);
        /*
        {
          sz: 2829,
          small: Ctx {
            buf: <Buffer 63 6f 6e 73 74 20 7b 5f 75 2c 5f 6e 2c 5f 74 2c 5f 66 7d 20 20 20 20 3d 20 72 65 71 75 69 72 65 28 22 6e 76 2d 66 61 63 75 74 69 6c 2d 75 6e 74 66 22 ... 4046 more bytes>,
            fd: 21,
            si: 0,
            ei: 2829,
            sz: 2829,
            max_ele_len: 4,
            bufsz: 4096
          },
          i: 2829,
          cost: 1
        }
        */
        r.small.release();


        var r = verify("./index.js",1024);
        /*
        {
          sz: 2829,
          small: Ctx {
            buf: <Buffer 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 28 5f 75 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ... 974 more bytes>,
            fd: 22,
            si: 2040,
            ei: 2829,
            sz: 2829,
            max_ele_len: 4,
            bufsz: 1024
          },
          i: 2829,
          cost: 0
        }
        */
        r.small.release()

        var r = small.verify("./index.js",64);

        /*
        {
          sz: 2829,
          small: Ctx {
            buf: <Buffer 65 61 74 5f 63 74 78 3b 0a 6e 65 77 20 43 74 78 28 66 6e 2c 62 75 66 73 7a 2c 6d 61 78 5f 65 6c 65 5f 6c 65 6e 29 3b 0a 0a 6d 6f 64 75 6c 65 2e 65 78 ... 14 more bytes>,
            fd: 23,
            si: 2820,
            ei: 2829,
            sz: 2829,
            max_ele_len: 4,
            bufsz: 64
          },
          i: 2829,
          cost: 1
        }
        */
        r.small.release()


        var r = verify("./index.js",12);
        /*
        {
          sz: 2829,
          small: Ctx {
            buf: <Buffer 63 74 78 3b 0a 61 74 5f 63 74 78 3b>,
            fd: 24,
            si: 2824,
            ei: 2829,
            sz: 2829,
            max_ele_len: 4,
            bufsz: 12
          },
          i: 2829,
          cost: 1
        }
        */

        r.small.release()

        verify("./index.js",8);
        /*
        Uncaught Error: bufsz:8 must >= max_ele_len:4 * 3
            at creat_err (/mnt/sda3/NV6-/nvbuf/pkgs/nv-buf-read-byte/node_modules/nv-facutil-thrw/index.js:4:29)
            at thrw_str (/mnt/sda3/NV6-/nvbuf/pkgs/nv-buf-read-byte/node_modules/nv-facutil-thrw/index.js:5:36)
            at new Ctx (/mnt/sda3/NV6-/nvbuf/pkgs/nv-buf-read-byte/index.js:61:13)
            at creat_ctx (/mnt/sda3/NV6-/nvbuf/pkgs/nv-buf-read-byte/index.js:98:52)
            at verify (REPL26:4:19)
        */

big

        const {_psj}  = require("nv-facutil-promise");


        const fs = require("fs");

        function verify(fn="/mnt/nas/webrtc.tar",bufsz=4096) {
            let begin     = (new Date).getTime();
            var total    = fs.lstatSync(fn).size;
            var small     = creat(fn,bufsz);
            var cursor    = 0;
            let [p,rs,rj] = _psj();
            var rs$       = fs.createReadStream(fn);
            rs$.on("data",(data)=>{
                for(let i=0;i<data.length;i++) {
                    let orig = data[i];
                    let curr = small.read_byte(cursor);
                    if(orig === curr) {
                        ++cursor;
                    } else {
                        small.release();
                        rj({
                            sz:total,
                            small,
                            cursor,
                        })
                    }             
                }
            });
            rs$.on("end",()=>{
                 let end = (new Date).getTime();
                 rs({
                      sz:total,
                      small,
                      cursor,
                      cost: (end - begin)         
                 })
            })
            return(p);
        }



        fs.lstatSync("/mnt/nas/webrtc.tar")
        /*
        Stats {
          dev: 78,
          mode: 33261,
          nlink: 1,
          uid: 0,
          gid: 0,
          rdev: 0,
          blksize: 1048576,
          ino: 76414987,
          size: 21592446325,
          blocks: 42174464,
          atimeMs: 1650852163805.1646,
          mtimeMs: 1650852163805.1646,
          ctimeMs: 1650852163805.1646,
          birthtimeMs: 1650851790823.6287,
          atime: 2022-04-25T02:02:43.805Z,
          mtime: 2022-04-25T02:02:43.805Z,
          ctime: 2022-04-25T02:02:43.805Z,
          birthtime: 2022-04-25T01:56:30.824Z
        }
        */


            r = await verify("/mnt/nas/webrtc.tar",4096);
            r.small.release();
        /*

        {
          sz: 21592446325,
          small: Ctx {
            buf: <Buffer bb 9c 01 70 08 6f e9 9d 73 a3 df ca 72 5c ab bc 27 d4 bc 67 d8 91 97 54 75 67 3d b3 51 a9 79 8f 74 ee c1 bb d0 0e 13 cb 2a 22 ea 8b 63 19 ac e9 44 ba ... 4046 more bytes>,
            fd: 21,
            si: 21592444632,
            ei: 21592446325,
            sz: 21592446325,
            max_ele_len: 4,
            bufsz: 4096
          },
          cursor: 21592446325,
          cost: 949770
        }
        */

test

            function loop() {
                let begin     = (new Date).getTime();
                for(let i=0;i<21592446325;++i) {}
                let end = (new Date).getTime();
                return(end - begin)
            }

            /*
              > loop()
                24129
              > 

              empty loop need 24 seconds 

            */


            function tst(fn="/mnt/nas/webrtc.tar",bufsz=4096) {
                let begin     = (new Date).getTime();
                    var total    = fs.lstatSync(fn).size;
                    var small     = creat(fn,bufsz);
                    var lst;
                    for(let i=0;i<total;++i) {
                         lst = small.read_byte(i)
                    }
                let end = (new Date).getTime();
                     return({
                          sz:total,
                          small,
                          lst,
                          cost: (end - begin)
                     })
            }



                r = await tst("/mnt/nas/webrtc.tar",4096);
                r.small.release();


            /*


                {
                  sz: 21592446325,
                  small: Ctx {
                    buf: <Buffer bb 9c 01 70 08 6f e9 9d 73 a3 df ca 72 5c ab bc 27 d4 bc 67 d8 91 97 54 75 67 3d b3 51 a9 79 8f 74 ee c1 bb d0 0e 13 cb 2a 22 ea 8b 63 19 ac e9 44 ba ... 4046 more bytes>,
                    fd: 23,
                    si: 21592444632,
                    ei: 21592446325,
                    sz: 21592446325,
                    max_ele_len: 4,
                    bufsz: 4096
                  },
                  lst: 202,
                  cost: 602217
                }

            */    
       

METHODS

       ctx.read_byte(index) 


      ctx.release()

      //// 

      ctx.is_idx_in_buf            
      ctx.read_byte_if_idx_in_buf  


      ctx.buf                      
      ctx.bufsz                    
      ctx.ei                       
      ctx.fd                       
      ctx.max_ele_len
      ctx.si                       
      ctx.sz

API

LICENSE

  • ISC
1.0.4

2 years ago

1.0.1

2 years ago