text:
package org.apache.arrow.vector.complex.writer;

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.Float4Vector;
import org.apache.arrow.vector.Float8Vector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.SchemaChangeCallBack;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.NonNullableStructVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.complex.impl.ComplexWriterImpl;
import org.apache.arrow.vector.complex.impl.SingleStructReaderImpl;
import org.apache.arrow.vector.complex.impl.SingleStructWriter;
import org.apache.arrow.vector.complex.impl.UnionListReader;
import org.apache.arrow.vector.complex.impl.UnionListWriter;
import org.apache.arrow.vector.complex.impl.UnionReader;
import org.apache.arrow.vector.complex.impl.UnionWriter;
import org.apache.arrow.vector.complex.reader.BaseReader.StructReader;
import org.apache.arrow.vector.complex.reader.BigIntReader;
import org.apache.arrow.vector.complex.reader.FieldReader;
import org.apache.arrow.vector.complex.reader.Float4Reader;
import org.apache.arrow.vector.complex.reader.Float8Reader;
import org.apache.arrow.vector.complex.reader.IntReader;
import org.apache.arrow.vector.complex.writer.BaseWriter.ComplexWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.ListWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.StructWriter;
import org.apache.arrow.vector.holders.DecimalHolder;
import org.apache.arrow.vector.holders.IntHolder;
import org.apache.arrow.vector.holders.NullableTimeStampNanoTZHolder;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeID;
import org.apache.arrow.vector.types.pojo.ArrowType.Int;
import org.apache.arrow.vector.types.pojo.ArrowType.Struct;
import org.apache.arrow.vector.types.pojo.ArrowType.Timestamp;
import org.apache.arrow.vector.types.pojo.ArrowType.Union;
import org.apache.arrow.vector.types.pojo.ArrowType.Utf8;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.util.CallBack;
import org.apache.arrow.vector.util.DateUtility;
import org.apache.arrow.vector.util.DecimalUtility;
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.JsonStringHashMap;
import org.apache.arrow.vector.util.Text;
import org.apache.arrow.vector.util.TransferPair;
import org.junit.Assert;
import org.junit.Test;

import io.netty.buffer.ArrowBuf;

public class TestComplexWriter {

  private static final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);

  private static final int COUNT = 100;

  @Test
  public void simpleNestedTypes() {
    NonNullableStructVector parent = populateStructVector(null);
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
    for (int i = 0; i < COUNT; i++) {
      rootReader.setPosition(i);
      Assert.assertEquals(i, rootReader.reader("int").readInteger().intValue());
      Assert.assertEquals(i, rootReader.reader("bigInt").readLong().longValue());
    }

    parent.close();
  }

  @Test
  public void transferPairSchemaChange() {
    SchemaChangeCallBack callBack1 = new SchemaChangeCallBack();
    SchemaChangeCallBack callBack2 = new SchemaChangeCallBack();
    NonNullableStructVector parent = populateStructVector(callBack1);

    TransferPair tp = parent.getTransferPair("newVector", allocator, callBack2);

    ComplexWriter writer = new ComplexWriterImpl("newWriter", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    IntWriter intWriter = rootWriter.integer("newInt");
    intWriter.writeInt(1);
    writer.setValueCount(1);

    assertTrue(callBack1.getSchemaChangedAndReset());
    // The second vector should not have registered a schema change
    assertFalse(callBack1.getSchemaChangedAndReset());
  }

  private NonNullableStructVector populateStructVector(CallBack callBack) {
    NonNullableStructVector parent =
        new NonNullableStructVector("parent", allocator, new FieldType(false, Struct.INSTANCE, null, null), callBack);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    IntWriter intWriter = rootWriter.integer("int");
    BigIntWriter bigIntWriter = rootWriter.bigInt("bigInt");
    for (int i = 0; i < COUNT; i++) {
      rootWriter.start();
      intWriter.writeInt(i);
      bigIntWriter.writeBigInt(i);
      rootWriter.end();
    }
    writer.setValueCount(COUNT);
    return parent;
  }

  @Test
  public void nullableStruct() {
    try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) {
      ComplexWriter writer = new ComplexWriterImpl("root", structVector);
      StructWriter rootWriter = writer.rootAsStruct();
      for (int i = 0; i < COUNT; i++) {
        rootWriter.start();
        if (i % 2 == 0) {
          StructWriter structWriter = rootWriter.struct("struct");
          structWriter.setPosition(i);
          structWriter.start();
          structWriter.bigInt("nested").writeBigInt(i);
          structWriter.end();
        }
        rootWriter.end();
      }
      writer.setValueCount(COUNT);
      checkNullableStruct(structVector);
    }
  }

  /**
   * This test is similar to {@link #nullableStruct()} ()} but we get the inner struct writer once at the beginning.
   */
  @Test
  public void nullableStruct2() {
    try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) {
      ComplexWriter writer = new ComplexWriterImpl("root", structVector);
      StructWriter rootWriter = writer.rootAsStruct();
      StructWriter structWriter = rootWriter.struct("struct");

      for (int i = 0; i < COUNT; i++) {
        rootWriter.start();
        if (i % 2 == 0) {
          structWriter.setPosition(i);
          structWriter.start();
          structWriter.bigInt("nested").writeBigInt(i);
          structWriter.end();
        }
        rootWriter.end();
      }
      writer.setValueCount(COUNT);
      checkNullableStruct(structVector);
    }
  }

  private void checkNullableStruct(NonNullableStructVector structVector) {
    StructReader rootReader = new SingleStructReaderImpl(structVector).reader("root");
    for (int i = 0; i < COUNT; i++) {
      rootReader.setPosition(i);
      assertTrue("index is set: " + i, rootReader.isSet());
      FieldReader struct = rootReader.reader("struct");
      if (i % 2 == 0) {
        assertTrue("index is set: " + i, struct.isSet());
        assertNotNull("index is set: " + i, struct.readObject());
        assertEquals(i, struct.reader("nested").readLong().longValue());
      } else {
        assertFalse("index is not set: " + i, struct.isSet());
        assertNull("index is not set: " + i, struct.readObject());
      }
    }
  }

  @Test
  public void testList() {
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();

    rootWriter.start();
    rootWriter.bigInt("int").writeBigInt(0);
    rootWriter.list("list").startList();
    rootWriter.list("list").bigInt().writeBigInt(0);
    rootWriter.list("list").endList();
    rootWriter.end();

    rootWriter.start();
    rootWriter.bigInt("int").writeBigInt(1);
    rootWriter.end();

    writer.setValueCount(2);

    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");

    rootReader.setPosition(0);
    assertTrue("row 0 list is not set", rootReader.reader("list").isSet());
    assertEquals(Long.valueOf(0), rootReader.reader("list").reader().readLong());

    rootReader.setPosition(1);
    assertFalse("row 1 list is set", rootReader.reader("list").isSet());
  }

  @Test
  public void listScalarType() {
    ListVector listVector = ListVector.empty("list", allocator);
    listVector.allocateNew();
    UnionListWriter listWriter = new UnionListWriter(listVector);
    for (int i = 0; i < COUNT; i++) {
      listWriter.startList();
      for (int j = 0; j < i % 7; j++) {
        if (j % 2 == 0) {
          listWriter.writeInt(j);
        } else {
          IntHolder holder = new IntHolder();
          holder.value = j;
          listWriter.write(holder);
        }
      }
      listWriter.endList();
    }
    listWriter.setValueCount(COUNT);
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      for (int j = 0; j < i % 7; j++) {
        listReader.next();
        assertEquals(j, listReader.reader().readInteger().intValue());
      }
    }
  }

  @Test
  public void listDecimalType() {
    ListVector listVector = ListVector.empty("list", allocator);
    listVector.allocateNew();
    UnionListWriter listWriter = new UnionListWriter(listVector);
    DecimalHolder holder = new DecimalHolder();
    holder.buffer = allocator.buffer(DecimalUtility.DECIMAL_BYTE_LENGTH);
    for (int i = 0; i < COUNT; i++) {
      listWriter.startList();
      for (int j = 0; j < i % 7; j++) {
        if (j % 2 == 0) {
          listWriter.writeDecimal(new BigDecimal(j));
        } else {
          DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0);
          holder.start = 0;
          holder.scale = 0;
          holder.precision = 10;
          listWriter.write(holder);
        }
      }
      listWriter.endList();
    }
    listWriter.setValueCount(COUNT);
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      for (int j = 0; j < i % 7; j++) {
        listReader.next();
        Object expected = new BigDecimal(j);
        Object actual = listReader.reader().readBigDecimal();
        assertEquals(expected, actual);
      }
    }
  }

  @Test
  public void listScalarTypeNullable() {
    ListVector listVector = ListVector.empty("list", allocator);
    listVector.allocateNew();
    UnionListWriter listWriter = new UnionListWriter(listVector);
    for (int i = 0; i < COUNT; i++) {
      if (i % 2 == 0) {
        listWriter.setPosition(i);
        listWriter.startList();
        for (int j = 0; j < i % 7; j++) {
          listWriter.writeInt(j);
        }
        listWriter.endList();
      }
    }
    listWriter.setValueCount(COUNT);
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      if (i % 2 == 0) {
        assertTrue("index is set: " + i, listReader.isSet());
        assertEquals("correct length at: " + i, i % 7, ((List<?>) listReader.readObject()).size());
      } else {
        assertFalse("index is not set: " + i, listReader.isSet());
        assertNull("index is not set: " + i, listReader.readObject());
      }
    }
  }

  @Test
  public void listStructType() {
    ListVector listVector = ListVector.empty("list", allocator);
    listVector.allocateNew();
    UnionListWriter listWriter = new UnionListWriter(listVector);
    StructWriter structWriter = listWriter.struct();
    for (int i = 0; i < COUNT; i++) {
      listWriter.startList();
      for (int j = 0; j < i % 7; j++) {
        structWriter.start();
        structWriter.integer("int").writeInt(j);
        structWriter.bigInt("bigInt").writeBigInt(j);
        structWriter.end();
      }
      listWriter.endList();
    }
    listWriter.setValueCount(COUNT);
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      for (int j = 0; j < i % 7; j++) {
        listReader.next();
        Assert.assertEquals("record: " + i, j, listReader.reader().reader("int").readInteger().intValue());
        Assert.assertEquals(j, listReader.reader().reader("bigInt").readLong().longValue());
      }
    }
  }

  @Test
  public void listListType() {
    try (ListVector listVector = ListVector.empty("list", allocator)) {
      listVector.allocateNew();
      UnionListWriter listWriter = new UnionListWriter(listVector);
      for (int i = 0; i < COUNT; i++) {
        listWriter.startList();
        for (int j = 0; j < i % 7; j++) {
          ListWriter innerListWriter = listWriter.list();
          innerListWriter.startList();
          for (int k = 0; k < i % 13; k++) {
            innerListWriter.integer().writeInt(k);
          }
          innerListWriter.endList();
        }
        listWriter.endList();
      }
      listWriter.setValueCount(COUNT);
      checkListOfLists(listVector);
    }
  }

  /**
   * This test is similar to {@link #listListType()} but we get the inner list writer once at the beginning.
   */
  @Test
  public void listListType2() {
    try (ListVector listVector = ListVector.empty("list", allocator)) {
      listVector.allocateNew();
      UnionListWriter listWriter = new UnionListWriter(listVector);
      ListWriter innerListWriter = listWriter.list();

      for (int i = 0; i < COUNT; i++) {
        listWriter.startList();
        for (int j = 0; j < i % 7; j++) {
          innerListWriter.startList();
          for (int k = 0; k < i % 13; k++) {
            innerListWriter.integer().writeInt(k);
          }
          innerListWriter.endList();
        }
        listWriter.endList();
      }
      listWriter.setValueCount(COUNT);
      checkListOfLists(listVector);
    }
  }

  private void checkListOfLists(final ListVector listVector) {
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      for (int j = 0; j < i % 7; j++) {
        listReader.next();
        FieldReader innerListReader = listReader.reader();
        for (int k = 0; k < i % 13; k++) {
          innerListReader.next();
          Assert.assertEquals("record: " + i, k, innerListReader.reader().readInteger().intValue());
        }
      }
    }
  }

  @Test
  public void unionListListType() {
    try (ListVector listVector = ListVector.empty("list", allocator)) {
      listVector.allocateNew();
      UnionListWriter listWriter = new UnionListWriter(listVector);
      for (int i = 0; i < COUNT; i++) {
        listWriter.startList();
        for (int j = 0; j < i % 7; j++) {
          ListWriter innerListWriter = listWriter.list();
          innerListWriter.startList();
          for (int k = 0; k < i % 13; k++) {
            if (k % 2 == 0) {
              innerListWriter.integer().writeInt(k);
            } else {
              innerListWriter.bigInt().writeBigInt(k);
            }
          }
          innerListWriter.endList();
        }
        listWriter.endList();
      }
      listWriter.setValueCount(COUNT);
      checkUnionList(listVector);
    }
  }

  /**
   * This test is similar to {@link #unionListListType()} but we get the inner list writer once at the beginning.
   */
  @Test
  public void unionListListType2() {
    try (ListVector listVector = ListVector.empty("list", allocator)) {
      listVector.allocateNew();
      UnionListWriter listWriter = new UnionListWriter(listVector);
      ListWriter innerListWriter = listWriter.list();

      for (int i = 0; i < COUNT; i++) {
        listWriter.startList();
        for (int j = 0; j < i % 7; j++) {
          innerListWriter.startList();
          for (int k = 0; k < i % 13; k++) {
            if (k % 2 == 0) {
              innerListWriter.integer().writeInt(k);
            } else {
              innerListWriter.bigInt().writeBigInt(k);
            }
          }
          innerListWriter.endList();
        }
        listWriter.endList();
      }
      listWriter.setValueCount(COUNT);
      checkUnionList(listVector);
    }
  }

  private void checkUnionList(ListVector listVector) {
    UnionListReader listReader = new UnionListReader(listVector);
    for (int i = 0; i < COUNT; i++) {
      listReader.setPosition(i);
      for (int j = 0; j < i % 7; j++) {
        listReader.next();
        FieldReader innerListReader = listReader.reader();
        for (int k = 0; k < i % 13; k++) {
          innerListReader.next();
          if (k % 2 == 0) {
            Assert.assertEquals("record: " + i, k, innerListReader.reader().readInteger().intValue());
          } else {
            Assert.assertEquals("record: " + i, k, innerListReader.reader().readLong().longValue());
          }
        }
      }
    }
  }

  @Test
  public void simpleUnion() {
    UnionVector vector = new UnionVector("union", allocator, null);
    UnionWriter unionWriter = new UnionWriter(vector);
    unionWriter.allocate();
    for (int i = 0; i < COUNT; i++) {
      unionWriter.setPosition(i);
      if (i % 2 == 0) {
        unionWriter.writeInt(i);
      } else {
        unionWriter.writeFloat4((float) i);
      }
    }
    vector.setValueCount(COUNT);
    UnionReader unionReader = new UnionReader(vector);
    for (int i = 0; i < COUNT; i++) {
      unionReader.setPosition(i);
      if (i % 2 == 0) {
        Assert.assertEquals(i, i, unionReader.readInteger());
      } else {
        Assert.assertEquals((float) i, unionReader.readFloat(), 1e-12);
      }
    }
    vector.close();
  }

  @Test
  public void promotableWriter() {
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    for (int i = 0; i < 100; i++) {
      BigIntWriter bigIntWriter = rootWriter.bigInt("a");
      bigIntWriter.setPosition(i);
      bigIntWriter.writeBigInt(i);
    }
    Field field = parent.getField().getChildren().get(0).getChildren().get(0);
    Assert.assertEquals("a", field.getName());
    Assert.assertEquals(Int.TYPE_TYPE, field.getType().getTypeID());
    Int intType = (Int) field.getType();

    Assert.assertEquals(64, intType.getBitWidth());
    Assert.assertTrue(intType.getIsSigned());
    for (int i = 100; i < 200; i++) {
      VarCharWriter varCharWriter = rootWriter.varChar("a");
      varCharWriter.setPosition(i);
      byte[] bytes = Integer.toString(i).getBytes();
      ArrowBuf tempBuf = allocator.buffer(bytes.length);
      tempBuf.setBytes(0, bytes);
      varCharWriter.writeVarChar(0, bytes.length, tempBuf);
    }
    field = parent.getField().getChildren().get(0).getChildren().get(0);
    Assert.assertEquals("a", field.getName());
    Assert.assertEquals(Union.TYPE_TYPE, field.getType().getTypeID());
    Assert.assertEquals(Int.TYPE_TYPE, field.getChildren().get(0).getType().getTypeID());
    Assert.assertEquals(Utf8.TYPE_TYPE, field.getChildren().get(1).getType().getTypeID());
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
    for (int i = 0; i < 100; i++) {
      rootReader.setPosition(i);
      FieldReader reader = rootReader.reader("a");
      Long value = reader.readLong();
      Assert.assertNotNull("index: " + i, value);
      Assert.assertEquals(i, value.intValue());
    }
    for (int i = 100; i < 200; i++) {
      rootReader.setPosition(i);
      FieldReader reader = rootReader.reader("a");
      Text value = reader.readText();
      Assert.assertEquals(Integer.toString(i), value.toString());
    }
  }

  /**
   * Even without writing to the writer, the union schema is created correctly.
   */
  @Test
  public void promotableWriterSchema() {
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    rootWriter.bigInt("a");
    rootWriter.varChar("a");

    Field field = parent.getField().getChildren().get(0).getChildren().get(0);
    Assert.assertEquals("a", field.getName());
    Assert.assertEquals(ArrowTypeID.Union, field.getType().getTypeID());

    Assert.assertEquals(ArrowTypeID.Int, field.getChildren().get(0).getType().getTypeID());
    Int intType = (Int) field.getChildren().get(0).getType();
    Assert.assertEquals(64, intType.getBitWidth());
    Assert.assertTrue(intType.getIsSigned());
    Assert.assertEquals(ArrowTypeID.Utf8, field.getChildren().get(1).getType().getTypeID());
  }

  private Set<String> getFieldNames(List<Field> fields) {
    Set<String> fieldNames = new HashSet<>();
    for (Field field : fields) {
      fieldNames.add(field.getName());
      if (!field.getChildren().isEmpty()) {
        for (String name : getFieldNames(field.getChildren())) {
          fieldNames.add(field.getName() + "::" + name);
        }
      }
    }
    return fieldNames;
  }

  @Test
  public void structWriterMixedCaseFieldNames() {
    // test case-sensitive StructWriter
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("rootCaseSensitive", parent, false, true);
    StructWriter rootWriterCaseSensitive = writer.rootAsStruct();
    rootWriterCaseSensitive.bigInt("int_field");
    rootWriterCaseSensitive.bigInt("Int_Field");
    rootWriterCaseSensitive.float4("float_field");
    rootWriterCaseSensitive.float4("Float_Field");
    StructWriter structFieldWriterCaseSensitive = rootWriterCaseSensitive.struct("struct_field");
    structFieldWriterCaseSensitive.varChar("char_field");
    structFieldWriterCaseSensitive.varChar("Char_Field");
    ListWriter listFieldWriterCaseSensitive = rootWriterCaseSensitive.list("list_field");
    StructWriter listStructFieldWriterCaseSensitive = listFieldWriterCaseSensitive.struct();
    listStructFieldWriterCaseSensitive.bit("bit_field");
    listStructFieldWriterCaseSensitive.bit("Bit_Field");

    List<Field> fieldsCaseSensitive = parent.getField().getChildren().get(0).getChildren();
    Set<String> fieldNamesCaseSensitive = getFieldNames(fieldsCaseSensitive);
    Assert.assertEquals(11, fieldNamesCaseSensitive.size());
    Assert.assertTrue(fieldNamesCaseSensitive.contains("int_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("Int_Field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("float_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("Float_Field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("struct_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("struct_field::char_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("struct_field::Char_Field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::bit_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::Bit_Field"));

    // test case-insensitive StructWriter
    ComplexWriter writerCaseInsensitive = new ComplexWriterImpl("rootCaseInsensitive", parent, false, false);
    StructWriter rootWriterCaseInsensitive = writerCaseInsensitive.rootAsStruct();

    rootWriterCaseInsensitive.bigInt("int_field");
    rootWriterCaseInsensitive.bigInt("Int_Field");
    rootWriterCaseInsensitive.float4("float_field");
    rootWriterCaseInsensitive.float4("Float_Field");
    StructWriter structFieldWriterCaseInsensitive = rootWriterCaseInsensitive.struct("struct_field");
    structFieldWriterCaseInsensitive.varChar("char_field");
    structFieldWriterCaseInsensitive.varChar("Char_Field");
    ListWriter listFieldWriterCaseInsensitive = rootWriterCaseInsensitive.list("list_field");
    StructWriter listStructFieldWriterCaseInsensitive = listFieldWriterCaseInsensitive.struct();
    listStructFieldWriterCaseInsensitive.bit("bit_field");
    listStructFieldWriterCaseInsensitive.bit("Bit_Field");

    List<Field> fieldsCaseInsensitive = parent.getField().getChildren().get(1).getChildren();
    Set<String> fieldNamesCaseInsensitive = getFieldNames(fieldsCaseInsensitive);
    Assert.assertEquals(7, fieldNamesCaseInsensitive.size());
    Assert.assertTrue(fieldNamesCaseInsensitive.contains("int_field"));
    Assert.assertTrue(fieldNamesCaseInsensitive.contains("float_field"));
    Assert.assertTrue(fieldNamesCaseInsensitive.contains("struct_field"));
    Assert.assertTrue(fieldNamesCaseInsensitive.contains("struct_field::char_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$"));
    Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::bit_field"));
  }

  @Test
  public void timeStampSecWriter() throws Exception {
    // test values
    final long expectedSecs = 981173106L;
    final LocalDateTime expectedSecDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 0);

    // write
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();

    {
      TimeStampSecWriter timeStampSecWriter = rootWriter.timeStampSec("sec");
      timeStampSecWriter.setPosition(0);
      timeStampSecWriter.writeTimeStampSec(expectedSecs);
    }
    {
      TimeStampSecTZWriter timeStampSecTZWriter = rootWriter.timeStampSecTZ("secTZ", "UTC");
      timeStampSecTZWriter.setPosition(1);
      timeStampSecTZWriter.writeTimeStampSecTZ(expectedSecs);
    }
    // schema
    List<Field> children = parent.getField().getChildren().get(0).getChildren();
    checkTimestampField(children.get(0), "sec");
    checkTimestampTZField(children.get(1), "secTZ", "UTC");

    // read
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
    {
      FieldReader secReader = rootReader.reader("sec");
      secReader.setPosition(0);
      LocalDateTime secDateTime = secReader.readLocalDateTime();
      Assert.assertEquals(expectedSecDateTime, secDateTime);
      long secLong = secReader.readLong();
      Assert.assertEquals(expectedSecs, secLong);
    }
    {
      FieldReader secTZReader = rootReader.reader("secTZ");
      secTZReader.setPosition(1);
      long secTZLong = secTZReader.readLong();
      Assert.assertEquals(expectedSecs, secTZLong);
    }
  }

  @Test
  public void timeStampMilliWriters() throws Exception {
    // test values
    final long expectedMillis = 981173106123L;
    final LocalDateTime expectedMilliDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123 * 1_000_000);

    // write
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    {
      TimeStampMilliWriter timeStampWriter = rootWriter.timeStampMilli("milli");
      timeStampWriter.setPosition(0);
      timeStampWriter.writeTimeStampMilli(expectedMillis);
    }
    String tz = DateUtility.getTimeZone(10);
    {
      TimeStampMilliTZWriter timeStampTZWriter = rootWriter.timeStampMilliTZ("milliTZ", tz);
      timeStampTZWriter.setPosition(0);
      timeStampTZWriter.writeTimeStampMilliTZ(expectedMillis);
    }
    // schema
    List<Field> children = parent.getField().getChildren().get(0).getChildren();
    checkTimestampField(children.get(0), "milli");
    checkTimestampTZField(children.get(1), "milliTZ", tz);

    // read
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");

    {
      FieldReader milliReader = rootReader.reader("milli");
      milliReader.setPosition(0);
      LocalDateTime milliDateTime = milliReader.readLocalDateTime();
      Assert.assertEquals(expectedMilliDateTime, milliDateTime);
      long milliLong = milliReader.readLong();
      Assert.assertEquals(expectedMillis, milliLong);
    }
    {
      FieldReader milliTZReader = rootReader.reader("milliTZ");
      milliTZReader.setPosition(0);
      long milliTZLong = milliTZReader.readLong();
      Assert.assertEquals(expectedMillis, milliTZLong);
    }

  }

  private void checkTimestampField(Field field, String name) {
    Assert.assertEquals(name, field.getName());
    Assert.assertEquals(ArrowType.Timestamp.TYPE_TYPE, field.getType().getTypeID());
  }

  private void checkTimestampTZField(Field field, String name, String tz) {
    checkTimestampField(field, name);
    Assert.assertEquals(tz, ((Timestamp) field.getType()).getTimezone());
  }

  @Test
  public void timeStampMicroWriters() throws Exception {
    // test values
    final long expectedMicros = 981173106123456L;
    final LocalDateTime expectedMicroDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123456 * 1000);

    // write
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();

    {
      TimeStampMicroWriter timeStampMicroWriter = rootWriter.timeStampMicro("micro");
      timeStampMicroWriter.setPosition(0);
      timeStampMicroWriter.writeTimeStampMicro(expectedMicros);
    }
    String tz = DateUtility.getTimeZone(5);
    {
      TimeStampMicroTZWriter timeStampMicroWriter = rootWriter.timeStampMicroTZ("microTZ", tz);
      timeStampMicroWriter.setPosition(1);
      timeStampMicroWriter.writeTimeStampMicroTZ(expectedMicros);
    }

    // schema
    List<Field> children = parent.getField().getChildren().get(0).getChildren();
    checkTimestampField(children.get(0), "micro");
    checkTimestampTZField(children.get(1), "microTZ", tz);

    // read
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
    {
      FieldReader microReader = rootReader.reader("micro");
      microReader.setPosition(0);
      LocalDateTime microDateTime = microReader.readLocalDateTime();
      Assert.assertEquals(expectedMicroDateTime, microDateTime);
      long microLong = microReader.readLong();
      Assert.assertEquals(expectedMicros, microLong);
    }
    {
      FieldReader microReader = rootReader.reader("microTZ");
      microReader.setPosition(1);
      long microLong = microReader.readLong();
      Assert.assertEquals(expectedMicros, microLong);
    }

  }

  @Test
  public void timeStampNanoWriters() throws Exception {
    // test values
    final long expectedNanos = 981173106123456789L;
    final LocalDateTime expectedNanoDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123456789);

    // write
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();

    {
      TimeStampNanoWriter timeStampNanoWriter = rootWriter.timeStampNano("nano");
      timeStampNanoWriter.setPosition(0);
      timeStampNanoWriter.writeTimeStampNano(expectedNanos);
    }
    String tz = DateUtility.getTimeZone(3);
    {
      TimeStampNanoTZWriter timeStampNanoWriter = rootWriter.timeStampNanoTZ("nanoTZ", tz);
      timeStampNanoWriter.setPosition(0);
      timeStampNanoWriter.writeTimeStampNanoTZ(expectedNanos);
    }
    // schema
    List<Field> children = parent.getField().getChildren().get(0).getChildren();
    checkTimestampField(children.get(0), "nano");
    checkTimestampTZField(children.get(1), "nanoTZ", tz);
    // read
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");

    {
      FieldReader nanoReader = rootReader.reader("nano");
      nanoReader.setPosition(0);
      LocalDateTime nanoDateTime = nanoReader.readLocalDateTime();
      Assert.assertEquals(expectedNanoDateTime, nanoDateTime);
      long nanoLong = nanoReader.readLong();
      Assert.assertEquals(expectedNanos, nanoLong);
    }
    {
      FieldReader nanoReader = rootReader.reader("nanoTZ");
      nanoReader.setPosition(0);
      long nanoLong = nanoReader.readLong();
      Assert.assertEquals(expectedNanos, nanoLong);
      NullableTimeStampNanoTZHolder h = new NullableTimeStampNanoTZHolder();
      nanoReader.read(h);
      Assert.assertEquals(expectedNanos, h.value);
    }
  }

  @Test
  public void fixedSizeBinaryWriters() throws Exception {
    // test values
    int numValues = 10;
    int byteWidth = 9;
    byte[][] values = new byte[numValues][byteWidth];
    for (int i = 0; i < numValues; i++) {
      for (int j = 0; j < byteWidth; j++) {
        values[i][j] = ((byte) i);
      }
    }
    ArrowBuf[] bufs = new ArrowBuf[numValues];
    for (int i = 0; i < numValues; i++) {
      bufs[i] = allocator.buffer(byteWidth);
      bufs[i].setBytes(0, values[i]);
    }

    // write
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();

    String fieldName = "fixedSizeBinary";
    FixedSizeBinaryWriter fixedSizeBinaryWriter = rootWriter.fixedSizeBinary(fieldName, byteWidth);
    for (int i = 0; i < numValues; i++) {
      fixedSizeBinaryWriter.setPosition(i);
      fixedSizeBinaryWriter.writeFixedSizeBinary(bufs[i]);
    }

    // schema
    List<Field> children = parent.getField().getChildren().get(0).getChildren();
    Assert.assertEquals(fieldName, children.get(0).getName());
    Assert.assertEquals(ArrowType.FixedSizeBinary.TYPE_TYPE, children.get(0).getType().getTypeID());

    // read
    StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");

    FieldReader fixedSizeBinaryReader = rootReader.reader(fieldName);
    for (int i = 0; i < numValues; i++) {
      fixedSizeBinaryReader.setPosition(i);
      byte[] readValues = fixedSizeBinaryReader.readByteArray();
      Assert.assertArrayEquals(values[i], readValues);
    }
  }

  @Test
  public void complexCopierWithList() {
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    ComplexWriter writer = new ComplexWriterImpl("root", parent);
    StructWriter rootWriter = writer.rootAsStruct();
    ListWriter listWriter = rootWriter.list("list");
    StructWriter innerStructWriter = listWriter.struct();
    IntWriter outerIntWriter = listWriter.integer();
    rootWriter.start();
    listWriter.startList();
    outerIntWriter.writeInt(1);
    outerIntWriter.writeInt(2);
    innerStructWriter.start();
    IntWriter intWriter = innerStructWriter.integer("a");
    intWriter.writeInt(1);
    innerStructWriter.end();
    innerStructWriter.start();
    intWriter = innerStructWriter.integer("a");
    intWriter.writeInt(2);
    innerStructWriter.end();
    listWriter.endList();
    rootWriter.end();
    writer.setValueCount(1);

    StructVector structVector = (StructVector) parent.getChild("root");
    TransferPair tp = structVector.getTransferPair(allocator);
    tp.splitAndTransfer(0, 1);
    NonNullableStructVector toStructVector = (NonNullableStructVector) tp.getTo();
    JsonStringHashMap<?, ?> toMapValue = (JsonStringHashMap<?, ?>) toStructVector.getObject(0);
    JsonStringArrayList<?> object = (JsonStringArrayList<?>) toMapValue.get("list");
    assertEquals(1, object.get(0));
    assertEquals(2, object.get(1));
    JsonStringHashMap<?, ?> innerStruct = (JsonStringHashMap<?, ?>) object.get(2);
    assertEquals(1, innerStruct.get("a"));
    innerStruct = (JsonStringHashMap<?, ?>) object.get(3);
    assertEquals(2, innerStruct.get("a"));
  }

  @Test
  public void testSingleStructWriter1() {
    /* initialize a SingleStructWriter with empty StructVector and then lazily
     * create all vectors with expected initialCapacity.
     */
    NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);
    SingleStructWriter singleStructWriter = new SingleStructWriter(parent);

    int initialCapacity = 1024;
    singleStructWriter.setInitialCapacity(initialCapacity);

    IntWriter intWriter = singleStructWriter.integer("intField");
    BigIntWriter bigIntWriter = singleStructWriter.bigInt("bigIntField");
    Float4Writer float4Writer = singleStructWriter.float4("float4Field");
    Float8Writer float8Writer = singleStructWriter.float8("float8Field");
    ListWriter listWriter = singleStructWriter.list("listField");

    int intValue = 100;
    long bigIntValue = 10000;
    float float4Value = 100.5f;
    double float8Value = 100.375;

    for (int i = 0; i < initialCapacity; i++) {
      singleStructWriter.start();

      intWriter.writeInt(intValue + i);
      bigIntWriter.writeBigInt(bigIntValue + (long)i);
      float4Writer.writeFloat4(float4Value + (float)i);
      float8Writer.writeFloat8(float8Value + (double)i);

      listWriter.setPosition(i);
      listWriter.startList();
      listWriter.integer().writeInt(intValue + i);
      listWriter.integer().writeInt(intValue + i + 1);
      listWriter.integer().writeInt(intValue + i + 2);
      listWriter.integer().writeInt(intValue + i + 3);
      listWriter.endList();

      singleStructWriter.end();
    }

    IntVector intVector = (IntVector)parent.getChild("intField");
    BigIntVector bigIntVector = (BigIntVector)parent.getChild("bigIntField");
    Float4Vector float4Vector = (Float4Vector)parent.getChild("float4Field");
    Float8Vector float8Vector = (Float8Vector)parent.getChild("float8Field");

    int capacity = singleStructWriter.getValueCapacity();
    assertTrue(capacity >= initialCapacity && capacity <  initialCapacity * 2);
    capacity = intVector.getValueCapacity();
    assertTrue(capacity >= initialCapacity && capacity <  initialCapacity * 2);
    capacity = bigIntVector.getValueCapacity();
    assertTrue(capacity >= initialCapacity && capacity <  initialCapacity * 2);
    capacity = float4Vector.getValueCapacity();
    assertTrue(capacity >= initialCapacity && capacity <  initialCapacity * 2);
    capacity = float8Vector.getValueCapacity();
    assertTrue(capacity >= initialCapacity && capacity <  initialCapacity * 2);

    StructReader singleStructReader = new SingleStructReaderImpl(parent);

    IntReader intReader = singleStructReader.reader("intField");
    BigIntReader bigIntReader = singleStructReader.reader("bigIntField");
    Float4Reader float4Reader = singleStructReader.reader("float4Field");
    Float8Reader float8Reader = singleStructReader.reader("float8Field");
    UnionListReader listReader = (UnionListReader)singleStructReader.reader("listField");

    for (int i = 0; i < initialCapacity; i++) {
      intReader.setPosition(i);
      bigIntReader.setPosition(i);
      float4Reader.setPosition(i);
      float8Reader.setPosition(i);
      listReader.setPosition(i);

      assertEquals(intValue + i, intReader.readInteger().intValue());
      assertEquals(bigIntValue + (long)i, bigIntReader.readLong().longValue());
      assertEquals(float4Value + (float)i, float4Reader.readFloat().floatValue(), 0);
      assertEquals(float8Value + (double)i, float8Reader.readDouble().doubleValue(), 0);

      for (int j = 0; j < 4; j++) {
        listReader.next();
        assertEquals(intValue + i + j, listReader.reader().readInteger().intValue());
      }
    }
  }
}


text:
package db.scalikejdbc

import db.scalikejdbc.rewrite.ImageDbNew.SelectionQuery
import org.specs2.mutable.Specification

class ImagesSqlSpec extends Specification with TestDb {

  val imageFields = "i.page_id as pi_on_i, i.contest as c_on_i, i.title as t_on_i, i.url as u_on_i, i.page_url as pu_on_i, " +
    "i.last_round as lr_on_i, i.width as w_on_i, i.height as h_on_i, i.monument_id as mi_on_i, i.description as d_on_i, " +
    "i.size as s_on_i, i.author as a_on_i"

  val selectionFields = "s.id as i_on_s, s.jury_id as ji_on_s, s.created_at as ca_on_s, s.deleted_at as da_on_s, " +
    "s.round_id as ri_on_s, s.page_id as pi_on_s, s.rate as r_on_s, s.criteria_id as ci_on_s"

  val allFields = imageFields + ", " + selectionFields

  def foldSpace(s: String) = s.replaceAll("\\s+", " ").trim

  sequential

  // TODO start mysql once per spec
  def check(q: SelectionQuery, expected: String, f: SelectionQuery => String = _.query()) = {
    withDb {
      foldSpace(f(q)) === foldSpace(expected)
    }
  }

  "list" should {
    "all" in {
      check(SelectionQuery(),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id"""
      )
    }

    "by user" in {
      check(SelectionQuery(userId = Some(123)),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id
            where
            s.jury_id = 123"""
      )
    }

    "by round" in {
      check(SelectionQuery(roundId = Some(234)),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id
            where
            s.round_id = 234"""
      )
    }

    "by rate" in {
      check(SelectionQuery(rate = Some(1)),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id
            where
            s.rate = 1"""
      )
    }

    "by user, round, rate" in {
      check(SelectionQuery(userId = Some(2), roundId = Some(3), rate = Some(1)),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id
            where
            s.jury_id = 2 and s.round_id = 3 and s.rate = 1"""
      )
    }
  }

  "grouped" should {

    "group by page list by round" in {
      check(SelectionQuery(roundId = Some(3), grouped = true),
        s"""select sum(s.rate) as rate, count(s.rate) as rate_count, $imageFields from images i
            join selection s
            on i.page_id = s.page_id
            where
            s.round_id = 3
          group by s.page_id"""
      )
    }
  }

  "by region" should {
   "list" in {
      check(SelectionQuery(regions = Set("12")),
        s"""select $allFields from images i
            join selection s
            on i.page_id = s.page_id
            join monument m
            on i.monument_id = m.id
            where
            m.adm0 in ('12')"""
      )
    }

    "stat" in {
      def regionStatQuery(q: SelectionQuery): String = q.query(byRegion = true)

      check(SelectionQuery(),
        s"""select m.adm0, count(DISTINCT i.page_id) from images i
            join selection s
            on i.page_id = s.page_id
            join monument m
            on i.monument_id = m.id
            group by m.adm0""", f = regionStatQuery
      )
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
<?php

class woocommerce_store_pricing_rules_admin {

	public $category_admin;
	public $membership_admin;
	public $totals_admin;
	public $group_admin;

	public $taxonomy_admins = array();

	public function __construct() {

		$this->category_admin = new woocommerce_category_pricing_rules_admin();
		$this->membership_admin = new woocommerce_membership_pricing_rules_admin();
		$this->group_admin = new woocommerce_group_pricing_rules_admin();
		$this->totals_admin = new woocommerce_totals_pricing_rules_admin();
        $this->taxonomy_admins['product_brand'] = new woocommerce_taxonomy_pricing_rules_admin('product_brand');

		if ( is_admin() ) {
			add_action( 'admin_enqueue_scripts', array(&$this, 'enqueue') );
			add_action( 'admin_init', array(&$this, 'register_settings') );
			add_action( 'admin_menu', array(&$this, 'on_admin_menu'), 99 );
			add_filter( 'woocommerce_screen_ids', array($this, 'get_woocommerce_screen_ids') );
		}
	}

	public function get_woocommerce_screen_ids( $screens ) {

		$screens[] = 'woocommerce_page_wc_dynamic_pricing';
		return $screens;
	}

	public function enqueue( $hook ) {
		global $post, $woocommerce, $wp_scripts;
		if ( $hook == 'woocommerce_page_wc_dynamic_pricing' || ( $post && $post->post_type == 'product') ) {
			if ( floatval( $woocommerce->version ) >= 2.0 ) {
				wp_enqueue_style( 'woocommerce-pricing-admin', WC_Dynamic_Pricing::plugin_url() . '/assets/admin/admin.css' );
				wp_enqueue_script( 'woocommerce-pricing-admin', WC_Dynamic_Pricing::plugin_url() . '/assets/admin/admin.js', array('jquery', 'jquery-ui-datepicker') );
			} else {
				wp_enqueue_style( 'woocommerce-pricing-admin', WC_Dynamic_Pricing::plugin_url() . '/assets/legacy/admin/admin.css' );
				wp_enqueue_script( 'woocommerce-pricing-admin', WC_Dynamic_Pricing::plugin_url() . '/assets/legacy/admin/admin.js', array('jquery') );
			}

			wp_localize_script( 'woocommerce-pricing-admin', 'woocommerce_pricing_admin', array(
			    'calendar_image' => $woocommerce->plugin_url() . '/assets/images/calendar.png'
			) );

			// Enqueue jQuery UI styles
			$jquery_version = isset( $wp_scripts->registered['jquery-ui-core']->ver ) ? $wp_scripts->registered['jquery-ui-core']->ver : '1.9.2';
			wp_enqueue_style( 'jquery-ui-style', '//ajax.googleapis.com/ajax/libs/jqueryui/' . $jquery_version . '/themes/smoothness/jquery-ui.css' );
		}
	}

	public function on_admin_menu() {
		$show_in_menu = current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : false;
		$slug = add_submenu_page( $show_in_menu, __( 'Dynamic Pricing' ), __( 'Dynamic Pricing' ), 'manage_woocommerce', 'wc_dynamic_pricing', array(&$this, 'on_admin_page') );
	}

	public function on_admin_page() {
		$current_tab = (isset( $_GET['tab'] )) ? $_GET['tab'] : 'order totals';
		$current_view = (isset( $_GET['view'] )) ? $_GET['view'] : 0;
		?>

				<?php
				$tabs = apply_filters( 'woocommerce_dynamic_pricing_tabs', array(
				    'order totals' => array(
					array(
					    'title' => __( 'Order Totals Pricing', 'wc_pricing' ),
					    'description' => 'Order Totals pricing allows you to configure price adjustments for the entire store based on the total order amount.  This is the last discount rule that will be applied.  If other discounts have been applied to cart items, these rules will not be applied to the cart.',
					    'function' => 'totals_tab'
					)
				    ),
				    'roles' => array(
					array(
					    'title' => __( 'Role Pricing', 'wc_pricing' ),
					    'description' => 'Role pricing allows you to configure price adjustments for the entire store based on a users role.',
					    'function' => 'membership_tab'
					)
				    ),
				    'category' => array(
					array(
					    'title' => __( 'Category Pricing', 'wc_pricing' ),
					    'description' => 'Use bulk category pricing to configure bulk price adjustments based on a product\'s category. Category pricing rules will apply before Membership (role-based pricing discounts), and will be cumulative with any Membership rules by default.   The cumulative filter can be used to change this behavior.',
					    'function' => 'basic_category_tab'
					),
					array(
					    'title' => __( 'Advanced Category Pricing', 'wc_pricing' ),
					    'description' => 'Use advanced category pricing to configure price adjustments on items in a customers cart based on quantities.  Adjustments are calculated when the rule matches the configured quantities and will be applied to all items in the cart matching the selected category / categories.
					     Advanced category adjustments take precedence over bulk category adjustments.',
					    'function' => 'advanced_category_tab'
					)
				    )
					) );

				if ( wc_dynamic_pricing_is_groups_active() ) {
					$tabs['groups'] = array(
					    array(
						'title' => __( 'Group Pricing', 'wc_pricing' ),
						'description' => 'Group pricing allows you to configure price adjustments for the entire store based on a users groups.',
						'function' => 'group_tab'
					    )
					);
				}

				if (wc_dynamic_pricing_is_brands_active()){
                    $tabs['brands'] = array(
                        array(
                            'title' => __( 'Brand Pricing', 'wc_pricing' ),
                            'description' => 'Use bulk brand pricing to configure bulk price adjustments based on a product\'s brand. Brand pricing rules will apply before Membership (role-based pricing discounts), and will be cumulative with any Membership rules by default.   The cumulative filter can be used to change this behavior.',
                            'function' => 'basic_brand_tab'
                        ),
                        array(
                            'title' => __( 'Advanced Brand Pricing', 'wc_pricing' ),
                            'description' => 'Use advanced brand pricing to configure price adjustments on items in a customers cart based on quantities.  Adjustments are calculated when the rule matches the configured quantities and will be applied to all items in the cart matching the selected brand.
					     Advanced category adjustments take precedence over bulk brand adjustments.',
                            'function' => 'advanced_brand_tab'
                        )
                    );
                }

				foreach ( $tabs as $name => $value ) :
					echo '' . ucfirst( $name ) . '';
				endforeach;
				?>

			<?php if ( sizeof( $tabs[$current_tab] ) > 0 ) : ?>

<?php
				$links = array();
				foreach ( $tabs[$current_tab] as $key => $tab ) :
					$link = '' . $tab['title'] . '';
					$links[] = $link;
				endforeach;
				echo implode( ' |

', $links );
				?>

<?php endif; ?>

			<?php if ( isset( $tabs[$current_tab][$current_view] ) ) : ?>
				<?php if ( !isset( $tabs[$current_tab][$current_view]['hide_title'] ) || $tabs[$current_tab][$current_view]['hide_title'] != true ) : ?>

<?php echo $tabs[$current_tab][$current_view]['title']; ?>

						<?php if ( $tabs[$current_tab][$current_view]['description'] ) : ?>

<?php echo $tabs[$current_tab][$current_view]['description']; ?>

						<?php endif; ?>

				<?php endif; ?>
				<?php
				$func = $tabs[$current_tab][$current_view]['function'];
				if ( $func && method_exists( $this, $func ) ) {
					$this->$func();
				}
				?>
			<?php endif; ?>

		<?php
	}

	public function membership_tab() {
		$this->membership_admin->basic_meta_box();
	}

	public function group_tab() {
		$this->group_admin->basic_meta_box();
	}

	public function basic_category_tab() {
		$this->category_admin->basic_meta_box();
	}

	public function advanced_category_tab() {
		$this->category_admin->advanced_meta_box();
	}

    public function basic_brand_tab() {
        $this->taxonomy_admins['product_brand']->basic_meta_box();
    }

    public function advanced_brand_tab() {
        $this->taxonomy_admins['product_brand']->advanced_meta_box();
    }

	public function totals_tab() {
		$this->totals_admin->advanced_metabox();
	}

	public function register_settings() {
		register_setting( '_s_membership_pricing_rules', '_s_membership_pricing_rules', array($this, 'on_store_settings_validation') );
		register_setting( '_s_group_pricing_rules', '_s_group_pricing_rules', array($this, 'on_store_settings_validation') );

		register_setting( '_a_category_pricing_rules', '_a_category_pricing_rules', array($this, 'on_category_settings_validation') );
		register_setting( '_s_category_pricing_rules', '_s_category_pricing_rules', array($this, 'on_store_settings_validation') );

		register_setting( '_a_totals_pricing_rules', '_a_totals_pricing_rules', array($this, 'on_totals_settings_validation') );

        register_setting( '_a_taxonomy_product_brand_pricing_rules', '_a_taxonomy_product_brand_pricing_rules', array($this, 'on_product_brand_settings_validation') );
        register_setting( '_s_taxonomy_product_brand_pricing_rules', '_s_taxonomy_product_brand_pricing_rules', array($this, 'on_store_settings_validation') );

    }

	public function on_store_settings_validation( $data ) {
		$pricing_rules = array();
		$rules = array();
		if ( isset( $_POST['pricing_rules'] ) ) {
			$pricing_rule_sets = $_POST['pricing_rules'];
			foreach ( $pricing_rule_sets as $key => $rule_set ) {
				$rules[$key] = $rule_set;
			}
			$data = $rules;
		} else {
			$data = array();
		}

		$this->clear_transients();

		return $data;
	}

	public function on_store_category_settings_validation( $data ) {

		if ( !isset( $data['free_shipping'] ) ) {
			$data['free_shipping'] = 'no';
		}

		$this->clear_transients();

		return $data;
	}

	public function on_category_settings_validation( $data ) {
		$pricing_rules = array();
		$rules = array();
		if ( isset( $_POST['pricing_rules'] ) ) {
			$pricing_rule_sets = $_POST['pricing_rules'];
			foreach ( $pricing_rule_sets as $key => $rule_set ) {
				$rules[$key] = $rule_set;
			}
			$data = $rules;
		} else {
			$data = array();
		}

		return $data;

		$pricing_rules = array();
		$rules = array();
		$message = '';

		if ( isset( $_POST['pricing_rules'] ) ) {
			$pricing_rule_sets = $_POST['pricing_rules'];
			foreach ( $pricing_rule_sets as $key => $rule_set ) {

				if ( $rule_set['rules_type'] == 'block' ) {
					$valid = true;
					$rules[$key] = $rule_set;
					$data = $rules;
				} else {
					$valid = true;
					foreach ( $rule_set['rules'] as $rule ) {
						if ( isset( $rule['adjust'] ) && !empty( $rule['adjust'] ) && isset( $rule['from'] ) && isset( $rule['amount'] ) && !empty( $rule['from'] ) && !empty( $rule['amount'] ) ) {

							if ( $rule['from'] != '*' && $rule['adjust'] != '*' && intval( $rule['adjust'] ) < intval( $rule['from'] ) ) {
								$valid = $valid & false;
								$message .= 'Invalid quantities configured';
							} else {
								$valid = $valid & true;
							}
						} else {
							$valid = $valid & false;
							$message .= 'You must enter values for all quantity and amount fields';
						}
					}

					if ( $valid ) {

						if ( isset( $rule_set['invalid'] ) ) {
							unset( $rules_set['invalid'] );
						}

						$rules[$key] = $rule_set;
					} else {
						$rule_set['invalid'] = $message;
						$rules[$key] = $rule_set;

						add_settings_error( '_a_category_pricing_rules', 'category-rule-invalid', $rule_set['invalid'] );
					}

					$data = $rules;
				}
			}
		} else {

			$data = array();
		}

		$this->clear_transients();

		return $data;
	}

    public function on_product_brand_settings_validation( $data ) {
        $pricing_rules = array();
        $rules = array();
        if ( isset( $_POST['pricing_rules'] ) ) {
            $pricing_rule_sets = $_POST['pricing_rules'];
            foreach ( $pricing_rule_sets as $key => $rule_set ) {
                $rules[$key] = $rule_set;
            }
            $data = $rules;
        } else {
            $data = array();
        }

        return $data;

        $pricing_rules = array();
        $rules = array();
        $message = '';

        if ( isset( $_POST['pricing_rules'] ) ) {
            $pricing_rule_sets = $_POST['pricing_rules'];
            foreach ( $pricing_rule_sets as $key => $rule_set ) {

                if ( $rule_set['rules_type'] == 'block' ) {
                    $valid = true;
                    $rules[$key] = $rule_set;
                    $data = $rules;
                } else {
                    $valid = true;
                    foreach ( $rule_set['rules'] as $rule ) {
                        if ( isset( $rule['adjust'] ) && !empty( $rule['adjust'] ) && isset( $rule['from'] ) && isset( $rule['amount'] ) && !empty( $rule['from'] ) && !empty( $rule['amount'] ) ) {

                            if ( $rule['from'] != '*' && $rule['adjust'] != '*' && intval( $rule['adjust'] ) < intval( $rule['from'] ) ) {
                                $valid = $valid & false;
                                $message .= 'Invalid quantities configured';
                            } else {
                                $valid = $valid & true;
                            }
                        } else {
                            $valid = $valid & false;
                            $message .= 'You must enter values for all quantity and amount fields';
                        }
                    }

                    if ( $valid ) {

                        if ( isset( $rule_set['invalid'] ) ) {
                            unset( $rules_set['invalid'] );
                        }

                        $rules[$key] = $rule_set;
                    } else {
                        $rule_set['invalid'] = $message;
                        $rules[$key] = $rule_set;

                        add_settings_error( '_a_taxonomy_product_brand_pricing_rules', 'category-rule-invalid', $rule_set['invalid'] );
                    }

                    $data = $rules;
                }
            }
        } else {

            $data = array();
        }

        $this->clear_transients();

        return $data;
    }

	public function on_totals_settings_validation( $data ) {
		$pricing_rules = array();
		$rules = array();
		$message = '';

		if ( isset( $_POST['pricing_rules'] ) ) {
			$pricing_rule_sets = $_POST['pricing_rules'];
			foreach ( $pricing_rule_sets as $key => $rule_set ) {
				$valid = true;

				foreach ( $rule_set['rules'] as $rule ) {
					if ( isset( $rule['to'] ) && !empty( $rule['to'] ) && isset( $rule['from'] ) && isset( $rule['amount'] ) && (!empty( $rule['from'] ) || $rule['from'] === '0') && !empty( $rule['amount'] ) ) {

						if ( $rule['from'] != '*' && $rule['to'] != '*' && intval( $rule['to'] ) < intval( $rule['from'] ) ) {
							$valid = $valid & false;
							$message .= 'Invalid totals configured';
						} else {
							$valid = $valid & true;
						}
					} else {
						$valid = $valid & false;
						$message .= 'You must enter values for all total and amount fields';
					}
				}

				if ( $valid ) {

					if ( isset( $rule_set['invalid'] ) ) {
						unset( $rules_set['invalid'] );
					}

					$rules[$key] = $rule_set;
				} else {
					$rule_set['invalid'] = $message;
					$rules[$key] = $rule_set;

					add_settings_error( '_a_totals_pricing_rules', 'totals-rule-invalid', $rule_set['invalid'] );
				}
			}

			$data = $rules;
		} else {

			$data = array();
		}

		$this->clear_transients();

		return $data;
	}

	public function clear_transients() {
		global $wpdb;
		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_wc_var_prices%'" );
		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_wc_var_prices%'" );
		wp_cache_flush();
	}

	private function selected( $value, $compare, $arg = true ) {
		if ( !$arg ) {
			echo '';
		} else if ( (string) $value == (string) $compare ) {
			echo 'selected="selected"';
		}
	}

}


text:
var express = require('express');
var app = express();

app.use(express.static('app'));
app.use('/tiles', express.static('../output'));

var server = app.listen(3000, function () {
  var port = server.address().port;
  console.log('Salt Torque example listening at http://localhost:%s', port);
});


text:
var express = require('express');
var debug = require('debug')('JITVIZ:server');
var logger = require('morgan');
var cors = require('express-cors');
var path = require('path');

var port = process.env.PORT || 8080;

var app = express();

app.use(cors({
    allowedOrigins: [
        'http://localhost:8080'
     ],
    methods: [
        'GET'
    ]
}));
app.use(logger('dev'));

// EPASS returns iso-8859-1
app.use(function(req, res, next) {
  console.log("Req from ", req.path);
  if (path.extname(req.path) === '.html') {
    res.set('content-type', "text/html; charset=iso-8859-1");
  }
  else if (path.extname(req.path) === '.js') {
    res.set('content-type', "application/javascript; charset=iso-8859-1");
  }
  next();
});

app.use(express.static('public'));

app.listen(port);
console.log('Static server is running on ' + port);

module.exports = app;


text:
package datadog

import (
	"fmt"
)

// Comment is a special form of event that appears in a stream.
type Comment struct {
	Id        *int    `json:"id,omitempty"`
	RelatedId *int    `json:"related_event_id,omitempty"`
	Handle    *string `json:"handle,omitempty"`
	Message   *string `json:"message,omitempty"`
	Resource  *string `json:"resource,omitempty"`
	Url       *string `json:"url,omitempty"`
}

// reqComment is the container for receiving commenst.
type reqComment struct {
	Comment *Comment `json:"comment,omitempty"`
}

// CreateComment adds a new comment to the system.
func (client *Client) CreateComment(handle, message string) (*Comment, error) {
	var out reqComment
	comment := Comment{Message: String(message)}
	if len(handle) > 0 {
		comment.Handle = String(handle)
	}
	if err := client.doJsonRequest("POST", "/v1/comments", &comment, &out); err != nil {
		return nil, err
	}
	return out.Comment, nil
}

// CreateRelatedComment adds a new comment, but lets you specify the related
// identifier for the comment.
func (client *Client) CreateRelatedComment(handle, message string,
	relid int) (*Comment, error) {
	var out reqComment
	comment := Comment{Message: String(message), RelatedId: Int(relid)}
	if len(handle) > 0 {
		comment.Handle = String(handle)
	}
	if err := client.doJsonRequest("POST", "/v1/comments", &comment, &out); err != nil {
		return nil, err
	}
	return out.Comment, nil
}

// EditComment changes the message and possibly handle of a particular comment.
func (client *Client) EditComment(id int, handle, message string) error {
	comment := Comment{Message: String(message)}
	if len(handle) > 0 {
		comment.Handle = String(handle)
	}
	return client.doJsonRequest("PUT", fmt.Sprintf("/v1/comments/%d", id),
		&comment, nil)
}

// DeleteComment does exactly what you expect.
func (client *Client) DeleteComment(id int) error {
	return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/comments/%d", id),
		nil, nil)
}


text:
package net.tomp2p.examples;

import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import net.tomp2p.dht.FutureGet;
import net.tomp2p.dht.FuturePut;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.StorageLayer.ProtectionEnable;
import net.tomp2p.dht.StorageLayer.ProtectionMode;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.Number160;
import net.tomp2p.storage.Data;
import net.tomp2p.utils.Utils;

public class ExampleDomainProtection
{
    final Random rnd = new Random( 42L );

    public static void main( String[] args )
        throws NoSuchAlgorithmException, IOException, ClassNotFoundException
    {
        exampleAllMaster();
        exampleNoneMaster();
    }

    public static void exampleAllMaster()
        throws NoSuchAlgorithmException, IOException, ClassNotFoundException
    {
        KeyPairGenerator gen = KeyPairGenerator.getInstance( "DSA" );
        KeyPair pair1 = gen.generateKeyPair();
        KeyPair pair2 = gen.generateKeyPair();
        KeyPair pair3 = gen.generateKeyPair();
        final Number160 peer2Owner = Utils.makeSHAHash( pair2.getPublic().getEncoded() );
        PeerDHT peer1 = new PeerBuilderDHT(new PeerBuilder( pair1 ).ports( 4001 ).start()).start();
        PeerDHT peer2 = new PeerBuilderDHT(new PeerBuilder( pair2 ).ports( 4002 ).start()).start();
        PeerDHT peer3 = new PeerBuilderDHT(new PeerBuilder( pair3 ).ports( 4003 ).start()).start();
        PeerDHT[] peers = new PeerDHT[] { peer1, peer2, peer3 };
        ExampleUtils.bootstrap( peers );
        setProtection( peers, ProtectionEnable.ALL, ProtectionMode.MASTER_PUBLIC_KEY );
        // peer 1 stores "test" in the domain key of owner peer 2
        FuturePut futurePut =
            peer1.put( Number160.ONE ).data( new Data( "test" ) ).domainKey( peer2Owner ).protectDomain().start();
        futurePut.awaitUninterruptibly();
        // peer 2 did not claim this domain, so we stored it
        System.out.println( "stored: " + futurePut.isSuccess() + " -> because no one claimed this domain" );
        // peer 3 want to store something
        futurePut =
            peer3.put( Number160.ONE ).data( new Data( "hello" ) ).domainKey( peer2Owner ).protectDomain().start();
        futurePut.awaitUninterruptibly();
        System.out.println( "stored: " + futurePut.isSuccess() + " -> becaues peer1 already claimed this domain" );
        // peer 2 claims this domain
        futurePut =
            peer2.put( Number160.ONE ).data( new Data( "MINE!" ) ).domainKey( peer2Owner ).protectDomain().start();
        futurePut.awaitUninterruptibly();
        System.out.println( "stored: " + futurePut.isSuccess() + " -> becaues peer2 is the owner" );
        // get the data!
        FutureGet futureGet = peer1.get( Number160.ONE ).domainKey( peer2Owner ).start();
        futureGet.awaitUninterruptibly();
        System.out.println( "we got " + futureGet.data().object() );
        shutdown( peers );
    }

    public static void exampleNoneMaster()
        throws NoSuchAlgorithmException, IOException, ClassNotFoundException
    {
        KeyPairGenerator gen = KeyPairGenerator.getInstance( "DSA" );
        KeyPair pair1 = gen.generateKeyPair();
        KeyPair pair2 = gen.generateKeyPair();
        KeyPair pair3 = gen.generateKeyPair();
        final Number160 peer2Owner = Utils.makeSHAHash( pair2.getPublic().getEncoded() );
        PeerDHT peer1 = new PeerBuilderDHT(new PeerBuilder( pair1 ).ports( 4001 ).start()).start();
        PeerDHT peer2 = new PeerBuilderDHT(new PeerBuilder( pair2 ).ports( 4002 ).start()).start();
        PeerDHT peer3 = new PeerBuilderDHT(new PeerBuilder( pair3 ).ports( 4003 ).start()).start();
        PeerDHT[] peers = new PeerDHT[] { peer1, peer2, peer3 };
        ExampleUtils.bootstrap( peers );
        setProtection( peers, ProtectionEnable.NONE, ProtectionMode.MASTER_PUBLIC_KEY );
        // peer 1 stores "test" in the domain key of owner peer 2
        FuturePut futurePut =
            peer1.put( Number160.ONE ).data( new Data( "test" ) ).protectDomain().domainKey( peer2Owner ).start();
        futurePut.awaitUninterruptibly();
        // peer 2 did not claim this domain, so we stored it
        System.out.println( "stored: " + futurePut.isSuccess()
            + " -> because no one can claim domains except the owner, storage ok but no protection" );
        // peer 3 want to store something
        futurePut =
            peer3.put( Number160.ONE ).data( new Data( "hello" ) ).protectDomain().domainKey( peer2Owner ).start();
        futurePut.awaitUninterruptibly();
        System.out.println( "stored: " + futurePut.isSuccess()
            + " -> because no one can claim domains except the owner, storage ok but no protection" );
        // peer 2 claims this domain
        futurePut =
            peer2.put( Number160.ONE ).data( new Data( "MINE!" ) ).protectDomain().domainKey( peer2Owner ).start();
        futurePut.awaitUninterruptibly();
        System.out.println( "stored: " + futurePut.isSuccess() + " -> becaues peer2 is the owner" );
        // get the data!
        FutureGet futureGet = peer1.get( Number160.ONE ).domainKey( peer2Owner ).start();
        futureGet.awaitUninterruptibly();
        System.out.println( "we got " + futureGet.data().object() );
        futurePut = peer3.put( Number160.ONE ).domainKey( peer2Owner ).data( new Data( "hello" ) ).start();
        futurePut.awaitUninterruptibly();
        System.out.println( "stored: " + futurePut.isSuccess() + " -> because this domain is claimed by peer2" );
        shutdown( peers );
    }

    private static void shutdown( PeerDHT[] peers )
    {
        for ( PeerDHT peer : peers )
        {
            peer.shutdown();
        }
    }

    private static void setProtection( PeerDHT[] peers, ProtectionEnable protectionEnable, ProtectionMode protectionMode )
    {
        for ( PeerDHT peer : peers )
        {
            peer.storageLayer().protection( protectionEnable, protectionMode, protectionEnable,
                                                           protectionMode );
        }
    }
}


text:
function ImageCommand(options) {
    Command.call(this, options);
    this.async = true;
    var attributes;

    function insertImage(img, range) {
        if (attributes.src && attributes.src != 'http://') {
            if (!img) {
                img = dom.create(documentFromRange(range), 'img', attributes);
                img.onload = img.onerror = function () {
                    img.removeAttribute('complete');
                    img.removeAttribute('width');
                    img.removeAttribute('height');
                }
                range.deleteContents();
                range.insertNode(img);
                range.setStartAfter(img);
                range.setEndAfter(img);
                selectRange(range);
                return true;
            } else
                dom.attr(img, attributes);
        }

        return false;
    }

    this.redo = function () {
        var range = this.lockRange();
        if (!insertImage(RangeUtils.image(range), range))
            this.releaseRange(range);
    }

    this.exec = function () {
        var range = this.lockRange();

        var applied = false;

        var img = RangeUtils.image(range);

        var self = this;

        function apply(e) {
            attributes = {
                src: $('#t-editor-image-url', dialog.element).val(),
                alt: $('#t-editor-image-title', dialog.element).val()
            };

            applied = insertImage(img, range);

            close(e);

            if (self.change)
                self.change();
        }

        function close(e) {
            e.preventDefault();
            dialog.destroy();

            windowFromDocument(documentFromRange(range)).focus();
            if (!applied)
                self.releaseRange(range);
        }

        var fileBrowser = this.editor.fileBrowser;
        var showBrowser = fileBrowser && fileBrowser.selectUrl !== undefined;

        function activate() {
            if (showBrowser) {
                new $t.imageBrowser($(this).find(".t-image-browser"), $.extend(fileBrowser, { apply: apply, element: self.editor.element, localization: self.editor.localization }));
            }
        }

        var dialog = $t.window.create($.extend({ width: 750 }, this.editor.dialogOptions, {
            title: "Insert image",
            html: new $.telerik.stringBuilder()
                        .cat('
')
                            .catIf('

', showBrowser)
                            .cat('
')
                                .cat('
Web address<input type="text" class="t-input" id="t-editor-image-url"/>
')
                                .cat('
Tooltip<input type="text" class="t-input" id="t-editor-image-title"/>
')
                            .cat('
')
                            .cat('
')
                                .cat('<button class="t-dialog-insert t-button">Insert</button>')
                                .cat(' or ')
                                .cat('Close')
                            .cat('
')
                        .cat('
')
                    .string(),
            onClose: close,
            onActivate: activate
        }))
        .hide()
        .find('.t-dialog-insert').click(apply).end()
        .find('.t-dialog-close').click(close).end()
        .find('.t-form-text-row input').keydown(function (e) {
            if (e.keyCode == 13)
                apply(e);
            else if (e.keyCode == 27)
                close(e);
        }).end()
        .toggleClass("t-imagebrowser", showBrowser)
        // IE < 8 returns absolute url if getAttribute is not used
        .find('#t-editor-image-url').val(img ? img.getAttribute('src', 2) : 'http://').end()
        .find('#t-editor-image-title').val(img ? img.alt : '').end()
        .show()
        .data('tWindow').center();

        $('#t-editor-image-url', dialog.element).focus().select();
    }
};


text:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <ccn/ccn.h>
#include <ccn/charbuf.h>
#include <ccn/coding.h>
#include <ccn/face_mgmt.h>
#include <ccn/sockcreate.h>
#include <ccn/strategy_mgmt.h>
#include <ccn/reg_mgmt.h>
#include <ccn/header.h>

/**
 * This is for testing.
 *
 * Reads ccnb-encoded data from stdin and
 * tries parsing with various parsers, and when successful turns
 * the result back into ccnb and tests for goodness.
 *
 */
int
main (int argc, char **argv)
{
    unsigned char buf[CCN_MAX_MESSAGE_BYTES];
    ssize_t size;
    struct ccn_face_instance *face_instance;
    struct ccn_forwarding_entry *forwarding_entry;
    struct ccn_strategy_selection *strategy_selection;
    struct ccn_header *header;
    int res = 1;
    struct ccn_charbuf *c = ccn_charbuf_create();
    int i;
    struct ccn_parsed_interest parsed_interest = {0};
    struct ccn_parsed_interest *pi = &parsed_interest;
    struct ccn_parsed_Link parsed_link = {0};
    struct ccn_parsed_Link *pl = &parsed_link;
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;

    size = read(0, buf, sizeof(buf));
    if (size < 0)
        exit(0);

    face_instance = ccn_face_instance_parse(buf, size);
    if (face_instance != NULL) {
        printf("face_instance OK\n");
        c->length = 0;
        res = ccnb_append_face_instance(c, face_instance);
        if (res != 0)
            printf("face_instance append failed\n");
        if (memcmp(buf, c->buf, c->length) != 0)
            printf("face_instance mismatch\n");
        ccn_face_instance_destroy(&face_instance);
        face_instance = ccn_face_instance_parse(c->buf, c->length);
        if (face_instance == NULL) {
            printf("face_instance reparse failed\n");
            res = 1;
        }
    }
    ccn_face_instance_destroy(&face_instance);

    forwarding_entry = ccn_forwarding_entry_parse(buf, size);
    if (forwarding_entry != NULL) {
        printf("forwarding_entry OK\n");
        c->length = 0;
        res = ccnb_append_forwarding_entry(c, forwarding_entry);
        if (res != 0)
            printf("forwarding_entry append failed\n");
        if (memcmp(buf, c->buf, c->length) != 0)
            printf("forwarding_entry mismatch\n");
        ccn_forwarding_entry_destroy(&forwarding_entry);
        forwarding_entry = ccn_forwarding_entry_parse(c->buf, c->length);
        if (forwarding_entry == NULL) {
            printf("forwarding_entry reparse failed\n");
            res = 1;
        }
    }
    ccn_forwarding_entry_destroy(&forwarding_entry);

    strategy_selection = ccn_strategy_selection_parse(buf, size);
    if (strategy_selection != NULL) {
        printf("strategy_selection OK\n");
        c->length = 0;
        res = ccnb_append_strategy_selection(c, strategy_selection);
        if (res != 0)
            printf("strategy_selection append failed\n");
        if (memcmp(buf, c->buf, c->length) != 0)
            printf("strategy_selection mismatch\n");
        ccn_strategy_selection_destroy(&strategy_selection);
        strategy_selection = ccn_strategy_selection_parse(c->buf, c->length);
        if (strategy_selection == NULL) {
            printf("strategy_selection reparse failed\n");
            res = 1;
        }
    }
    ccn_strategy_selection_destroy(&strategy_selection);

    header = ccn_header_parse(buf, size);
    if (header != NULL) {
        printf("header OK\n");
        c->length = 0;
        res = ccnb_append_header(c, header);
        if (res != 0)
            printf("header append failed\n");
        if (memcmp(buf, c->buf, c->length) != 0)
            printf("header mismatch\n");
        ccn_header_destroy(&header);
        header = ccn_header_parse(c->buf, c->length);
        if (header == NULL) {
            printf("header reparse failed\n");
            res = 1;
        }
    }
    ccn_header_destroy(&header);

    i = ccn_parse_interest(buf, size, pi, NULL);
    if (i >= 0) {
        res = 0;
        printf("interest OK lifetime %jd (%d seconds)\n",
               ccn_interest_lifetime(buf, pi),
               ccn_interest_lifetime_seconds(buf, pi));
    }

    d = ccn_buf_decoder_start(&decoder, buf, size);
    i = ccn_parse_Link(d, pl, NULL);
    if (i >= 0) {
        res = 0;
        printf("link OK\n");
    }

    d = ccn_buf_decoder_start(&decoder, buf, size);
    i = ccn_parse_Collection_start(d);
    if (i >= 0) {
        while ((i = ccn_parse_Collection_next(d, pl, NULL)) > 0) {
	  printf("collection link OK\n");
        }
        if (i == 0) {
            res = 0;
            printf("collection OK\n");
        }
    }
    if (res != 0) {
        printf("URP\n");
    }
    exit(res);
}


text:
package http_request_to_kafka_sarama_encode

import (
	"context"
	nethttp "net/http"
	"testing"

	"github.com/Shopify/sarama"

	"github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2"
	cloudevents "github.com/cloudevents/sdk-go/v2"
	"github.com/cloudevents/sdk-go/v2/binding"
	"github.com/cloudevents/sdk-go/v2/protocol/http"
)

var (
	event                 cloudevents.Event
	structuredHttpRequest *nethttp.Request
	binaryHttpRequest     *nethttp.Request

	ctx = context.TODO()
)

func init() {
	event = test.FullEvent()

	structuredHttpRequest, _ = nethttp.NewRequest("POST", "http://localhost", nil)
	Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), structuredHttpRequest)
	if Err != nil {
		panic(Err)
	}

	binaryHttpRequest, _ = nethttp.NewRequest("POST", "http://localhost", nil)
	Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), binaryHttpRequest)
	if Err != nil {
		panic(Err)
	}
}

// Avoid DCE
var M binding.Message
var Req *nethttp.Request
var Err error

func BenchmarkBaselineStructured(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Req, _ = nethttp.NewRequest("POST", "http://localhost", nil)
		Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), Req)
	}
}

func BenchmarkStructured(b *testing.B) {
	for i := 0; i < b.N; i++ {
		tempReq, _ := nethttp.NewRequest("POST", "http://localhost", nil)
		Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), tempReq)

		M = http.NewMessageFromHttpRequest(tempReq)
		Req, Err = nethttp.NewRequest("POST", "http://localhost", nil)
		producerMessage := &sarama.ProducerMessage{}
		Err = kafka_sarama.WriteProducerMessage(ctx, M, producerMessage)
	}
}

func BenchmarkBaselineBinary(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Req, _ = nethttp.NewRequest("POST", "http://localhost", nil)
		Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), Req)
	}
}

func BenchmarkBinary(b *testing.B) {
	for i := 0; i < b.N; i++ {
		tempReq, _ := nethttp.NewRequest("POST", "http://localhost", nil)
		Err = http.WriteRequest(context.TODO(), (*binding.EventMessage)(&event), tempReq)

		M = http.NewMessageFromHttpRequest(tempReq)
		Req, Err = nethttp.NewRequest("POST", "http://localhost", nil)
		producerMessage := &sarama.ProducerMessage{}
		Err = kafka_sarama.WriteProducerMessage(ctx, M, producerMessage)
	}
}


text:
import React, { Component } from 'react'
import t from 't-component'

export default class Footer extends Component {
  render () {
    let source = null
    if (this.props.source) {
      source = (

              {t('proposal-article.view-original-text')}

      )
    }
    let links = null
    if (this.props.links && this.props.links.length > 0) {
      links = (

{t('common.more-information')}

          {
            this.props.links
              .map(function (link) {
                if (!link.text) return null
                return (

                      {link.text}

                )
              })
          }

      )
    }

    return (

        {source}
        {links}

    )
  }
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
in Skottsberg, Nat. Hist. Juan Fernandez Easter Isl. , 2 (Bot. ) 3(11): 366 (1924)

#### Original name
Bacidia arceutina var. hyposcotina Zahlbr.

### Remarks
null


text:
<?php
/**
 * BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
 *
 * @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
 * @license http://framework.zend.com/license/new-bsd New BSD License
 */
namespace BjyAuthorize\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Factory responsible of building {@see \BjyAuthorize\Provider\Identity\ProviderInterface}
 *
 * @author Marco Pivetta <ocramius@gmail.com>
 */
class IdentityProviderServiceFactory implements FactoryInterface
{

    /**
     * {@inheritDoc}
     *
     * @return \BjyAuthorize\Provider\Identity\ProviderInterface
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('BjyAuthorize\Config');

        return $serviceLocator->get($config['identity_provider']);
    }
}


text:
package scodec
package codecs

/** Mixin for codecs/decoders that are known to discriminate by values of type `D`. */
trait KnownDiscriminatorType[D]:

  /** Error raised when an unknown discriminator is encountered when decoding. */
  case class UnknownDiscriminator(discriminator: D, context: List[String]) extends Err:
    def this(discriminator: D) = this(discriminator, Nil)
    def message = s"Unknown discriminator $discriminator"
    def pushContext(ctx: String) = copy(context = ctx :: context)


text:
import logging
from io import open

logger = logging.getLogger(__name__)

def get_all(isamAppliance, check_mode=False, force=False):
    """
    Retrieve a list of mapping rules
    """
    return isamAppliance.invoke_get("Retrieve a list of mapping rules",
                                    "/iam/access/v8/mapping-rules")

def get(isamAppliance, name, check_mode=False, force=False):
    """
    Retrieve a specific mapping rule
    """
    ret_obj = search(isamAppliance, name=name)
    id = ret_obj['data']

    if id == {}:
        return isamAppliance.create_return_object()
    else:
        return _get(isamAppliance, id)

def _get(isamAppliance, id):
    """
    Internal function to get data using "id" - used to avoid extra calls

    :param isamAppliance:
    :param id:
    :return:
    """
    return isamAppliance.invoke_get("Retrieve a specific mapping rule",
                                    "/iam/access/v8/mapping-rules/{0}".format(id))

def search(isamAppliance, name, check_mode=False, force=False):
    """
    Search mapping rule by name

    :param isamAppliance:
    :param name:
    :param check_mode:
    :param force:
    :return:
    """
    ret_obj = get_all(isamAppliance, check_mode, force)

    return_obj = isamAppliance.create_return_object()

    for obj in ret_obj['data']:
        if obj['name'] == name:
            return_obj['data'] = obj['id']
            logger.debug("Found id: {0}".format(obj['id']))
            return_obj['rc'] = 0

    return return_obj

def set(isamAppliance, name, category, filename=None, content=None, upload_filename=None, check_mode=False,
        force=False):
    """
    Creating or Modifying an Mapping Rule
    """

    if _check(isamAppliance, name=name) is False:
        # Force the add - we already know connection does not exist
        return add(isamAppliance, name=name, filename=filename, content=content, category=category, upload_filename=upload_filename,
                   check_mode=check_mode, force=True)
    else:
        # Update request
        return update(isamAppliance, name=name, content=content, upload_filename=upload_filename, check_mode=check_mode, force=force)

def add(isamAppliance, name, filename=None, content=None, category=None, upload_filename=None, check_mode=False, force=False):
    """
    Add a mapping rule
    """

    if force is True or _check(isamAppliance, name) is False:
        if check_mode is True:
            return isamAppliance.create_return_object(changed=True)
        else:
            if content is None or content == '':
                if upload_filename is None or upload_filename == '':
                    return isamAppliance.create_return_object(
                        warnings="Need to pass content or upload_filename for set() to work.")
                else:
                    with open(upload_filename, 'r') as contentFile:
                        content = contentFile.read()
            if filename is None or filename == '':
                if upload_filename is None or upload_filename == '':
                    return isamAppliance.create_return_object(
                        warnings="Need to pass filename or upload_filename for set() to work.")
                else:
                    filename = _extract_filename(upload_filename)
            return isamAppliance.invoke_post(
                "Add a mapping rule",
                "/iam/access/v8/mapping-rules",
                {
                    "name": name,
                    "fileName": filename,
                    "content": content,
                    "category": category
                })

    return isamAppliance.create_return_object()

def delete(isamAppliance, name, check_mode=False, force=False):
    """
    Delete a mapping rule
    """
    if force is False:
        ret_obj = search(isamAppliance, name)

    if force is True or ret_obj['data'] != {}:
        if check_mode is True:
            return isamAppliance.create_return_object(changed=True)
        else:
            return isamAppliance.invoke_delete(
                "Delete a mapping rule",
                "/iam/access/v8/mapping-rules/{0}".format(ret_obj['data']))

    return isamAppliance.create_return_object()

def update(isamAppliance, name, content=None, upload_filename=None, check_mode=False, force=False):
    """
    Update a specified mapping rule
    """

    if content is None or content == '':
        if upload_filename is None or upload_filename == '':
            return isamAppliance.create_return_object(
                warnings="Need to pass content or upload_filename for set() to work.")
        else:
            with open(upload_filename, 'r') as contentFile:
                content = contentFile.read()

    update_required = False
    ret_obj = search(isamAppliance, name)
    id = ret_obj['data']
    if force is False:
        if id:
            ret_obj_content = _get(isamAppliance, id)
            # Having to strip whitespace to get a good comparison (suspect carriage returns added after save happens)
            if (ret_obj_content['data']['content']).strip() != (content).strip():
                update_required = True

    if force is True or update_required is True:
        if check_mode is True:
            return isamAppliance.create_return_object(changed=True)
        else:
            return isamAppliance.invoke_put(
                "Update a specified mapping rule",
                "/iam/access/v8/mapping-rules/{0}".format(id),
                {
                    'content': content
                })

    return isamAppliance.create_return_object()

def export_file(isamAppliance, name, filename, check_mode=False, force=False):
    """
    Export a specific mapping rule
    """
    import os.path
    if force is False:
        ret_obj = search(isamAppliance, name)

    if force is True or (ret_obj['data'] != {} and os.path.exists(filename) is False):
        if check_mode is False:  # No point downloading a file if in check_mode
            id = ret_obj['data']
            return isamAppliance.invoke_get_file(
                "Export a specific mapping rule",
                "/iam/access/v8/mapping-rules/{0}/file/".format(id),
                filename)

    return isamAppliance.create_return_object()

def upload(isamAppliance, name, upload_filename, filename=None, category="OAUTH", check_mode=False, force=False):
    """
    Import a new mapping rule
    """
    if force is True or _check(isamAppliance, name) is False:
        if check_mode is True:
            return isamAppliance.create_return_object(changed=True)
        else:
            if filename is None:
                filename = _extract_filename(upload_filename)

            return isamAppliance.invoke_post_files(
                "Import a new mapping rule",
                "/iam/access/v8/mapping-rules",
                [
                    {
                        'file_formfield': 'file',
                        'filename': upload_filename,
                        'mimetype': 'application/octet-stream'
                    }
                ],
                {
                    "name": name,
                    "filename": filename,
                    "category": category
                })

    return isamAppliance.create_return_object()

def _extract_filename(upload_filename):
    """
    Extract filename from fully qualified path to use if no filename provided
    """
    import os.path
    return os.path.basename(upload_filename)

def import_file(isamAppliance, name, filename, check_mode=False, force=False):
    """
    Import a mapping rule (replace)
    """
    update_required = False
    if force is False:
        ret_obj = search(isamAppliance, name)
        if ret_obj['data'] != {}:
            ret_obj_content = _get(isamAppliance, ret_obj['data'])
            with open(filename, 'r') as infile:
                content = infile.read()
            if (ret_obj_content['data']['content']).strip() != content.strip():
                update_required = True

    if force is True or update_required is True:
        if check_mode is True:
            return isamAppliance.create_return_object(changed=True)
        else:
            return isamAppliance.invoke_post_files(
                "Import a mapping rule (replace)",
                "/iam/access/v8/mapping-rules/{0}/file".format(ret_obj['data']),
                [
                    {
                        'file_formfield': 'file',
                        'filename': filename,
                        'mimetype': 'application/file'
                    }
                ],
                {})

    return isamAppliance.create_return_object()

def _check(isamAppliance, name):
    """
    Check if Mapping Rules already exists
    """
    ret_obj = get_all(isamAppliance)

    for obj in ret_obj['data']:
        if obj['name'] == name:
            return True

    return False

def compare(isamAppliance1, isamAppliance2):
    """
    Compare Mapping Rules between two appliances
    """
    ret_obj1 = get_all(isamAppliance1)
    ret_obj2 = get_all(isamAppliance2)

    for obj in ret_obj1['data']:
        del obj['id']
    for obj in ret_obj2['data']:
        del obj['id']

    import ibmsecurity.utilities.tools
    return ibmsecurity.utilities.tools.json_compare(ret_obj1, ret_obj2, deleted_keys=['id'])