Exemplo n.º 1
0
  /**
   * Insert a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key.
   *
   * @param table The name of the table
   * @param key The record key of the record to insert.
   * @param values A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error
   */
  public int insert(String table, String key, HashMap<String, ByteIterator> values) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      if (_debug) {
        System.out.println("Inserting key: " + key);
      }

      try {
        ByteBuffer wrappedKey = ByteBuffer.wrap(key.getBytes("UTF-8"));

        ColumnOrSuperColumn column;
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
          column = new ColumnOrSuperColumn();
          Column subColumn = new Column(ByteBuffer.wrap(entry.getKey().getBytes("UTF-8")));
          subColumn.setValue(ByteBuffer.wrap(entry.getValue().toArray()));
          subColumn.setTimestamp(System.currentTimeMillis());
          column.setColumn(subColumn);

          mutations.add(new Mutation().setColumn_or_supercolumn(column));
        }

        mutationMap.put(column_family, mutations);
        record.put(wrappedKey, mutationMap);

        client.batch_mutate(record, ConsistencyLevel.ONE);

        mutations.clear();
        mutationMap.clear();
        record.clear();

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }

    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
  /** test get_count() to work correctly with 'count' settings around page size. (CASSANDRA-4833) */
  @Test
  public void test_get_count() throws Exception {
    Schema.instance.clear(); // Schema are now written on disk and will be reloaded
    new EmbeddedCassandraService().start();
    ThriftSessionManager.instance.setCurrentSocket(new InetSocketAddress(9160));

    DecoratedKey key = Util.dk("testkey");
    for (int i = 0; i < 3050; i++) {
      RowMutation rm = new RowMutation("Keyspace1", key.key);
      rm.add(
          new QueryPath("Standard1", null, ByteBufferUtil.bytes(String.valueOf(i))),
          ByteBufferUtil.EMPTY_BYTE_BUFFER,
          System.currentTimeMillis());
      rm.apply();
    }

    CassandraServer server = new CassandraServer();
    server.set_keyspace("Keyspace1");

    // same as page size
    int count =
        server.get_count(
            key.key, new ColumnParent("Standard1"), predicateWithCount(1024), ConsistencyLevel.ONE);
    assert count == 1024 : "expected 1024 but was " + count;

    // 1 above page size
    count =
        server.get_count(
            key.key, new ColumnParent("Standard1"), predicateWithCount(1025), ConsistencyLevel.ONE);
    assert count == 1025 : "expected 1025 but was " + count;

    // above number of columns
    count =
        server.get_count(
            key.key, new ColumnParent("Standard1"), predicateWithCount(4000), ConsistencyLevel.ONE);
    assert count == 3050 : "expected 3050 but was " + count;

    // same as number of columns
    count =
        server.get_count(
            key.key, new ColumnParent("Standard1"), predicateWithCount(3050), ConsistencyLevel.ONE);
    assert count == 3050 : "expected 3050 but was " + count;

    // 1 above number of columns
    count =
        server.get_count(
            key.key, new ColumnParent("Standard1"), predicateWithCount(3051), ConsistencyLevel.ONE);
    assert count == 3050 : "expected 3050 but was " + count;
  }
    public boolean printProgress() {
      boolean done = true;
      StringBuilder sb = new StringBuilder();
      sb.append("\rprogress: ");
      long totalProgress = 0;
      long totalSize = 0;
      for (Map.Entry<InetAddress, Collection<PendingFile>> entry : filesByHost.entrySet()) {
        long progress = 0;
        long size = 0;
        int completed = 0;
        Collection<PendingFile> pendings = entry.getValue();
        for (PendingFile f : pendings) {
          progress += f.progress;
          size += f.size;
          if (f.progress == f.size) completed++;
        }
        totalProgress += progress;
        totalSize += size;
        if (completed != pendings.size()) done = false;
        sb.append("[").append(entry.getKey());
        sb.append(" ").append(completed).append("/").append(pendings.size());
        sb.append(" (").append(size == 0 ? 100L : progress * 100L / size).append(")] ");
      }
      long time = System.currentTimeMillis();
      long deltaTime = time - lastTime;
      lastTime = time;
      long deltaProgress = totalProgress - lastProgress;
      lastProgress = totalProgress;

      sb.append("[total: ")
          .append(totalSize == 0 ? 100L : totalProgress * 100L / totalSize)
          .append(" - ");
      sb.append(mbPerSec(deltaProgress, deltaTime)).append("MB/s");
      sb.append(" (avg: ").append(mbPerSec(totalProgress, time - startTime)).append("MB/s)]");
      ;
      System.out.print(sb.toString());
      return done;
    }
Exemplo n.º 4
0
  /**
   * Delete a record from the database.
   *
   * @param table The name of the table
   * @param key The record key of the record to delete.
   * @return Zero on success, a non-zero error code on error
   */
  public int delete(String table, String key) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      try {
        client.remove(
            ByteBuffer.wrap(key.getBytes("UTF-8")),
            new ColumnPath(column_family),
            System.currentTimeMillis(),
            ConsistencyLevel.ONE);

        if (_debug) {
          System.out.println("Delete key: " + key);
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
 public void start() {
   startTime = System.currentTimeMillis();
 }