1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.ehcache.writer.writebehind.operations;
17
18 import net.sf.ehcache.CacheEntry;
19 import net.sf.ehcache.Element;
20 import net.sf.ehcache.writer.CacheWriter;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /***
26 * Implements the delete operation for write behind
27 *
28 * @author Geert Bevin
29 * @version $Id: DeleteOperation.html 13146 2011-08-01 17:12:39Z oletizi $
30 */
31 public class DeleteOperation implements SingleOperation {
32 private final CacheEntry entry;
33 private final long creationTime;
34
35 /***
36 * Create a new delete operation for a particular entry
37 *
38 * @param entry the entry to delete
39 */
40 public DeleteOperation(CacheEntry entry) {
41 this(entry, System.currentTimeMillis());
42 }
43
44 /***
45 * Create a new delete operation for a particular entry and creation time
46 *
47 * @param entry the entry to delete
48 * @param creationTime the creation time of the operation
49 */
50 public DeleteOperation(CacheEntry entry, long creationTime) {
51 this.entry = duplicateCacheEntryElement(entry);
52 this.creationTime = creationTime;
53 }
54
55 private CacheEntry duplicateCacheEntryElement(CacheEntry entry) {
56 if (null == entry.getElement()) {
57 return entry;
58 } else {
59 Element element = entry.getElement();
60 return new CacheEntry(entry.getKey(), new Element(element.getObjectKey(), element.getObjectValue(), element.getVersion(),
61 element.getCreationTime(), element.getLastAccessTime(), element.getHitCount(), false,
62 element.getTimeToLive(), element.getTimeToIdle(), element.getLastUpdateTime()));
63 }
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public void performSingleOperation(CacheWriter cacheWriter) {
70 cacheWriter.delete(entry);
71 }
72
73 /***
74 * {@inheritDoc}
75 */
76 public BatchOperation createBatchOperation(List<SingleOperation> operations) {
77 final List<CacheEntry> entries = new ArrayList<CacheEntry>();
78 for (KeyBasedOperation operation : operations) {
79 entries.add(((DeleteOperation) operation).entry);
80 }
81 return new DeleteAllOperation(entries);
82 }
83
84 /***
85 * {@inheritDoc}
86 */
87 public Object getKey() {
88 return entry.getKey();
89 }
90
91 /***
92 * {@inheritDoc}
93 */
94 public long getCreationTime() {
95 return creationTime;
96 }
97
98 /***
99 * Retrieves the entry that will be used for this operation
100 */
101 public CacheEntry getEntry() {
102 return entry;
103 }
104
105 /***
106 * {@inheritDoc}
107 */
108 public SingleOperationType getType() {
109 return SingleOperationType.DELETE;
110 }
111 }