async icon indicating copy to clipboard operation
async copied to clipboard

Refactor ByteParser

Open Joannis opened this issue 7 years ago • 0 comments

ByteParser is useful but isn't as useful as it can be. I suggest splitting it up into more fine grained and specialized parser helper by enforcing splitting up the parser in 3 stages.

Stage 1 - boundaries Input == ByteBuffer, Output == PacketBuffer:

Parses only the length bytes, creates a buffer efficiently with as few copies as possible (refferring to the asynchronously available buffer)

  final class ManualBuffer {
    let buffer: MutableByteBuffer
    deinit { 
      buffer.baseAddress?.deallocate(capacity: buffer.count)
    }
  }
  
  enum Buffer {
    case temporary(ByteBuffer)
    case persistent(ManualBuffer)
  } 
}

Stage 2 - Classification Input == PacketBuffer, Output == SomeSpecificPacket:

Transform the PacketBuffer into a specific packet with more details In SQL that’s be a “row” packet, “column” packet, query packet

  let buffer: PacketBuffer

  init(buffer: PacketBuffer) throws {
    self.buffer = buffer
    // optional validation
  }
}

Stage 3 - LazyParsing:

Add an extension to a specific packet type that parses the actual data.

  var someInt32: Int32 {
    return self.buffer.makeInt32(offset: 4)
  }
}```

Add helpers to the `PacketBuffer` that help efficiently and easily parse data

Joannis avatar Jan 19 '18 01:01 Joannis