1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from bson.objectid import ObjectId
10 from concurrent.futures import ThreadPoolExecutor
11 from operator import itemgetter
14 def thread_execute(method, *args, **kwargs):
15 with ThreadPoolExecutor(max_workers=2) as executor:
16 result = executor.submit(method, *args, **kwargs)
20 class MemCursor(object):
21 def __init__(self, collection):
22 self.collection = collection
23 self.count = len(self.collection)
26 def _is_next_exist(self):
27 return self.count != 0
31 return thread_execute(self._is_next_exist)
33 def next_object(self):
35 return self.collection.pop()
37 def sort(self, key_or_list):
38 key = key_or_list[0][0]
39 if key_or_list[0][1] == -1:
44 if key_or_list is not None:
45 self.collection = sorted(self.collection,
46 key=itemgetter(key), reverse=reverse)
49 def limit(self, limit):
50 if limit != 0 and limit < len(self.collection):
51 self.collection = self.collection[0:limit]
62 def _find_one(self, spec_or_id=None, *args):
63 if spec_or_id is not None and not isinstance(spec_or_id, dict):
64 spec_or_id = {"_id": spec_or_id}
65 if '_id' in spec_or_id:
66 spec_or_id['_id'] = str(spec_or_id['_id'])
67 cursor = self._find(spec_or_id, *args)
72 def find_one(self, spec_or_id=None, *args):
73 return thread_execute(self._find_one, spec_or_id, *args)
75 def _insert(self, doc_or_docs, check_keys=True):
79 if isinstance(docs, dict):
86 doc['_id'] = str(ObjectId())
87 if not check_keys or not self._find_one(doc['_id']):
88 ids.append(doc['_id'])
89 self.contents.append(doc_or_docs)
98 def insert(self, doc_or_docs, check_keys=True):
99 return thread_execute(self._insert, doc_or_docs, check_keys)
102 def _compare_date(spec, value):
103 for k, v in spec.iteritems():
104 if k == '$gte' and value >= v:
109 def _in(content, *args):
111 for k, v in arg.iteritems():
112 if k == 'start_date':
113 if not MemDb._compare_date(v, content.get(k)):
115 elif k == 'trust_indicator':
116 if float(content.get(k)) != float(v):
118 elif content.get(k, None) != v:
123 def _find(self, *args):
125 for content in self.contents:
126 if self._in(content, *args):
131 def find(self, *args):
132 return MemCursor(self._find(*args))
134 def _update(self, spec, document):
136 for index in range(len(self.contents)):
137 content = self.contents[index]
138 if self._in(content, spec):
139 for k, v in document.iteritems():
142 self.contents[index] = content
145 def update(self, spec, document):
146 return thread_execute(self._update, spec, document)
148 def _remove(self, spec_or_id=None):
149 if spec_or_id is None:
151 if not isinstance(spec_or_id, dict):
152 spec_or_id = {'_id': spec_or_id}
153 for index in range(len(self.contents)):
154 content = self.contents[index]
155 if self._in(content, spec_or_id):
156 del self.contents[index]
160 def remove(self, spec_or_id=None):
161 return thread_execute(self._remove, spec_or_id)