File: base.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (50 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download
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
# frozen_string_literal: true

require_relative '../base'

module Mongo
  module DriverBench
    module Parallel
      module LDJSON
        # The abstract base class for parallel LDSON benchmarks.
        #
        # @api private
        class Base < Mongo::DriverBench::Parallel::Base
          def file_name_at(index)
            format('parallel/ldjson_multi/ldjson%03d.txt', index)
          end

          private

          attr_reader :collection

          def insert_docs_from_file(file_name, ids_relative_to: nil)
            next_id = ids_relative_to
            docs = File.readlines(path_to_file(file_name)).map do |line|
              JSON.parse(line).tap do |doc|
                if ids_relative_to
                  doc['_id'] = next_id
                  next_id += 1
                end
              end
            end

            collection.insert_many(docs)
          end

          def setup
            super
            @dataset_size = 100.times.sum { |i| File.size(path_to_file(file_name_at(i))) }
          end

          def prepare_collection
            @collection = @client.database[:corpus].tap do |corpus|
              corpus.drop
              corpus.create
            end
          end
        end
      end
    end
  end
end