Software /
code /
prosody
Comparison
core/storagemanager.lua @ 6330:a678d15e590e
core/storagemanager: When map store isn't available, fallback to keyval store
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Thu, 07 Aug 2014 12:15:15 -0400 |
parent | 5776:bd0ff8ae98a8 |
child | 6336:11510d4d3b57 |
comparison
equal
deleted
inserted
replaced
6329:6b3eb1611587 | 6330:a678d15e590e |
---|---|
78 driver = null_storage_driver; | 78 driver = null_storage_driver; |
79 end | 79 end |
80 return driver, driver_name; | 80 return driver, driver_name; |
81 end | 81 end |
82 | 82 |
83 local map_shim_mt = { | |
84 __index = { | |
85 get = function(self, username, key) | |
86 local ret, err = self.keyval_store:get(username); | |
87 if ret == nil and err then return nil, err end | |
88 return ret[key]; | |
89 end; | |
90 set = function(self, username, key, data) | |
91 local current, err = self.keyval_store:get(username); | |
92 if current == nil then | |
93 if err then | |
94 return nil, err; | |
95 else | |
96 current = {}; | |
97 end | |
98 end | |
99 current[key] = data; | |
100 return self.keyval_store:set(username, current); | |
101 end; | |
102 }; | |
103 } | |
104 local function create_map_shim(host, store) | |
105 local keyval_store, err = open(host, store, "keyval"); | |
106 if keyval_store == nil then return nil, err end | |
107 return setmetatable({ | |
108 keyval_store = keyval_store; | |
109 }, map_shim_mt); | |
110 end | |
111 | |
83 function open(host, store, typ) | 112 function open(host, store, typ) |
84 local driver, driver_name = get_driver(host, store); | 113 local driver, driver_name = get_driver(host, store); |
85 local ret, err = driver:open(store, typ); | 114 local ret, err = driver:open(store, typ); |
86 if not ret then | 115 if not ret then |
87 if err == "unsupported-store" then | 116 if err == "unsupported-store" then |
117 if typ == "map" then -- Use shim on top of keyval store | |
118 log("debug", "map storage driver unavailable, using shim on top of keyval store."); | |
119 return create_map_shim(host, store); | |
120 end | |
88 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver", | 121 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver", |
89 driver_name, store, typ or "<nil>"); | 122 driver_name, store, typ or "<nil>"); |
90 ret = null_storage_driver; | 123 ret = null_storage_driver; |
91 err = nil; | 124 err = nil; |
92 end | 125 end |