couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r677353 - in /incubator/couchdb/branches/runtimeconfig: ./ share/www/script/couch.js share/www/script/couch_tests.js src/couchdb/couch_db.erl src/couchdb/couch_file.erl src/couchdb/couch_httpd.erl
Date Wed, 16 Jul 2008 17:23:36 GMT
Author: jan
Date: Wed Jul 16 10:23:36 2008
New Revision: 677353

URL: http://svn.apache.org/viewvc?rev=677353&view=rev
Log:
Merged revisions 674334,675699,675780,677087 via svnmerge from 
https://svn.apache.org/repos/asf/incubator/couchdb/trunk


Modified:
    incubator/couchdb/branches/runtimeconfig/   (props changed)
    incubator/couchdb/branches/runtimeconfig/share/www/script/couch.js
    incubator/couchdb/branches/runtimeconfig/share/www/script/couch_tests.js
    incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_db.erl
    incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_file.erl
    incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_httpd.erl

Propchange: incubator/couchdb/branches/runtimeconfig/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Wed Jul 16 10:23:36 2008
@@ -1 +1 @@
-/incubator/couchdb/trunk:1-674103
+/incubator/couchdb/trunk:1-677342

Modified: incubator/couchdb/branches/runtimeconfig/share/www/script/couch.js
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/share/www/script/couch.js?rev=677353&r1=677352&r2=677353&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/share/www/script/couch.js [utf-8] (original)
+++ incubator/couchdb/branches/runtimeconfig/share/www/script/couch.js [utf-8] Wed Jul 16
10:23:36 2008
@@ -80,6 +80,16 @@
     return result;
   }
 
+  // Deletes an attachment from a document
+  this.deleteDocAttachment = function(doc, attachment_name) {
+    var req = request("DELETE", this.uri + encodeURIComponent(doc._id) + "/" + attachment_name
+ "?rev=" + doc._rev);
+    var result = JSON.parse(req.responseText);
+    if (req.status != 200)
+      throw result;
+    doc._rev = result.rev; //record rev in input document
+    return result;
+  }
+  
   this.bulkSave = function(docs, options) {
     var req = request("POST", this.uri + "_bulk_docs" + encodeOptions(options), {
       body: JSON.stringify({"docs": docs})

Modified: incubator/couchdb/branches/runtimeconfig/share/www/script/couch_tests.js
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/share/www/script/couch_tests.js?rev=677353&r1=677352&r2=677353&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/share/www/script/couch_tests.js [utf-8] (original)
+++ incubator/couchdb/branches/runtimeconfig/share/www/script/couch_tests.js [utf-8] Wed Jul
16 10:23:36 2008
@@ -556,8 +556,70 @@
     T(db.save(binAttDoc).ok);
 
     var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt");
-    T(xhr.responseText == "This is a base64 encoded text")
-    T(xhr.getResponseHeader("Content-Type") == "text/plain")
+    T(xhr.responseText == "This is a base64 encoded text");
+    T(xhr.getResponseHeader("Content-Type") == "text/plain");
+    
+    // test RESTful doc API
+    
+    var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt", {
+      body:"This is no base64 encoded text",
+      headers:{"Content-Type": "text/plain;charset=utf-8"}
+    });
+    T(xhr.status == 201);
+    var rev = JSON.parse(xhr.responseText).rev;
+    
+    var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo2.txt");
+    T(xhr.responseText == "This is no base64 encoded text");
+    T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8");
+    
+    // test without rev, should fail
+    var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt");
+    T(xhr.status == 412);
+
+    // test with rev, should not fail
+    var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt?rev=" + rev);
+    T(xhr.status == 200);
+    
+    
+    // test binary data
+    var bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])}    ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np";
+    var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", {
+      headers:{"Content-Type":"text/plain;charset=utf-8"},
+      body:bin_data
+    });
+    T(xhr.status == 201);
+    var rev = JSON.parse(xhr.responseText).rev;
+    
+    var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt");
+    T(xhr.responseText == bin_data);
+    T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8");
+    
+    var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", {
+      headers:{"Content-Type":"text/plain;charset=utf-8"},
+      body:bin_data
+    });
+    T(xhr.status == 412);
+
+    var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev,
{
+      headers:{"Content-Type":"text/plain;charset=utf-8"},
+      body:bin_data
+    });
+    T(xhr.status == 201);
+    var rev = JSON.parse(xhr.responseText).rev;
+
+    var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt");
+    T(xhr.responseText == bin_data);
+    T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8");
+
+    var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
+    T(xhr.responseText == bin_data);
+    T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8");
+
+    var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
+    T(xhr.status == 200);
+    
+    var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
+    T(xhr.status == 404);
   },
 
   content_negotiation: function(debug) {

Modified: incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_db.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_db.erl?rev=677353&r1=677352&r2=677353&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_db.erl (original)
+++ incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_db.erl Wed Jul 16 10:23:36
2008
@@ -298,13 +298,13 @@
     % flush unwritten binaries to disk.
     DocBuckets3 = [[doc_flush_binaries(Doc, Db#db.fd) || Doc <- Bucket] || Bucket <-
DocBuckets2],
 
-    case gen_server:call(MainPid, {update_docs, DocBuckets3, [new_edits | Options]}) of
+    case gen_server:call(MainPid, {update_docs, DocBuckets3, [new_edits | Options]}, infinity)
of
     ok -> {ok, NewRevs};
     retry ->
         Db2 = get_db(MainPid),
         DocBuckets4 = [[doc_flush_binaries(Doc, Db2#db.fd) || Doc <- Bucket] || Bucket
<- DocBuckets3],
         % We only retry once
-        case gen_server:call(MainPid, {update_docs, DocBuckets4, [new_edits | Options]})
of
+        case gen_server:call(MainPid, {update_docs, DocBuckets4, [new_edits | Options]},
infinity) of
         ok -> {ok, NewRevs};
         Else -> throw(Else)
         end;
@@ -320,7 +320,7 @@
     Db = get_db(MainPid),
     DocBuckets = group_alike_docs(Docs),
     DocBuckets2 = [[doc_flush_binaries(Doc, Db#db.fd) || Doc <- Bucket] || Bucket <-
DocBuckets],
-    ok = gen_server:call(MainPid, {update_docs, DocBuckets2, Options}).
+    ok = gen_server:call(MainPid, {update_docs, DocBuckets2, Options}, infinity).
 
 
 doc_flush_binaries(Doc, Fd) ->

Modified: incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_file.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_file.erl?rev=677353&r1=677352&r2=677353&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_file.erl (original)
+++ incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_file.erl Wed Jul 16 10:23:36
2008
@@ -156,7 +156,7 @@
 %%----------------------------------------------------------------------
 
 sync(Fd) ->
-    gen_server:call(Fd, sync).
+    gen_server:call(Fd, sync, infinity).
 
 %%----------------------------------------------------------------------
 %% Purpose: Close the file. Is performed asynchronously.

Modified: incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_httpd.erl?rev=677353&r1=677352&r2=677353&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_httpd.erl (original)
+++ incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_httpd.erl Wed Jul 16 10:23:36
2008
@@ -653,8 +653,59 @@
         throw(Error)
     end;
 
+handle_attachment_request(Req, Method, _DbName, Db, DocId, FileName)
+    when (Method == 'PUT') or (Method == 'DELETE') ->
+
+    Rev = extract_header_rev(Req),
+    
+    NewAttachment = case Method of
+        'DELETE' ->
+            [];
+        _ ->
+            [{FileName, {
+                Req:get_header_value("Content-Type"),
+                Req:recv_body()
+            }}]
+    end,
+
+    Doc =
+    case Rev of
+    missing_rev -> % make the new doc
+        #doc{id=DocId};
+    _ ->
+        case couch_db:open_doc_revs(Db, DocId, [Rev], []) of
+        {ok, [{ok, Doc0}]}   -> Doc0#doc{revs=[Rev]};
+        {ok, [Error]}       -> throw(Error)
+        end
+    end,
+    
+    #doc{attachments=Attachments} = Doc,
+    DocEdited = Doc#doc{
+        attachments = NewAttachment ++ proplists:delete(FileName, Attachments)},
+    {ok, UpdatedRev} = couch_db:update_doc(Db, DocEdited, []),
+    send_json(Req, case Method of 'DELETE' -> 200; _ -> 201 end, {obj, [
+        {ok, true},
+        {id, DocId},
+        {rev, UpdatedRev}
+    ]});
+
 handle_attachment_request(_Req, _Method, _DbName, _Db, _DocId, _FileName) ->
-    throw({method_not_allowed, "GET,HEAD"}).
+    throw({method_not_allowed, "GET,HEAD,DELETE,PUT"}).
+
+extract_header_rev(Req) ->
+    QueryRev = proplists:get_value("rev", Req:parse_qs()),
+    Etag = case Req:get_header_value("If-Match") of
+        undefined -> undefined;
+        Tag -> string:strip(Tag, both, $")
+    end,
+    case {QueryRev, Etag} of
+    {undefined, undefined} -> missing_rev;
+    {_, undefined} -> QueryRev;
+    {undefined, _} -> Etag;
+    _ when QueryRev == Etag -> Etag;
+    _ ->
+        throw({bad_request, "Document rev and etag have different values"})
+    end.
 
 % Config request handlers
 



Mime
View raw message