/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq 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 Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.network.p2p.storage.persistence;

import bisq.network.p2p.storage.P2PDataStorage;

import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.proto.persistable.PersistablePayload;
import bisq.common.storage.Storage;

import java.io.File;

import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * Handles persisted data which is stored in a map.
 *
 * @param <T>
 * @param <R>
 */
@Slf4j
public abstract class MapStoreService<T extends PersistableEnvelope, R extends PersistablePayload> extends StoreService<T> {


    ///////////////////////////////////////////////////////////////////////////////////////////
    // Constructor
    ///////////////////////////////////////////////////////////////////////////////////////////

    public MapStoreService(File storageDir, Storage<T> storage) {
        super(storageDir, storage);
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // API
    ///////////////////////////////////////////////////////////////////////////////////////////

    public abstract Map<P2PDataStorage.ByteArray, R> getMap();

    public abstract boolean canHandle(R payload);

    void put(P2PDataStorage.ByteArray hash, R payload) {
        getMap().put(hash, payload);
        persist();
    }

    R putIfAbsent(P2PDataStorage.ByteArray hash, R payload) {
        R previous = getMap().putIfAbsent(hash, payload);
        persist();
        return previous;
    }

    R remove(P2PDataStorage.ByteArray hash) {
        final R result = getMap().remove(hash);
        persist();
        return result;
    }

    boolean containsKey(P2PDataStorage.ByteArray hash) {
        return getMap().containsKey(hash);
    }
}

@@ -767,24 +767,75 @@
 taStorage;%0A%0A
+import bisq.common.persistence.PersistenceManager;%0A
 import bisq.
@@ -880,16 +880,16 @@
 velope;%0A
-
 import b
@@ -940,44 +940,8 @@
 oad;
-%0Aimport bisq.common.storage.Storage;
 %0A%0Aim
@@ -1503,26 +1503,48 @@
 ir, 
-Stor
+PersistenceMan
 age
+r
 %3CT%3E 
-stor
+persistenceMan
 age
+r
 ) %7B%0A
@@ -1569,23 +1569,34 @@
 ageDir, 
-stor
+persistenceMan
 age
+r
 );%0A    %7D
@@ -2011,39 +2011,50 @@
 yload);%0A        
-p
+requestP
 ersist
+ence
 ();%0A    %7D%0A%0A    R
@@ -2168,39 +2168,50 @@
 yload);%0A        
-p
+requestP
 ersist
+ence
 ();%0A        retu
@@ -2288,14 +2288,8 @@
     
-final 
 R re
@@ -2326,23 +2326,34 @@
         
-p
+requestP
 ersist
+ence
 ();%0A    

Use PersistenceManager for MapStoreService (not yet supported for subclasses)