Skip to content

Commit 1a4750e

Browse files
committed
more cleaning/migrating to org.bson
1 parent 1d256dd commit 1a4750e

8 files changed

Lines changed: 173 additions & 130 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ mongo.jar
4141
mongo-*.jar
4242
bson.jar
4343
bson-*.jar
44-
org
4544

4645
TAGS
4746

src/main/com/mongodb/ByteDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected int decodeNext( DBObject o , String path ){
218218
break;
219219

220220
case REGEX:
221-
created = Pattern.compile( readCStr() , Bytes.patternFlags( readCStr() ) );
221+
created = Pattern.compile( readCStr() , Bytes.regexFlags( readCStr() ) );
222222
break;
223223

224224
case BINARY:

src/main/com/mongodb/ByteEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ private int putPattern( String name, Pattern p ) {
490490
int start = _buf.position();
491491
_put( REGEX , name );
492492
_put( p.pattern() );
493-
_put( patternFlags( p.flags() ) );
493+
_put( regexFlags( p.flags() ) );
494494
return _buf.position() - start;
495495
}
496496

src/main/com/mongodb/Bytes.java

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import java.util.*;
2525
import java.util.logging.*;
2626

27+
import org.bson.*;
28+
2729
/**
2830
* Handles byte functions for <code>ByteEncoder</code> and <code>ByteDecoder</code>.
2931
*/
30-
public class Bytes {
32+
public class Bytes extends BSON {
3133

3234
static Logger LOGGER = Logger.getLogger( "com.mongodb" );
3335

@@ -63,32 +65,6 @@ public class Bytes {
6365
NUM_ENCODERS = numBufs;
6466
}
6567

66-
static final byte EOO = 0;
67-
static final byte NUMBER = 1;
68-
static final byte STRING = 2;
69-
static final byte OBJECT = 3;
70-
static final byte ARRAY = 4;
71-
static final byte BINARY = 5;
72-
static final byte UNDEFINED = 6;
73-
static final byte OID = 7;
74-
static final byte BOOLEAN = 8;
75-
static final byte DATE = 9;
76-
static final byte NULL = 10;
77-
static final byte REGEX = 11;
78-
static final byte REF = 12;
79-
static final byte CODE = 13;
80-
static final byte SYMBOL = 14;
81-
static final byte CODE_W_SCOPE = 15;
82-
static final byte NUMBER_INT = 16;
83-
static final byte TIMESTAMP = 17;
84-
static final byte NUMBER_LONG = 18;
85-
86-
static final byte MINKEY = -1;
87-
static final byte MAXKEY = 127;
88-
89-
private static final int GLOBAL_FLAG = 256;
90-
91-
9268
/*
9369
these are binary types
9470
so the format would look like
@@ -154,98 +130,6 @@ public static byte getType( Object o ){
154130
return 0;
155131
}
156132

157-
/** Converts a string of regular expression flags from the database in Java regular
158-
* expression flags.
159-
* @param flags flags from database
160-
* @return the Java flags
161-
*/
162-
public static int patternFlags( String flags ){
163-
flags = flags.toLowerCase();
164-
int fint = 0;
165-
166-
for( int i=0; i<flags.length(); i++ ) {
167-
Flag flag = Flag.getByCharacter( flags.charAt( i ) );
168-
if( flag != null ) {
169-
fint |= flag.javaFlag;
170-
if( flag.unsupported != null )
171-
_warnUnsupported( flag.unsupported );
172-
}
173-
else {
174-
throw new IllegalArgumentException( "unrecognized flag: "+flags.charAt( i ) );
175-
}
176-
}
177-
return fint;
178-
}
179-
180-
public static int getFlag( char c ){
181-
Flag flag = Flag.getByCharacter( c );
182-
if ( flag == null )
183-
throw new IllegalArgumentException( "unrecognized flag: " + c );
184-
185-
if ( flag.unsupported != null ){
186-
_warnUnsupported( flag.unsupported );
187-
return 0;
188-
}
189-
190-
return flag.javaFlag;
191-
}
192-
193-
/** Converts Java regular expression flags into a string of flags for the database
194-
* @param flags Java flags
195-
* @return the flags for the database
196-
*/
197-
public static String patternFlags( int flags ){
198-
StringBuilder buf = new StringBuilder();
199-
200-
for( Flag flag : Flag.values() ) {
201-
if( ( flags & flag.javaFlag ) > 0 ) {
202-
buf.append( flag.flagChar );
203-
flags -= flag.javaFlag;
204-
}
205-
}
206-
207-
if( flags > 0 )
208-
throw new IllegalArgumentException( "some flags could not be recognized." );
209-
210-
return buf.toString();
211-
}
212-
213-
private static enum Flag {
214-
CANON_EQ( Pattern.CANON_EQ, 'c', "Pattern.CANON_EQ" ),
215-
UNIX_LINES(Pattern.UNIX_LINES, 'd', "Pattern.UNIX_LINES" ),
216-
GLOBAL( GLOBAL_FLAG, 'g', null ),
217-
CASE_INSENSITIVE( Pattern.CASE_INSENSITIVE, 'i', null ),
218-
MULTILINE(Pattern.MULTILINE, 'm', null ),
219-
DOTALL( Pattern.DOTALL, 's', "Pattern.DOTALL" ),
220-
LITERAL( Pattern.LITERAL, 't', "Pattern.LITERAL" ),
221-
UNICODE_CASE( Pattern.UNICODE_CASE, 'u', "Pattern.UNICODE_CASE" ),
222-
COMMENTS( Pattern.COMMENTS, 'x', null );
223-
224-
private static final Map<Character, Flag> byCharacter = new HashMap<Character, Flag>();
225-
226-
static {
227-
for (Flag flag : values()) {
228-
byCharacter.put(flag.flagChar, flag);
229-
}
230-
}
231-
232-
public static Flag getByCharacter(char ch) {
233-
return byCharacter.get(ch);
234-
}
235-
public final int javaFlag;
236-
public final char flagChar;
237-
public final String unsupported;
238-
Flag( int f, char ch, String u ) {
239-
javaFlag = f;
240-
flagChar = ch;
241-
unsupported = u;
242-
}
243-
}
244-
245-
private static void _warnUnsupported( String flag ) {
246-
System.out.println( "flag " + flag + " not supported by db." );
247-
}
248-
249133
public static void addEncodingHook( Class c , Transformer t ){
250134
_anyHooks = true;
251135
List<Transformer> l = _encodingHooks.get( c );

src/main/com/mongodb/util/JSON.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static void serialize( Object o , StringBuilder buf ){
149149
}
150150

151151
if (o instanceof Pattern) {
152-
buf.append("/").append(o.toString()).append("/").append(Bytes.patternFlags( ((Pattern)o).flags() ));
152+
buf.append("/").append(o.toString()).append("/").append(Bytes.regexFlags( ((Pattern)o).flags() ));
153153
return;
154154
}
155155

@@ -314,7 +314,7 @@ public Pattern parsePatter(){
314314
if ( Character.isWhitespace( current ) ||
315315
! Character.isLetter( current ) )
316316
break;
317-
flags |= Bytes.getFlag( current );
317+
flags |= Bytes.regexFlag( current );
318318
current = read();
319319
}
320320

src/main/org/bson/BSON.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// BSON.java
2+
3+
package org.bson;
4+
5+
import java.util.*;
6+
import java.util.regex.*;
7+
8+
public class BSON {
9+
10+
// ---- basics ----
11+
12+
public static final byte EOO = 0;
13+
public static final byte NUMBER = 1;
14+
public static final byte STRING = 2;
15+
public static final byte OBJECT = 3;
16+
public static final byte ARRAY = 4;
17+
public static final byte BINARY = 5;
18+
public static final byte UNDEFINED = 6;
19+
public static final byte OID = 7;
20+
public static final byte BOOLEAN = 8;
21+
public static final byte DATE = 9;
22+
public static final byte NULL = 10;
23+
public static final byte REGEX = 11;
24+
public static final byte REF = 12;
25+
public static final byte CODE = 13;
26+
public static final byte SYMBOL = 14;
27+
public static final byte CODE_W_SCOPE = 15;
28+
public static final byte NUMBER_INT = 16;
29+
public static final byte TIMESTAMP = 17;
30+
public static final byte NUMBER_LONG = 18;
31+
32+
public static final byte MINKEY = -1;
33+
public static final byte MAXKEY = 127;
34+
35+
36+
37+
// ---- regular expression handling ----
38+
39+
/** Converts a string of regular expression flags from the database in Java regular
40+
* expression flags.
41+
* @param flags flags from database
42+
* @return the Java flags
43+
*/
44+
public static int regexFlags( String flags ){
45+
flags = flags.toLowerCase();
46+
int fint = 0;
47+
48+
for( int i=0; i<flags.length(); i++ ) {
49+
RegexFlag flag = RegexFlag.getByCharacter( flags.charAt( i ) );
50+
if( flag != null ) {
51+
fint |= flag.javaFlag;
52+
if( flag.unsupported != null )
53+
_warnUnsupportedRegex( flag.unsupported );
54+
}
55+
else {
56+
throw new IllegalArgumentException( "unrecognized flag: "+flags.charAt( i ) );
57+
}
58+
}
59+
return fint;
60+
}
61+
62+
public static int regexFlag( char c ){
63+
RegexFlag flag = RegexFlag.getByCharacter( c );
64+
if ( flag == null )
65+
throw new IllegalArgumentException( "unrecognized flag: " + c );
66+
67+
if ( flag.unsupported != null ){
68+
_warnUnsupportedRegex( flag.unsupported );
69+
return 0;
70+
}
71+
72+
return flag.javaFlag;
73+
}
74+
75+
/** Converts Java regular expression flags into a string of flags for the database
76+
* @param flags Java flags
77+
* @return the flags for the database
78+
*/
79+
public static String regexFlags( int flags ){
80+
StringBuilder buf = new StringBuilder();
81+
82+
for( RegexFlag flag : RegexFlag.values() ) {
83+
if( ( flags & flag.javaFlag ) > 0 ) {
84+
buf.append( flag.flagChar );
85+
flags -= flag.javaFlag;
86+
}
87+
}
88+
89+
if( flags > 0 )
90+
throw new IllegalArgumentException( "some flags could not be recognized." );
91+
92+
return buf.toString();
93+
}
94+
95+
private static enum RegexFlag {
96+
CANON_EQ( Pattern.CANON_EQ, 'c', "Pattern.CANON_EQ" ),
97+
UNIX_LINES(Pattern.UNIX_LINES, 'd', "Pattern.UNIX_LINES" ),
98+
GLOBAL( GLOBAL_FLAG, 'g', null ),
99+
CASE_INSENSITIVE( Pattern.CASE_INSENSITIVE, 'i', null ),
100+
MULTILINE(Pattern.MULTILINE, 'm', null ),
101+
DOTALL( Pattern.DOTALL, 's', "Pattern.DOTALL" ),
102+
LITERAL( Pattern.LITERAL, 't', "Pattern.LITERAL" ),
103+
UNICODE_CASE( Pattern.UNICODE_CASE, 'u', "Pattern.UNICODE_CASE" ),
104+
COMMENTS( Pattern.COMMENTS, 'x', null );
105+
106+
private static final Map<Character, RegexFlag> byCharacter = new HashMap<Character, RegexFlag>();
107+
108+
static {
109+
for (RegexFlag flag : values()) {
110+
byCharacter.put(flag.flagChar, flag);
111+
}
112+
}
113+
114+
public static RegexFlag getByCharacter(char ch) {
115+
return byCharacter.get(ch);
116+
}
117+
public final int javaFlag;
118+
public final char flagChar;
119+
public final String unsupported;
120+
121+
RegexFlag( int f, char ch, String u ) {
122+
javaFlag = f;
123+
flagChar = ch;
124+
unsupported = u;
125+
}
126+
}
127+
128+
private static void _warnUnsupportedRegex( String flag ) {
129+
System.out.println( "flag " + flag + " not supported by db." );
130+
}
131+
132+
private static final int GLOBAL_FLAG = 256;
133+
134+
135+
}

src/main/org/bson/BSONEncoder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// BSONEncoder.java
2+
3+
package org.bson;
4+
5+
import org.bson.io.*;
6+
7+
/**
8+
* this is meant to be pooled or cached
9+
* there is some per instance memory for string conversion, etc...
10+
*/
11+
public class BSONEncoder {
12+
13+
public BSONEncoder(){
14+
15+
}
16+
17+
public void set( OutputBuffer out ){
18+
if ( _out != null )
19+
throw new IllegalStateException( "in the middle of something" );
20+
21+
_out = out;
22+
}
23+
24+
private OutputBuffer _out;
25+
}

src/test/com/mongodb/ByteTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ public void testEncodeDecode() {
305305
@Test(groups = {"basic"})
306306
public void testPatternFlags() {
307307
boolean threw = false;
308-
assertEquals( 0, Bytes.patternFlags( "" ) );
309-
assertEquals( "", Bytes.patternFlags( 0 ) );
308+
assertEquals( 0, Bytes.regexFlags( "" ) );
309+
assertEquals( "", Bytes.regexFlags( 0 ) );
310310

311311
try {
312-
Bytes.patternFlags( "f" );
312+
Bytes.regexFlags( "f" );
313313
}
314314
catch( RuntimeException e ) {
315315
threw = true;
@@ -318,7 +318,7 @@ public void testPatternFlags() {
318318
threw = false;
319319

320320
try {
321-
Bytes.patternFlags( 513 );
321+
Bytes.regexFlags( 513 );
322322
}
323323
catch( RuntimeException e ) {
324324
threw = true;
@@ -335,15 +335,15 @@ public void testPatternFlags() {
335335
Pattern.COMMENTS |
336336
256 );
337337

338-
String s = Bytes.patternFlags( lotsoflags.flags() );
338+
String s = Bytes.regexFlags( lotsoflags.flags() );
339339
char prev = s.charAt( 0 );
340340
for( int i=1; i<s.length(); i++ ) {
341341
char current = s.charAt( i );
342342
assertTrue( prev < current );
343343
prev = current;
344344
}
345345

346-
int check = Bytes.patternFlags( s );
346+
int check = Bytes.regexFlags( s );
347347
assertEquals( lotsoflags.flags(), check );
348348
}
349349

0 commit comments

Comments
 (0)