Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

And read/write transactions to cache interface #330

Merged
merged 10 commits into from
Mar 17, 2017
Merged

Conversation

BenSchwab
Copy link
Contributor

Closes #327

  • Adds read/write transactions. Each transaction is facade over RealCache, surfacing only the methods that are safe to call in each transaction

To better match the general apollo architecture: https://airbnb.quip.com/wsn6AS0cYH84

  • changes key subscriber to have key filtering done in subscribers

remove accidental refactor

fix checkstyle

Make read/write transactions invalid after closing
}
}

@Override public Record read(@Nonnull String key) {
@Override public RealReadTransaction readTransaction() {
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is good idea to lock in one place but unlock in RealReadTransaction (via closeRead).
It would be better do it in one place RealReadTransaction so pass lock object to RealReadTransaction

return cache.read(key);
}

@Override public Collection<Record> read(@Nonnull Collection<String> keys) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be more generic like:

<R> R execute(Transactional<Cache, R> action) {
  return action.call(cache);
}

Usage:

transaction.execute(new Transactional<>() {
  public Collection<Record> call(Cache cache) {
    return cache.read(keys);  
  }
})

Even maybe we can merge Read and Write transactions to one class

@BenSchwab
Copy link
Contributor Author

@sav007 Took a crack at the generic functional approach. Agree that keeping the locking logic in one spot is a lot cleaner!

} finally {
lock.readLock().unlock();
}
@Override public <R> Transaction<ReadableCache, R> readTransaction() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it makes sense, but we can simplify API even more, so instead returning the transaction why don't pass Transactional as a parameter here:

@Override public <R> R executeWithReadTransaction(Transactional<ReadableCache, R> transactional) {
  try {
    lock.readLock().lock();
    return transactional.call(RealCache.this);
  } finally {
    lock.readLock().unlock();
  }
}

The same for write. In this case we can rid off one indirection Transaction class at all.

@BenSchwab
Copy link
Contributor Author

Yep - you are 100% right the two levels of indirection is unnecssary

@BenSchwab BenSchwab merged commit 0c021f9 into master Mar 17, 2017
@BenSchwab BenSchwab deleted the bschwab-read-lock branch March 17, 2017 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants