PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/persistence/MRU.java

#
Java | 121 lines | 53 code | 11 blank | 57 comment | 15 complexity | 0d6466890adb9511f5201feed3642814 MD5 | raw file
  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
 19package org.apache.hadoop.hive.ql.exec.persistence;
 20
 21/**
 22 * An MRU (Most Recently Used) cache implementation. This implementation
 23 * maintains a doubly circular linked list and it can be used with an auxiliary
 24 * data structure such as a HashMap to locate the item quickly.
 25 */
 26public class MRU<T extends DCLLItem> {
 27
 28  T head; // head of the linked list -- MRU; tail (head.prev) will be the LRU
 29
 30  public MRU() {
 31    head = null;
 32  }
 33
 34  /**
 35   * Insert a value into the MRU. It will appear as the head.
 36   */
 37  public T put(T item) {
 38    addToHead(item);
 39    return item;
 40  }
 41
 42  /**
 43   * Remove a item from the MRU list.
 44   * 
 45   * @param v
 46   *          linked list item.
 47   */
 48  public void remove(T v) {
 49    if (v == null) {
 50      return;
 51    }
 52    if (v == head) {
 53      if (head != head.getNext()) {
 54        head = (T) head.getNext();
 55      } else {
 56        head = null;
 57      }
 58    }
 59    v.remove();
 60  }
 61
 62  /**
 63   * Get the most recently used.
 64   * 
 65   * @return the most recently used item.
 66   */
 67  public T head() {
 68    return head;
 69  }
 70
 71  /**
 72   * Get the least recently used.
 73   * 
 74   * @return the least recently used item.
 75   */
 76  public T tail() {
 77    return (T) head.getPrev();
 78  }
 79
 80  /**
 81   * Insert a new item as the head.
 82   * 
 83   * @param v
 84   *          the new linked list item to be added to the head.
 85   */
 86  private void addToHead(T v) {
 87    if (head == null) {
 88      head = v;
 89    } else {
 90      head.insertBefore(v);
 91      head = v;
 92    }
 93  }
 94
 95  /**
 96   * Move an existing item to the head.
 97   * 
 98   * @param v
 99   *          the linked list item to be moved to the head.
100   */
101  public void moveToHead(T v) {
102    assert (head != null);
103    if (head != v) {
104      v.remove();
105      head.insertBefore(v);
106      head = v;
107    }
108  }
109
110  /**
111   * Clear all elements in the MRU list. This is not very efficient (linear)
112   * since it will call remove() to every item in the list.
113   */
114  public void clear() {
115    while (head.getNext() != head) {
116      head.getNext().remove();
117    }
118    head.remove();
119    head = null;
120  }
121}