Пример #1
0
 private static CreateTableStatement getCreateTableStatement(String cql) {
   CreateTableStatement statement;
   try {
     statement = (CreateTableStatement) QueryProcessor.parseStatement(cql).prepare().statement;
   } catch (RequestValidationException e) {
     // Cannot proceed if an error occurs
     throw new RuntimeException("Error configuring SSTable reader. Cannot proceed", e);
   }
   return statement;
 }
Пример #2
0
 /**
  * Deserialize only Keyspace attributes without nested ColumnFamilies
  *
  * @param row Keyspace attributes in serialized form
  * @return deserialized keyspace without cf_defs
  * @throws IOException if deserialization failed
  */
 public static KSMetaData fromSchema(Row row, Iterable<CFMetaData> cfms) throws IOException {
   UntypedResultSet.Row result =
       QueryProcessor.resultify("SELECT * FROM system.schema_keyspaces", row).one();
   try {
     return new KSMetaData(
         result.getString("name"),
         AbstractReplicationStrategy.getClass(result.getString("strategy_class")),
         fromJsonMap(result.getString("strategy_options")),
         result.getBoolean("durable_writes"),
         cfms);
   } catch (ConfigurationException e) {
     throw new RuntimeException(e);
   }
 }
  public void setup() {
    Auth.setupTable(PERMISSIONS_CF, PERMISSIONS_CF_SCHEMA);

    try {
      String query =
          String.format(
              "SELECT permissions FROM %s.%s WHERE username = ? AND resource = ?",
              Auth.AUTH_KS, PERMISSIONS_CF);
      authorizeStatement =
          (SelectStatement) QueryProcessor.parseStatement(query).prepare().statement;
    } catch (RequestValidationException e) {
      throw new AssertionError(e); // not supposed to happen
    }
  }
Пример #4
0
  /**
   * Deserialize ColumnFamilies from low-level schema representation, all of them belong to the same
   * keyspace
   *
   * @param row
   * @return map containing name of the ColumnFamily and it's metadata for faster lookup
   */
  public static Map<String, CFMetaData> deserializeColumnFamilies(Row row) {
    if (row.cf == null) return Collections.emptyMap();

    Map<String, CFMetaData> cfms = new HashMap<String, CFMetaData>();
    UntypedResultSet results =
        QueryProcessor.resultify("SELECT * FROM system.schema_columnfamilies", row);
    for (UntypedResultSet.Row result : results) {
      CFMetaData cfm = CFMetaData.fromSchema(result);
      cfms.put(cfm.cfName, cfm);
    }

    for (CFMetaData cfm : cfms.values()) {
      Row columnRow = ColumnDefinition.readSchema(cfm.ksName, cfm.cfName);
      for (ColumnDefinition cd : ColumnDefinition.fromSchema(columnRow, cfm))
        cfm.column_metadata.put(cd.name, cd);
    }

    return cfms;
  }
Пример #5
0
  public Message.Response execute(QueryState state) {
    try {
      UUID tracingId = null;
      if (isTracingRequested()) {
        tracingId = UUIDGen.getTimeUUID();
        state.prepareTracingSession(tracingId);
      }

      if (state.traceNextQuery()) {
        state.createTracingSession();
        Tracing.instance().begin("Preparing CQL3 query", ImmutableMap.of("query", query));
      }

      Message.Response response = QueryProcessor.prepare(query, state.getClientState(), false);

      if (tracingId != null) response.setTracingId(tracingId);

      return response;
    } catch (Exception e) {
      return ErrorMessage.fromException(e);
    } finally {
      Tracing.instance().stopSession();
    }
  }
 private static UntypedResultSet process(String query) throws RequestExecutionException {
   return QueryProcessor.process(query, ConsistencyLevel.ONE);
 }
  public RowMutation mutationForKey(
      CFDefinition cfDef,
      ByteBuffer key,
      ColumnNameBuilder builder,
      boolean isRange,
      UpdateParameters params,
      ColumnGroupMap group)
      throws InvalidRequestException {
    QueryProcessor.validateKey(key);
    RowMutation rm = new RowMutation(cfDef.cfm.ksName, key);
    ColumnFamily cf = rm.addOrGet(columnFamily());

    if (columns.isEmpty() && builder.componentCount() == 0) {
      // No columns, delete the row
      cf.delete(new DeletionInfo(params.timestamp, params.localDeletionTime));
    } else {
      if (isRange) {
        ByteBuffer start = builder.copy().build();
        ByteBuffer end = builder.buildAsEndOfRange();
        QueryProcessor.validateColumnName(start); // If start is good, end is too
        cf.addAtom(params.makeRangeTombstone(start, end));
      } else {
        // Delete specific columns
        if (cfDef.isCompact) {
          ByteBuffer columnName = builder.build();
          QueryProcessor.validateColumnName(columnName);
          cf.addColumn(params.makeTombstone(columnName));
        } else {
          Iterator<Pair<CFDefinition.Name, Term>> iter = toRemove.iterator();
          while (iter.hasNext()) {
            Pair<CFDefinition.Name, Term> p = iter.next();
            CFDefinition.Name column = p.left;

            if (column.type.isCollection()) {
              CollectionType validator = (CollectionType) column.type;
              Term keySelected = p.right;

              if (keySelected == null) {
                // Delete the whole collection
                ByteBuffer start = builder.copy().add(column.name.key).build();
                QueryProcessor.validateColumnName(start);
                ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
                ByteBuffer end = b.add(column.name.key).buildAsEndOfRange();
                cf.addAtom(params.makeRangeTombstone(start, end));
              } else {
                builder.add(column.name.key);
                List<Term> args = Collections.singletonList(keySelected);

                Operation op;
                switch (validator.kind) {
                  case LIST:
                    op = ListOperation.DiscardKey(args);
                    break;
                  case SET:
                    op = SetOperation.Discard(args);
                    break;
                  case MAP:
                    op = MapOperation.DiscardKey(keySelected);
                    break;
                  default:
                    throw new InvalidRequestException("Unknown collection type: " + validator.kind);
                }

                op.execute(
                    cf,
                    builder,
                    validator,
                    params,
                    group == null ? null : group.getCollection(column.name.key));
              }
            } else {
              ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
              ByteBuffer columnName = b.add(column.name.key).build();
              QueryProcessor.validateColumnName(columnName);
              cf.addColumn(params.makeTombstone(columnName));
            }
          }
        }
      }
    }

    return rm;
  }