Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ByteParser #48

Open
Joannis opened this issue Jan 19, 2018 · 0 comments
Open

Refactor ByteParser #48

Joannis opened this issue Jan 19, 2018 · 0 comments
Milestone

Comments

@Joannis
Copy link
Contributor

Joannis commented Jan 19, 2018

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
@tanner0101 tanner0101 added this to the 1.0 milestone Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants