Skip to content

Test for single doc purging and mult docs purging.

Chang Hua Guo edited this page Jan 3, 2017 · 1 revision

#Purge doc - a single doc ##create a database and a document

curl -X PUT foo:bar@127.0.0.1:15984/testdb1
{"ok":true}
curl -X POST foo:bar@127.0.0.1:15984/testdb1 -H "Content-Type: application/json" -d @apple.json
{"ok":true,"id":"apple","rev":"1-3c37b500f170eafa50a6dc410504daa2"}
curl -X POST foo:bar@127.0.0.1:15984/testdb1 -H "Content-Type: application/json" -d @search.json
{"ok":true,"id":"_design/search","rev":"1-6d029f247fcf1c8bb79e1baf7ee6b9d4"}

##search it first

curl -X GET foo:bar@127.0.0.1:15984/testdb1/_design/search/_search/index?q="color:red"
{"total_rows":1,"bookmark":"g2wAAAABaANkAA9ub2RlMUAxMjcuMC4wLjFsAAAAAmEAbgQA_____2poAkY_8AAAAAAAAGEAag","rows":[{"id":"apple","order":[1.0,0],"fields":{}}]}

1 hit, and the doc id is apple.

##purge doc

(node1@127.0.0.1)1> DBFullName = <<"shards/00000000-ffffffff/testdb1.1483421821">>.
<<"shards/00000000-ffffffff/testdb1.1483421821">>
(node1@127.0.0.1)2> {ok, Db} = couch_db:open_int(DBFullName, []).
{ok,{db,<<"shards/00000000-ffffffff/testdb1.1483421821">>,
        "/Users/chguo/Documents/git/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/testdb1.1483421821.couch",
        {couch_bt_engine,{st,"/Users/chguo/Documents/git/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/testdb1.1483421821.couch",
                             <0.161.1>,#Ref<0.0.1.16652>,
                             [before_header,after_header,on_file_open],
                             {db_header,6,2,0,
                                        {4528,{2,0,{size_info,337,411}},171},
                                        {4699,2,175},
                                        nil,0,nil,163,1000,<<"7919c9068caa"...>>,
                                        [{'node1@127.0.0.1',...}],
                                        0,1000,...},
                             false,
                             {btree,<0.161.1>,
                                    {4528,{2,0,{size_info,337,411}},171},
                                    #Fun<couch_bt_engine.id_tree_split.1>,
                                    #Fun<couch_bt_engine.id_tree_join.2>,undefined,
                                    #Fun<couch_bt_engine.id_tree_reduce.2>,snappy},
                             {btree,<0.161.1>,
                                    {4699,2,175},
                                    #Fun<couch_bt_engine.seq_tree_split.1>,
                                    #Fun<couch_bt_engine.seq_tree_join.2>,undefined,
                                    #Fun<couch_bt_engine.seq_tree_reduce.2>,snappy},
                             {btree,<0.161.1>,nil,
                                    #Fun<couch_bt_engine.local_tree_split.1>,
                                    #Fun<couch_bt_engine.local_tree_join.2>,undefined,nil,
                                    snappy},
                             snappy,
                             {btree,<0.161.1>,nil,undefined,undefined,undefined,
                                    #Fun<couch_bt_engine.purge_tree_reduce.2>,snappy}}},
        <0.160.1>,nil,2,<<"1483421821206860">>,
        {user_ctx,null,[],undefined},
        [],undefined,nil,nil,nil,
        [{engine,<<"couch">>},
         {placement,undefined},
         {q,"1"},
         {n,"1"},
         {default_security_object,[]}],
        undefined}}
(node1@127.0.0.1)3> FDI = couch_db:get_full_doc_info(Db, <<"apple">>).
{full_doc_info,<<"apple">>,1,false,
               [{1,
                 {<<60,55,181,0,241,112,234,250,80,166,220,65,5,4,218,162>>,
                  {leaf,false,172,1,{size_info,86,123},[]},
                  []}}],
               {size_info,86,123},
               []}
(node1@127.0.0.1)4> rd(doc_info, {id = <<"">>, high_seq = 0, revs = []}).
doc_info
(node1@127.0.0.1)5> rd(rev_info, {rev, seq =0, deleted = false, body_sp = nil}).
rev_info
(node1@127.0.0.1)6> #doc_info{ revs = [#rev_info{} = PrevRev | _] } = couch_doc:to_doc_info(FDI).
#doc_info{id = <<"apple">>,high_seq = 1,
          revs = [#rev_info{rev = {1,
                                   <<60,55,181,0,241,112,234,250,80,166,220,65,5,4,218,162>>},
                            seq = 1,deleted = false,body_sp = 172}]}
(node1@127.0.0.1)7> Rev = PrevRev#rev_info.rev.
{1,
 <<60,55,181,0,241,112,234,250,80,166,220,65,5,4,218,162>>}
(node1@127.0.0.1)8> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"apple">>, [Rev]}]).
{ok,{1,
     [{ok,[{1,
            <<60,55,181,0,241,112,234,250,80,166,220,65,5,4,218,
              162>>}]}]}}               

##search it again

macgch:couchdb chguo$ curl -X GET foo:bar@127.0.0.1:15984/testdb1/_design/search/_search/index?q="color:red"
{"total_rows":0,"bookmark":"g2o","rows":[]}

0 hit, the doc id apple is purged.

#Purge doc - more docs ##create a database and a document

curl -X PUT foo:bar@127.0.0.1:15984/testdb2
{"ok":true}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @apple.json
{"ok":true,"id":"apple","rev":"1-db30a6971ffbf55b1a009c79d1643e06"}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @tomato.json
{"ok":true,"id":"tomato","rev":"1-db30a6971ffbf55b1a009c79d1643e06"}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @cherry.json
{"ok":true,"id":"cherry","rev":"1-db30a6971ffbf55b1a009c79d1643e06"}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @strawberry.json
{"ok":true,"id":"strawberry","rev":"1-db30a6971ffbf55b1a009c79d1643e06"}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @haw.json
{"ok":true,"id":"haw","rev":"1-db30a6971ffbf55b1a009c79d1643e06"}
macgch:couchdb chguo$ curl -X POST foo:bar@127.0.0.1:15984/testdb2 -H "Content-Type: application/json" -d @search.json
{"ok":true,"id":"_design/search","rev":"1-8a4ac7cb2445eb8c75271fd7bb84441c"}

##search it first

macgch:couchdb chguo$ curl -X GET foo:bar@127.0.0.1:15984/testdb2/_design/search/_search/index?q="color:red"
{"total_rows":5,"bookmark":"g2wAAAABaANkAA9ub2RlMUAxMjcuMC4wLjFsAAAAAmEAbgQA_____2poAkY_8AAAAAAAAGEEag","rows":[{"id":"apple","order":[1.0,0],"fields":{}},{"id":"tomato","order":[1.0,1],"fields":{}},{"id":"cherry","order":[1.0,2],"fields":{}},{"id":"strawberry","order":[1.0,3],"fields":{}},{"id":"haw","order":[1.0,4],"fields":{}}]}

5 hits, and the doc ids are apple, tomato, cherry, strawberry, haw.

##purge docs

(node1@127.0.0.1)1> DBFullName = <<"shards/00000000-ffffffff/testdb2.1483425723">>.
<<"shards/00000000-ffffffff/testdb2.1483425723">>
(node1@127.0.0.1)2> {ok, Db} = couch_db:open_int(DBFullName, []).
{ok,{db,<<"shards/00000000-ffffffff/testdb2.1483425723">>,
        "/Users/chguo/Documents/git/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/testdb2.1483425723.couch",
        {couch_bt_engine,{st,"/Users/chguo/Documents/git/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/testdb2.1483425723.couch",
                             <0.1535.3>,#Ref<0.0.1.247848>,
                             [before_header,after_header,on_file_open],
                             {db_header,6,6,0,
                                        {20915,{6,0,{size_info,663,885}},268},
                                        {21183,6,265},
                                        nil,0,nil,163,1000,<<"c3e19b4c8d1e"...>>,
                                        [{'node1@127.0.0.1',...}],
                                        0,1000,...},
                             false,
                             {btree,<0.1535.3>,
                                    {20915,{6,0,{size_info,663,885}},268},
                                    #Fun<couch_bt_engine.id_tree_split.1>,
                                    #Fun<couch_bt_engine.id_tree_join.2>,undefined,
                                    #Fun<couch_bt_engine.id_tree_reduce.2>,snappy},
                             {btree,<0.1535.3>,
                                    {21183,6,265},
                                    #Fun<couch_bt_engine.seq_tree_split.1>,
                                    #Fun<couch_bt_engine.seq_tree_join.2>,undefined,
                                    #Fun<couch_bt_engine.seq_tree_reduce.2>,snappy},
                             {btree,<0.1535.3>,nil,
                                    #Fun<couch_bt_engine.local_tree_split.1>,
                                    #Fun<couch_bt_engine.local_tree_join.2>,undefined,nil,
                                    snappy},
                             snappy,
                             {btree,<0.1535.3>,nil,undefined,undefined,undefined,
                                    #Fun<couch_bt_engine.purge_tree_reduce.2>,snappy}}},
        <0.1534.3>,nil,6,<<"1483425723620236">>,
        {user_ctx,null,[],undefined},
        [],undefined,nil,nil,nil,
        [{engine,<<"couch">>},
         {placement,undefined},
         {q,"1"},
         {n,"1"},
         {default_security_object,[]}],
        undefined}}
(node1@127.0.0.1)4> rd(doc_info, {id = <<"">>, high_seq = 0, revs = []}).
doc_info
(node1@127.0.0.1)5> rd(rev_info, {rev, seq =0, deleted = false, body_sp = nil}).
rev_info
(node1@127.0.0.1)6> FDI1 = couch_db:get_full_doc_info(Db, <<"apple">>).
{full_doc_info,<<"apple">>,1,false,
               [{1,
                 {<<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>,
                  {leaf,false,172,1,{size_info,83,120},[]},
                  []}}],
               {size_info,83,120},
               []}
(node1@127.0.0.1)7> FDI2 = couch_db:get_full_doc_info(Db, <<"tomato">>).
{full_doc_info,<<"tomato">>,2,false,
               [{1,
                 {<<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>,
                  {leaf,false,4274,2,{size_info,83,120},[]},
                  []}}],
               {size_info,83,120},
               []}
(node1@127.0.0.1)8> FDI3 = couch_db:get_full_doc_info(Db, <<"cherry">>).
{full_doc_info,<<"cherry">>,3,false,
               [{1,
                 {<<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>,
                  {leaf,false,8373,3,{size_info,83,120},[]},
                  []}}],
               {size_info,83,120},
               []}
(node1@127.0.0.1)9> FDI4 = couch_db:get_full_doc_info(Db, <<"strawberry">>).
{full_doc_info,<<"strawberry">>,4,false,
               [{1,
                 {<<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>,
                  {leaf,false,12472,4,{size_info,83,120},[]},
                  []}}],
               {size_info,83,120},
               []}
(node1@127.0.0.1)10> FDI5 = couch_db:get_full_doc_info(Db, <<"haw">>).
{full_doc_info,<<"haw">>,5,false,
               [{1,
                 {<<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>,
                  {leaf,false,16571,5,{size_info,83,120},[]},
                  []}}],
               {size_info,83,120},
               []}
(node1@127.0.0.1)11> #doc_info{ revs = [#rev_info{rev = Rev1} | _] } = couch_doc:to_doc_info(FDI1).
#doc_info{id = <<"apple">>,high_seq = 1,
          revs = [#rev_info{rev = {1,
                                   <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>},
                            seq = 1,deleted = false,body_sp = 172}]}
(node1@127.0.0.1)12> #doc_info{ revs = [#rev_info{rev = Rev2} | _] } = couch_doc:to_doc_info(FDI2).
#doc_info{id = <<"tomato">>,high_seq = 2,
          revs = [#rev_info{rev = {1,
                                   <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>},
                            seq = 2,deleted = false,body_sp = 4274}]}
(node1@127.0.0.1)13> #doc_info{ revs = [#rev_info{rev = Rev3} | _] } = couch_doc:to_doc_info(FDI3).
#doc_info{id = <<"cherry">>,high_seq = 3,
          revs = [#rev_info{rev = {1,
                                   <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>},
                            seq = 3,deleted = false,body_sp = 8373}]}
(node1@127.0.0.1)14> #doc_info{ revs = [#rev_info{rev = Rev4} | _] } = couch_doc:to_doc_info(FDI4).
#doc_info{id = <<"strawberry">>,high_seq = 4,
          revs = [#rev_info{rev = {1,
                                   <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>},
                            seq = 4,deleted = false,body_sp = 12472}]}
(node1@127.0.0.1)15> #doc_info{ revs = [#rev_info{rev = Rev5} | _] } = couch_doc:to_doc_info(FDI5).
#doc_info{id = <<"haw">>,high_seq = 5,
          revs = [#rev_info{rev = {1,
                                   <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,62,6>>},
                            seq = 5,deleted = false,body_sp = 16571}]}
(node1@127.0.0.1)16> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"apple">>, [Rev1]}]).
{ok,{1,
     [{ok,[{1,
            <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,
              62,6>>}]}]}}
(node1@127.0.0.1)17> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"tomato">>, [Rev2]}]).
{ok,{2,
     [{ok,[{1,
            <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,
              62,6>>}]}]}}
(node1@127.0.0.1)18> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"cherry">>, [Rev3]}]).
{ok,{3,
     [{ok,[{1,
            <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,
              62,6>>}]}]}}
(node1@127.0.0.1)19> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"strawberry">>, [Rev4]}]).
{ok,{4,
     [{ok,[{1,
            <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,
              62,6>>}]}]}}
(node1@127.0.0.1)20> {ok, {_, [{ok, _}]}} = couch_db:purge_docs(Db, [{<<"haw">>, [Rev5]}]).
{ok,{5,
     [{ok,[{1,
            <<219,48,166,151,31,251,245,91,26,0,156,121,209,100,
              62,6>>}]}]}}

##search it again.

macgch:couchdb chguo$ curl -X GET foo:bar@127.0.0.1:15984/testdb2/_design/search/_search/index?q="color:red"
{"total_rows":0,"bookmark":"g2o","rows":[]}

0 hit.

#files apple.json

{
    "_id":"apple",
    "color":"red",
    "size":"1"
}

tomato.json

{
    "_id":"tomato",
    "color":"red",
    "size":"1"
}

cherry.json

{
    "_id":"cherry",
    "color":"red",
    "size":"1"
}

strawberry.json

{
    "_id":"strawberry",
    "color":"red",
    "size":"1"
}

haw.json

{
    "_id":"haw",
    "color":"red",
    "size":"1"
}

search.json

{
    "_id":"_design/search",
    "views":{},
    "language":"javascript",
    "indexes":{
        "index":{
            "analyzer":"standard",
            "index":"function (doc) {\n  index(\"default\", doc._id);\n  if(doc.color) {\n    index(\"color\", doc.color);\n  }\n  if(doc.size) {\n    index(\"size\", doc.size);\n  }\n}\n"
        }
    }
}

Clone this wiki locally