Skip to content
This repository
Browse code

Merge pull request #1581 from Montellese/jsonrpc_optimisations

jsonrpc: optimize handling of properties requested by the client
  • Loading branch information...
commit e811d6bbbddc1c096b1e683500ee20e9eca6dff7 2 parents 34fccf3 + ef7cae0
Sascha Montellese Montellese authored
242 xbmc/interfaces/json-rpc/FileItemHandler.cpp
@@ -41,111 +41,125 @@ using namespace MUSIC_INFO;
41 41 using namespace JSONRPC;
42 42 using namespace XFILE;
43 43
44   -void CFileItemHandler::FillDetails(ISerializable* info, CFileItemPtr item, const CVariant& fields, CVariant &result, CThumbLoader *thumbLoader /* = NULL */)
  44 +bool CFileItemHandler::GetField(const std::string &field, const CVariant &info, const CFileItemPtr &item, CVariant &result, bool &fetchedArt, CThumbLoader *thumbLoader /* = NULL */)
45 45 {
46   - if (info == NULL || fields.size() == 0)
47   - return;
48   -
49   - CVariant serialization;
50   - info->Serialize(serialization);
  46 + if (result.isMember(field) && !result[field].empty())
  47 + return true;
51 48
52   - bool fetchedArt = false;
53   -
54   - for (unsigned int i = 0; i < fields.size(); i++)
  49 + if (info.isMember(field) && !info[field].isNull())
55 50 {
56   - CStdString field = fields[i].asString();
  51 + result[field] = info[field];
  52 + return true;
  53 + }
57 54
58   - if (item)
  55 + if (item)
  56 + {
  57 + if (item->IsAlbum())
59 58 {
60   - if (item->IsAlbum() && field.Equals("albumlabel"))
61   - field = "label";
62   - if (item->IsAlbum())
  59 + if (field == "albumlabel")
63 60 {
64   - if (field == "label")
65   - {
66   - result["albumlabel"] = item->GetProperty("album_label");
67   - continue;
68   - }
69   - if (item->HasProperty("album_" + field + "_array"))
70   - {
71   - result[field] = item->GetProperty("album_" + field + "_array");
72   - continue;
73   - }
74   - if (item->HasProperty("album_" + field))
75   - {
76   - result[field] = item->GetProperty("album_" + field);
77   - continue;
78   - }
  61 + result[field] = item->GetProperty("album_label");
  62 + return true;
79 63 }
80   -
81   - if (item->HasProperty("artist_" + field + "_array"))
  64 + if (item->HasProperty("album_" + field + "_array"))
82 65 {
83   - result[field] = item->GetProperty("artist_" + field + "_array");
84   - continue;
  66 + result[field] = item->GetProperty("album_" + field + "_array");
  67 + return true;
85 68 }
86   - if (item->HasProperty("artist_" + field))
  69 + if (item->HasProperty("album_" + field))
87 70 {
88   - result[field] = item->GetProperty("artist_" + field);
89   - continue;
  71 + result[field] = item->GetProperty("album_" + field);
  72 + return true;
90 73 }
91   -
92   - if (field == "thumbnail")
  74 + }
  75 +
  76 + if (item->HasProperty("artist_" + field + "_array"))
  77 + {
  78 + result[field] = item->GetProperty("artist_" + field + "_array");
  79 + return true;
  80 + }
  81 + if (item->HasProperty("artist_" + field))
  82 + {
  83 + result[field] = item->GetProperty("artist_" + field);
  84 + return true;
  85 + }
  86 +
  87 + if (field == "thumbnail")
  88 + {
  89 + if (thumbLoader != NULL && !item->HasThumbnail() && !fetchedArt &&
  90 + ((item->HasVideoInfoTag() && item->GetVideoInfoTag()->m_iDbId > -1) || (item->HasMusicInfoTag() && item->GetMusicInfoTag()->GetDatabaseId() > -1)))
93 91 {
94   - if (thumbLoader != NULL && !item->HasThumbnail() && !fetchedArt &&
95   - ((item->HasVideoInfoTag() && item->GetVideoInfoTag()->m_iDbId > -1) || (item->HasMusicInfoTag() && item->GetMusicInfoTag()->GetDatabaseId() > -1)))
96   - {
97   - thumbLoader->FillLibraryArt(*item);
98   - fetchedArt = true;
99   - }
100   - else if (item->HasPictureInfoTag() && !item->HasThumbnail())
101   - item->SetThumbnailImage(CTextureCache::GetWrappedThumbURL(item->GetPath()));
102   -
103   - if (item->HasThumbnail())
104   - result["thumbnail"] = CTextureCache::GetWrappedImageURL(item->GetThumbnailImage());
105   - else
106   - result["thumbnail"] = "";
107   - continue;
  92 + thumbLoader->FillLibraryArt(*item);
  93 + fetchedArt = true;
108 94 }
109   -
110   - if (field == "fanart")
  95 + else if (item->HasPictureInfoTag() && !item->HasThumbnail())
  96 + item->SetThumbnailImage(CTextureCache::GetWrappedThumbURL(item->GetPath()));
  97 +
  98 + if (item->HasThumbnail())
  99 + result["thumbnail"] = CTextureCache::GetWrappedImageURL(item->GetThumbnailImage());
  100 + else
  101 + result["thumbnail"] = "";
  102 +
  103 + return true;
  104 + }
  105 +
  106 + if (field == "fanart")
  107 + {
  108 + if (thumbLoader != NULL && !item->HasProperty("fanart_image") && !fetchedArt &&
  109 + ((item->HasVideoInfoTag() && item->GetVideoInfoTag()->m_iDbId > -1) || (item->HasMusicInfoTag() && item->GetMusicInfoTag()->GetDatabaseId() > -1)))
111 110 {
112   - if (thumbLoader != NULL && !item->HasProperty("fanart_image") && !fetchedArt &&
113   - ((item->HasVideoInfoTag() && item->GetVideoInfoTag()->m_iDbId > -1) || (item->HasMusicInfoTag() && item->GetMusicInfoTag()->GetDatabaseId() > -1)))
114   - {
115   - thumbLoader->FillLibraryArt(*item);
116   - fetchedArt = true;
117   - }
118   -
119   - if (item->HasProperty("fanart_image"))
120   - result["fanart"] = CTextureCache::GetWrappedImageURL(item->GetProperty("fanart_image").asString());
121   - else
122   - result["fanart"] = "";
123   - continue;
  111 + thumbLoader->FillLibraryArt(*item);
  112 + fetchedArt = true;
124 113 }
125   -
126   - if (item->HasVideoInfoTag() && item->GetVideoContentType() == VIDEODB_CONTENT_TVSHOWS)
  114 +
  115 + if (item->HasProperty("fanart_image"))
  116 + result["fanart"] = CTextureCache::GetWrappedImageURL(item->GetProperty("fanart_image").asString());
  117 + else
  118 + result["fanart"] = "";
  119 +
  120 + return true;
  121 + }
  122 +
  123 + if (item->HasVideoInfoTag() && item->GetVideoContentType() == VIDEODB_CONTENT_TVSHOWS)
  124 + {
  125 + if (item->GetVideoInfoTag()->m_iSeason < 0 && field == "season")
127 126 {
128   - if (item->GetVideoInfoTag()->m_iSeason < 0 && field == "season")
129   - {
130   - result[field] = (int)item->GetProperty("totalseasons").asInteger();
131   - continue;
132   - }
133   - if (field == "watchedepisodes")
134   - {
135   - result[field] = (int)item->GetProperty("watchedepisodes").asInteger();
136   - continue;
137   - }
  127 + result[field] = (int)item->GetProperty("totalseasons").asInteger();
  128 + return true;
138 129 }
139   -
140   - if (field == "lastmodified" && item->m_dateTime.IsValid())
  130 + if (field == "watchedepisodes")
141 131 {
142   - result[field] = item->m_dateTime.GetAsLocalizedDateTime();
143   - continue;
  132 + result[field] = (int)item->GetProperty("watchedepisodes").asInteger();
  133 + return true;
144 134 }
145 135 }
  136 +
  137 + if (field == "lastmodified" && item->m_dateTime.IsValid())
  138 + {
  139 + result[field] = item->m_dateTime.GetAsLocalizedDateTime();
  140 + return true;
  141 + }
  142 + }
  143 +
  144 + return false;
  145 +}
  146 +
  147 +void CFileItemHandler::FillDetails(const ISerializable *info, const CFileItemPtr &item, std::set<std::string> &fields, CVariant &result, CThumbLoader *thumbLoader /* = NULL */)
  148 +{
  149 + if (info == NULL || fields.size() == 0)
  150 + return;
  151 +
  152 + CVariant serialization;
  153 + info->Serialize(serialization);
  154 +
  155 + bool fetchedArt = false;
  156 +
  157 + std::set<std::string> originalFields = fields;
146 158
147   - if (serialization.isMember(field) && !serialization[field].isNull() && (!result.isMember(field) || result[field].empty()))
148   - result[field] = serialization[field];
  159 + for (std::set<std::string>::const_iterator fieldIt = originalFields.begin(); fieldIt != originalFields.end(); fieldIt++)
  160 + {
  161 + if (GetField(*fieldIt, serialization, item, result, fetchedArt, thumbLoader))
  162 + fields.erase(*fieldIt);
149 163 }
150 164 }
151 165
@@ -179,11 +193,18 @@ void CFileItemHandler::HandleFileItemList(const char *ID, bool allowFile, const
179 193 thumbLoader->Initialize();
180 194 }
181 195
  196 + std::set<std::string> fields;
  197 + if (parameterObject.isMember("properties") && parameterObject["properties"].isArray())
  198 + {
  199 + for (CVariant::const_iterator_array field = parameterObject["properties"].begin_array(); field != parameterObject["properties"].end_array(); field++)
  200 + fields.insert(field->asString());
  201 + }
  202 +
182 203 for (int i = start; i < end; i++)
183 204 {
184 205 CVariant object;
185 206 CFileItemPtr item = items.Get(i);
186   - HandleFileItem(ID, allowFile, resultname, item, parameterObject, parameterObject["properties"], result, true, thumbLoader);
  207 + HandleFileItem(ID, allowFile, resultname, item, parameterObject, fields, result, true, thumbLoader);
187 208 }
188 209
189 210 delete thumbLoader;
@@ -191,28 +212,37 @@ void CFileItemHandler::HandleFileItemList(const char *ID, bool allowFile, const
191 212
192 213 void CFileItemHandler::HandleFileItem(const char *ID, bool allowFile, const char *resultname, CFileItemPtr item, const CVariant &parameterObject, const CVariant &validFields, CVariant &result, bool append /* = true */, CThumbLoader *thumbLoader /* = NULL */)
193 214 {
  215 + std::set<std::string> fields;
  216 + if (parameterObject.isMember("properties") && parameterObject["properties"].isArray())
  217 + {
  218 + for (CVariant::const_iterator_array field = parameterObject["properties"].begin_array(); field != parameterObject["properties"].end_array(); field++)
  219 + fields.insert(field->asString());
  220 + }
  221 +
  222 + HandleFileItem(ID, allowFile, resultname, item, parameterObject, fields, result, append, thumbLoader);
  223 +}
  224 +
  225 +void CFileItemHandler::HandleFileItem(const char *ID, bool allowFile, const char *resultname, CFileItemPtr item, const CVariant &parameterObject, const std::set<std::string> &validFields, CVariant &result, bool append /* = true */, CThumbLoader *thumbLoader /* = NULL */)
  226 +{
194 227 CVariant object;
195   - bool hasFileField = false;
  228 + std::set<std::string> fields(validFields.begin(), validFields.end());
196 229
197 230 if (item.get())
198 231 {
199   - for (unsigned int i = 0; i < validFields.size(); i++)
200   - {
201   - CStdString field = validFields[i].asString();
202   -
203   - if (field == "file")
204   - hasFileField = true;
205   - }
206   -
207   - if (allowFile && hasFileField)
  232 + std::set<std::string>::const_iterator fileField = fields.find("file");
  233 + if (fileField != fields.end())
208 234 {
209   - if (item->HasVideoInfoTag() && !item->GetVideoInfoTag()->GetPath().IsEmpty())
210   - object["file"] = item->GetVideoInfoTag()->GetPath().c_str();
211   - if (item->HasMusicInfoTag() && !item->GetMusicInfoTag()->GetURL().IsEmpty())
212   - object["file"] = item->GetMusicInfoTag()->GetURL().c_str();
  235 + if (allowFile)
  236 + {
  237 + if (item->HasVideoInfoTag() && !item->GetVideoInfoTag()->GetPath().IsEmpty())
  238 + object["file"] = item->GetVideoInfoTag()->GetPath().c_str();
  239 + if (item->HasMusicInfoTag() && !item->GetMusicInfoTag()->GetURL().IsEmpty())
  240 + object["file"] = item->GetMusicInfoTag()->GetURL().c_str();
213 241
214   - if (!object.isMember("file"))
215   - object["file"] = item->GetPath().c_str();
  242 + if (!object.isMember("file"))
  243 + object["file"] = item->GetPath().c_str();
  244 + }
  245 + fields.erase(fileField);
216 246 }
217 247
218 248 if (ID)
@@ -260,14 +290,14 @@ void CFileItemHandler::HandleFileItem(const char *ID, bool allowFile, const char
260 290 }
261 291 }
262 292
263   - FillDetails(item.get(), item, validFields, object, thumbLoader);
264   -
265 293 if (item->HasVideoInfoTag())
266   - FillDetails(item->GetVideoInfoTag(), item, validFields, object, thumbLoader);
  294 + FillDetails(item->GetVideoInfoTag(), item, fields, object, thumbLoader);
267 295 if (item->HasMusicInfoTag())
268   - FillDetails(item->GetMusicInfoTag(), item, validFields, object, thumbLoader);
  296 + FillDetails(item->GetMusicInfoTag(), item, fields, object, thumbLoader);
269 297 if (item->HasPictureInfoTag())
270   - FillDetails(item->GetPictureInfoTag(), item, validFields, object, thumbLoader);
  298 + FillDetails(item->GetPictureInfoTag(), item, fields, object, thumbLoader);
  299 +
  300 + FillDetails(item.get(), item, fields, object, thumbLoader);
271 301
272 302 if (deleteThumbloader)
273 303 delete thumbLoader;
6 xbmc/interfaces/json-rpc/FileItemHandler.h
@@ -19,6 +19,8 @@
19 19 *
20 20 */
21 21
  22 +#include <set>
  23 +
22 24 #include "JSONRPC.h"
23 25 #include "JSONUtils.h"
24 26 #include "FileItem.h"
@@ -30,13 +32,15 @@ namespace JSONRPC
30 32 class CFileItemHandler : public CJSONUtils
31 33 {
32 34 protected:
33   - static void FillDetails(ISerializable* info, CFileItemPtr item, const CVariant& fields, CVariant &result, CThumbLoader *thumbLoader = NULL);
  35 + static void FillDetails(const ISerializable *info, const CFileItemPtr &item, std::set<std::string> &fields, CVariant &result, CThumbLoader *thumbLoader = NULL);
34 36 static void HandleFileItemList(const char *ID, bool allowFile, const char *resultname, CFileItemList &items, const CVariant &parameterObject, CVariant &result, bool sortLimit = true);
35 37 static void HandleFileItemList(const char *ID, bool allowFile, const char *resultname, CFileItemList &items, const CVariant &parameterObject, CVariant &result, int size, bool sortLimit = true);
36 38 static void HandleFileItem(const char *ID, bool allowFile, const char *resultname, CFileItemPtr item, const CVariant &parameterObject, const CVariant &validFields, CVariant &result, bool append = true, CThumbLoader *thumbLoader = NULL);
  39 + static void HandleFileItem(const char *ID, bool allowFile, const char *resultname, CFileItemPtr item, const CVariant &parameterObject, const std::set<std::string> &validFields, CVariant &result, bool append = true, CThumbLoader *thumbLoader = NULL);
37 40
38 41 static bool FillFileItemList(const CVariant &parameterObject, CFileItemList &list);
39 42 private:
40 43 static void Sort(CFileItemList &items, const CVariant& parameterObject);
  44 + static bool GetField(const std::string &field, const CVariant &info, const CFileItemPtr &item, CVariant &result, bool &fetchedArt, CThumbLoader *thumbLoader = NULL);
41 45 };
42 46 }

0 comments on commit e811d6b

Please sign in to comment.
Something went wrong with that request. Please try again.