Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rough Comparable support for BSONWritable by Sarthak Dudhara <sarthak…

….83@gmail.com>
  • Loading branch information...
commit 0ace17e7defdbb5f4b3f7098198d77f6ce52b645 1 parent 2a330ec
Brendan W. McAdams authored March 16, 2011
68  src/main/com/mongodb/hadoop/io/BSONWritable.java
@@ -20,17 +20,13 @@
20 20
 import java.io.*;
21 21
 import java.util.*;
22 22
 
23  
-import org.bson.*;
24  
-import org.bson.io.*;
25  
-
26 23
 import org.apache.commons.logging.*;
27  
-
28 24
 import org.apache.hadoop.io.*;
  25
+import org.bson.*;
  26
+import org.bson.io.*;
29 27
 
30 28
 @SuppressWarnings( "deprecation" )
31  
-public class BSONWritable implements BSONObject, Writable {
32  
-
33  
-    private static final Log log = LogFactory.getLog( BSONWritable.class );
  29
+public class BSONWritable implements BSONObject, WritableComparable {
34 30
 
35 31
     /**
36 32
      * Constructs a new instance.
@@ -50,7 +46,6 @@ public BSONWritable(BSONWritable other) {
50 46
         copy( other );
51 47
     }
52 48
 
53  
-
54 49
     /**
55 50
      * Constructs a new instance around an existing BSONObject
56 51
      */
@@ -167,6 +162,23 @@ public void readFields( DataInput in ) throws IOException{
167 162
         byte[] buf = new byte[dataLen];
168 163
         in.readFully( buf );
169 164
         dec.decode( buf, cb );
  165
+        _doc = (BSONObject) cb.get();
  166
+        log.info( "Decoded a BSON Object: " + _doc );
  167
+    }
  168
+
  169
+    /**
  170
+     * {@inheritDoc}
  171
+     */
  172
+    @Override
  173
+    public String toString(){
  174
+        BSONEncoder enc = new BSONEncoder();
  175
+        BasicOutputBuffer buf = new BasicOutputBuffer();
  176
+        enc.set( buf );
  177
+        enc.putObject( _doc );
  178
+        enc.done();
  179
+        String str = buf.asString();
  180
+        log.debug( "Output As String: '" + str + "'" );
  181
+        return str;
170 182
     }
171 183
 
172 184
     /** Used by child copy constructors. */
@@ -190,20 +202,34 @@ protected synchronized void copy( Writable other ){
190 202
         }
191 203
     }
192 204
 
193  
-    /**
194  
-     * {@inheritDoc}
195  
-     */
  205
+    public static class Comparator extends WritableComparator {
  206
+        public Comparator() {
  207
+            super( BSONWritable.class );
  208
+        }
  209
+
  210
+        public int compare( WritableComparable a , WritableComparable b ){
  211
+
  212
+            if ( a instanceof BSONWritable && b instanceof BSONWritable ) {
  213
+                return ( (BSONWritable) a )._doc.toString().compareTo( ( (BSONWritable) b )._doc.toString() );
  214
+            }
  215
+            else {
  216
+                return -1;
  217
+            }
  218
+        }
  219
+
  220
+    }
  221
+
  222
+    static { // register this comparator
  223
+        WritableComparator.define( BSONWritable.class, new Comparator() );
  224
+    }
  225
+
196 226
     @Override
197  
-    public String toString(){
198  
-        BSONEncoder enc = new BSONEncoder();
199  
-        BasicOutputBuffer buf = new BasicOutputBuffer();
200  
-        enc.set( buf );
201  
-        enc.putObject( _doc );
202  
-        enc.done();
203  
-        String str = buf.asString();
204  
-        log.debug( "Output As String: '" + str + "'" );
205  
-        return str;
  227
+    public int compareTo( Object o ){
  228
+        return new Comparator().compare( this, o );
206 229
     }
207 230
 
208  
-    final BSONObject _doc;
  231
+    BSONObject _doc;
  232
+
  233
+    private static final Log log = LogFactory.getLog( BSONWritable.class );
  234
+
209 235
 }

0 notes on commit 0ace17e

Please sign in to comment.
Something went wrong with that request. Please try again.