Skip to content

Commit

Permalink
MONDRIAN: [MONDRIAN-860] Makes the BitKey serializable and adds a new…
Browse files Browse the repository at this point in the history
… bit operation. Gives the DenseSegmentBody class a better toString() representation. Modifies the SegmentCacheTest so that it de-registers itself on tearDown().

[git-p4: depot-paths = "//open/mondrian/": change = 14245]
  • Loading branch information
lucboudreau committed May 3, 2011
1 parent 4cd2107 commit 6029fc3
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 72 deletions.
28 changes: 28 additions & 0 deletions src/main/mondrian/olap/Util.java
Expand Up @@ -24,6 +24,9 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
Expand Down Expand Up @@ -169,6 +172,31 @@ public static byte[] checksumSha256(String source) {
return algorithm.digest();
}

/**
* Creates an {@link ExecutorService} object.
* @param maxNbThreads Maximum number of concurrent
* threads.
* @param name The name of the threads.
* @return An executor service preconfigured.
*/
public static ExecutorService getExecutorService(
final int maxNbThreads,
final String name)
{
return Executors.newFixedThreadPool(
maxNbThreads,
new ThreadFactory() {
public Thread newThread(Runnable r) {
final Thread thread =
Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
thread.setName(name);
return thread;
}
}
);
}

/**
* Encodes string for MDX (escapes ] as ]] inside a name).
*/
Expand Down
139 changes: 133 additions & 6 deletions src/main/mondrian/rolap/BitKey.java
Expand Up @@ -13,6 +13,7 @@

package mondrian.rolap;

import java.io.Serializable;
import java.util.BitSet;
import java.util.Iterator;

Expand All @@ -38,7 +39,9 @@
* @author Richard M. Emberson
* @version $Id$
*/
public interface BitKey extends Comparable<BitKey>, Iterable<Integer> {
public interface BitKey
extends Serializable, Comparable<BitKey>, Iterable<Integer>
{
/**
* The BitKey with no bits set.
*/
Expand Down Expand Up @@ -89,6 +92,13 @@ public interface BitKey extends Comparable<BitKey>, Iterable<Integer> {
*/
BitKey or(BitKey bitKey);

/**
* XOr the parameter <code>BitKey</code> with <code>this</code>.
*
* @param bitKey Bit key
*/
BitKey orNot(BitKey bitKey);

/**
* Returns the boolean AND of this bitkey and the given bitkey.
*
Expand Down Expand Up @@ -176,17 +186,33 @@ public abstract class Factory {
* @param size Number of bits in key
*/
public static BitKey makeBitKey(int size) {
return makeBitKey(size, false);
}

/**
* Creates a {@link BitKey} with a capacity for a given number of bits.
* @param size Number of bits in key
* @param init The default value of all bits.
*/
public static BitKey makeBitKey(int size, boolean init) {
if (size < 0) {
String msg = "Negative size \"" + size + "\" not allowed";
throw new IllegalArgumentException(msg);
}
final BitKey bk;
if (size < 64) {
return new BitKey.Small();
bk = new BitKey.Small();
} else if (size < 128) {
return new BitKey.Mid128();
bk = new BitKey.Mid128();
} else {
return new BitKey.Big(size);
bk = new BitKey.Big(size);
}
if (init) {
for (int i = 0; i < size; i++) {
bk.set(i, init);
}
}
return bk;
}

/**
Expand All @@ -208,7 +234,7 @@ public static BitKey makeBitKey(BitSet bitSet) {
* Abstract implementation of {@link BitKey}.
*/
abstract class AbstractBitKey implements BitKey {

private static final long serialVersionUID = -2942302671676103450L;
// chunk is a long, which has 64 bits
protected static final int ChunkBitCount = 6;
protected static final int Mask = 63;
Expand Down Expand Up @@ -412,6 +438,7 @@ static int compareUnsigned(long i1, long i2) {
* Implementation of {@link BitKey} for bit counts less than 64.
*/
public class Small extends AbstractBitKey {
private static final long serialVersionUID = -7891880560056571197L;
private long bits;

/**
Expand Down Expand Up @@ -458,6 +485,10 @@ private void or(long bits) {
this.bits |= bits;
}

private void orNot(long bits) {
this.bits ^= bits;
}

private void and(long bits) {
this.bits &= bits;
}
Expand Down Expand Up @@ -485,6 +516,29 @@ public BitKey or(BitKey bitKey) {
throw createException(bitKey);
}

public BitKey orNot(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small other = (BitKey.Small) bitKey;
final BitKey.Small bk = (BitKey.Small) copy();
bk.orNot(other.bits);
return bk;

} else if (bitKey instanceof BitKey.Mid128) {
final BitKey.Mid128 other = (BitKey.Mid128) bitKey;
final BitKey.Mid128 bk = (BitKey.Mid128) other.copy();
bk.orNot(this.bits, 0);
return bk;

} else if (bitKey instanceof BitKey.Big) {
final BitKey.Big other = (BitKey.Big) bitKey;
final BitKey.Big bk = (BitKey.Big) other.copy();
bk.orNot(this.bits);
return bk;
}

throw createException(bitKey);
}

public BitKey and(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small other = (BitKey.Small) bitKey;
Expand Down Expand Up @@ -750,6 +804,7 @@ public boolean isEmpty() {
* Implementation of {@link BitKey} good for sizes less than 128.
*/
public class Mid128 extends AbstractBitKey {
private static final long serialVersionUID = -8409143207943258659L;
private long bits0;
private long bits1;

Expand Down Expand Up @@ -808,6 +863,11 @@ private void or(long bits0, long bits1) {
this.bits1 |= bits1;
}

private void orNot(long bits0, long bits1) {
this.bits0 ^= bits0;
this.bits1 ^= bits1;
}

private void and(long bits0, long bits1) {
this.bits0 &= bits0;
this.bits1 &= bits1;
Expand Down Expand Up @@ -836,6 +896,29 @@ public BitKey or(BitKey bitKey) {
throw createException(bitKey);
}

public BitKey orNot(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small other = (BitKey.Small) bitKey;
final BitKey.Mid128 bk = (BitKey.Mid128) copy();
bk.orNot(other.bits, 0);
return bk;

} else if (bitKey instanceof BitKey.Mid128) {
final BitKey.Mid128 other = (BitKey.Mid128) bitKey;
final BitKey.Mid128 bk = (BitKey.Mid128) copy();
bk.orNot(other.bits0, other.bits1);
return bk;

} else if (bitKey instanceof BitKey.Big) {
final BitKey.Big other = (BitKey.Big) bitKey;
final BitKey.Big bk = (BitKey.Big) other.copy();
bk.orNot(this.bits0, this.bits1);
return bk;
}

throw createException(bitKey);
}

public BitKey and(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small other = (BitKey.Small) bitKey;
Expand Down Expand Up @@ -1159,7 +1242,7 @@ int compareToBig(Big that) {
* {@link java.util.BitSet}, but does not require dynamic resizing.
*/
public class Big extends AbstractBitKey {

private static final long serialVersionUID = -3715282769845236295L;
private long[] bits;

private Big(int size) {
Expand Down Expand Up @@ -1229,6 +1312,21 @@ private void or(long[] bits) {
}
}

private void orNot(long bits0) {
this.bits[0] ^= bits0;
}

private void orNot(long bits0, long bits1) {
this.bits[0] ^= bits0;
this.bits[1] ^= bits1;
}

private void orNot(long[] bits) {
for (int i = 0; i < bits.length; i++) {
this.bits[i] ^= bits[i];
}
}

private void and(long[] bits) {
int length = Math.min(bits.length, this.bits.length);
for (int i = 0; i < length; i++) {
Expand Down Expand Up @@ -1268,6 +1366,35 @@ public BitKey or(BitKey bitKey) {
throw createException(bitKey);
}

public BitKey orNot(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small other = (BitKey.Small) bitKey;
final BitKey.Big bk = (BitKey.Big) copy();
bk.orNot(other.bits);
return bk;

} else if (bitKey instanceof BitKey.Mid128) {
final BitKey.Mid128 other = (BitKey.Mid128) bitKey;
final BitKey.Big bk = (BitKey.Big) copy();
bk.orNot(other.bits0, other.bits1);
return bk;

} else if (bitKey instanceof BitKey.Big) {
final BitKey.Big other = (BitKey.Big) bitKey;
if (other.size() > size()) {
final BitKey.Big bk = (BitKey.Big) other.copy();
bk.orNot(bits);
return bk;
} else {
final BitKey.Big bk = (BitKey.Big) copy();
bk.orNot(other.bits);
return bk;
}
}

throw createException(bitKey);
}

public BitKey and(BitKey bitKey) {
if (bitKey instanceof BitKey.Small) {
final BitKey.Small bk = (BitKey.Small) bitKey.copy();
Expand Down
24 changes: 16 additions & 8 deletions src/main/mondrian/rolap/agg/DenseDoubleSegmentBody.java
Expand Up @@ -49,14 +49,22 @@ public SegmentDataset createSegmentDataset(Segment segment) {

@Override
public String toString() {
return "DenseDoubleSegmentBody(size=" + size
+ ", data=" + Arrays.toString(data)
+ ", nullIndicators=" + nullIndicators
+ ", axisValueSets=" + Arrays.toString(getAxisValueSets())
+ ", nullAxisFlags=" + Arrays.toString(getNullAxisFlags())
+ ", aVS[0]=" + getAxisValueSets()[0].getClass()
+ ", aVS[0][0]=" + getAxisValueSets()[0].iterator().next().getClass()
+ ")";
StringBuilder sb = new StringBuilder();
sb.append("DenseDoubleSegmentBody(size=" + size);
sb.append(", data=");
sb.append(Arrays.toString(data));
sb.append(", nullIndicators=" + nullIndicators);
sb.append(", axisValueSets=" + Arrays.toString(getAxisValueSets()));
sb.append(", nullAxisFlags=" + Arrays.toString(getNullAxisFlags()));
if (getAxisValueSets().length > 0) {
if (getAxisValueSets()[0].iterator().hasNext()) {
sb.append(", aVS[0]=" + getAxisValueSets()[0].getClass());
sb.append(", aVS[0][0]=");
sb.append(getAxisValueSets()[0].iterator().next().getClass());
}
}
sb.append(")");
return sb.toString();
}
}

Expand Down

0 comments on commit 6029fc3

Please sign in to comment.