couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r795277 - in /couchdb/trunk/src/couchdb: couch_db.erl couch_file.erl
Date Fri, 17 Jul 2009 23:23:03 GMT
Author: damien
Date: Fri Jul 17 23:23:03 2009
New Revision: 795277

URL: http://svn.apache.org/viewvc?rev=795277&view=rev
Log:
MD5 term checking by using leading bit in 32 bit length to indicate MD5 presence.

Modified:
    couchdb/trunk/src/couchdb/couch_db.erl
    couchdb/trunk/src/couchdb/couch_file.erl

Modified: couchdb/trunk/src/couchdb/couch_db.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_db.erl?rev=795277&r1=795276&r2=795277&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_db.erl (original)
+++ couchdb/trunk/src/couchdb/couch_db.erl Fri Jul 17 23:23:03 2009
@@ -870,12 +870,8 @@
 read_doc(#db{fd=Fd}, OldStreamPointer) when is_tuple(OldStreamPointer) ->
     % 09 UPGRADE CODE
     couch_stream:old_read_term(Fd, OldStreamPointer);
-read_doc(#db{header=#db_header{disk_version=Version},fd=Fd}, Pos)
-        when Version == 3 ->
-    % 09 UPGRADE CODE
-    couch_file:pread_term(Fd, Pos);
 read_doc(#db{fd=Fd}, Pos) ->
-    couch_file:pread_term_md5(Fd, Pos).
+    couch_file:pread_term(Fd, Pos).
 
 
 doc_to_tree(#doc{revs={Start, RevIds}}=Doc) ->

Modified: couchdb/trunk/src/couchdb/couch_file.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_file.erl?rev=795277&r1=795276&r2=795277&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_file.erl (original)
+++ couchdb/trunk/src/couchdb/couch_file.erl Fri Jul 17 23:23:03 2009
@@ -25,8 +25,7 @@
 -export([open/1, open/2, close/1, bytes/1, sync/1, append_binary/2,old_pread/3]).
 -export([append_term/2, pread_term/2, pread_iolist/2, write_header/2]).
 -export([pread_binary/2, read_header/1, truncate/2, upgrade_old_header/2]).
--export([append_term_md5/2, pread_iolist_md5/2, pread_binary_md5/2]).
--export([pread_term_md5/2]).
+-export([append_term_md5/2,append_binary_md5/2]).
 -export([init/1, terminate/2, handle_call/3, handle_cast/2, code_change/3, handle_info/2]).
 
 %%----------------------------------------------------------------------
@@ -84,12 +83,13 @@
 
 append_binary(Fd, Bin) ->
     Size = iolist_size(Bin),
-    gen_server:call(Fd, {append_bin, [<<Size:32/integer>>, Bin]}, infinity).
+    gen_server:call(Fd, {append_bin,
+            [<<0:1/integer,Size:31/integer>>, Bin]}, infinity).
     
 append_binary_md5(Fd, Bin) ->
     Size = iolist_size(Bin),
     gen_server:call(Fd, {append_bin, 
-            [<<Size:32/integer>>, erlang:md5(Bin), Bin]}, infinity).
+            [<<1:1/integer,Size:31/integer>>, erlang:md5(Bin), Bin]}, infinity).
 
 
 %%----------------------------------------------------------------------
@@ -104,10 +104,6 @@
     {ok, Bin} = pread_binary(Fd, Pos),
     {ok, binary_to_term(Bin)}.
 
-pread_term_md5(Fd, Pos) ->
-    {ok, Bin} = pread_binary_md5(Fd, Pos),
-    {ok, binary_to_term(Bin)}.
-
 
 %%----------------------------------------------------------------------
 %% Purpose: Reads a binrary from a file that was written with append_binary
@@ -121,21 +117,26 @@
     {ok, iolist_to_binary(L)}.
 
 pread_iolist(Fd, Pos) ->
-    {ok, LenIolist, NextPos} =read_raw_iolist(Fd, Pos, 4),
-    <<Len:32/integer>> = iolist_to_binary(LenIolist),
-    {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len),
-    {ok, Iolist}.
-    
-pread_binary_md5(Fd, Pos) ->
-    {ok, L} = pread_iolist_md5(Fd, Pos),
-    {ok, iolist_to_binary(L)}.
-
-pread_iolist_md5(Fd, Pos) ->
     {ok, LenIolist, NextPos} =read_raw_iolist(Fd, Pos, 20),
-    <<Len:32/integer, Md5/binary>> = iolist_to_binary(LenIolist),
-    {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len),
-    Md5 = erlang:md5(Iolist),
-    {ok, Iolist}.
+    case iolist_to_binary(LenIolist) of
+    <<1:1/integer,Len:31/integer,Md5/binary>> ->
+        {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len),
+        case erlang:md5(Iolist) of
+        Md5 -> ok;
+        _ ->  throw(file_corruption)
+        end, 
+        {ok, Iolist};
+    <<0:1/integer,Len:31/integer,First16Bytes/binary>> ->
+        if Len =< 16 ->
+            <<Final:Len/binary,_/binary>> = First16Bytes,
+            {ok, Final};
+        true ->
+            {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len - 16),
+            {ok, [First16Bytes, Iolist]}
+        end
+    end.
+            
+       
 
 read_raw_iolist(Fd, Pos, Len) ->
     BlockOffset = Pos rem ?SIZE_BLOCK,



Mime
View raw message