@Override public void fromBinary(final byte[] bytes) { final ByteBuffer buf = ByteBuffer.wrap(bytes); final int numSfcs = buf.getInt(); final int numDimensions = buf.getInt(); final int mappingSize = buf.getInt(); maxEstimatedDuplicateIds = buf.getLong(); maxEstimatedDuplicateIdsBigInteger = BigInteger.valueOf(maxEstimatedDuplicateIds); orderedSfcs = new SpaceFillingCurve[numSfcs]; baseDefinitions = new NumericDimensionDefinition[numDimensions]; for (int i = 0; i < numSfcs; i++) { final byte[] sfc = new byte[buf.getInt()]; buf.get(sfc); orderedSfcs[i] = PersistenceUtils.fromBinary(sfc, SpaceFillingCurve.class); } for (int i = 0; i < numDimensions; i++) { final byte[] dim = new byte[buf.getInt()]; buf.get(dim); baseDefinitions[i] = PersistenceUtils.fromBinary(dim, NumericDimensionDefinition.class); } final Builder<Integer, Byte> bimapBuilder = ImmutableBiMap.builder(); for (int i = 0; i < mappingSize; i++) { bimapBuilder.put(Byte.valueOf(buf.get()).intValue(), buf.get()); } orderedSfcIndexToTierId = bimapBuilder.build(); }
@Override public byte[] toBinary() { int byteBufferLength = 20 + (2 * orderedSfcIndexToTierId.size()); final List<byte[]> orderedSfcBinaries = new ArrayList<byte[]>(orderedSfcs.length); final List<byte[]> dimensionBinaries = new ArrayList<byte[]>(baseDefinitions.length); for (final SpaceFillingCurve sfc : orderedSfcs) { final byte[] sfcBinary = PersistenceUtils.toBinary(sfc); byteBufferLength += (4 + sfcBinary.length); orderedSfcBinaries.add(sfcBinary); } for (final NumericDimensionDefinition dimension : baseDefinitions) { final byte[] dimensionBinary = PersistenceUtils.toBinary(dimension); byteBufferLength += (4 + dimensionBinary.length); dimensionBinaries.add(dimensionBinary); } final ByteBuffer buf = ByteBuffer.allocate(byteBufferLength); buf.putInt(orderedSfcs.length); buf.putInt(baseDefinitions.length); buf.putInt(orderedSfcIndexToTierId.size()); buf.putLong(maxEstimatedDuplicateIds); for (final byte[] sfcBinary : orderedSfcBinaries) { buf.putInt(sfcBinary.length); buf.put(sfcBinary); } for (final byte[] dimensionBinary : dimensionBinaries) { buf.putInt(dimensionBinary.length); buf.put(dimensionBinary); } for (final Entry<Integer, Byte> entry : orderedSfcIndexToTierId.entrySet()) { buf.put(entry.getKey().byteValue()); buf.put(entry.getValue()); } return buf.array(); }
@SuppressWarnings("unchecked") protected T entryToValue(final Entry<Key, Value> entry) { final T result = (T) PersistenceUtils.fromBinary(entry.getValue().get(), Persistable.class); if (result != null) { addObjectToCache(getPrimaryId(result), getSecondaryId(result), result); } return result; }
@Override public byte[] toBinary() { final byte[] indexStrategyBytes = PersistenceUtils.toBinary(indexStrategy); final byte[] coordinateRangesBinary = new ArrayOfArrays(coordinateRanges).toBinary(); final ByteBuffer buf = ByteBuffer.allocate(coordinateRangesBinary.length + indexStrategyBytes.length + 4); buf.putInt(indexStrategyBytes.length); buf.put(indexStrategyBytes); buf.put(coordinateRangesBinary); return buf.array(); }
@Override protected void addObject(final ByteArrayId id, final ByteArrayId secondaryId, final T object) { addObjectToCache(id, secondaryId, object); try { final Writer writer = accumuloOperations.createWriter(getTablename(), true); synchronized (this) { if (!iteratorsAttached) { iteratorsAttached = true; final IteratorConfig[] configs = getIteratorConfig(); if ((configs != null) && (configs.length > 0)) { accumuloOperations.attachIterators(getTablename(), true, true, true, null, configs); } } } final Mutation mutation = new Mutation(new Text(id.getBytes())); final Text cf = getSafeText(getColumnFamily()); final Text cq = getSafeText(getColumnQualifier(object)); final byte[] visibility = getVisibility(object); if (visibility != null) { mutation.put( cf, cq, new ColumnVisibility(visibility), new Value(PersistenceUtils.toBinary(object))); } else { mutation.put(cf, cq, new Value(PersistenceUtils.toBinary(object))); } writer.write(mutation); try { writer.close(); } catch (final IOException e) { LOGGER.warn("Unable to close metadata writer", e); } } catch (final TableNotFoundException e) { LOGGER.error("Unable add object", e); } }
@Override public void fromBinary(final byte[] bytes) { final ByteBuffer buf = ByteBuffer.wrap(bytes); try { final int indexStrategyLength = buf.getInt(); final byte[] indexStrategyBytes = new byte[indexStrategyLength]; buf.get(indexStrategyBytes); indexStrategy = PersistenceUtils.fromBinary(indexStrategyBytes, NumericIndexStrategy.class); final byte[] coordRangeBytes = new byte[bytes.length - indexStrategyLength - 4]; buf.get(coordRangeBytes); final ArrayOfArrays arrays = new ArrayOfArrays(); arrays.fromBinary(coordRangeBytes); coordinateRanges = arrays.getCoordinateArrays(); rangeCache = RangeLookupFactory.createMultiRangeLookup(coordinateRanges); } catch (final Exception e) { LOGGER.warn("Unable to read parameters", e); } }
@Override public void init( final SortedKeyValueIterator<Key, Value> source, final Map<String, String> options, final IteratorEnvironment env) throws IOException { this.source = source; if (options == null) { throw new IllegalArgumentException( "Arguments must be set for " + NumericIndexStrategyFilterIterator.class.getName()); } try { if (options.containsKey(INDEX_STRATEGY_KEY)) { final String idxStrategyStr = options.get(INDEX_STRATEGY_KEY); final byte[] idxStrategyBytes = ByteArrayUtils.byteArrayFromString(idxStrategyStr); indexStrategy = PersistenceUtils.fromBinary(idxStrategyBytes, NumericIndexStrategy.class); } else { throw new IllegalArgumentException( "'" + INDEX_STRATEGY_KEY + "' must be set for " + NumericIndexStrategyFilterIterator.class.getName()); } if (options.containsKey(COORDINATE_RANGE_KEY)) { final String coordRangeStr = options.get(COORDINATE_RANGE_KEY); final byte[] coordRangeBytes = ByteArrayUtils.byteArrayFromString(coordRangeStr); final ArrayOfArrays arrays = new ArrayOfArrays(); arrays.fromBinary(coordRangeBytes); rangeCache = RangeLookupFactory.createMultiRangeLookup(arrays.getCoordinateArrays()); } else { throw new IllegalArgumentException( "'" + COORDINATE_RANGE_KEY + "' must be set for " + NumericIndexStrategyFilterIterator.class.getName()); } } catch (final Exception e) { throw new IllegalArgumentException(e); } }