All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.indeed.mph.serializers.SmartHashSerializer Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.indeed.mph.serializers;

import com.indeed.mph.LinearDiophantineEquation;
import com.indeed.mph.Parseable;
import com.indeed.mph.TableMeta;
import it.unimi.dsi.sux4j.mph.AbstractHashFunction;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;

/**
 * An Integer serializer which compresses its values to a smaller
 * range using a pre-existing minimal perfect hash.  Conversion occurs
 * only on parsing, as a convenience for translating mapreduce output.
 *
 * @author alexs
 */
public class SmartHashSerializer extends SmartIntegerSerializer {
    private static final LinearDiophantineEquation FOUR =
        LinearDiophantineEquation.constantValue(4L);
    private static final long serialVersionUID = 2009124557;
    private final AbstractHashFunction mph;
    private final Parseable parser;
    private final boolean ignoreErrors;

    public SmartHashSerializer(final AbstractHashFunction mph, final Parseable parser, final boolean ignoreErrors) {
        this.mph = mph;
        this.parser = parser;
        this.ignoreErrors = ignoreErrors;
    }

    public SmartHashSerializer(final AbstractHashFunction mph, final Parseable parser) {
        this(mph, parser, false);
    }

    public SmartHashSerializer(final AbstractHashFunction mph) {
        this(mph, null);
    }

    public SmartHashSerializer(final String metaPath, final Parseable parser, final boolean ignoreErrors) throws IOException {
        this(TableMeta.load(new File(metaPath)).getMph(), parser, ignoreErrors);
    }

    public SmartHashSerializer(final String metaPath, final Parseable parser) throws IOException {
        this(metaPath, parser, false);
    }

    public SmartHashSerializer(final String metaPath) throws IOException {
        this(metaPath, null);
    }

    @Override
    public Integer parseFromString(final String s) throws IOException {
        final Long value = parser == null ? Long.parseLong(s) : parser.parseFromString(s);
        final Long hash = value == null ? null : mph.get(value);
        if (!ignoreErrors && (hash == null || hash < 0)) {
            throw new IOException("bad key: " + s);
        }
        return hash == null ? null : hash.intValue();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy