]> gitweb.factorcode.org Git - factor.git/blob - extra/bson/constants/constants.factor
use radix literals
[factor.git] / extra / bson / constants / constants.factor
1 ! Copyright (C) 2010 Sascha Matzke.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs calendar combinators
4 combinators.short-circuit constructors kernel linked-assocs
5 math math.bitwise random strings uuid ;
6 IN: bson.constants
7
8 : <objid> ( -- objid )
9    uuid1 ; inline
10
11 TUPLE: oid { a initial: 0 } { b initial: 0 } ;
12
13 : <oid> ( -- oid )
14     oid new
15     now timestamp>micros >>a
16     8 random-bits 16 shift 0xFF0000 mask
17     16 random-bits 0xFFFF mask
18     bitor >>b ;
19
20 TUPLE: dbref ref id db ;
21
22 TUPLE: mongo-timestamp incr seconds ;
23
24 : <mongo-timestamp> ( incr seconds -- mongo-timestamp )
25     mongo-timestamp boa ;
26
27 TUPLE: mongo-scoped-code code object ;
28
29 : <mongo-scoped-code> ( code object -- mongo-scoped-code )
30     mongo-scoped-code boa ;
31
32 CONSTRUCTOR: dbref ( ref id -- dbref ) ;
33
34 : dbref>assoc ( dbref -- assoc )
35     [ <linked-hash> ] dip over
36     {
37         [ [ ref>> "$ref" ] [ set-at ] bi* ]
38         [ [ id>> "$id" ] [ set-at ] bi* ]
39         [ over db>> [
40                 [ db>> "$db" ] [ set-at ] bi*
41             ] [ 2drop ] if ]
42     } 2cleave ; inline
43
44 : assoc>dbref ( assoc -- dbref )
45     [ "$ref" swap at ] [ "$id" swap at ] [ "$db" swap at ] tri
46     dbref boa ; inline
47
48 : dbref-assoc? ( assoc -- ? )
49     { [ "$ref" swap key? ] [ "$id" swap key? ] } 1&& ; inline
50
51 TUPLE: mdbregexp { regexp string } { options string } ;
52
53 : <mdbregexp> ( string -- mdbregexp )
54    [ mdbregexp new ] dip >>regexp ;
55
56
57 CONSTANT: MDB_OID_FIELD "_id"
58 CONSTANT: MDB_META_FIELD "_mfd"
59
60 CONSTANT: T_EOO     0
61 CONSTANT: T_Double  0x1
62 CONSTANT: T_String  0x2
63 CONSTANT: T_Object  0x3
64 CONSTANT: T_Array   0x4
65 CONSTANT: T_Binary  0x5
66 CONSTANT: T_Undefined  0x6
67 CONSTANT: T_OID     0x7
68 CONSTANT: T_Boolean 0x8
69 CONSTANT: T_Date    0x9
70 CONSTANT: T_NULL    0xA
71 CONSTANT: T_Regexp  0xB
72 CONSTANT: T_DBRef   0xC
73 CONSTANT: T_Code    0xD
74 CONSTANT: T_Symbol  0xE
75 CONSTANT: T_ScopedCode 0xF
76 CONSTANT: T_Integer 0x10
77 CONSTANT: T_Timestamp 0x11
78 CONSTANT: T_Integer64 0x12
79 CONSTANT: T_MinKey  0xFF
80 CONSTANT: T_MaxKey  0x7F
81
82 CONSTANT: T_Binary_Default                  0x0
83 CONSTANT: T_Binary_Function                 0x1
84 CONSTANT: T_Binary_Bytes_Deprecated         0x2
85 CONSTANT: T_Binary_UUID                     0x3
86 CONSTANT: T_Binary_MD5                      0x5
87 CONSTANT: T_Binary_Custom                   0x80
88