// Path: src/main/java/org/iq80/memcached/Item.java
// public static class PrevChain implements Iterable<Item>
// {
//     private final Allocator allocator;
//     private final long start;
// 
//     public PrevChain(Allocator allocator, long start)
//     {
//         this.allocator = allocator;
//         this.start = start;
//     }
// 
//     public Iterator<Item> iterator()
//     {
//         return new Iterator<Item>()
//         {
//             private Item next = cast(allocator, start);
// 
//             public boolean hasNext()
//             {
//                 return next.getAddress() != 0;
//             }
// 
//             public Item next()
//             {
//                 if (!hasNext()) {
//                     throw new NoSuchElementException();
//                 }
// 
//                 next.setAddress(allocator, next.getPrev());
//                 return next;
//             }
// 
//             public void remove()
//             {
//                 throw new UnsupportedOperationException();
//             }
//         };
//     }
// }


import org.iq80.memcached.Item.PrevChain;
/*
 * Copyright 2010 Proofpoint, Inc.
 * Copyright (C) 2012, FuseSource Corp.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.iq80.memcached;


public class ItemLru {
    /**
     * We only reposition items in the LRU queue if they haven't been
     * repositioned in this many seconds. That saves us from churning on
     * frequently-accessed items.
     */
    private static final int ITEM_UPDATE_INTERVAL = 60;

    /**
     * How long an object can reasonably be assumed to be locked before
     * harvesting it on a low memory condition? 3 Hours.
     */
    private static final long TAIL_REPAIR_TIME = 3 * 3600;

    private final SlabManager slabManager;
    private final boolean evictToFree;
    private final Monitor monitor;
    private final ItemStats stats;
    private long head;
    private long tail;
    private long size;

    public ItemLru(SlabManager slabManager, boolean evictToFree, Monitor monitor, ItemStats stats) {
        this.slabManager = slabManager;
        this.evictToFree = evictToFree;
        this.monitor = monitor;
        this.stats = stats;
    }

    public long size() {
        return size;
    }

    /**
     * Check if there are any expired items on the tail.
     */
    public Item findExpired(int tries, int currentTime) {
        // do a quick check if we have any expired items in the tail..
// Path: src/main/java/org/iq80/memcached/Item.java
// public static class PrevChain implements Iterable<Item>
// {
//     private final Allocator allocator;
//     private final long start;
// 
//     public PrevChain(Allocator allocator, long start)
//     {
//         this.allocator = allocator;
//         this.start = start;
//     }
// 
//     public Iterator<Item> iterator()
//     {
//         return new Iterator<Item>()
//         {
//             private Item next = cast(allocator, start);
// 
//             public boolean hasNext()
//             {
//                 return next.getAddress() != 0;
//             }
// 
//             public Item next()
//             {
//                 if (!hasNext()) {
//                     throw new NoSuchElementException();
//                 }
// 
//                 next.setAddress(allocator, next.getPrev());
//                 return next;
//             }
// 
//             public void remove()
//             {
//                 throw new UnsupportedOperationException();
//             }
//         };
//     }
// }


// Path: src/main/java/org/iq80/memcached/ItemLru.java
import org.iq80.memcached.Item.PrevChain;

/*
 * Copyright 2010 Proofpoint, Inc.
 * Copyright (C) 2012, FuseSource Corp.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.iq80.memcached;


public class ItemLru {
    /**
     * We only reposition items in the LRU queue if they haven't been
     * repositioned in this many seconds. That saves us from churning on
     * frequently-accessed items.
     */
    private static final int ITEM_UPDATE_INTERVAL = 60;

    /**
     * How long an object can reasonably be assumed to be locked before
     * harvesting it on a low memory condition? 3 Hours.
     */
    private static final long TAIL_REPAIR_TIME = 3 * 3600;

    private final SlabManager slabManager;
    private final boolean evictToFree;
    private final Monitor monitor;
    private final ItemStats stats;
    private long head;
    private long tail;
    private long size;

    public ItemLru(SlabManager slabManager, boolean evictToFree, Monitor monitor, ItemStats stats) {
        this.slabManager = slabManager;
        this.evictToFree = evictToFree;
        this.monitor = monitor;
        this.stats = stats;
    }

    public long size() {
        return size;
    }

    /**
     * Check if there are any expired items on the tail.
     */
    public Item findExpired(int tries, int currentTime) {
        // do a quick check if we have any expired items in the tail..
        for (Item search : new PrevChain(slabManager.getAllocator(), tail)) {