Skip to content

Commit

Permalink
Added serialization for array of primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
jombie committed Nov 28, 2014
1 parent 2178f17 commit 5cdc0db
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=hsearch-core
version=1.0.3
version=1.0.4

company=Bizosys Technologies Pvt Ltd.

Expand Down
3 changes: 2 additions & 1 deletion classes/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/com
/com/
/org/
Binary file modified hsearch-core.jar
Binary file not shown.
Binary file modified src-hsearch-core.jar
Binary file not shown.
196 changes: 90 additions & 106 deletions src/java/com/bizosys/hsearch/byteutils/SortedBytesBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,132 +20,148 @@
package com.bizosys.hsearch.byteutils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.bizosys.hsearch.federate.BitSetWrapper;

public final class SortedBytesBoolean extends SortedBytesBase<Boolean>{

int size = 0;
BitSetWrapper bs = null;
public static final ISortedByte<Boolean> getInstance() {
return new SortedBytesBoolean();
}

List<Boolean> parsedBooleans = null;
public static final SortedBytesBoolean getInstanceBoolean() {
return new SortedBytesBoolean();
}

private SortedBytesBoolean() {
this.dataSize = -1;
}
}

@Override
public final ISortedByte<Boolean> parse(final byte[] bytes) throws IOException {
if ( null != this.parsedBooleans) {
this.parsedBooleans.clear();
this.parsedBooleans = null;
}
return super.parse(bytes);
if ( null != bytes) {
this.size = Storable.getInt(0, bytes);
bs = BitSetWrapper.valueOf(ByteBuffer.wrap(bytes, 4, bytes.length - 4));
return super.parse(bytes);
} else {
this.size = 0;
this.bs = null;
return this;
}
}

@Override
public final ISortedByte<Boolean> parse(final byte[] bytes, final int offset, final int length) throws IOException {
if ( null != this.parsedBooleans) {
this.parsedBooleans.clear();
this.parsedBooleans = null;
}
return super.parse(bytes, offset, length);
this.size = Storable.getInt(offset, bytes);
bs = BitSetWrapper.valueOf(ByteBuffer.wrap(bytes, offset + 4, length - 4));
return super.parse(bytes, offset, length);
}

@Override
public final byte[] toBytes(final Collection<Boolean> sortedCollection) throws IOException {
int available = sortedCollection.size();
int packed = available/8;
int remaining = available - packed * 8;

int neededBytes = packed;
if ( remaining > 0 ) neededBytes++;

neededBytes = neededBytes + 4; // How many

byte[] out = new byte[neededBytes];

bs = new BitSetWrapper(sortedCollection.size());
int index = -1;
for (Boolean val : sortedCollection) {
index++;
if ( val ) bs.set(index);
}

byte[] serBits = bs.toByteArray();
int serBitsLen = ( null == serBits) ? 0 : serBits.length;
byte[] serComplete = new byte[4 + serBitsLen];

System.arraycopy(Storable.putInt(available), 0, out, 0, 4);
int totalElements = sortedCollection.size();
System.arraycopy(Storable.putInt(totalElements), 0, serComplete, 0, 4);
System.arraycopy(serBits, 0, serComplete, 4, serBitsLen);

return serComplete;
}

public final byte[] toBytes(final List<boolean[]> sortedList, int sortedListAT) {


Iterator<Boolean> itr = sortedCollection.iterator();
for ( int i=0; i<packed; i++) {
out[4+i] = ByteUtil.fromBits(new boolean[] {
itr.next(), itr.next(), itr.next(), itr.next(),
itr.next(), itr.next(), itr.next(), itr.next()});
}
bs = new BitSetWrapper(sortedListAT);

if ( remaining > 0 ) {
boolean[] remainingBits = new boolean[8];
Arrays.fill(remainingBits, true);
for ( int j=0; j<remaining; j++) {
remainingBits[j] = itr.next();
int index = 0;
for (boolean[] aValA : sortedList) {
for ( boolean aVal : aValA ) {
bs.set(index, aVal);
index++;
if ( index >= sortedListAT) break;
}

out[4+packed] = ByteUtil.fromBits(remainingBits);
}
return out;
}

byte[] serBits = bs.toByteArray();
int serBitsLen = ( null == serBits) ? 0 : serBits.length;
byte[] serComplete = new byte[4 + serBitsLen];

System.arraycopy(Storable.putInt(sortedListAT), 0, serComplete, 0, 4);
System.arraycopy(serBits, 0, serComplete, 4, serBitsLen);

return serComplete;
}

@Override
public final int getSize() throws IOException {
if ( null == this.inputBytes ) return 0;
return Storable.getInt(this.offset, this.inputBytes);

return this.size;
}

@Override
public final void addAll(final Collection<Boolean> vals) throws IOException {
if ( null != this.parsedBooleans ) parse();
if ( null == this.parsedBooleans ) this.parsedBooleans = new ArrayList<Boolean>();
this.parsedBooleans.addAll(vals);

int index = 0;

if ( null == bs ) bs = new BitSetWrapper(vals.size());
else index = bs.size();

for (Boolean val : vals) {
if ( val ) bs.set(index);
index++;
}
}

@Override
public final Boolean getValueAt(final int pos) throws IndexOutOfBoundsException {
initialize();
return this.parsedBooleans.get(pos);
return bs.get(pos);
}

@Override
public final int getEqualToIndex(final Boolean matchNo) throws IOException {
initialize();

boolean matchNoL = matchNo.booleanValue();
int pos = -1;
for (boolean entity : this.parsedBooleans) {
pos++;
if ( matchNoL == entity) return pos;
}
return -1;
if ( matchNo ) return bs.nextSetBit(0);
else return bs.nextClearBit(0);
}

@Override
public final void getEqualToIndexes(final Boolean matchNo, final Collection<Integer> matchings) throws IOException {

initialize();

boolean matchNoL = matchNo.booleanValue();
int pos = -1;
for (boolean entity : this.parsedBooleans) {
pos++;
if ( matchNoL == entity) matchings.add(pos);
if( matchNo ){
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
matchings.add(i);
}
} else {
for (int i = bs.nextClearBit(0); i < this.size; i = bs.nextClearBit(i+1)) {
matchings.add(i);
}
}
}

@Override
public final void getNotEqualToIndexes(final Boolean matchNo, final Collection<Integer> matchings) throws IOException {

initialize();

boolean matchNoL = matchNo.booleanValue();
int pos = -1;
for (boolean entity : this.parsedBooleans) {
pos++;
if ( matchNoL != entity) matchings.add(pos);
if( matchNo ){
for (int i = bs.nextClearBit(0); i >= 0; i = bs.nextClearBit(i+1)) {
matchings.add(i);
}
} else {
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
matchings.add(i);
}
}
}

Expand All @@ -168,7 +184,7 @@ public void getLessThanIndexes(Boolean matchingNo, Collection<Integer> matchingP
public void getLessThanEqualToIndexes(Boolean matchingNo, Collection<Integer> matchingPos) throws IOException {
throw new RuntimeException("Not implemented");
}

@Override
public void getRangeIndexes(Boolean matchNoStart, Boolean matchNoEnd, Collection<Integer> matchings) throws IOException {
throw new RuntimeException("Not implemented");
Expand All @@ -178,41 +194,9 @@ public void getRangeIndexes(Boolean matchNoStart, Boolean matchNoEnd, Collection
public void getRangeIndexesInclusive(Boolean matchNoStart, Boolean matchNoEnd, Collection<Integer> matchings) throws IOException {
throw new RuntimeException("Not implemented");
}

private final void parse() throws IndexOutOfBoundsException {
if ( null == this.inputBytes) return;

int available = Storable.getInt(offset, this.inputBytes);
int packed = available/8;
int remaining = available - packed * 8;

this.parsedBooleans = new ArrayList<Boolean>(available);
int seq = 0;
for (int i=0; i<packed; i++) {
for (boolean val : Storable.byteToBits(this.inputBytes[offset + 4 + i])) {
seq++;
if ( available < seq) break;
this.parsedBooleans.add(val);
}
}

if ( remaining > 0 ) {
boolean[] x = Storable.byteToBits(this.inputBytes[offset + 4 + packed]);
for ( int i=0; i<remaining; i++) {
seq++;
if ( available < seq) break;
parsedBooleans.add(x[i]);
}
}
}

public final void initialize() throws IndexOutOfBoundsException {
if ( null == parsedBooleans ) parse();
if ( null == parsedBooleans ) throw new IndexOutOfBoundsException("SortedBytesBoolean - No data exists");
}

@Override
protected final int compare(final byte[] inputB, final int offset, final Boolean matchNo) {
protected int compare(byte[] inputB, int offset, Boolean matchNo) {
throw new RuntimeException("Not implemented");
}

Expand Down
25 changes: 25 additions & 0 deletions src/java/com/bizosys/hsearch/byteutils/SortedBytesChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
package com.bizosys.hsearch.byteutils;

import java.util.Collection;
import java.util.List;

public final class SortedBytesChar extends SortedBytesBase<Byte>{

public final static ISortedByte<Byte> getInstance() {
return new SortedBytesChar();
}

public final static SortedBytesChar getInstanceChar() {
return new SortedBytesChar();
}

private SortedBytesChar() {
this.dataSize = 1;
Expand All @@ -49,6 +54,26 @@ public final byte[] toBytes(final Collection<Byte> sortedList) {
return inputsB;
}

public final byte[] toBytes(final List<byte[]> sortedList, int sortedListAT) {

byte[] inputsB = new byte[sortedListAT];

int index = 0;
for (byte[] aValA : sortedList) {
if ( null == aValA) continue;
int valSize = aValA.length;

int nextSize = index + valSize;
if ( nextSize < sortedListAT) {
System.arraycopy(aValA, 0, inputsB, index, valSize);
index = nextSize;
} else {
System.arraycopy(aValA, 0, inputsB, index, sortedListAT - index);
}
}
return inputsB;
}

@Override
protected final int compare(final byte[] inputB, final int offset, final Byte matchNo) {
byte val = inputB[offset];
Expand Down
19 changes: 19 additions & 0 deletions src/java/com/bizosys/hsearch/byteutils/SortedBytesDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.bizosys.hsearch.byteutils;

import java.util.Collection;
import java.util.List;

public final class SortedBytesDouble extends SortedBytesBase<Double> {

Expand All @@ -28,6 +29,10 @@ public final static ISortedByte<Double> getInstance() {
return new SortedBytesDouble();
}

public final static SortedBytesDouble getInstanceDouble() {
return new SortedBytesDouble();
}

private SortedBytesDouble() {
this.dataSize = 8;
}
Expand All @@ -38,7 +43,21 @@ public final Double getValueAt(final int pos) {
return Storable.getDouble( this.offset + (pos * this.dataSize) , this.inputBytes);
}

public final byte[] toBytes(final List<double[]> sortedList, int sortedListAT) {

byte[] inputsB = new byte[sortedListAT * dataSize];

int index = 0;
for (double[] aValA : sortedList) {
for ( double aVal : aValA ) {
System.arraycopy(Storable.putLong(Double.doubleToLongBits(aVal)), 0, inputsB, index * dataSize, dataSize);
index++;
if ( index >= sortedListAT) break;
}
}
return inputsB;
}

@Override
public final byte[] toBytes(final Collection<Double> sortedList) {

Expand Down
Loading

0 comments on commit 5cdc0db

Please sign in to comment.