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.omid.tso; 19 20 import org.slf4j.Logger; 21 import org.slf4j.LoggerFactory; 22 23 /** 24 * Stores the mapping between a particular cell id and the commit timestamp 25 * of the last transaction that changed it. 26 * 27 * The mapping is implemented as a long -> long mapping, using a single long []. 28 * For a map of size N we create an array of size 2*N and store the keys 29 * on even indexes and values on odd indexes. The rationale is that we want 30 * queries to be fast and touch as least memory regions as possible. 31 * 32 * Each time an entry is removed, the caller updates the largestDeletedTimestamp 33 * if the entry's commit timestamp is greater than this value. 34 * 35 * TODO: improve garbage collection, right now an entry is picked at random 36 * (by hash) which could cause the eviction of a very recent timestamp 37 */ 38 39 class CommitHashMap { 40 41 private static final Logger LOG = LoggerFactory.getLogger(CommitHashMap.class); 42 43 private final LongCache cellIdToCommitMap; 44 45 /** 46 * Constructs a new, empty hashtable with a default size of 1000 47 */ 48 public CommitHashMap() { 49 this(1000); 50 } 51 52 /** 53 * Constructs a new, empty hashtable with the specified size 54 * 55 * @param size 56 * the initial size of the hashtable. 57 * @throws IllegalArgumentException 58 * if the size is less than zero. 59 */ 60 public CommitHashMap(int size) { 61 if (size < 0) { 62 throw new IllegalArgumentException("Illegal size: " + size); 63 } 64 65 this.cellIdToCommitMap = new LongCache(size, 32); 66 LOG.info("CellId -> CommitTS map created with [{}] buckets (32 elems/bucket)", size); 67 } 68 69 public long getLatestWriteForCell(long hash) { 70 return cellIdToCommitMap.get(hash); 71 } 72 73 public long putLatestWriteForCell(long hash, long commitTimestamp) { 74 return cellIdToCommitMap.set(hash, commitTimestamp); 75 } 76 }