add WritableSupplier and IndexedMultivalue

This commit is contained in:
Xavier Léauté 2015-04-13 22:04:46 -07:00
parent 1abb9cce7c
commit 5c23679238
4 changed files with 127 additions and 4 deletions

View File

@ -0,0 +1,26 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you 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 io.druid.segment.data;
import java.io.Closeable;
public interface IndexedMultivalue<T extends IndexedInts> extends Indexed<T>, Closeable
{
}

View File

@ -29,7 +29,7 @@ import java.util.Iterator;
/**
*/
public class VSizeIndexed implements Indexed<VSizeIndexedInts>
public class VSizeIndexed implements IndexedMultivalue<IndexedInts>
{
private static final byte version = 0x1;
@ -140,7 +140,7 @@ public class VSizeIndexed implements Indexed<VSizeIndexedInts>
}
@Override
public int indexOf(VSizeIndexedInts value)
public int indexOf(IndexedInts value)
{
throw new UnsupportedOperationException("Reverse lookup not allowed.");
}
@ -176,8 +176,44 @@ public class VSizeIndexed implements Indexed<VSizeIndexedInts>
}
@Override
public Iterator<VSizeIndexedInts> iterator()
public Iterator<IndexedInts> iterator()
{
return IndexedIterable.create(this).iterator();
}
@Override
public void close() throws IOException
{
// no-op
}
public WritableSupplier<IndexedMultivalue<IndexedInts>> asWritableSupplier() {
return new VSizeIndexedSupplier(this);
}
public static class VSizeIndexedSupplier implements WritableSupplier<IndexedMultivalue<IndexedInts>> {
final VSizeIndexed delegate;
public VSizeIndexedSupplier(VSizeIndexed delegate) {
this.delegate = delegate;
}
@Override
public long getSerializedSize()
{
return delegate.getSerializedSize();
}
@Override
public void writeToChannel(WritableByteChannel channel) throws IOException
{
delegate.writeToChannel(channel);
}
@Override
public IndexedMultivalue<IndexedInts> get()
{
return delegate;
}
}
}

View File

@ -151,7 +151,7 @@ public class VSizeIndexedInts implements IndexedInts, Comparable<VSizeIndexedInt
return numBytes;
}
public int getSerializedSize()
public long getSerializedSize()
{
// version, numBytes, size, remaining
return 1 + 1 + 4 + buffer.remaining();
@ -201,4 +201,34 @@ public class VSizeIndexedInts implements IndexedInts, Comparable<VSizeIndexedInt
{
}
public WritableSupplier<IndexedInts> asWritableSupplier() {
return new VSizeIndexedIntsSupplier(this);
}
public static class VSizeIndexedIntsSupplier implements WritableSupplier<IndexedInts> {
final VSizeIndexedInts delegate;
public VSizeIndexedIntsSupplier(VSizeIndexedInts delegate) {
this.delegate = delegate;
}
@Override
public long getSerializedSize()
{
return delegate.getSerializedSize();
}
@Override
public void writeToChannel(WritableByteChannel channel) throws IOException
{
delegate.writeToChannel(channel);
}
@Override
public IndexedInts get()
{
return delegate;
}
}
}

View File

@ -0,0 +1,31 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you 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 io.druid.segment.data;
import com.google.common.base.Supplier;
import java.io.IOException;
import java.nio.channels.WritableByteChannel;
public interface WritableSupplier<T> extends Supplier<T>
{
long getSerializedSize();
void writeToChannel(WritableByteChannel channel) throws IOException;
}