summaryrefslogtreecommitdiff
path: root/test/elixir/test/partition_helpers.exs
blob: 4920c11980dae52f33a4b44eec7b7ddbb2dfacd6 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
defmodule PartitionHelpers do
  use ExUnit.Case
  use CouchTestCase

  def create_partition_docs(db_name, pk1 \\ "foo", pk2 \\ "bar") do
    docs =
      for i <- 1..100 do
        id =
          if rem(i, 2) == 0 do
            "#{pk1}:#{i}"
          else
            "#{pk2}:#{i}"
          end

        group =
          if rem(i, 3) == 0 do
            "one"
          else
            "two"
          end

        %{
          :_id => id,
          :value => i,
          :some => "field",
          :group => group
        }
      end

    resp = Couch.post("/#{db_name}/_bulk_docs", body: %{:w => 3, :docs => docs})
    assert resp.status_code == 201
  end

  def create_partition_ddoc(db_name, opts \\ %{}) do
    map_fn = """
      function(doc) {
        if (doc.some) {
          emit(doc.value, doc.some);
        }
      }
    """

    default_ddoc = %{
      views: %{
        some: %{
          map: map_fn
        }
      }
    }

    ddoc = Enum.into(opts, default_ddoc)

    resp = Couch.put("/#{db_name}/_design/mrtest", body: ddoc)
    assert resp.status_code == 201
    assert Map.has_key?(resp.body, "ok") == true
  end

  def crud_partition_doc(db_name, pk \\ "foo") do
    resp = Couch.post("/#{db_name}", body: %{:_id => "#{pk}:id1", :a => 1, :b => 1}).body
    assert resp["ok"]

    url = "/#{db_name}/_partition/#{pk}"
    resp = Couch.get(url)

    assert resp.status_code == 200
    %{:body => body} = resp
    assert body["doc_count"] == 1
    assert body["doc_del_count"] == 0
    assert body["partition"] == "#{pk}"
    exernal_size1 = body["sizes"]["external"]
    assert exernal_size1 > 0

    resp = Couch.post("/#{db_name}", body: %{:_id => "#{pk}:id2", :a => 2, :b => 22}).body
    rev = resp["rev"]
    assert resp["ok"]

    url = "/#{db_name}/_partition/#{pk}"
    resp = Couch.get(url)

    assert resp.status_code == 200
    %{:body => body} = resp
    assert body["doc_count"] == 2
    assert body["doc_del_count"] == 0
    assert body["partition"] == "#{pk}"
    exernal_size2 = body["sizes"]["external"]
    assert exernal_size2 > exernal_size1

    resp = Couch.delete("/#{db_name}/#{pk}:id2?rev=#{rev}").body
    assert resp["ok"]
    :timer.sleep(300)

    url = "/#{db_name}/_partition/#{pk}"
    resp = Couch.get(url)

    assert resp.status_code == 200
    %{:body => body} = resp
    assert body["doc_count"] == 1
    assert body["doc_del_count"] == 1
    assert body["partition"] == "#{pk}"
    exernal_size3 = body["sizes"]["external"]
    assert exernal_size3 <= exernal_size2

    compact(db_name)

    url = "/#{db_name}/_partition/#{pk}"
    resp = Couch.get(url)
    %{:body => body} = resp
    exernal_size4 = body["sizes"]["external"]
    assert exernal_size4 < exernal_size3

    map = ~s"""
    function (doc) {
      emit(doc.integer, doc.integer);
      emit(doc.integer, doc.integer);
    };
    """
    red_doc = %{:views => %{:bar => %{:map => map}}}

    assert Couch.put("/#{db_name}/_design/#{pk}_foo", body: red_doc).body["ok"]

    url = "/#{db_name}/_partition/#{pk}"
    resp = Couch.get(url)

    assert resp.status_code == 200
    %{:body => body} = resp
    assert body["doc_count"] == 1
    assert body["doc_del_count"] == 1
    assert body["partition"] == "#{pk}"
    exernal_size5 = body["sizes"]["external"]
    assert exernal_size5 == exernal_size4
  end

  def get_ids(resp) do
    %{:body => %{"rows" => rows}} = resp
    Enum.map(rows, fn row -> row["id"] end)
  end

  def get_partitions(resp) do
    %{:body => %{"rows" => rows}} = resp

    Enum.map(rows, fn row ->
      [partition, _] = String.split(row["id"], ":")
      partition
    end)
  end

  def assert_correct_partition(partitions, correct_partition) do
    assert Enum.all?(partitions, fn partition ->
             partition == correct_partition
           end)
  end

  def compact(db) do
    assert Couch.post("/#{db}/_compact").status_code == 202

    retry_until(
      fn ->
        Couch.get("/#{db}").body["compact_running"] == false
      end,
      200,
      20_000
    )
  end
end