File: kill_cursors.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (137 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (C) 2014-2017 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  module Protocol

    # MongoDB Wire protocol KillCursors message.
    #
    # This is a client request message that is sent to the server in order
    # to kill a number of cursors.
    #
    # @api semipublic
    class KillCursors < Message

      # Creates a new KillCursors message
      #
      # @example Kill the cursor on the server with id 1.
      #   KillCursors.new([1])
      #
      # @param [ Mongo::Database ] collection The collection.
      # @param [ Mongo::Database ] database The database.
      # @param [ Array<Fixnum> ] cursor_ids The cursor ids to kill.
      def initialize(collection, database, cursor_ids)
        @database = database
        @cursor_ids = cursor_ids
        @id_count   = @cursor_ids.size
        @upconverter = Upconverter.new(collection, cursor_ids)
        super
      end

      # Return the event payload for monitoring.
      #
      # @example Return the event payload.
      #   message.payload
      #
      # @return [ BSON::Document ] The event payload.
      #
      # @since 2.1.0
      def payload
        BSON::Document.new(
          command_name: 'killCursors',
          database_name: @database,
          command: upconverter.command,
          request_id: request_id
        )
      end

      protected

      attr_reader :upconverter

      private

      # The operation code required to specify +KillCursors+ message.
      # @return [Fixnum] the operation code.
      #
      # @since 2.5.0
      OP_CODE = 2007

      # Field representing Zero encoded as an Int32.
      field :zero, Zero

      # @!attribute
      # @return [Fixnum] Count of the number of cursor ids.
      field :id_count, Int32

      # @!attribute
      # @return [Array<Fixnum>] Cursors to kill.
      field :cursor_ids, Int64, true

      # Converts legacy insert messages to the appropriare OP_COMMAND style
      # message.
      #
      # @since 2.1.0
      class Upconverter

        # The kill cursors constant.
        #
        # @since 2.2.0
        KILL_CURSORS = 'killCursors'.freeze

        # The cursors constant.
        #
        # @since 2.2.0
        CURSORS = 'cursors'.freeze

        # @return [ String ] collection The name of the collection.
        attr_reader :collection

        # @return [ Array<Integer> ] cursor_ids The cursor ids.
        attr_reader :cursor_ids

        # Instantiate the upconverter.
        #
        # @example Instantiate the upconverter.
        #   Upconverter.new('users', [ 1, 2, 3 ])
        #
        # @param [ String ] collection The name of the collection.
        # @param [ Array<Integer> ] cursor_ids The cursor ids.
        #
        # @since 2.1.0
        def initialize(collection, cursor_ids)
          @collection = collection
          @cursor_ids = cursor_ids
        end

        # Get the upconverted command.
        #
        # @example Get the command.
        #   upconverter.command
        #
        # @return [ BSON::Document ] The upconverted command.
        #
        # @since 2.1.0
        def command
          document = BSON::Document.new
          document.store(KILL_CURSORS, collection)
          document.store(CURSORS, cursor_ids)
          document
        end
      end

      Registry.register(OP_CODE, self)
    end
  end
end