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

org.ethereum.datasource.inmem.HashMapDBSimple Maven / Gradle / Ivy

Go to download

Java implementation of the Ethereum protocol adapted to use for Hedera Smart Contract Service

The newest version!
/*
 * Copyright (c) [2016] [  ]
 * This file is part of the ethereumJ library.
 *
 * The ethereumJ library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The ethereumJ library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the ethereumJ library. If not, see .
 */
package org.ethereum.datasource.inmem;

import org.ethereum.datasource.DbSettings;
import org.ethereum.datasource.DbSource;
import org.ethereum.util.ByteArrayMap;
import org.ethereum.util.FastByteComparisons;

import java.util.Map;
import java.util.Set;

/**
 * Created by Anton Nashatyrev on 12.10.2016.
 */
public class HashMapDBSimple implements DbSource {

    protected final Map storage;

    public HashMapDBSimple() {
        this(new ByteArrayMap());
    }

    public HashMapDBSimple(ByteArrayMap storage) {
        this.storage = storage;
    }

    @Override
    public void put(byte[] key, V val) {
        if (val == null) {
            delete(key);
        } else {
            storage.put(key, val);
        }
    }

    @Override
    public V get(byte[] key) {
        return storage.get(key);
    }

    @Override
    public void delete(byte[] key) {
        storage.remove(key);
    }

    @Override
    public boolean flush() {
        return true;
    }

    @Override
    public void setName(String name) {}

    @Override
    public String getName() {
        return "in-memory";
    }

    @Override
    public void init() {}

    @Override
    public void init(DbSettings settings) {}

    @Override
    public boolean isAlive() {
        return true;
    }

    @Override
    public void close() {}

    @Override
    public Set keys() {
        return getStorage().keySet();
    }

    @Override
    public void reset() {
        storage.clear();
    }

    @Override
    public V prefixLookup(byte[] key, int prefixBytes) {

        for (Map.Entry e : storage.entrySet())
            if (FastByteComparisons.compareTo(key, 0, prefixBytes, e.getKey(), 0, prefixBytes) == 0) {
                return e.getValue();
            }

        return null;
    }

    @Override
    public void updateBatch(Map rows) {
        for (Map.Entry entry : rows.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    public Map getStorage() {
        return storage;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy