<commit_before>package com.readytalk.oss.dbms.server.protocol;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;

import com.readytalk.oss.dbms.Column;
import com.readytalk.oss.dbms.ForeignKey;
import com.readytalk.oss.dbms.Index;
import com.readytalk.oss.dbms.Table;
import com.readytalk.oss.dbms.server.EpidemicServer;
import com.readytalk.oss.dbms.server.EpidemicServer.NodeID;
import com.readytalk.oss.dbms.server.StreamUtil;

public class Protocol {
  
  private static final int ClassDefinition = 6;
  private static final int ClassReference = 7;
  private static final int Reference = 8;

  private static volatile Map<Class<?>, Serializer<?>> serializers = new HashMap<Class<?>, Serializer<?>>();
  private static volatile Map<Class<?>, Deserializer<?>> deserializers
    = new HashMap<Class<?>, Deserializer<?>>();

  static {
    serializers.put(Boolean.class, new Serializer<Boolean>() {
      public void writeTo(WriteContext context, Boolean v) throws IOException {
        writeBoolean(context.out, v);
      }
    });

    deserializers.put(Boolean.class, new Deserializer<Boolean>() {
      public Boolean readFrom(ReadContext context, Class<? extends Boolean> c) throws IOException {
        return readBoolean(context.in);
      }
    });

    serializers.put(Integer.class, new Serializer<Integer>() {
      public void writeTo(WriteContext context, Integer v) throws IOException {
        writeInteger(context.out, v);
      }
    });

    deserializers.put(Integer.class, new Deserializer<Integer>() {
      public Integer readFrom(ReadContext context, Class<? extends Integer> c) throws IOException {
        return readInteger(context.in);
      }
    });

    serializers.put(Long.class, new Serializer<Long>() {
      public void writeTo(WriteContext context, Long v) throws IOException {
        writeLong(context.out, v);
      }
    });

    deserializers.put(Long.class, new Deserializer<Long>() {
      public Long readFrom(ReadContext context, Class<? extends Long> c) throws IOException {
        return readLong(context.in);
      }
    });

    serializers.put(String.class, new Serializer<String>() {
      public void writeTo(WriteContext context, String v) throws IOException {
        writeString(context.out, v);
      }
    });

    deserializers.put(String.class, new Deserializer<String>() {
      public String readFrom(ReadContext context, Class<? extends String> c) throws IOException {
        return readString(context.in);
      }
    });

    serializers.put(Class.class, new Serializer<Class<?>>() {
      public void writeTo(WriteContext context, Class<?> v) throws IOException {
        write(context, v.getName());
      }
    });

    deserializers.put(Class.class, new Deserializer<Class<?>>() {
      public Class<?> readFrom(ReadContext context, Class<? extends Class<?>> c) throws IOException {
        try {
          return Class.forName((String) read(context));
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(e);
        }
      }
    });

    serializers.put(Writable.class, new Serializer<Writable>() {
      public void writeTo(WriteContext context, Writable v) throws IOException {
        v.writeTo(context);
      }
    });

    deserializers.put(Readable.class, new Deserializer<Readable>() {
      public Readable readFrom(ReadContext context, Class<? extends Readable> c) throws IOException {
        Readable v;
        try {
          v = (Readable) c.newInstance();
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        }
        v.readFrom(context);
        return v;
      }
    });

    serializers.put(Stringable.class, new Serializer<Stringable>() {
      public void writeTo(WriteContext context, Stringable v)
        throws IOException
      {
        writeString(context.out, v.asString());
      }
    });

    deserializers.put(Stringable.class, new Deserializer<Stringable>() {
      public Stringable readFrom(ReadContext context,
                                 Class<? extends Stringable> c)
        throws IOException
      {
        try {
          return (Stringable) c.getConstructor(String.class).newInstance
            (readString(context.in));
        } catch (NoSuchMethodException e) {
          throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
          throw new RuntimeException(e);
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        }
      }
    });

    serializers.put(Table.class, new Serializer<Table>() {
      public void writeTo(WriteContext context, Table t) throws IOException {
        write(context, t.id);
        writeInteger(context.out, t.order);
        List<Column> columns = t.primaryKey.columns;
        writeInteger(context.out, columns.size());
        for (Column<?> c: columns) {
          write(context, c);
        }
      }
    });

    deserializers.put(Table.class, new Deserializer<Table>() {
      public Table readFrom(ReadContext context, Class<? extends Table> c) throws IOException {
        String id = (String) read(context);
        int order = readInteger(context.in);
        int columnCount = readInteger(context.in);
        List<Column> columns = new ArrayList<Column>(columnCount);
        for (int i = 0; i < columnCount; ++i) {
          columns.add((Column<?>) read(context));
        }
        return new Table(columns, id);
      }
    });

    serializers.put(Column.class, new Serializer<Column<?>>() {
      public void writeTo(WriteContext context, Column<?> c) throws IOException {
        write(context, c.type);
        write(context, c.id);
      }
    });

    deserializers.put(Column.class, new Deserializer<Column<?>>() {
      public Column<?> readFrom(ReadContext context, Class<? extends Column<?>> c) throws IOException {
        return new Column<Object>((Class) read(context), (String) read(context));
      }
    });

    serializers.put(ForeignKey.class, new Serializer<ForeignKey>() {<commit_msg>use 3-arg constructor when deserializing Tables<commit_after>package com.readytalk.oss.dbms.server.protocol;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;

import com.readytalk.oss.dbms.Column;
import com.readytalk.oss.dbms.ForeignKey;
import com.readytalk.oss.dbms.Index;
import com.readytalk.oss.dbms.Table;
import com.readytalk.oss.dbms.server.EpidemicServer;
import com.readytalk.oss.dbms.server.EpidemicServer.NodeID;
import com.readytalk.oss.dbms.server.StreamUtil;

public class Protocol {
  
  private static final int ClassDefinition = 6;
  private static final int ClassReference = 7;
  private static final int Reference = 8;

  private static volatile Map<Class<?>, Serializer<?>> serializers = new HashMap<Class<?>, Serializer<?>>();
  private static volatile Map<Class<?>, Deserializer<?>> deserializers
    = new HashMap<Class<?>, Deserializer<?>>();

  static {
    serializers.put(Boolean.class, new Serializer<Boolean>() {
      public void writeTo(WriteContext context, Boolean v) throws IOException {
        writeBoolean(context.out, v);
      }
    });

    deserializers.put(Boolean.class, new Deserializer<Boolean>() {
      public Boolean readFrom(ReadContext context, Class<? extends Boolean> c) throws IOException {
        return readBoolean(context.in);
      }
    });

    serializers.put(Integer.class, new Serializer<Integer>() {
      public void writeTo(WriteContext context, Integer v) throws IOException {
        writeInteger(context.out, v);
      }
    });

    deserializers.put(Integer.class, new Deserializer<Integer>() {
      public Integer readFrom(ReadContext context, Class<? extends Integer> c) throws IOException {
        return readInteger(context.in);
      }
    });

    serializers.put(Long.class, new Serializer<Long>() {
      public void writeTo(WriteContext context, Long v) throws IOException {
        writeLong(context.out, v);
      }
    });

    deserializers.put(Long.class, new Deserializer<Long>() {
      public Long readFrom(ReadContext context, Class<? extends Long> c) throws IOException {
        return readLong(context.in);
      }
    });

    serializers.put(String.class, new Serializer<String>() {
      public void writeTo(WriteContext context, String v) throws IOException {
        writeString(context.out, v);
      }
    });

    deserializers.put(String.class, new Deserializer<String>() {
      public String readFrom(ReadContext context, Class<? extends String> c) throws IOException {
        return readString(context.in);
      }
    });

    serializers.put(Class.class, new Serializer<Class<?>>() {
      public void writeTo(WriteContext context, Class<?> v) throws IOException {
        write(context, v.getName());
      }
    });

    deserializers.put(Class.class, new Deserializer<Class<?>>() {
      public Class<?> readFrom(ReadContext context, Class<? extends Class<?>> c) throws IOException {
        try {
          return Class.forName((String) read(context));
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(e);
        }
      }
    });

    serializers.put(Writable.class, new Serializer<Writable>() {
      public void writeTo(WriteContext context, Writable v) throws IOException {
        v.writeTo(context);
      }
    });

    deserializers.put(Readable.class, new Deserializer<Readable>() {
      public Readable readFrom(ReadContext context, Class<? extends Readable> c) throws IOException {
        Readable v;
        try {
          v = (Readable) c.newInstance();
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        }
        v.readFrom(context);
        return v;
      }
    });

    serializers.put(Stringable.class, new Serializer<Stringable>() {
      public void writeTo(WriteContext context, Stringable v)
        throws IOException
      {
        writeString(context.out, v.asString());
      }
    });

    deserializers.put(Stringable.class, new Deserializer<Stringable>() {
      public Stringable readFrom(ReadContext context,
                                 Class<? extends Stringable> c)
        throws IOException
      {
        try {
          return (Stringable) c.getConstructor(String.class).newInstance
            (readString(context.in));
        } catch (NoSuchMethodException e) {
          throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
          throw new RuntimeException(e);
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        }
      }
    });

    serializers.put(Table.class, new Serializer<Table>() {
      public void writeTo(WriteContext context, Table t) throws IOException {
        write(context, t.id);
        writeInteger(context.out, t.order);
        List<Column> columns = t.primaryKey.columns;
        writeInteger(context.out, columns.size());
        for (Column<?> c: columns) {
          write(context, c);
        }
      }
    });

    deserializers.put(Table.class, new Deserializer<Table>() {
      public Table readFrom(ReadContext context, Class<? extends Table> c) throws IOException {
        String id = (String) read(context);
        int order = readInteger(context.in);
        int columnCount = readInteger(context.in);
        List<Column> columns = new ArrayList<Column>(columnCount);
        for (int i = 0; i < columnCount; ++i) {
          columns.add((Column<?>) read(context));
        }
        return new Table(columns, id, order);
      }
    });

    serializers.put(Column.class, new Serializer<Column<?>>() {
      public void writeTo(WriteContext context, Column<?> c) throws IOException {
        write(context, c.type);
        write(context, c.id);
      }
    });

    deserializers.put(Column.class, new Deserializer<Column<?>>() {
      public Column<?> readFrom(ReadContext context, Class<? extends Column<?>> c) throws IOException {
        return new Column<Object>((Class) read(context), (String) read(context));
      }
    });

    serializers.put(ForeignKey.class, new Serializer<ForeignKey>() {
{'fraction_non_alphanumeric': 0.11751988263454559, 'fraction_numerical': 0.0006949270326615705, 'mean_word_length': 3.004947433518862, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6717115', 'n_tokens_mistral': 3586, 'n_tokens_neox': 3563, 'n_words': 866}