blob: d1ada8f269b8adc7dfd5d882de369d7662e2b458 [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 Jampani3d76c942015-06-29 23:37:10 -070078 private void updateInternal(K key, MapValue<V> newValue) {
Jonathan Hartca335e92015-03-05 10:34:32 -080079 byte[] keyBytes = serializer.encode(key);
Jonathan Hartca335e92015-03-05 10:34:32 -080080
81 items.compute(keyBytes, (k, existingBytes) -> {
Madan Jampani3d76c942015-06-29 23:37:10 -070082 MapValue<V> existing = existingBytes == null ? null :
Jonathan Hartca335e92015-03-05 10:34:32 -080083 serializer.decode(existingBytes);
Madan Jampani3d76c942015-06-29 23:37:10 -070084 if (existing == null || newValue.isNewerThan(existing)) {
85 return serializer.encode(newValue);
Jonathan Hartca335e92015-03-05 10:34:32 -080086 } else {
Madan Jampani3d76c942015-06-29 23:37:10 -070087 return existingBytes;
Jonathan Hartca335e92015-03-05 10:34:32 -080088 }
89 });
Jonathan Hartca335e92015-03-05 10:34:32 -080090 database.commit();
91 }
Jonathan Hartca335e92015-03-05 10:34:32 -080092}