]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/bson/constants/constants.factor
use radix literals
[factor.git] / extra / bson / constants / constants.factor
index 5148413b6104851f9a525f944f0820f96982507e..359a3935164e75dea32a304c50b34ee0b1a161fe 100644 (file)
@@ -1,5 +1,8 @@
-USING: accessors constructors kernel strings uuid ;
-
+! Copyright (C) 2010 Sascha Matzke.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors assocs calendar combinators
+combinators.short-circuit constructors kernel linked-assocs
+math math.bitwise random strings uuid ;
 IN: bson.constants
 
 : <objid> ( -- objid )
@@ -7,9 +10,43 @@ IN: bson.constants
 
 TUPLE: oid { a initial: 0 } { b initial: 0 } ;
 
-TUPLE: objref ns objid ;
+: <oid> ( -- oid )
+    oid new
+    now timestamp>micros >>a
+    8 random-bits 16 shift 0xFF0000 mask
+    16 random-bits 0xFFFF mask
+    bitor >>b ;
+
+TUPLE: dbref ref id db ;
+
+TUPLE: mongo-timestamp incr seconds ;
+
+: <mongo-timestamp> ( incr seconds -- mongo-timestamp )
+    mongo-timestamp boa ;
+
+TUPLE: mongo-scoped-code code object ;
+
+: <mongo-scoped-code> ( code object -- mongo-scoped-code )
+    mongo-scoped-code boa ;
+
+CONSTRUCTOR: dbref ( ref id -- dbref ) ;
+
+: dbref>assoc ( dbref -- assoc )
+    [ <linked-hash> ] dip over
+    {
+        [ [ ref>> "$ref" ] [ set-at ] bi* ]
+        [ [ id>> "$id" ] [ set-at ] bi* ]
+        [ over db>> [
+                [ db>> "$db" ] [ set-at ] bi*
+            ] [ 2drop ] if ]
+    } 2cleave ; inline
+
+: assoc>dbref ( assoc -- dbref )
+    [ "$ref" swap at ] [ "$id" swap at ] [ "$db" swap at ] tri
+    dbref boa ; inline
 
-CONSTRUCTOR: objref ( ns objid -- objref ) ;
+: dbref-assoc? ( assoc -- ? )
+    { [ "$ref" swap key? ] [ "$id" swap key? ] } 1&& ; inline
 
 TUPLE: mdbregexp { regexp string } { options string } ;
 
@@ -20,30 +57,32 @@ TUPLE: mdbregexp { regexp string } { options string } ;
 CONSTANT: MDB_OID_FIELD "_id"
 CONSTANT: MDB_META_FIELD "_mfd"
 
-CONSTANT: T_EOO  0  
-CONSTANT: T_Double  1  
-CONSTANT: T_Integer  16  
-CONSTANT: T_Boolean  8  
-CONSTANT: T_String  2  
-CONSTANT: T_Object  3  
-CONSTANT: T_Array  4  
-CONSTANT: T_Binary  5  
-CONSTANT: T_Undefined  6  
-CONSTANT: T_OID  7  
-CONSTANT: T_Date  9  
-CONSTANT: T_NULL  10  
-CONSTANT: T_Regexp  11  
-CONSTANT: T_DBRef  12  
-CONSTANT: T_Code  13  
-CONSTANT: T_ScopedCode  17  
-CONSTANT: T_Symbol  14  
-CONSTANT: T_JSTypeMax  16  
-CONSTANT: T_MaxKey  127  
-
-CONSTANT: T_Binary_Function 1   
-CONSTANT: T_Binary_Bytes 2
-CONSTANT: T_Binary_UUID 3
-CONSTANT: T_Binary_MD5 5
-CONSTANT: T_Binary_Custom 128
+CONSTANT: T_EOO     0
+CONSTANT: T_Double  0x1
+CONSTANT: T_String  0x2
+CONSTANT: T_Object  0x3
+CONSTANT: T_Array   0x4
+CONSTANT: T_Binary  0x5
+CONSTANT: T_Undefined  0x6
+CONSTANT: T_OID     0x7
+CONSTANT: T_Boolean 0x8
+CONSTANT: T_Date    0x9
+CONSTANT: T_NULL    0xA
+CONSTANT: T_Regexp  0xB
+CONSTANT: T_DBRef   0xC
+CONSTANT: T_Code    0xD
+CONSTANT: T_Symbol  0xE
+CONSTANT: T_ScopedCode 0xF
+CONSTANT: T_Integer 0x10
+CONSTANT: T_Timestamp 0x11
+CONSTANT: T_Integer64 0x12
+CONSTANT: T_MinKey  0xFF
+CONSTANT: T_MaxKey  0x7F
 
+CONSTANT: T_Binary_Default                  0x0
+CONSTANT: T_Binary_Function                 0x1
+CONSTANT: T_Binary_Bytes_Deprecated         0x2
+CONSTANT: T_Binary_UUID                     0x3
+CONSTANT: T_Binary_MD5                      0x5
+CONSTANT: T_Binary_Custom                   0x80