PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/HDFSGatewayEventImpl.java

https://gitlab.com/kidaa/incubator-geode
Java | 173 lines | 113 code | 32 blank | 28 comment | 7 complexity | feb2e44ae0c3f6b50708683b7e937c4a MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * one or more patents listed at http://www.pivotal.io/patents.
  6. *========================================================================
  7. */
  8. package com.gemstone.gemfire.cache.hdfs.internal;
  9. import java.io.DataInput;
  10. import java.io.DataOutput;
  11. import java.io.IOException;
  12. import com.gemstone.gemfire.DataSerializer;
  13. import com.gemstone.gemfire.cache.EntryEvent;
  14. import com.gemstone.gemfire.internal.InternalDataSerializer;
  15. import com.gemstone.gemfire.internal.cache.EntryEventImpl;
  16. import com.gemstone.gemfire.internal.cache.EnumListenerEvent;
  17. import com.gemstone.gemfire.internal.cache.LocalRegion;
  18. import com.gemstone.gemfire.internal.cache.lru.Sizeable;
  19. import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerHelper;
  20. import com.gemstone.gemfire.internal.cache.versions.VersionTag;
  21. import com.gemstone.gemfire.internal.cache.wan.GatewaySenderEventImpl;
  22. import com.gemstone.gemfire.internal.offheap.StoredObject;
  23. import com.gemstone.gemfire.internal.offheap.annotations.Retained;
  24. import com.gemstone.gemfire.internal.util.BlobHelper;
  25. /**
  26. * Gateway event extended for HDFS functionality
  27. *
  28. * @author Hemant Bhanawat
  29. */
  30. public class HDFSGatewayEventImpl extends GatewaySenderEventImpl {
  31. private static final long serialVersionUID = 4642852957292192406L;
  32. protected transient boolean keyIsSerialized = false;
  33. protected byte[] serializedKey = null;
  34. protected VersionTag versionTag;
  35. public HDFSGatewayEventImpl(){
  36. }
  37. @Retained
  38. public HDFSGatewayEventImpl(EnumListenerEvent operation, EntryEvent event,
  39. Object substituteValue)
  40. throws IOException {
  41. super(operation, event, substituteValue);
  42. initializeHDFSGatewayEventObject(event);
  43. }
  44. @Retained
  45. public HDFSGatewayEventImpl(EnumListenerEvent operation, EntryEvent event,
  46. Object substituteValue, boolean initialize, int bucketId) throws IOException {
  47. super(operation, event,substituteValue, initialize, bucketId);
  48. initializeHDFSGatewayEventObject(event);
  49. }
  50. @Retained
  51. public HDFSGatewayEventImpl(EnumListenerEvent operation, EntryEvent event,
  52. Object substituteValue, boolean initialize) throws IOException {
  53. super(operation, event, substituteValue, initialize);
  54. initializeHDFSGatewayEventObject(event);
  55. }
  56. protected HDFSGatewayEventImpl(HDFSGatewayEventImpl offHeapEvent) {
  57. super(offHeapEvent);
  58. this.keyIsSerialized = offHeapEvent.keyIsSerialized;
  59. this.serializedKey = offHeapEvent.serializedKey;
  60. this.versionTag = offHeapEvent.versionTag;
  61. }
  62. @Override
  63. protected GatewaySenderEventImpl makeCopy() {
  64. return new HDFSGatewayEventImpl(this);
  65. }
  66. private void initializeHDFSGatewayEventObject(EntryEvent event)
  67. throws IOException {
  68. serializeKey();
  69. versionTag = ((EntryEventImpl)event).getVersionTag();
  70. if (versionTag != null && versionTag.getMemberID() == null) {
  71. versionTag.setMemberID(((LocalRegion)getRegion()).getVersionMember());
  72. }
  73. }
  74. private void serializeKey() throws IOException {
  75. if (!keyIsSerialized && isInitialized())
  76. {
  77. this.serializedKey = CacheServerHelper.serialize(this.key);
  78. keyIsSerialized = true;
  79. }
  80. }
  81. /**MergeGemXDHDFSToGFE This function needs to enabled if similar functionality is added to gatewaysendereventimpl*/
  82. /*@Override
  83. protected StoredObject obtainOffHeapValueBasedOnOp(EntryEventImpl event,
  84. boolean hasNonWanDispatcher) {
  85. return event.getOffHeapNewValue();
  86. }*/
  87. /**MergeGemXDHDFSToGFE This function needs to enabled if similar functionality is added to gatewaysendereventimpl*/
  88. /*@Override
  89. protected Object obtainHeapValueBasedOnOp(EntryEventImpl event,
  90. boolean hasNonWanDispatcher) {
  91. return event.getRawNewValue(shouldApplyDelta());
  92. }*/
  93. @Override
  94. protected boolean shouldApplyDelta() {
  95. return true;
  96. }
  97. @Override
  98. public void toData(DataOutput out) throws IOException {
  99. super.toData(out);
  100. DataSerializer.writeObject(this.versionTag, out);
  101. }
  102. @Override
  103. protected void serializeKey(DataOutput out) throws IOException {
  104. DataSerializer.writeByteArray((byte[])this.serializedKey, out);
  105. }
  106. @Override
  107. public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  108. super.fromData(in);
  109. this.versionTag = (VersionTag)DataSerializer.readObject(in);
  110. }
  111. @Override
  112. protected void deserializeKey(DataInput in) throws IOException,
  113. ClassNotFoundException {
  114. this.serializedKey = DataSerializer.readByteArray(in);
  115. this.key = BlobHelper.deserializeBlob(this.serializedKey,
  116. InternalDataSerializer.getVersionForDataStreamOrNull(in), null);
  117. keyIsSerialized = true;
  118. }
  119. @Override
  120. public int getDSFID() {
  121. return HDFS_GATEWAY_EVENT_IMPL;
  122. }
  123. public byte[] getSerializedKey() {
  124. return this.serializedKey;
  125. }
  126. public VersionTag getVersionTag() {
  127. return this.versionTag;
  128. }
  129. /**
  130. * Returns the size on HDFS of this event
  131. * @param writeOnly
  132. */
  133. public int getSizeOnHDFSInBytes(boolean writeOnly) {
  134. if (writeOnly)
  135. return UnsortedHDFSQueuePersistedEvent.getSizeInBytes(this.serializedKey.length,
  136. getSerializedValueSize(), this.versionTag);
  137. else
  138. return SortedHDFSQueuePersistedEvent.getSizeInBytes(this.serializedKey.length,
  139. getSerializedValueSize(), this.versionTag);
  140. }
  141. }