blob: f5ce47fcf3457e0810ab86ed99ae42312ca3eaae [file] [log] [blame]
Jonathan Hartca335e92015-03-05 10:34:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.store.ecmap;
18
Jonathan Hartca335e92015-03-05 10:34:32 -080019import org.mapdb.DB;
20import org.mapdb.DBMaker;
21import org.mapdb.Hasher;
22import org.mapdb.Serializer;
Jonathan Hartca335e92015-03-05 10:34:32 -080023import org.onosproject.store.serializers.KryoSerializer;
24
25import java.io.File;
26import java.util.Map;
27import java.util.concurrent.ExecutorService;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * MapDB based implementation of a persistent store.
33 */
34class MapDbPersistentStore<K, V> implements PersistentStore<K, V> {
35
36 private final ExecutorService executor;
37 private final KryoSerializer serializer;
38
39 private final DB database;
40
41 private final Map<byte[], byte[]> items;
Jonathan Hartca335e92015-03-05 10:34:32 -080042
43 /**
44 * Creates a new MapDB based persistent store.
45 *
46 * @param filename filename of the database on disk
47 * @param executor executor to use for tasks that write to the disk
48 * @param serializer serializer for keys and values
49 */
50 MapDbPersistentStore(String filename, ExecutorService executor,
51 KryoSerializer serializer) {
52 this.executor = checkNotNull(executor);
53 this.serializer = checkNotNull(serializer);
54
55 File databaseFile = new File(filename);
56
57 database = DBMaker.newFileDB(databaseFile).make();
58
59 items = database.createHashMap("items")
60 .keySerializer(Serializer.BYTE_ARRAY)
61 .valueSerializer(Serializer.BYTE_ARRAY)
62 .hasher(Hasher.BYTE_ARRAY)
63 .makeOrGet();
Jonathan Hartca335e92015-03-05 10:34:32 -080064 }
65
66 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -070067 public void readInto(Map<K, MapValue<V>> items) {
Jonathan Hartca335e92015-03-05 10:34:32 -080068 this.items.forEach((keyBytes, valueBytes) ->
69 items.put(serializer.decode(keyBytes),
Madan Jampani3d76c942015-06-29 23:37:10 -070070 serializer.decode(valueBytes)));
Jonathan Hartca335e92015-03-05 10:34:32 -080071 }
72
73 @Override
Madan Jampani3d76c942015-06-29 23:37:10 -070074 public void update(K key, MapValue<V> value) {
75 executor.submit(() -> updateInternal(key, value));
Jonathan Hartca335e92015-03-05 10:34:32 -080076 }
77
Madan Jampani4f1f4cd2015-07-08 23:05:35 -070078 @Override
79 public void remove(K key) {
80 executor.submit(() -> removeInternal(key));
81 }
82
Madan Jampani3d76c942015-06-29 23:37:10 -070083 private void updateInternal(K key, MapValue<V> newValue) {
Jonathan Hartca335e92015-03-05 10:34:32 -080084 byte[] keyBytes = serializer.encode(key);
Jonathan Hartca335e92015-03-05 10:34:32 -080085
86 items.compute(keyBytes, (k, existingBytes) -> {
Madan Jampani3d76c942015-06-29 23:37:10 -070087 MapValue<V> existing = existingBytes == null ? null :
Jonathan Hartca335e92015-03-05 10:34:32 -080088 serializer.decode(existingBytes);
Madan Jampani3d76c942015-06-29 23:37:10 -070089 if (existing == null || newValue.isNewerThan(existing)) {
90 return serializer.encode(newValue);
Jonathan Hartca335e92015-03-05 10:34:32 -080091 } else {
Madan Jampani3d76c942015-06-29 23:37:10 -070092 return existingBytes;
Jonathan Hartca335e92015-03-05 10:34:32 -080093 }
94 });
Jonathan Hartca335e92015-03-05 10:34:32 -080095 database.commit();
96 }
Madan Jampani4f1f4cd2015-07-08 23:05:35 -070097
98 private void removeInternal(K key) {
99 byte[] keyBytes = serializer.encode(key);
100 items.remove(keyBytes);
101 database.commit();
102 }
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700103}