Search in sources :

Example 1 with Column

use of org.apache.cassandra.db.Column in project eiger by wlloyd.

the class MigrationManager method makeMigrationMessage.

// other half of transformation is in DefinitionsUpdateResponseVerbHandler.
private static Message makeMigrationMessage(Collection<IColumn> migrations, int version) throws IOException {
    FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    dout.writeInt(migrations.size());
    // problem during upgrades.
    for (IColumn col : migrations) {
        assert col instanceof Column;
        ByteBufferUtil.writeWithLength(col.name(), dout);
        ByteBufferUtil.writeWithLength(col.value(), dout);
    }
    dout.close();
    byte[] body = bout.toByteArray();
    return new Message(FBUtilities.getBroadcastAddress(), StorageService.Verb.DEFINITIONS_UPDATE, body, version);
}
Also used : FastByteArrayOutputStream(org.apache.cassandra.io.util.FastByteArrayOutputStream) Message(org.apache.cassandra.net.Message) IColumn(org.apache.cassandra.db.IColumn) Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn)

Example 2 with Column

use of org.apache.cassandra.db.Column in project eiger by wlloyd.

the class CassandraStorage method putNext.

public void putNext(Tuple t) throws ExecException, IOException {
    ByteBuffer key = objToBB(t.get(0));
    DefaultDataBag pairs = (DefaultDataBag) t.get(1);
    ArrayList<Mutation> mutationList = new ArrayList<Mutation>();
    CfDef cfDef = getCfDef(storeSignature);
    try {
        for (Tuple pair : pairs) {
            Mutation mutation = new Mutation();
            if (// supercolumn
            DataType.findType(pair.get(1)) == DataType.BAG) {
                org.apache.cassandra.thrift.SuperColumn sc = new org.apache.cassandra.thrift.SuperColumn();
                sc.name = objToBB(pair.get(0));
                ArrayList<org.apache.cassandra.thrift.Column> columns = new ArrayList<org.apache.cassandra.thrift.Column>();
                for (Tuple subcol : (DefaultDataBag) pair.get(1)) {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = objToBB(subcol.get(0));
                    column.value = objToBB(subcol.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    columns.add(column);
                }
                if (// a deletion
                columns.isEmpty()) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.super_column = objToBB(pair.get(0));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    sc.columns = columns;
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.super_column = sc;
                }
            } else // assume column since it couldn't be anything else
            {
                if (pair.get(1) == null) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.predicate = new org.apache.cassandra.thrift.SlicePredicate();
                    mutation.deletion.predicate.column_names = Arrays.asList(objToBB(pair.get(0)));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = objToBB(pair.get(0));
                    column.value = objToBB(pair.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.column = column;
                }
            }
            mutationList.add(mutation);
        }
    } catch (ClassCastException e) {
        throw new IOException(e + " Output must be (key, {(column,value)...}) for ColumnFamily or (key, {supercolumn:{(column,value)...}...}) for SuperColumnFamily", e);
    }
    try {
        writer.write(key, mutationList);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) org.apache.cassandra.thrift(org.apache.cassandra.thrift) Deletion(org.apache.cassandra.thrift.Deletion) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) Mutation(org.apache.cassandra.thrift.Mutation)

Example 3 with Column

use of org.apache.cassandra.db.Column in project eiger by wlloyd.

the class MigrationManager method makeColumns.

// other half of this transformation is in MigrationManager.
public static Collection<Column> makeColumns(Message msg) throws IOException {
    Collection<Column> cols = new ArrayList<Column>();
    DataInputStream in = new DataInputStream(new FastByteArrayInputStream(msg.getMessageBody()));
    int count = in.readInt();
    for (int i = 0; i < count; i++) {
        byte[] name = new byte[in.readInt()];
        in.readFully(name);
        byte[] value = new byte[in.readInt()];
        in.readFully(value);
        cols.add(new Column(ByteBuffer.wrap(name), ByteBuffer.wrap(value)));
    }
    in.close();
    return cols;
}
Also used : FastByteArrayInputStream(org.apache.cassandra.io.util.FastByteArrayInputStream) Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn)

Example 4 with Column

use of org.apache.cassandra.db.Column in project eiger by wlloyd.

the class SSTableExportTest method testEscapingDoubleQuotes.

@Test
public void testEscapingDoubleQuotes() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "ValuesWithQuotes");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "ValuesWithQuotes");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(null, new Column(ByteBufferUtil.bytes("data"), UTF8Type.instance.fromString("{\"foo\":\"bar\"}")));
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("ValuesWithQuotes", ".json");
    SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]);
    JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson));
    JSONArray rowA = (JSONArray) json.get(asHex("rowA"));
    JSONArray data = (JSONArray) rowA.get(0);
    assert hexToBytes((String) data.get(0)).equals(ByteBufferUtil.bytes("data"));
    assert data.get(1).equals("{\"foo\":\"bar\"}");
}
Also used : PrintStream(java.io.PrintStream) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) JSONObject(org.json.simple.JSONObject) Column(org.apache.cassandra.db.Column) ExpiringColumn(org.apache.cassandra.db.ExpiringColumn) CounterColumn(org.apache.cassandra.db.CounterColumn) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) JSONArray(org.json.simple.JSONArray) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 5 with Column

use of org.apache.cassandra.db.Column in project brisk by riptano.

the class CassandraStorage method putNext.

public void putNext(Tuple t) throws ExecException, IOException {
    ByteBuffer key = objToBB(t.get(0));
    DefaultDataBag pairs = (DefaultDataBag) t.get(1);
    ArrayList<Mutation> mutationList = new ArrayList<Mutation>();
    CfDef cfDef = getCfDef();
    List<AbstractType> marshallers = getDefaultMarshallers(cfDef);
    Map<ByteBuffer, AbstractType> validators = getValidatorMap(cfDef);
    try {
        for (Tuple pair : pairs) {
            Mutation mutation = new Mutation();
            if (// supercolumn
            DataType.findType(pair.get(1)) == DataType.BAG) {
                org.apache.cassandra.thrift.SuperColumn sc = new org.apache.cassandra.thrift.SuperColumn();
                sc.name = objToBB(pair.get(0));
                ArrayList<org.apache.cassandra.thrift.Column> columns = new ArrayList<org.apache.cassandra.thrift.Column>();
                for (Tuple subcol : (DefaultDataBag) pair.get(1)) {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = objToBB(subcol.get(0));
                    column.value = objToBB(subcol.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    columns.add(column);
                }
                if (// a deletion
                columns.isEmpty()) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.super_column = objToBB(pair.get(0));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    sc.columns = columns;
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.super_column = sc;
                }
            } else // assume column since it couldn't be anything else
            {
                if (pair.get(1) == null) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.predicate = new org.apache.cassandra.thrift.SlicePredicate();
                    mutation.deletion.predicate.column_names = Arrays.asList(objToBB(pair.get(0)));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = marshallers.get(0).decompose((pair.get(0)));
                    if (validators.get(column.name) == null)
                        // Have to special case BytesType to convert DataByteArray into ByteBuffer
                        if (marshallers.get(1) instanceof BytesType)
                            column.value = objToBB(pair.get(1));
                        else
                            column.value = marshallers.get(1).decompose(pair.get(1));
                    else
                        column.value = validators.get(column.name).decompose(pair.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.column = column;
                }
            }
            mutationList.add(mutation);
        }
    } catch (ClassCastException e) {
        throw new IOException(e + " Output must be (key, {(column,value)...}) for ColumnFamily or (key, {supercolumn:{(column,value)...}...}) for SuperColumnFamily");
    }
    try {
        writer.write(key, mutationList);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SuperColumn(org.apache.cassandra.db.SuperColumn) org.apache.cassandra.thrift(org.apache.cassandra.thrift) Deletion(org.apache.cassandra.thrift.Deletion) BytesType(org.apache.cassandra.db.marshal.BytesType) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SuperColumn(org.apache.cassandra.db.SuperColumn) Mutation(org.apache.cassandra.thrift.Mutation)

Aggregations

Column (org.apache.cassandra.db.Column)7 IColumn (org.apache.cassandra.db.IColumn)6 ByteBuffer (java.nio.ByteBuffer)4 ColumnOrSuperColumn (org.apache.cassandra.thrift.ColumnOrSuperColumn)4 IOException (java.io.IOException)2 SuperColumn (org.apache.cassandra.db.SuperColumn)2 AbstractType (org.apache.cassandra.db.marshal.AbstractType)2 BytesType (org.apache.cassandra.db.marshal.BytesType)2 org.apache.cassandra.thrift (org.apache.cassandra.thrift)2 Deletion (org.apache.cassandra.thrift.Deletion)2 Mutation (org.apache.cassandra.thrift.Mutation)2 File (java.io.File)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 ColumnFamily (org.apache.cassandra.db.ColumnFamily)1 CounterColumn (org.apache.cassandra.db.CounterColumn)1 ExpiringColumn (org.apache.cassandra.db.ExpiringColumn)1 SSTableReader (org.apache.cassandra.io.sstable.SSTableReader)1 SSTableUtils.tempSSTableFile (org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile)1 SSTableWriter (org.apache.cassandra.io.sstable.SSTableWriter)1