blob: d561784ced0cda3f1815fd433e9480c588ed94f4 [file] [log] [blame]
Brian O'Connor520c0522014-11-23 23:50:47 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.store.trivial.impl;
17
18import org.apache.felix.scr.annotations.Component;
19import org.apache.felix.scr.annotations.Service;
20import org.onlab.onos.core.IdBlock;
21import org.onlab.onos.core.IdBlockStore;
22
23import java.util.Map;
24import java.util.concurrent.ConcurrentHashMap;
25import java.util.concurrent.atomic.AtomicLong;
26
27/**
28 * Simple implementation of id block store.
29 */
30@Component(immediate = true)
31@Service
32public class SimpleIdBlockStore implements IdBlockStore {
33
34 private static final long DEFAULT_BLOCK_SIZE = 1000L;
35
36 private final Map<String, AtomicLong> topicBlocks = new ConcurrentHashMap<>();
37
38 @Override
39 public synchronized IdBlock getIdBlock(String topic) {
40 AtomicLong blockGenerator = topicBlocks.get(topic);
41 if (blockGenerator == null) {
42 blockGenerator = new AtomicLong(0);
43 topicBlocks.put(topic, blockGenerator);
44 }
45 Long blockBase = blockGenerator.getAndAdd(DEFAULT_BLOCK_SIZE);
46 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
47 }
48}