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