Exemplo n.º 1
0
  private static void addMutation(
      Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap,
      ByteBuffer key,
      String columnFamily,
      ByteBuffer superColumn,
      ByteBuffer column,
      ByteBuffer value) {
    Map<String, List<Mutation>> keyMutations = mutationMap.get(key);
    // If the key doesn't exist yet, create the mutation holder
    if (keyMutations == null) {
      keyMutations = new HashMap<String, List<Mutation>>();
      mutationMap.put(key, keyMutations);
    }
    // If the columnfamily doesn't exist yet, create the mutation holder
    List<Mutation> columnFamilyMutations = keyMutations.get(columnFamily);
    if (columnFamilyMutations == null) {
      columnFamilyMutations = new ArrayList<Mutation>();
      keyMutations.put(columnFamily, columnFamilyMutations);
    }

    if (value == null) { // Delete
      Deletion deletion = new Deletion(microTimestamp());
      if (superColumn != null) {
        deletion.setSuper_column(superColumn);
      }
      if (column != null) { // Single column delete		
        deletion.setPredicate(
            new SlicePredicate().setColumn_names(Collections.singletonList(column)));
      } // else Delete entire column family or supercolumn
      columnFamilyMutations.add(new Mutation().setDeletion(deletion));
    } else { // Insert/update
      ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
      if (superColumn != null) {
        List<Column> columns = new ArrayList<Column>();
        columns.add(new Column(column, value, microTimestamp()));
        cosc.setSuper_column(new SuperColumn(superColumn, columns));
      } else {
        cosc.setColumn(new Column(column, value, microTimestamp()));
      }
      columnFamilyMutations.add(new Mutation().setColumn_or_supercolumn(cosc));
    }
  }