blob: 011475e8ec60c4956356407835027f5150ee6a16 [file] [log] [blame]
// Copyright (C) 2010 The Android Open Source Project
//
// 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 com.google.gerrit.server;
import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.StarredChange;
import com.google.gerrit.server.cache.Cache;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.cache.EntryCreator;
import com.google.gwtorm.client.Column;
import com.google.gwtorm.client.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import java.util.List;
@Singleton
public class StarredChangesCacheImpl implements StarredChangesCache {
private static final String BY_ACCOUNT_ID = "starred_user";
private static final String BY_CHANGE_ID = "starred_change";
protected static class StarredChangeList {
@Column(id = 1)
protected List<StarredChange> list;
protected StarredChangeList() {
}
public StarredChangeList(List<StarredChange> list) {
this.list = list;
}
}
public static Module module() {
return new CacheModule() {
@Override
protected void configure() {
final TypeLiteral<Cache<Account.Id, StarredChangeList>> byAccountIdType =
new TypeLiteral<Cache<Account.Id, StarredChangeList>>() {};
core(byAccountIdType, BY_ACCOUNT_ID).populateWith(
ByAccountIdLoader.class);
final TypeLiteral<Cache<Change.Id, StarredChangeList>> byChangeIdType =
new TypeLiteral<Cache<Change.Id, StarredChangeList>>() {};
core(byChangeIdType, BY_CHANGE_ID).populateWith(ByChangeIdLoader.class);
bind(StarredChangesCacheImpl.class);
bind(StarredChangesCache.class).to(StarredChangesCacheImpl.class);
}
};
}
private final Cache<Account.Id, StarredChangeList> byAccountId;
private final Cache<Change.Id, StarredChangeList> byChangeId;
@Inject
StarredChangesCacheImpl(
@Named(BY_ACCOUNT_ID) Cache<Account.Id, StarredChangeList> byAccountId,
@Named(BY_CHANGE_ID) Cache<Change.Id, StarredChangeList> byChangeId) {
this.byAccountId = byAccountId;
this.byChangeId = byChangeId;
}
@Override
public List<StarredChange> byAccount(Account.Id id) {
return byAccountId.get(id).list;
}
@Override
public List<StarredChange> byChange(Change.Id id) {
return byChangeId.get(id).list;
}
@Override
public void evict(StarredChange.Key key) {
byAccountId.remove(key.getParentKey());
byChangeId.remove(key.getChangeId());
}
static class ByAccountIdLoader extends
EntryCreator<Account.Id, StarredChangeList> {
private final SchemaFactory<ReviewDb> schema;
@Inject
ByAccountIdLoader(SchemaFactory<ReviewDb> schema) {
this.schema = schema;
}
@Override
public StarredChangeList createEntry(Account.Id id) throws Exception {
final ReviewDb db = schema.open();
try {
return new StarredChangeList(db.starredChanges().byAccount(id).toList());
} finally {
db.close();
}
}
}
static class ByChangeIdLoader extends
EntryCreator<Change.Id, StarredChangeList> {
private final SchemaFactory<ReviewDb> schema;
@Inject
ByChangeIdLoader(SchemaFactory<ReviewDb> schema) {
this.schema = schema;
}
@Override
public StarredChangeList createEntry(Change.Id id) throws Exception {
final ReviewDb db = schema.open();
try {
return new StarredChangeList(db.starredChanges().byChange(id).toList());
} finally {
db.close();
}
}
}
}