Skip to content
This repository was archived by the owner on Jul 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/addthis/bundle/core/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* multiple fields. Iterator returns a list of fields with bound values.
* Field iterators MUST obey the same constraints as the getFormat() call.
*/
public interface Bundle extends Iterable<BundleField>, BundleFormatted, BundleFactory {
public interface Bundle extends Iterable<BundleField>, BundleFormatted, BundleFactory, IndexBundle {

/**
* gets the specified field as a String value.
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/addthis/bundle/core/BundleComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.Comparator;

import com.addthis.bundle.value.ValueType;
import com.addthis.codec.Codec;
import com.addthis.bundle.value.ValueObject;

Expand Down Expand Up @@ -75,9 +76,9 @@ public int compare(final Bundle left, final Bundle right) {
if (vRight == null) {
return asc ? -1 : 1;
}
final ValueObject.TYPE tLeft = vLeft.getObjectType();
final ValueObject.TYPE tRight = vRight.getObjectType();
if (tLeft != tRight || tLeft == ValueObject.TYPE.STRING) {
final ValueType tLeft = vLeft.getObjectType();
final ValueType tRight = vRight.getObjectType();
if (tLeft != tRight || tLeft == ValueType.STRING) {
final int val = vLeft.toString().compareTo(vRight.toString());
if (val != 0) {
return asc ? val : -val;
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/addthis/bundle/core/BundleFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* unless the underlying format has changed. A change in format object
* is used by many stream clients to perform re-binding.
*/
public interface BundleFormat extends Iterable<BundleField> {
public interface BundleFormat extends Iterable<BundleField>, IndexFormat {

/**
* returns a field for the given name. creates a new field
Expand All @@ -41,11 +41,6 @@ public interface BundleFormat extends Iterable<BundleField> {
*/
public BundleField getField(int pos);

/**
* @return numnber of bound fields in this format
*/
public int getFieldCount();

/**
* @return an object that changes only when the format changes
*/
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/addthis/bundle/core/IndexBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.bundle.core;

import com.addthis.bundle.value.ValueObject;


/**
* Represents one "line" or "packet" of data having multiple fields.
*/
public interface IndexBundle {

/**
* gets the field at the given index as a ValueObject
*/
public ValueObject value(int index);

/**
* sets the field as the given index to a ValueObject.
*/
public void value(int index, ValueObject value);

/**
* returns the Format for the bundle. It has metadata
* about the columns in this bundle which varies by implementation.
*
* returns the format for this bundle. maps to known fields. the
* object returned by this call MUST NOT change unless the underlying
* stream or object has changed in an incompatible way. the order
* of fields presented by this object MUST remain consistent.
*/
public IndexFormat format();
}
25 changes: 25 additions & 0 deletions src/main/java/com/addthis/bundle/core/IndexFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.bundle.core;


/**
*/
public interface IndexFormat {

/**
* @return numnber of fields in this format
*/
public int getFieldCount();
}
51 changes: 51 additions & 0 deletions src/main/java/com/addthis/bundle/core/index/ArrayBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.bundle.core.index;

import java.util.Arrays;

import com.addthis.bundle.core.IndexBundle;
import com.addthis.bundle.core.IndexFormat;
import com.addthis.bundle.value.ValueObject;

public class ArrayBundle implements IndexBundle {

final ValueObject[] valueArray;
final IndexFormat format;

public ArrayBundle(IndexFormat format) {
this.format = format;
this.valueArray = new ValueObject[format.getFieldCount()];
}

@Override
public String toString() {
return Arrays.toString(valueArray);
}

@Override
public ValueObject value(int index) {
return valueArray[index];
}

@Override
public void value(int index, ValueObject value) {
valueArray[index] = value;
}

@Override
public IndexFormat format() {
return format;
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/addthis/bundle/core/index/ExtIndexBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.bundle.core.index;

import com.addthis.bundle.core.IndexBundle;
import com.addthis.bundle.value.ValueArray;
import com.addthis.bundle.value.ValueCustom;
import com.addthis.bundle.value.ValueMap;
import com.addthis.bundle.value.ValueObject;
import com.addthis.bundle.value.ValueString;

import io.netty.buffer.ByteBuf;

/**
* Non-standard (not implemented or needed by 'Bundle') methods
* specific to a sub-set of IndexBundle implementations that provide
* convenience or alternate interfacing.
*
* These get/ set methods make it possible for implementations to
* be more flexible with their storage options, potentially avoid
* some wrapper object overhead, use different object types without
* interacting with standard bundles, and hopefully allow more concise
* and explicit downstream code.
*/
public interface ExtIndexBundle extends IndexBundle, Iterable<ValueObject> {

ValueString string(int index);
void string(int index, ValueString string);

ValueArray array(int index);
void array(int index, ValueArray array);

ValueMap map(int index);
void map(int index, ValueMap map);

ByteBuf buf(int index);
void buf(int index, ByteBuf buf);

<T extends ValueCustom> T custom(int index);
void custom(int index, ValueCustom custom);

long integer(int index);
void integer(int index, long integer);

double floating(int index);
void floating(int index, double floating);

@Override
public ExtIndexFormat format();
}
42 changes: 42 additions & 0 deletions src/main/java/com/addthis/bundle/core/index/ExtIndexFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.bundle.core.index;


import com.addthis.bundle.core.IndexFormat;
import com.addthis.bundle.value.ValueType;

/**
* Non-standard (not implemented or needed by 'Bundle') methods
* specific to a sub-set of IndexBundle implementations that provide
* convenience or alternate interfacing.
*/
public interface ExtIndexFormat extends IndexFormat {

/** returns label at column index. Labels are for human use only */
CharSequence label(int index);
/** set label at column index. Labels are for human use only */
void label(int index, CharSequence label);

/** returns value type expected for a given column index. For human and computer use */
ValueType type(int index);
/** sets the type expected for a given column index. For human and computer use */
void type(int index, ValueType type);

/** create a new bundle based on this format */
ExtIndexBundle newBundle();

/** alias for getFieldCount */
int size();
}
106 changes: 106 additions & 0 deletions src/main/java/com/addthis/bundle/core/index/ExtendedArrayBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.addthis.bundle.core.index;

import java.util.Iterator;

import com.addthis.bundle.value.ValueArray;
import com.addthis.bundle.value.ValueCustom;
import com.addthis.bundle.value.ValueFactory;
import com.addthis.bundle.value.ValueMap;
import com.addthis.bundle.value.ValueObject;
import com.addthis.bundle.value.ValueString;

import com.google.common.collect.Iterators;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class ExtendedArrayBundle extends ArrayBundle implements ExtIndexBundle {

public ExtendedArrayBundle(ExtendedArrayFormat format) {
super(format);
}

@Override
public ValueString string(int index) {
return value(index).asString();
}

@Override
public void string(int index, ValueString string) {
value(index, string);
}

@Override
public ValueArray array(int index) {
return value(index).asArray();
}

@Override
public void array(int index, ValueArray array) {
value(index, array);
}

@Override
public ValueMap map(int index) {
return value(index).asMap();
}

@Override
public void map(int index, ValueMap map) {
value(index, map);
}

@Override
public ByteBuf buf(int index) {
return Unpooled.wrappedBuffer(value(index).asBytes().getBytes());
}

@Override
public void buf(int index, ByteBuf buf) {
if (buf.hasArray()) {
value(index, ValueFactory.create(buf.array()));
} else {
throw new UnsupportedOperationException("only heap bufs supported just now due to ValueBytes legacy");
}
}

@Override
public <T extends ValueCustom> T custom(int index) {
return (T) value(index).asCustom();
}

@Override
public void custom(int index, ValueCustom custom) {
value(index, custom);
}

@Override
public long integer(int index) {
return value(index).asLong().getLong();
}

@Override
public void integer(int index, long integer) {
value(index, ValueFactory.create(integer));
}

@Override
public double floating(int index) {
return value(index).asDouble().getDouble();
}

@Override
public void floating(int index, double floating) {
value(index, ValueFactory.create(floating));
}

@Override
public ExtIndexFormat format() {
return (ExtIndexFormat) super.format();
}

@Override
public Iterator<ValueObject> iterator() {
return Iterators.forArray(valueArray);
}
}
Loading