Anonymous avatar Anonymous committed 74f0c0e

fixing oid encode/decode function

Comments (0)

Files changed (2)

 		 find_all/2, find_all/3, find_all/4, get_more/4,
 		 get_more/5, find_one/3, find_one/4, kill_cursors/2,
 		 insert/3, update/4, update/5, delete/2, delete/3,
-		 count/2]).
+		 count/2, dec2hex/1, hex2dec/1]).
 
 -include("emongo.hrl").
 
 	
 get_pool(PoolId, [Pool|Tail], Others) ->
 	get_pool(PoolId, Tail, [Pool|Others]).
+
+dec2hex(Dec) ->
+	dec2hex(<<>>, Dec).
 	
+dec2hex(N, <<I:8,Rem/binary>>) ->
+	dec2hex(<<N/binary, (hex0((I band 16#f0) bsr 4)):8, (hex0((I band 16#0f))):8>>, Rem);
+dec2hex(N,<<>>) ->
+	N.
+
+hex2dec(Hex) when is_list(Hex) ->
+	hex2dec(list_to_binary(Hex));
+	
+hex2dec(Hex) ->
+	hex2dec(<<>>, Hex).
+	
+hex2dec(N,<<A:8,B:8,Rem/binary>>) ->
+	hex2dec(<<N/binary, ((dec0(A) bsl 4) + dec0(B)):8>>, Rem);
+hex2dec(N,<<>>) ->
+	N.
+
+dec0($a) ->	10;
+dec0($b) ->	11;
+dec0($c) ->	12;
+dec0($d) ->	13;
+dec0($e) ->	14;
+dec0($f) ->	15;
+dec0(X) ->	X - $0.
+
+hex0(10) -> $a;
+hex0(11) -> $b;
+hex0(12) -> $c;
+hex0(13) -> $d;
+hex0(14) -> $e;
+hex0(15) -> $f;
+hex0(I) ->  $0 + I.
 	
 	
 	

src/emongo_bson.erl

 	<<5, Key1/binary, 0, (byte_size(Val)):32/little-signed, SubType:8, Val/binary>>;
 		
 %% OID
-encode_key_value(Key, {oid, <<First:8/little-binary-unit:8, Second:4/little-binary-unit:8>>}) ->
+encode_key_value(Key, {oid, HexString}) when is_list(HexString) ->
+	encode_key_value(Key, {oid, emongo:hex2dec(HexString)});
+	
+encode_key_value(Key, {oid, OID}) when is_binary(OID) ->
 	Key1 = encode_key(Key),
-	FirstReversed = lists:reverse(binary_to_list(First)),
-	SecondReversed = lists:reverse(binary_to_list(Second)),
-	OID = list_to_binary(lists:append(FirstReversed, SecondReversed)),
 	<<7, Key1/binary, 0, OID/binary>>;
 	
 %% BOOL
   	{{binary, SubType, BinData}, Tail};
 
 %% OID
-decode_value(7, <<First:8/little-binary-unit:8, Second:4/little-binary-unit:8, Tail/binary>>) ->
-	FirstReversed = lists:reverse(binary_to_list(First)),
-	SecondReversed = lists:reverse(binary_to_list(Second)),
-	OID = list_to_binary(lists:append(FirstReversed, SecondReversed)),
+decode_value(7, <<OID:12/binary, Tail/binary>>) ->
 	{{oid, OID}, Tail};
 	
 %% BOOL	
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.