Java Code Examples for org.apache.cassandra.transport.Frame

Following code examples demonstrate how to use org.apache.cassandra.transport.Framefrom cassandra. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.cassandra.transport.Frameand various code implementation of this class.

    
    private Collection<? extends IMutation> getMutations(QueryOptions options, boolean local, long now, Frame sourceFrame)
    throws RequestExecutionException, RequestValidationException
    {
        List<ByteBuffer> keys = buildPartitionKeyNames(options);
        Composite clusteringPrefix = createClusteringPrefix(options);

        UpdateParameters params = makeUpdateParameters(keys, clusteringPrefix, options, local, now);

        Collection<IMutation> mutations = new ArrayList<IMutation>(keys.size());
        for (ByteBuffer key: keys)
        {
            ThriftValidation.validateKey(cfm, key);
            ColumnFamily cf = ArrayBackedSortedColumns.factory.create(cfm);
            addUpdateForKey(cf, key, clusteringPrefix, params);
            Mutation mut = new Mutation(cfm.ksName, key, cf);
            mut.setSourceFrame(sourceFrame);

            mutations.add(isCounter() ? new CounterMutation(mut, options.getConsistency()) : mut);
        }
        return mutations;
    } 


         static <M extends Message.Response> Frame makeFrame(M response, Message.Codec codec, ProtocolVersion version, int streamId) {
            response.setStreamId(streamId);
            int messageSize = codec.encodedSize(response, version);
            Frame frame = Message.ProtocolEncoder.makeFrame(response, messageSize, version);
            codec.encode(response, frame.body, version);
            return frame;
         } 

    }

    public Frame getSourceFrame()
    {
        return sourceFrame;
    } 

    }

    public void setSourceFrame(Frame sourceFrame)
    {
        this.sourceFrame = sourceFrame;
    } 

    }

    private Collection<? extends IMutation> getMutations(BatchQueryOptions options, boolean local, long now, Frame sourceFrame)
    throws RequestExecutionException, RequestValidationException
    {
        Map<String, Map<ByteBuffer, IMutation>> mutations = new HashMap<>();
        for (int i = 0; i < statements.size(); i++)
        {
            ModificationStatement statement = statements.get(i);
            QueryOptions statementOptions = options.forStatement(i);
            long timestamp = attrs.getTimestamp(now, statementOptions);
            addStatementMutations(statement, statementOptions, local, timestamp, mutations, sourceFrame);
        }
        return unzipMutations(mutations);
    } 

    }

    private void addStatementMutations(ModificationStatement statement,
                                       QueryOptions options,
                                       boolean local,
                                       long now,
                                       Map<String, Map<ByteBuffer, IMutation>> mutations,
                                       Frame sourceFrame)
    throws RequestExecutionException, RequestValidationException
    {
        String ksName = statement.keyspace();
        Map<ByteBuffer, IMutation> ksMap = mutations.get(ksName);
        if (ksMap == null)
        {
            ksMap = new HashMap<>();
            mutations.put(ksName, ksMap);
        }

        List<ByteBuffer> keys = statement.buildPartitionKeyNames(options);
        Composite clusteringPrefix = statement.createClusteringPrefix(options);
        UpdateParameters params = statement.makeUpdateParameters(keys, clusteringPrefix, options, local, now);

        for (ByteBuffer key : keys)
        {
            IMutation mutation = ksMap.get(key);
            Mutation mut;
            if (mutation == null)
            {
                mut = new Mutation(ksName, key);
                mut.setSourceFrame(sourceFrame);
                mutation = statement.cfm.isCounter() ? new CounterMutation(mut, options.getConsistency()) : mut;
                ksMap.put(key, mutation);
            }
            else
            {
                mut = statement.cfm.isCounter() ? ((CounterMutation)mutation).getMutation() : (Mutation)mutation;
            }

            statement.addUpdateForKey(mut.addOrGet(statement.cfm), key, clusteringPrefix, params);
        }
    } 

      private void processPage(Frame frame) {
         if(this.aborted()) {
            frame.release();
         } else {
            this.channel.write(frame);
            this.channel.flush();
         }

      } 

   void sendPage(Frame frame, boolean hasMorePages) {
      if(this.writer.canceled) {
         logger.trace("Discarding page because writer was cancelled");
         frame.release();
      } else {
         assert !this.writer.completed() : "Received unexpected page when writer was already completed";

         try {
            this.pages.add(frame);
         } catch (Throwable var4) {
            logger.warn("Failed to add continuous paging result to queue: {}", var4.getMessage());
            frame.release();
            throw var4;
         }

         if(!hasMorePages) {
            this.writer.complete();
         } else {
            this.writer.schedule(0L);
         }

      }
   } 

        }
        catch (IOException e)
        {
            logger.info("IO error during compression of frame body chunk", e);
            throw new ProtocolException("Error compressing frame body chunk");
        } 

    }

    public Frame getSourceFrame()
    {
        return sourceFrame;
    } 

    }

    public void setSourceFrame(Frame sourceFrame)
    {
        this.sourceFrame = sourceFrame;
    } 

    public Frame compress(Frame frame) throws IOException {
        compressorIn.addAndGet(frame.body.readableBytes());
        Frame result = delegate.compress(frame);
        compressorOut.addAndGet(result.body.readableBytes());
        return result;
    } 

    public Frame decompress(Frame frame) throws IOException {
        decompressorIn.addAndGet(frame.body.readableBytes());
        Frame result = delegate.decompress(frame);
        decompressorOut.addAndGet(result.body.readableBytes());
        return result;
    } 

Advertisement
Javadoc
An on-wire frame consists of a header and a body. The header is defined the following way in native

protocol version 3 and later: 0 8 16 24 32 40 +---------+---------+---------+---------+---------+ | version | flags | stream | opcode | +---------+---------+---------+---------+---------+ | length | +---------+---------+---------+---------+

Read More
Advertisement