summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/com/att/dao/CachedDAO.java
blob: bcfccbd1d8c352a38833a9e5aaeeaaee9ecdc250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aai
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * Copyright © 2017 Amdocs
 * * ===========================================================================
 * * 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.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.dao;

import java.util.ArrayList;
import java.util.List;

import com.att.authz.layer.Result;
import com.att.dao.aaf.cass.Status;
import com.att.inno.env.Trans;

/**
 * CachedDAO
 * 
 * Cache the response of "get" of any DAO.  
 * 
 * For simplicity's sake, at this time, we only do this for single Object keys  
 * 
 *
 * @param <DATA>
 */
public class CachedDAO<TRANS extends Trans,D extends DAO<TRANS,DATA>,DATA extends Cacheable> 
		extends Cached<TRANS,DATA> implements DAO_RO<TRANS,DATA>{
//	private final String dirty_str; 
	
	private final D dao;

	public CachedDAO(D dao, CIDAO<TRANS> info, int segsize) {
		super(info, dao.table(), segsize);
		
		// Instantiate a new Cache per DAO name (so separate instances use the same cache) 
		this.dao = dao;
		//read_str = "Cached READ for " + dao.table();
//		dirty_str = "Cache DIRTY on " + dao.table();
		if(dao instanceof CassDAOImpl) {
			((CassDAOImpl<?,?>)dao).cache = this;
		}
	}
	
	public static<T extends Trans, DA extends DAO<T,DT>, DT extends Cacheable> 
			CachedDAO<T,DA,DT> create(DA dao, CIDAO<T> info, int segsize) {
		return new CachedDAO<T,DA,DT>(dao,info, segsize);
	}

	public void add(DATA data)  {
		String key = keyFromObjs(dao.keyFrom(data));
		List<DATA> list = new ArrayList<DATA>();
		list.add(data);
		super.add(key,list);
	}
	
//	public void invalidate(TRANS trans, Object ... objs)  {
//		TimeTaken tt = trans.start(dirty_str, Env.SUB);
//		try {
//			super.invalidate(keyFromObjs(objs));
//		} finally {
//			tt.done();
//		}
//	}

	public static String keyFromObjs(Object ... objs) {
		String key;
		if(objs.length==1 && objs[0] instanceof String) {
			key = (String)objs[0];
		} else {
			StringBuilder sb = new StringBuilder();
			boolean first = true;
			for(Object o : objs) {
				if(o!=null) {
					if(first) {
					    first =false;
					} else {
					    sb.append('|');
					}
					sb.append(o.toString());
				}
			}
			key = sb.toString();
		}
		return key;
	}

	public Result<DATA> create(TRANS trans, DATA data) {
		Result<DATA> d = dao.create(trans,data);
		if(d.status==Status.OK) {
		    add(d.value);
		} else {
			trans.error().log(d.errorString());
		}
		invalidate(trans,data);
		return d;
	}

	protected class DAOGetter implements Getter<DATA> {
		protected TRANS trans;
		protected Object objs[];
		protected D dao;
		public Result<List<DATA>> result;

		public DAOGetter(TRANS trans, D dao, Object ... objs) {
			this.trans = trans;
			this.dao = dao;
			this.objs = objs;
		}
		
		/**
		 * Separated into single call for easy overloading
		 * @return
		 */
		public Result<List<DATA>> call() {
			return dao.read(trans, objs);
		}
		
		@Override
		public final Result<List<DATA>> get() {
			return call();
//			if(result.isOKhasData()) { // Note, given above logic, could exist, but stale
//				return result.value;
//			} else {
//				return null;
//			}
		}
	}

	@Override
	public Result<List<DATA>> read(final TRANS trans, final Object ... objs) {
		DAOGetter getter = new DAOGetter(trans,dao,objs); 
		return get(trans, keyFromObjs(objs),getter);
//		if(ld!=null) {
//			return Result.ok(ld);//.emptyList(ld.isEmpty());
//		}
//		// Result Result if exists
//		if(getter.result==null) {
//			return Result.err(Status.ERR_NotFound, "No Cache or Lookup found on [%s]",dao.table());
//		}
//		return getter.result;
	}

	// Slight Improved performance available when String and Obj versions are known. 
	public Result<List<DATA>> read(final String key, final TRANS trans, final Object ... objs) {
		DAOGetter getter = new DAOGetter(trans,dao,objs); 
		return get(trans, key, getter);
//		if(ld!=null) {
//			return Result.ok(ld);//.emptyList(ld.isEmpty());
//		}
//		// Result Result if exists
//		if(getter.result==null) {
//			return Result.err(Status.ERR_NotFound, "No Cache or Lookup found on [%s]",dao.table());
//		}
//		return getter.result;
	}
	
	@Override
	public Result<List<DATA>> read(TRANS trans, DATA data) {
		return read(trans,dao.keyFrom(data));
	}
	public Result<Void> update(TRANS trans, DATA data) {
		Result<Void> d = dao.update(trans, data);
		if(d.status==Status.OK) {
		    add(data);
		} else {
			trans.error().log(d.errorString());
		}
		return d;
	}

	public Result<Void> delete(TRANS trans, DATA data, boolean reread) {
		if(reread) { // If reread, get from Cache, if possible, not DB exclusively
			Result<List<DATA>> rd = read(trans,data);
			if(rd.notOK()) {
			    return Result.err(rd);
			} else {
				trans.error().log(rd.errorString());
			}
			if(rd.isEmpty()) {
				data.invalidate(this);
				return Result.err(Status.ERR_NotFound,"Not Found");
			}
			data = rd.value.get(0);
		}
		Result<Void> rv=dao.delete(trans, data, false);
		data.invalidate(this);
		return rv;
	}
	
	@Override
	public void close(TRANS trans) {
		if(dao!=null) {
		    dao.close(trans);
		}
	}
	

	@Override
	public String table() {
		return dao.table();
	}
	
	public D dao() {
		return dao;
	}
	
	public void invalidate(TRANS trans, DATA data) {
        if(info.touch(trans, dao.table(),data.invalidate(this)).notOK()) {
	    trans.error().log("Cannot touch CacheInfo for Role");
	}
	}
}