| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Copyright 2006-2008 Sun Microsystems, Inc. |
| | | * Copyright 2006-2009 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.replication.server; |
| | | |
| | | import static org.opends.messages.ReplicationMessages.*; |
| | | import static org.opends.server.loggers.ErrorLogger.logError; |
| | | |
| | | import java.util.SortedMap; |
| | | import java.util.TreeMap; |
| | | |
| | | import org.opends.messages.Message; |
| | | import org.opends.server.replication.common.ChangeNumber; |
| | | import org.opends.server.replication.protocol.UpdateMsg; |
| | | |
| | |
| | | { |
| | | private SortedMap<ChangeNumber, UpdateMsg> map = |
| | | new TreeMap<ChangeNumber, UpdateMsg>(); |
| | | private final Object lock = new Object(); |
| | | |
| | | // The total number of bytes for all the message in the queue. |
| | | private int bytesCount = 0; |
| | |
| | | */ |
| | | public UpdateMsg first() |
| | | { |
| | | return map.get(map.firstKey()); |
| | | synchronized (lock) |
| | | { |
| | | return map.get(map.firstKey()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public UpdateMsg last() |
| | | { |
| | | return map.get(map.lastKey()); |
| | | synchronized (lock) |
| | | { |
| | | return map.get(map.lastKey()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public int count() |
| | | { |
| | | return map.size(); |
| | | synchronized (lock) |
| | | { |
| | | return map.size(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public int bytesCount() |
| | | { |
| | | return bytesCount; |
| | | synchronized (lock) |
| | | { |
| | | return bytesCount; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public boolean isEmpty() |
| | | { |
| | | return map.isEmpty(); |
| | | synchronized (lock) |
| | | { |
| | | return map.isEmpty(); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | public void add(UpdateMsg update) |
| | | { |
| | | map.put(update.getChangeNumber(), update); |
| | | bytesCount += update.size(); |
| | | synchronized (lock) |
| | | { |
| | | map.put(update.getChangeNumber(), update); |
| | | bytesCount += update.size(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public UpdateMsg removeFirst() |
| | | { |
| | | UpdateMsg msg = map.get(map.firstKey()); |
| | | map.remove(msg.getChangeNumber()); |
| | | bytesCount -= msg.size(); |
| | | return msg; |
| | | synchronized (lock) |
| | | { |
| | | UpdateMsg update = map.get(map.firstKey()); |
| | | map.remove(update.getChangeNumber()); |
| | | bytesCount -= update.size(); |
| | | if ((map.size() == 0) && (bytesCount != 0)) |
| | | { |
| | | Message msg = ERR_BYTE_COUNT.get(); |
| | | logError(msg); |
| | | bytesCount = 0; |
| | | } |
| | | return update; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public boolean contains(UpdateMsg msg) |
| | | { |
| | | return map.containsKey(msg.getChangeNumber()); |
| | | synchronized (lock) |
| | | { |
| | | return map.containsKey(msg.getChangeNumber()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public void clear() |
| | | { |
| | | map.clear(); |
| | | bytesCount = 0; |
| | | synchronized (lock) |
| | | { |
| | | map.clear(); |
| | | bytesCount = 0; |
| | | } |
| | | } |
| | | } |