All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mauricio.async.db.mysql.decoder.ColumnDefinitionDecoder.scala Maven / Gradle / Ivy

The newest version!
/*
* Copyright 2013 Maurício Linhares
*
* Maurício Linhares licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.github.mauricio.async.db.mysql.decoder

import io.netty.buffer.ByteBuf
import com.github.mauricio.async.db.mysql.message.server.ColumnDefinitionMessage
import com.github.mauricio.async.db.util.ChannelWrapper.bufferToWrapper
import com.github.mauricio.async.db.util.Log
import com.google.common.cache._
import java.nio.charset.Charset
import java.util.concurrent.{ Callable, TimeUnit }
import com.github.mauricio.async.db.mysql.codec.DecoderRegistry

object ColumnDefinitionDecoder {
  final val log = Log.get[ColumnDefinitionDecoder]
  final val cache : Cache[String, ColumnDefinitionMessage] = CacheBuilder
    .newBuilder()
    .maximumSize(4096)
    .expireAfterAccess(120, TimeUnit.SECONDS)
    .build()
}

class ColumnDefinitionDecoder(charset: Charset, registry : DecoderRegistry) extends MessageDecoder {

  override def decode(buffer: ByteBuf): ColumnDefinitionMessage = {

    val catalog = buffer.readLengthEncodedString(charset)
    val schema = buffer.readLengthEncodedString(charset)
    val table = buffer.readLengthEncodedString(charset)
    val originalTable = buffer.readLengthEncodedString(charset)
    val name = buffer.readLengthEncodedString(charset)
    val originalName = buffer.readLengthEncodedString(charset)

    buffer.readBinaryLength

    val characterSet = buffer.readUnsignedShort()
    val columnLength = buffer.readUnsignedInt()
    val columnType = buffer.readUnsignedByte()
    val flags = buffer.readShort()
    val decimals = buffer.readByte()

    buffer.readShort()
    val key = s"$schema|$table|$originalTable|$name|$originalName|$characterSet|$columnType|$columnLength|$flags|$decimals"
    ColumnDefinitionDecoder.cache.get(key, new Callable[ColumnDefinitionMessage] {
      def call() = new ColumnDefinitionMessage(
        catalog,
        schema,
        table,
        originalTable,
        name,
        originalName,
        characterSet,
        columnLength,
        columnType,
        flags,
        decimals,
        registry.binaryDecoderFor(columnType, characterSet),
        registry.textDecoderFor(columnType,characterSet)
      )
    })
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy