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

package com.ning.metrics.action.hdfs.data.parser;

import com.ning.metrics.action.hdfs.data.Row;
import com.ning.metrics.action.hdfs.data.RowAccessException;
import com.ning.metrics.action.hdfs.data.RowFactory;
import com.ning.metrics.action.hdfs.data.RowThrift;
import com.ning.metrics.action.hdfs.data.schema.ColumnKey;
import com.ning.metrics.action.hdfs.data.schema.DynamicColumnKey;
import com.ning.metrics.action.hdfs.data.schema.RowSchema;
import com.ning.metrics.action.schema.Registrar;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WritableRowSerializer implements RowSerializer
{
    @Override
    public boolean accept(Object o)
    {
        return (o instanceof Writable);
    }

    @Override
    public Row toRow(Registrar r, Object value) throws RowAccessException
    {
        Row row;

        if (value instanceof Text) {
            String[] data = value.toString().split("\t");
            List<ColumnKey> columnKeyList = new ArrayList<ColumnKey>();

            for (int i = 0; i < data.length; i++) {
                columnKeyList.add(new DynamicColumnKey(String.valueOf("col-" + i)));
            }

            row = RowFactory.getRow(new RowSchema("Text", columnKeyList), Arrays.asList(data));
        }
        else if (value instanceof RowThrift) {
            row = (RowThrift) value;
        }
        else {
            throw new RowAccessException(String.format("Writable [%s] is not a known row type", value == null ? null : value.getClass()));
        }

        return row;
    }
}

@@ -1108,16 +1108,59 @@
 istrar;%0A
+import org.apache.hadoop.io.BytesWritable;%0A
 import o
@@ -1224,16 +1224,49 @@
 table;%0A%0A
+import java.nio.charset.Charset;%0A
 import j
@@ -2029,32 +2029,419 @@
 ta));%0A        %7D%0A
+        else if (value instanceof BytesWritable) %7B%0A            byte%5B%5D data = ((BytesWritable) value).getBytes();%0A%0A            ArrayList%3CString%3E listData = new ArrayList%3CString%3E();%0A            listData.add(new String(data, Charset.forName(%22UTF-8%22)));%0A%0A            row = RowFactory.getRow(new RowSchema(%22BytesWritable%22, new DynamicColumnKey(String.valueOf(%22col-1%22))), listData);%0A        %7D%0A
         else if 

support BytesWritable