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 19 package org.apache.giraph.mapping; 20 21 import org.apache.hadoop.io.LongWritable; 22 23 /** 24 * MappingStoreOps implementation used to embed target information into 25 * vertex id. Stores information in the higher order bits of the long id 26 */ 27 public class DefaultEmbeddedLongByteOps extends AbstractLongByteOps { 28 /** Bit mask for first 9 bits in a long */ 29 private static final long MASK = ((long) 0x1FF) << 55; 30 /** Inverse of MASK */ 31 private static final long IMASK = ~ MASK; 32 33 /** 34 * Default constructor (do not use) 35 */ 36 public DefaultEmbeddedLongByteOps() { 37 } 38 39 @Override 40 public boolean hasEmbedding() { 41 return true; 42 } 43 44 @Override 45 public void embedTargetInfo(LongWritable id) { 46 if ((id.get() & MASK) != 0) { 47 throw new IllegalStateException("Expected first 9 bits of long " + 48 " to be empty"); 49 } 50 byte target = mappingStore.getByteTarget(id); 51 // first bit = 0 & rest 8 bits set to target 52 // add 1 to distinguish between not set and assignment to worker-0 53 // (prefix bits = 0 can mean one of two things : 54 // no entry in the mapping, in which case target = -1, so -1 + 1 = 0 55 // vertex is created later during computation, so prefix bits are 0 anyway) 56 long maskValue = ((1L + target) & 0xFF) << 55; 57 id.set(id.get() | maskValue); 58 } 59 60 @Override 61 public void removeTargetInfo(LongWritable id) { 62 id.set(id.get() & IMASK); 63 } 64 65 @Override 66 public int getPartition(LongWritable id, int partitionCount, 67 int workerCount) { 68 // extract last 8 bits 69 // subtract 1 since added 1 during embedInfo (unset = -1) 70 byte target = (byte) (((id.get() >>> 55) & 0xFF) - 1); 71 return computePartition(id, partitionCount, workerCount, target); 72 } 73 }