| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | /* |
| 3 | Copyright 2006 Peter Lawrey |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import org.jetbrains.annotations.NotNull; |
| 19 | import org.jtoolkit.essence.app.pojo.DataValue; |
| 20 | import org.jtoolkit.essence.app.pojo.Datable; |
| 21 | import org.jtoolkit.essence.app.pojo.DatableUtils; |
| 22 | import org.jtoolkit.essence.concurrency.Immutable; |
| 23 | import org.jtoolkit.essence.data.Mapping; |
| 24 | |
| 25 | import javax.cache.CacheEntry; |
| 26 | import java.io.DataInput; |
| 27 | import java.io.DataOutput; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | /** |
| 31 | * This class represents MetaData for CacheEntry. |
| 32 | */ |
| 33 | @Immutable(false) |
| 34 | @SuppressWarnings({"unchecked"}) |
| 35 | public class MetaData<K, V> extends DataValue implements CacheEntry<K, V>, Datable { |
| 36 | private final long creationTimeMS; |
| 37 | @Immutable(false) |
| 38 | private long expirationTime = Long.MAX_VALUE; |
| 39 | @Immutable(false) |
| 40 | private int hits; |
| 41 | @Immutable(false) |
| 42 | private long lastAccessTimeUS; |
| 43 | private final String modifiedSource; |
| 44 | private final long modifiedTimeMS; |
| 45 | |
| 46 | private final K key; |
| 47 | private final V value; |
| 48 | |
| 49 | public MetaData(long creationTimeMS, long expirationTime, int hits, K key, String modifiedSource, long modifiedTimeMS, V value) { |
| 50 | this.creationTimeMS = creationTimeMS; |
| 51 | this.expirationTime = expirationTime; |
| 52 | this.hits = hits; |
| 53 | this.key = key; |
| 54 | this.modifiedSource = modifiedSource; |
| 55 | this.modifiedTimeMS = modifiedTimeMS; |
| 56 | lastAccessTimeUS = modifiedTimeMS * 1000; |
| 57 | this.value = value; |
| 58 | } |
| 59 | |
| 60 | public static boolean isBefore(String source, long timeMS, MetaData metaData) { |
| 61 | if (metaData == null) return false; |
| 62 | //noinspection SimplifiableIfStatement |
| 63 | if (timeMS < metaData.modifiedTimeMS) return true; |
| 64 | return timeMS == metaData.modifiedTimeMS && metaData.modifiedSource.compareTo(source) > 0; |
| 65 | } |
| 66 | |
| 67 | public K getKey() { |
| 68 | return key; |
| 69 | } |
| 70 | |
| 71 | public V getValue() { |
| 72 | return value; |
| 73 | } |
| 74 | |
| 75 | public V setValue(V value) { |
| 76 | throw new UnsupportedOperationException(); |
| 77 | } |
| 78 | |
| 79 | public long getCost() { |
| 80 | try { |
| 81 | return DatableUtils.toBytes(this).length; |
| 82 | } catch (IOException ignored) { |
| 83 | return ~0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public long getCreationTime() { |
| 88 | return creationTimeMS; |
| 89 | } |
| 90 | |
| 91 | public long getExpirationTime() { |
| 92 | return expirationTime; |
| 93 | } |
| 94 | |
| 95 | public int getHits() { |
| 96 | return hits; |
| 97 | } |
| 98 | |
| 99 | public void touchRead(long time) { |
| 100 | hits++; |
| 101 | lastAccessTimeUS = time; |
| 102 | } |
| 103 | |
| 104 | public long getLastAccessTime() { |
| 105 | return lastAccessTimeUS; |
| 106 | } |
| 107 | |
| 108 | public long getLastUpdateTime() { |
| 109 | return modifiedTimeMS; |
| 110 | } |
| 111 | |
| 112 | public String getModifiedSource() { |
| 113 | return modifiedSource; |
| 114 | } |
| 115 | |
| 116 | public long getModifiedTimeMS() { |
| 117 | return modifiedTimeMS; |
| 118 | } |
| 119 | |
| 120 | public long getVersion() { |
| 121 | return modifiedTimeMS; |
| 122 | } |
| 123 | |
| 124 | public boolean isValid() { |
| 125 | return value != null; |
| 126 | } |
| 127 | |
| 128 | // must define a constructor. |
| 129 | static { |
| 130 | DatableUtils.registerBuilder(MetaData.class, new Mapping<DataInput, MetaData>() { |
| 131 | public MetaData convert(DataInput in) { |
| 132 | try { |
| 133 | return new MetaData(in); |
| 134 | } catch (IOException e) { |
| 135 | throw new IllegalArgumentException(e); |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | public MetaData(@NotNull DataInput in) throws IOException { |
| 142 | creationTimeMS = in.readLong(); |
| 143 | long time = DatableUtils.readBER(in); |
| 144 | expirationTime = time == ~0 ? Long.MAX_VALUE : time; |
| 145 | hits = (int) DatableUtils.readBER(in); |
| 146 | lastAccessTimeUS = in.readLong(); |
| 147 | modifiedSource = in.readUTF(); |
| 148 | modifiedTimeMS = in.readLong(); |
| 149 | key = (K) DatableUtils.readObject(in); |
| 150 | value = (V) DatableUtils.readObject(in); |
| 151 | } |
| 152 | |
| 153 | public void writeData(@NotNull DataOutput out) throws IOException { |
| 154 | out.writeLong(creationTimeMS); |
| 155 | DatableUtils.writeBER(out, expirationTime == Long.MAX_VALUE ? ~0 : expirationTime); |
| 156 | DatableUtils.writeBER(out, hits); |
| 157 | out.writeLong(lastAccessTimeUS); |
| 158 | out.writeUTF(modifiedSource); |
| 159 | out.writeLong(modifiedTimeMS); |
| 160 | DatableUtils.writeObject(out, key); |
| 161 | DatableUtils.writeObject(out, value); |
| 162 | } |
| 163 | |
| 164 | public boolean isBefore(MetaData md) { |
| 165 | return isBefore(modifiedSource, modifiedTimeMS, md); |
| 166 | } |
| 167 | } |