mail-index-alloc-cache.c revision 04dff721d2a63566a4dbe1c856f8218b6550aa3e
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
550d2fe097e95f12e8fa60ef52753ea7fe53d4eaTimo Sirainen MODULE_CONTEXT(obj, mail_index_alloc_cache_index_module)
550d2fe097e95f12e8fa60ef52753ea7fe53d4eaTimo Sirainen/* How many seconds to keep index opened for reuse after it's been closed */
550d2fe097e95f12e8fa60ef52753ea7fe53d4eaTimo Sirainen/* How many closed indexes to keep */
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(mail_index_alloc_cache_index_module,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic struct mail_index_alloc_cache_list *indexes = NULL;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenmail_index_alloc_cache_add(struct mail_index *index,
c13fce16374a6fa8d127742c527498d38e777789Timo Sirainen list = i_new(struct mail_index_alloc_cache_list, 1);
c13fce16374a6fa8d127742c527498d38e777789Timo Sirainen MODULE_CONTEXT_SET(index, mail_index_alloc_cache_index_module, list);
c13fce16374a6fa8d127742c527498d38e777789Timo Sirainenmail_index_alloc_cache_list_free(struct mail_index_alloc_cache_list *list)
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainenmail_index_alloc_cache_get(const char *mailbox_path,
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen struct mail_index_alloc_cache_list **indexp, *rec, *match;
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen /* compare index_dir inodes so we don't break even with symlinks.
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen if index_dir doesn't exist yet or if using in-memory indexes, just
b44033e45e9f48f8a6e1ac5905234fec5de6d6ccAki Tuomi compare mailbox paths */
cdc7ca129c3433d3e4b9d5e90f4c209e4546dfe9Timo Sirainen /* in-memory indexes */
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen /* it'll be created later */
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen i_error("%s", eacces_error_get("stat", index_dir));
1b7cd57585d8c2f133dd612d2d5d9c775595659fTimo Sirainen /* already found the index. we're just going through
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen the rest of them to drop 0 refcounts */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen } else if (rec->refcount == 0 && rec->index->open_count == 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* index is already closed. don't even try to
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen } else if (index_dir != NULL && rec->index_dir_ino != 0) {
009217abb57a24a4076092e8e4e165545747839eStephan Bosch /* make sure the directory still exists.
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen it might have been renamed and we're trying
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen to access it via its new path now. */
433f5c9cc560a8cbff47257513d0bacb1cf250f4Timo Sirainen if (strcmp(mailbox_path, rec->mailbox_path) == 0)
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen struct mail_index *index = mail_index_alloc(index_dir, prefix);
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen match = mail_index_alloc_cache_add(index, mailbox_path, &st);
89d6cd658eabf46e07e40037b0e641ed9be1a2a3Timo Sirainen struct mail_index_alloc_cache_list **list, *rec;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void index_removal_timeout(void *context ATTR_UNUSED)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid mail_index_alloc_cache_unref(struct mail_index **_index)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen struct mail_index_alloc_cache_list *list, **listp;
4aab01f4eade3d278b61471516c062ce30a84b5fTimo Sirainen for (listp = &indexes; *listp != NULL; listp = &(*listp)->next) {
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen list->destroy_time = ioloop_time + INDEX_CACHE_TIMEOUT;
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen if (list->refcount == 0 && index->open_count == 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* index was already closed. don't even try to cache it. */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen to_index = timeout_add(INDEX_CACHE_TIMEOUT*1000/2,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenvoid mail_index_alloc_cache_destroy_unrefed(void)
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainenvoid mail_index_alloc_cache_index_opened(struct mail_index *index)
550d2fe097e95f12e8fa60ef52753ea7fe53d4eaTimo Sirainen if (list != NULL && list->index_dir_ino == 0 &&
550d2fe097e95f12e8fa60ef52753ea7fe53d4eaTimo Sirainen /* newly created index directory. update its stat. */