1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hdfs.shortcircuit;
19 
20 import org.apache.hadoop.classification.InterfaceAudience;
21 
22 import java.io.Closeable;
23 import java.nio.MappedByteBuffer;
24 
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 
28 /**
29  * A reference to a memory-mapped region used by an HDFS client.
30  */
31 @InterfaceAudience.Private
32 public class ClientMmap implements Closeable {
33   static final Log LOG = LogFactory.getLog(ClientMmap.class);
34 
35   /**
36    * A reference to the block replica which this mmap relates to.
37    */
38   private ShortCircuitReplica replica;
39 
40   /**
41    * The java ByteBuffer object.
42    */
43   private final MappedByteBuffer map;
44 
45   /**
46    * Whether or not this ClientMmap anchors the replica into memory while
47    * it exists.  Closing an anchored ClientMmap unanchors the replica.
48    */
49   private final boolean anchored;
50 
ClientMmap(ShortCircuitReplica replica, MappedByteBuffer map, boolean anchored)51   ClientMmap(ShortCircuitReplica replica, MappedByteBuffer map,
52       boolean anchored) {
53     this.replica = replica;
54     this.map = map;
55     this.anchored = anchored;
56   }
57 
58   /**
59    * Close the ClientMmap object.
60    */
61   @Override
close()62   public void close() {
63     if (replica != null) {
64       if (anchored) {
65         replica.removeNoChecksumAnchor();
66       }
67       replica.unref();
68     }
69     replica = null;
70   }
71 
getMappedByteBuffer()72   public MappedByteBuffer getMappedByteBuffer() {
73     return map;
74   }
75 }