Search in sources :

Example 1 with Freezable

use of water.Freezable in project h2o-3 by h2oai.

the class IcedHashMapGeneric method write_impl.

// This comment is stolen from water.parser.Categorical:
//
// Since this is a *concurrent* hashtable, writing it whilst its being
// updated is tricky.  If the table is NOT being updated, then all is written
// as expected.  If the table IS being updated we only promise to write the
// Keys that existed at the time the table write began.  If elements are
// being deleted, they may be written anyways.  If the Values are changing, a
// random Value is written.
public final AutoBuffer write_impl(AutoBuffer ab) {
    _write_lock = true;
    try {
        for (Entry<K, V> e : map().entrySet()) {
            K key = e.getKey();
            assert key != null;
            V val = e.getValue();
            assert val != null;
            int mode = 0;
            if (key instanceof String) {
                if (val instanceof String) {
                    mode = 1;
                } else if (val instanceof Freezable) {
                    mode = 3;
                } else if (val instanceof Freezable[]) {
                    mode = 5;
                } else if (val instanceof Integer) {
                    mode = 7;
                } else {
                    throw new IllegalArgumentException("unsupported value class " + val.getClass().getName());
                }
            } else {
                if (!(key instanceof Iced))
                    throw new IllegalArgumentException("key must be String or Freezable, got " + key.getClass().getName());
                if (val instanceof String) {
                    mode = 2;
                } else if (val instanceof Freezable) {
                    mode = 4;
                } else if (val instanceof Freezable[]) {
                    mode = 6;
                } else if (val instanceof Integer) {
                    mode = 8;
                } else {
                    throw new IllegalArgumentException("unsupported value class " + val.getClass().getName());
                }
            }
            // Type of hashmap being serialized
            ab.put1(mode);
            // put key
            if (isStringKey(mode))
                ab.putStr((String) key);
            else
                ab.put((Freezable) key);
            // put value
            if (isStringVal(mode))
                ab.putStr((String) val);
            else if (isFreezeVal(mode))
                ab.put((Freezable) val);
            else if (isFArrayVal(mode)) {
                ab.put4(((Freezable[]) val).length);
                for (Freezable v : (Freezable[]) val) ab.put(v);
            } else if (isIntegrVal(mode))
                ab.put4((Integer) val);
            else
                throw H2O.fail();
        }
        ab.put1(-1);
    } catch (Throwable t) {
        System.err.println("Iced hash map serialization failed! " + t.toString() + ", msg = " + t.getMessage());
        t.printStackTrace();
        throw H2O.fail("Iced hash map serialization failed!" + t.toString() + ", msg = " + t.getMessage());
    } finally {
        _write_lock = false;
    }
    return ab;
}
Also used : Freezable(water.Freezable) Iced(water.Iced)

Aggregations

Freezable (water.Freezable)1 Iced (water.Iced)1