Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added simple string name/value doc

  • Loading branch information...
commit 38ba51ef7b8f445bb1c053f7e74477b43f857559 1 parent 6ff97b9
@christkv authored
Showing with 43 additions and 5 deletions.
  1. +10 −0 include/bson.hrl
  2. +27 −3 src/bson.erl
  3. +6 −2 src/bson_tests.erl
View
10 include/bson.hrl
@@ -0,0 +1,10 @@
+% Use these macros to write/read numbers from bson or mongo binary format
+-define (put_int32 (N), (N):32/signed-little).
+-define (put_int64 (N), (N):64/signed-little).
+-define (put_float (N), (N):64/float-little).
+-define (put_bits32 (B7,B6,B5,B4,B3,B2,B1,B0), (B7):1,(B6):1,(B5):1,(B4):1,(B3):1,(B2):1,(B1):1,(B0):1,0:24).
+
+-define (get_int32 (N), N:32/signed-little).
+-define (get_int64 (N), N:64/signed-little).
+-define (get_float (N), N:64/float-little).
+-define (get_bits32 (B7,B6,B5,B4,B3,B2,B1,B0), B7:1,B6:1,B5:1,B4:1,B3:1,B2:1,B1:1,B0:1,_:24).
View
30 src/bson.erl
@@ -8,6 +8,10 @@
% Exporting all availble types
-export_type ([utf8/0]).
+% -include ("bson.hrl").
+-define (put_int32 (N), (N):32/signed-little).
+
+
% Calculate the size of the document so we can do a single allocation of a binary
calculate_document_size(Doc) ->
% Return the size of the document we passed in
@@ -36,11 +40,31 @@ calculate_document_size_objects([]) ->
% Serialize document
serialize(Doc) ->
+ erlang:display("-------------------------------------------------- PRE DOC"),
erlang:display(Doc),
- Size = calculate_document_size(Doc),
- erlang:display(Size),
- <<>>.
+ BinDoc = serialize_doc(Doc),
+ erlang:display("-------------------------------------------------- POST DOC"),
+ erlang:display(BinDoc),
+ BinDoc.
+serialize_doc(Doc) ->
+ % Create final binary
+ Bin = list_to_binary(serialize_doc_objects(Doc)),
+ % Add document header
+ list_to_binary([<<?put_int32(byte_size(Bin) + 4 + 1)>>, Bin, <<0>>]).
+
+serialize_doc_objects([Head|Tail]) ->
+ case Head of
+ {Name, [Value]} when is_binary(Name), is_binary(Value) ->
+ erlang:display("================================== calculate_document_size string"),
+ % "\x02" cstring int32 cstring"
+ [list_to_binary([<<02>>, Name, <<0>>, <<?put_int32(byte_size(Value) + 1)>>, Value, <<0>>])];
+ _ ->
+ erlang:display("================================== calculate_document_size done"),
+ serialize_doc_objects(Tail)
+ end;
+serialize_doc_objects([]) -> [].
+
% Deserialize document
deserialize(BinDoc) ->
[].
View
8 src/bson_tests.erl
@@ -5,5 +5,9 @@
simple_bson_serialization_test() ->
% Build a simple string key-value doc and serialize the doc
Doc = orddict:append(bson:utf8("name"), bson:utf8("value"), orddict:new()),
- BinDoc = bson:serialize(Doc),
- erlang:display(BinDoc).
+ BinDoc = bson:serialize(Doc),
+ <<21,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,0>> = BinDoc,
+ erlang:display("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"),
+ ?debugFmt("~p~n", [BinDoc]).
+
+ % erlang:display(BinDoc).
Please sign in to comment.
Something went wrong with that request. Please try again.