Skip to content

Commit 60d714e

Browse files
hoffrocketagirbal
authored andcommitted
Fix (maybe? not yet tested) for n^2 query construction
1 parent 6771256 commit 60d714e

4 files changed

Lines changed: 146 additions & 4 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Copyright (C) 2010 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.util;
17+
18+
import java.util.Collection;
19+
import java.util.Iterator;
20+
import java.util.Set;
21+
22+
public class StringRangeSet implements Set<String> {
23+
private final int size;
24+
public StringRangeSet(int size){
25+
this.size = size;
26+
}
27+
public int size(){
28+
return size;
29+
}
30+
31+
public Iterator<String> iterator(){
32+
return new Iterator<String>(){
33+
int index = 0;
34+
@Override
35+
public boolean hasNext() {
36+
return index < size;
37+
}
38+
39+
@Override
40+
public String next() {
41+
return String.valueOf(index++);
42+
}
43+
44+
@Override
45+
public void remove() {
46+
throw new UnsupportedOperationException();
47+
}
48+
49+
};
50+
}
51+
@Override
52+
public boolean add(String e) {
53+
throw new UnsupportedOperationException();
54+
}
55+
@Override
56+
public boolean addAll(Collection<? extends String> c) {
57+
throw new UnsupportedOperationException();
58+
}
59+
@Override
60+
public void clear() {
61+
throw new UnsupportedOperationException();
62+
}
63+
64+
@Override
65+
public boolean contains(Object o) {
66+
int t = Integer.valueOf(String.valueOf(o));
67+
return t >= 0 && t < size;
68+
}
69+
@Override
70+
public boolean containsAll(Collection<?> c) {
71+
for (Object o : c) {
72+
if (!contains(o)){
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
@Override
79+
public boolean isEmpty() {
80+
return false;
81+
}
82+
@Override
83+
public boolean remove(Object o) {
84+
throw new UnsupportedOperationException();
85+
}
86+
@Override
87+
public boolean removeAll(Collection<?> c) {
88+
throw new UnsupportedOperationException();
89+
}
90+
@Override
91+
public boolean retainAll(Collection<?> c) {
92+
throw new UnsupportedOperationException();
93+
}
94+
@Override
95+
public Object[] toArray() {
96+
String[] array = new String[size()];
97+
for (int i = 0; i < size; i++){
98+
array[i] = String.valueOf(i+1);
99+
}
100+
return array;
101+
}
102+
@Override
103+
public <T> T[] toArray(T[] a) {
104+
throw new UnsupportedOperationException();
105+
}
106+
107+
}

src/main/org/bson/types/BasicBSONList.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.bson.types;
2020

2121
import com.mongodb.util.OrderedSet;
22+
import com.mongodb.util.StringRangeSet;
2223

2324
import org.bson.*;
2425

26+
2527
import java.util.*;
2628

2729
/**
@@ -132,10 +134,7 @@ public boolean containsField( String key ){
132134
}
133135

134136
public Set<String> keySet(){
135-
Set<String> s = new OrderedSet<String>();
136-
for ( int i=0; i<size(); i++ )
137-
s.add( String.valueOf( i ) );
138-
return s;
137+
return new StringRangeSet(size());
139138
}
140139

141140
public Map toMap() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (C) 2010 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.util;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
22+
import org.testng.annotations.Test;
23+
24+
public class StringRangeSetTest extends com.mongodb.util.TestCase {
25+
26+
@org.testng.annotations.Test
27+
public void rangeOfSizeProducesCorrectIteration(){
28+
StringRangeSet set = new StringRangeSet(3);
29+
assertEquals(Arrays.asList("0","1","2"), new ArrayList<String>(set));
30+
}
31+
32+
public static void main( String args[] ){
33+
(new StringRangeSetTest()).runConsole();
34+
}
35+
}

testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
<test name="main tests">
66
<classes>
7+
<class name="com.mongodb.util.StringRangeSetTest"/>
78
<class name="com.mongodb.util.SimplePoolTest"/>
89
<class name="com.mongodb.util.JSONTest"/>
910

0 commit comments

Comments
 (0)