private void restoreIncrementalSnapshot(PartitionIdStateHolder partitionIdStateHolder,
                                            Map<String, Map<Long, Map<IncrementalSnapshotInfo,
                                                    byte[]>>> incrementalStateByTime) {
        if (incrementalStateByTime != null) {
            String id = null;
            State state = null;
            StateHolder stateHolder = null;
            Map<String, Object> deserializedStateMap = null;
            try {
                for (Iterator<Map.Entry<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>> iterator =
                     incrementalStateByTime.entrySet().iterator(); iterator.hasNext(); ) {
                    Map.Entry<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>> incrementalStateByTimeEntry =
                            iterator.next();
                    iterator.remove();
                    for (Iterator<Map.Entry<Long, Map<IncrementalSnapshotInfo, byte[]>>> partitionGroupByKeyIterator =
                         incrementalStateByTimeEntry.getValue().entrySet().iterator();
                         partitionGroupByKeyIterator.hasNext(); ) {
                        Map.Entry<Long, Map<IncrementalSnapshotInfo, byte[]>>
                                partitionGroupByKeyStateByTimeEntry = partitionGroupByKeyIterator.next();
                        partitionGroupByKeyIterator.remove();
                        for (Iterator<Map.Entry<IncrementalSnapshotInfo, byte[]>> iterator1 =
                             partitionGroupByKeyStateByTimeEntry.getValue().entrySet().iterator();
                             iterator1.hasNext(); ) {
                            Map.Entry<IncrementalSnapshotInfo, byte[]> incrementalStateByInfoEntry = iterator1.next();
                            iterator1.remove();
                            IncrementalSnapshotInfo incrementalSnapshotInfo = incrementalStateByInfoEntry.getKey();
                            Map<String, Object> singleIncrementSnapshot = (Map<String, Object>)
                                    ByteSerializer.byteToObject(
                                            incrementalStateByInfoEntry.getValue(),
                                            siddhiAppContext);
                            if (singleIncrementSnapshot != null) {
                                if (!incrementalSnapshotInfo.getId().equals(id)) {
                                    if (id != null) {
                                        state.restore(deserializedStateMap);
                                        SiddhiAppContext.startPartitionFlow(id);
                                        try {
                                            stateHolder.returnState(state);
                                        } finally {
                                            SiddhiAppContext.stopPartitionFlow();
                                        }
                                        id = null;
                                        state = null;
                                        stateHolder = null;
                                        deserializedStateMap = null;
                                    }
                                    ElementStateHolder elementStateHolder = partitionIdStateHolder.
                                            queryStateHolderMap.get(incrementalSnapshotInfo.getQueryName());
                                    if (elementStateHolder == null) {
                                        continue;
                                    }
                                    stateHolder = elementStateHolder.elementHolderMap.get(
                                            incrementalSnapshotInfo.getElementId());
                                    if (stateHolder == null) {
                                        continue;
                                    }
                                    String partitionKey = null;
                                    String groupByKey = null;
                                    String[] keys = incrementalSnapshotInfo.getPartitionGroupByKey().split("--");
                                    if (keys.length == 2) {
                                        if (!keys[0].equals("null")) {
                                            partitionKey = keys[0];
                                        }
                                        if (!keys[1].equals("null")) {
                                            groupByKey = keys[1];
                                        }
                                    }
                                    SiddhiAppContext.startPartitionFlow(partitionKey);
                                    SiddhiAppContext.startGroupByFlow(groupByKey);
                                    try {
                                        state = stateHolder.getState();
                                    } finally {
                                        SiddhiAppContext.stopGroupByFlow();
                                        SiddhiAppContext.stopPartitionFlow();
                                    }
                                    if (state != null) {
                                        id = incrementalSnapshotInfo.getId();
                                        deserializedStateMap = new HashMap<>();
                                    }
                                }
                                if (state != null) {
                                    for (Map.Entry<String, Object> singleIncrementSnapshotEntry :
                                            singleIncrementSnapshot.entrySet()) {
                                        if (singleIncrementSnapshotEntry.getValue() instanceof Snapshot) {
                                            Snapshot snapshot = (Snapshot)
                                                    singleIncrementSnapshotEntry.getValue();
                                            SnapshotStateList snapshotStateList = (SnapshotStateList)
                                                    deserializedStateMap.computeIfAbsent(
                                                            singleIncrementSnapshotEntry.getKey(),
                                                            k -> new SnapshotStateList());
                                            if (snapshot.isIncrementalSnapshot()) {
                                                snapshotStateList.putSnapshotState(
                                                        partitionGroupByKeyStateByTimeEntry.getKey(),
                                                        snapshot);
                                            } else {
                                                snapshotStateList.getSnapshotStates().clear();
                                                snapshotStateList.putSnapshotState(
                                                        partitionGroupByKeyStateByTimeEntry.getKey(),
                                                        snapshot);
                                            }
                                        } else {
                                            deserializedStateMap.put(singleIncrementSnapshotEntry.getKey(),
                                                    singleIncrementSnapshotEntry.getValue());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (id != null) {
                        state.restore(deserializedStateMap);
                        SiddhiAppContext.startPartitionFlow(id);
                        try {
                            stateHolder.returnState(state);
                        } finally {
                            SiddhiAppContext.stopPartitionFlow();
                        }
                        id = null;
                        state = null;
                        stateHolder = null;
                        deserializedStateMap = null;
                    }
                }
            } finally {
                if (id != null && stateHolder != null && state != null) {
                    SiddhiAppContext.startPartitionFlow(id);
                    try {
                        stateHolder.returnState(state);
                    } finally {
                        SiddhiAppContext.stopPartitionFlow();
                    }
                    id = null;
                    state = null;
                    stateHolder = null;
                }
            }

        }
    }
Service level implementation to take/restore snapshots of processing elements.
Memory   : PartitionId + QueryName + ElementId + PartitionGroupByKey + Item
Snapshot : PartitionId + PartitionGroupByKey + QueryName + ElementId + Item
modules/siddhi-core/src/main/java/io/siddhi/core/util/snapshot/SnapshotService.java