Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Component; |
| 19 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.core.IdBlock; |
| 21 | import org.onosproject.core.IdBlockStore; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | import java.util.concurrent.atomic.AtomicLong; |
| 26 | |
| 27 | /** |
| 28 | * Simple implementation of id block store. |
| 29 | */ |
| 30 | @Component(immediate = true) |
| 31 | @Service |
| 32 | public class SimpleIdBlockStore implements IdBlockStore { |
| 33 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 34 | private static final long DEFAULT_BLOCK_SIZE = 0x1000L; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 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 | } |