diff options
author | Prashanth Pai <ppai@redhat.com> | 2016-02-23 14:58:06 +0530 |
---|---|---|
committer | Thiago da Silva <thiago@redhat.com> | 2016-04-04 09:24:42 -0700 |
commit | c73037e90c3f551caf18df41efd7fa9750454a10 (patch) | |
tree | 4d9120b70d2e83d7240130522893986aaf9c0118 /gluster/swift/account | |
parent | 539d20e3b13096cfa9107fc2b619943c494c4ab3 (diff) |
Don't fetch metadata for plain container listing
Fetch metadata (xattr) for containers in an account ONLY when the client
asks for it (using content-type indicating JSON or XML response).
This avoids a lot of unnecessarry stat() and getxattr() calls whose
results would anyways be unused. The performance gain is obvious in this
case.
This change is restricted to container listing. The same can be extended
to object listing as well (will be sent as a separate change)
Change-Id: Ibff1c5a90519f11053c0b651d8ea3385dda43a2f
Signed-off-by: Prashanth Pai <ppai@redhat.com>
Reviewed-on: http://review.gluster.org/13497
Reviewed-by: Thiago da Silva <thiago@redhat.com>
Tested-by: Thiago da Silva <thiago@redhat.com>
Diffstat (limited to 'gluster/swift/account')
-rw-r--r-- | gluster/swift/account/utils.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/gluster/swift/account/utils.py b/gluster/swift/account/utils.py new file mode 100644 index 0000000..99fe5ea --- /dev/null +++ b/gluster/swift/account/utils.py @@ -0,0 +1,71 @@ +# Copyright (c) 2016 Red Hat, Inc. +# +# 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. + +from swift.account.utils import FakeAccountBroker, get_response_headers +from swift.common.swob import HTTPOk, HTTPNoContent +from swift.common.utils import json +from xml.sax import saxutils + + +def account_listing_response(account, req, response_content_type, broker=None, + limit='', marker='', end_marker='', prefix='', + delimiter=''): + """ + This is an exact copy of swift.account.utis.account_listing_response() + except for one difference i.e this method passes response_content_type + to broker.list_containers_iter() method. + """ + if broker is None: + broker = FakeAccountBroker() + + resp_headers = get_response_headers(broker) + + account_list = broker.list_containers_iter(limit, marker, end_marker, + prefix, delimiter, + response_content_type) + if response_content_type == 'application/json': + data = [] + for (name, object_count, bytes_used, is_subdir) in account_list: + if is_subdir: + data.append({'subdir': name}) + else: + data.append({'name': name, 'count': object_count, + 'bytes': bytes_used}) + account_list = json.dumps(data) + elif response_content_type.endswith('/xml'): + output_list = ['<?xml version="1.0" encoding="UTF-8"?>', + '<account name=%s>' % saxutils.quoteattr(account)] + for (name, object_count, bytes_used, is_subdir) in account_list: + if is_subdir: + output_list.append( + '<subdir name=%s />' % saxutils.quoteattr(name)) + else: + item = '<container><name>%s</name><count>%s</count>' \ + '<bytes>%s</bytes></container>' % \ + (saxutils.escape(name), object_count, bytes_used) + output_list.append(item) + output_list.append('</account>') + account_list = '\n'.join(output_list) + else: + if not account_list: + resp = HTTPNoContent(request=req, headers=resp_headers) + resp.content_type = response_content_type + resp.charset = 'utf-8' + return resp + account_list = '\n'.join(r[0] for r in account_list) + '\n' + ret = HTTPOk(body=account_list, request=req, headers=resp_headers) + ret.content_type = response_content_type + ret.charset = 'utf-8' + return ret |