blob: e8b69b0c15dfa977dad5896d020479a99b66fc85 [file] [log] [blame]
Brian O'Connor520c0522014-11-23 23:50:47 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Brian O'Connor520c0522014-11-23 23:50:47 -08003 *
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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Brian O'Connor520c0522014-11-23 23:50:47 -080017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.core.IdBlock;
19import org.onosproject.core.IdBlockStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.osgi.service.component.annotations.Component;
Brian O'Connor520c0522014-11-23 23:50:47 -080021
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.atomic.AtomicLong;
25
26/**
27 * Simple implementation of id block store.
28 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029@Component(immediate = true, service = IdBlockStore.class)
Brian O'Connor520c0522014-11-23 23:50:47 -080030public class SimpleIdBlockStore implements IdBlockStore {
31
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080032 private static final long DEFAULT_BLOCK_SIZE = 0x1000L;
Brian O'Connor520c0522014-11-23 23:50:47 -080033
34 private final Map<String, AtomicLong> topicBlocks = new ConcurrentHashMap<>();
35
36 @Override
37 public synchronized IdBlock getIdBlock(String topic) {
38 AtomicLong blockGenerator = topicBlocks.get(topic);
39 if (blockGenerator == null) {
40 blockGenerator = new AtomicLong(0);
41 topicBlocks.put(topic, blockGenerator);
42 }
43 Long blockBase = blockGenerator.getAndAdd(DEFAULT_BLOCK_SIZE);
44 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
45 }
46}