All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.EventUtil Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client;

import Alachisoft.NCache.Caching.EventCacheEntry;
import Alachisoft.NCache.Common.Caching.UserBinaryObject;

import com.alachisoft.ncache.client.internal.caching.CacheItemRemovedReason;
import com.alachisoft.ncache.client.internal.caching.EventTypeInternal;
import com.alachisoft.ncache.client.internal.messaging.MessageCollectionEventItem;
import com.alachisoft.ncache.client.internal.messaging.MessageEventItem;
import com.alachisoft.ncache.common.protobuf.EventIdProtocol.EventId;
import com.alachisoft.ncache.runtime.CacheItemPriority;
import com.alachisoft.ncache.runtime.caching.MessageEventArgs;
import com.alachisoft.ncache.runtime.events.EventType;
import com.google.protobuf.ByteString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

public final class EventUtil {

    public static Alachisoft.NCache.Caching.EventId convertToEventID(com.alachisoft.ncache.common.protobuf.BulkEventItemResponseProtocol.BulkEventItemResponse eventItem, Alachisoft.NCache.Persistence.EventType eventType) {
        Alachisoft.NCache.Caching.EventId eventId;
        eventId = eventId = new Alachisoft.NCache.Caching.EventId();
        EventId varEventId;

        switch (eventType) {
            case ITEM_ADDED_EVENT:
                varEventId = eventItem.getItemAddedEvent().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.ITEM_ADDED_EVENT);
                break;

            case ITEM_UPDATED_EVENT:
                varEventId = eventItem.getItemUpdatedEvent().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.ITEM_UPDATED_EVENT);
                break;

            case ITEM_UPDATED_CALLBACK:
                varEventId = eventItem.getItemUpdatedCallback().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.ITEM_UPDATED_CALLBACK);
                break;
            case CQ_CALLBACK:

                break;
            case ITEM_REMOVED_CALLBACK:
                varEventId = eventItem.getItemRemoveCallback().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.ITEM_REMOVED_CALLBACK);
                break;

            case ITEM_REMOVED_EVENT:
                varEventId = eventItem.getItemRemovedEvent().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.ITEM_REMOVED_EVENT);
                break;

            case CACHE_CLEARED_EVENT:
                varEventId = eventItem.getCacheClearedEvent().getEventId();
                eventId.setEventUniqueID(varEventId.getEventUniqueId());
                eventId.setEventCounter(varEventId.getEventCounter());
                eventId.setOperationCounter(varEventId.getOperationCounter());
                eventId.setEventType(Alachisoft.NCache.Persistence.EventType.CACHE_CLEARED_EVENT);
                break;
        }
        return eventId;
    }

    public static EventCacheItem convertToEventEntry(com.alachisoft.ncache.common.protobuf.EventCacheItemCommandProtocol.EventCacheItem cacheItem) {
        byte[] objectValue = null;

        if (cacheItem == null) {
            return null;
        }

        EventCacheItem eventCacheItem = new EventCacheItem();
        eventCacheItem.setCacheItemPriority(CacheItemPriority.forValue(cacheItem.getPriority()));
        eventCacheItem.setCacheItemVersion(new CacheItemVersion(cacheItem.getItemVersion()));
        eventCacheItem.setGroup(tangible.DotNetToJavaStringHelper.isNullOrEmpty(cacheItem.getGroup()) ? null : cacheItem.getGroup());
        eventCacheItem.setReSyncExpiredItems(cacheItem.getResyncExpiredItems());

        List cacheItemValueList = cacheItem.getValueList();

        List bite = new ArrayList();

        for (ByteString byteString : cacheItemValueList) {
            bite.add(byteString.toByteArray());
        }

        if (cacheItemValueList != null && !cacheItemValueList.isEmpty()) {
            UserBinaryObject ubObject = UserBinaryObject.createUserBinaryObject(Arrays.asList((Object[]) bite.toArray()));
            objectValue = ubObject.getFullObject();
            eventCacheItem.setValue(objectValue);
        }

        return eventCacheItem;

    }


    /**
     * For Inproc only
     *
     * @param entry
     * @return
     */
    public static EventCacheItem convertToItem(EventCacheEntry entry) {
        byte[] objectValue = null;

        EventCacheItem item = new EventCacheItem();

        item.setCacheItemPriority(CacheItemPriority.forValue(entry.getPriority()));

        CacheItemVersion version = new CacheItemVersion(entry.getVersion());
        version.setVersion(entry.getVersion());

        item.setCacheItemVersion(version);
        item.setGroup(entry.getGroup());
        item.setReSyncExpiredItems(entry.getReSyncExpiredItems());
        if (entry.getValue() != null) {
            UserBinaryObject ubObject = (UserBinaryObject) ((entry.getValue() instanceof UserBinaryObject) ? entry.getValue() : null);
            if (ubObject != null) {
                objectValue = ubObject.getFullObject();
                item.setValue(objectValue);
            } else {
                item.setValue(entry.getValue());
            }
        }
        return item;
    }

    public static CacheItemRemovedReason convertToCIRemoveReason(CacheItemRemovedReason reason) {
        switch (reason) {
            case DependencyChanged:
                return CacheItemRemovedReason.DependencyChanged;
            case DependencyInvalid:
                return CacheItemRemovedReason.DependencyChanged;
            case Expired:
                return CacheItemRemovedReason.Expired;
            case Removed:
                return CacheItemRemovedReason.Removed;
            case Underused:
                return CacheItemRemovedReason.Underused;
            default:
                return CacheItemRemovedReason.Underused;
        }
    }


    public static EventType getEventType(EnumSet eventEnumSet) {

        if (eventEnumSet.contains(EventType.ItemAdded)) {
            return EventType.ItemAdded;
        } else if (eventEnumSet.contains(EventType.ItemRemoved)) {
            return EventType.ItemRemoved;
        } else if (eventEnumSet.contains(EventType.ItemUpdated)) {
            return EventType.ItemUpdated;
        }

        return EventType.ItemAdded;
    }

    public static EventType getEventType(EventTypeInternal eventType) {
        EventType type = EventType.ItemAdded;;
        if (eventType == EventTypeInternal.ItemAdded) //ItemAdded not supported Yet
        {
            type = EventType.ItemAdded;
        } else if (eventType == EventTypeInternal.ItemUpdated) //ItemAdded not supported Yet
        {
            type = EventType.ItemUpdated;
        } else if (eventType == EventTypeInternal.ItemRemoved) //ItemAdded not supported Yet
        {
            type = EventType.ItemRemoved;
        }
        return type;
    }

    public static EventTypeInternal getEventTypeInternal(EnumSet eventType) {
        EventTypeInternal eventTypeInternal = EventTypeInternal.None;
        for (EventType type : EventType.values()) {
            if (type == EventType.ItemAdded) //ItemAdded not supported Yet
            {
                eventTypeInternal = EventTypeInternal.ItemAdded;
            } else if (type == EventType.ItemUpdated) //ItemAdded not supported Yet
            {
                eventTypeInternal = EventTypeInternal.ItemUpdated;
            } else if (type == EventType.ItemRemoved) //ItemAdded not supported Yet
            {
                eventTypeInternal = EventTypeInternal.ItemRemoved;
            }
        }
        return eventTypeInternal;
    }

    public static EnumSet getEventTypeInternalEnumSet(EnumSet eventTypes) {
        if (eventTypes == null)
            return EnumSet.of(EventTypeInternal.None);
        EnumSet eventTypeInternalEnumSet = EnumSet.noneOf(EventTypeInternal.class);
        for (EventType type : eventTypes) {
            eventTypeInternalEnumSet.add(EventTypeInternal.forValue(type.getValue()));
        }
        return eventTypeInternalEnumSet;
    }

    public static EventTypeInternal getEventTypeInternal(EventType eventType) {
        EventTypeInternal eventTypeInternal = EventTypeInternal.None;
        if (eventType == EventType.ItemAdded) //ItemAdded not supported Yet
        {
            eventTypeInternal = EventTypeInternal.ItemAdded;
        } else if (eventType == EventType.ItemUpdated) //ItemAdded not supported Yet
        {
            eventTypeInternal = EventTypeInternal.ItemUpdated;
        } else if (eventType == EventType.ItemRemoved) //ItemAdded not supported Yet
        {
            eventTypeInternal = EventTypeInternal.ItemRemoved;
        }
        return eventTypeInternal;
    }

    public static MessageItemType getMessageItemType(MessageItem messageItem) {
        if (messageItem != null) {
            if (messageItem.getPayload() != null) {
                if (messageItem.getPayload() instanceof MessageEventItem[]) {
                    return MessageItemType.MessageEventItems;
                } else if (messageItem.getPayload() instanceof MessageEventItem) {
                    return MessageItemType.MessageEventItem;
                } else if (messageItem.getPayload() instanceof MessageCollectionEventItem) {
                    return MessageItemType.MessageEventCollection;
                } else if (messageItem.getPayload() instanceof MessageCollectionEventItem[]) {
                    return MessageItemType.MessageEventCollections;
                }

                return MessageItemType.MessageItems;
            }
        }
        return MessageItemType.MessageItems;

    }

    public static MessageItemType getMessageItemType(MessageEventArgs messageEventArgs)
    {
        if (messageEventArgs != null)
        {
            if (messageEventArgs.getMessage() != null && messageEventArgs.getMessage().getPayload() != null)
            {
                if (messageEventArgs.getMessage().getPayload() instanceof MessageEventItem[])
                {
                    return MessageItemType.MessageEventItems;
                }
                else if (messageEventArgs.getMessage().getPayload() instanceof MessageEventItem)
                {
                    return MessageItemType.MessageEventItem;
                }
                else if (messageEventArgs.getMessage().getPayload() instanceof MessageCollectionEventItem)
                {
                    return MessageItemType.MessageEventCollection;
                }
                else if (messageEventArgs.getMessage().getPayload() instanceof MessageCollectionEventItem[])
                {
                    return MessageItemType.MessageEventCollections;
                }

                return MessageItemType.MessageItems;
            }
        }
        return MessageItemType.MessageItems;

    }

    public enum MessageItemType {
        MessageEventItem,
        MessageEventItems,
        MessageItems,
        MessageEventCollection,
        MessageEventCollections;

        public static MessageItemType forValue(int value) {
            return values()[value];
        }

        public int getValue() {
            return this.ordinal();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy