delta-trace-db 0.0.18.post1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. delta_trace_db-0.0.18.post1/LICENSE +201 -0
  2. delta_trace_db-0.0.18.post1/PKG-INFO +120 -0
  3. delta_trace_db-0.0.18.post1/README.md +107 -0
  4. delta_trace_db-0.0.18.post1/delta_trace_db/__init__.py +1 -0
  5. delta_trace_db-0.0.18.post1/delta_trace_db/db/__init__.py +1 -0
  6. delta_trace_db-0.0.18.post1/delta_trace_db/db/delta_trace_db_collection.py +222 -0
  7. delta_trace_db-0.0.18.post1/delta_trace_db/db/delta_trace_db_core.py +200 -0
  8. delta_trace_db-0.0.18.post1/delta_trace_db/db/util_copy.py +30 -0
  9. delta_trace_db-0.0.18.post1/delta_trace_db/query/__init__.py +0 -0
  10. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/__init__.py +0 -0
  11. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/actor.py +59 -0
  12. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/cause.py +57 -0
  13. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/enum_actor_type.py +7 -0
  14. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/temporal_trace/__init__.py +0 -0
  15. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/temporal_trace/temporal_trace.py +39 -0
  16. delta_trace_db-0.0.18.post1/delta_trace_db/query/cause/temporal_trace/timestamp_node.py +38 -0
  17. delta_trace_db-0.0.18.post1/delta_trace_db/query/enum_query_type.py +21 -0
  18. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/__init__.py +0 -0
  19. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/comparison_node.py +434 -0
  20. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/enum_node_type.py +22 -0
  21. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/enum_value_type.py +11 -0
  22. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/logical_node.py +65 -0
  23. delta_trace_db-0.0.18.post1/delta_trace_db/query/nodes/query_node.py +65 -0
  24. delta_trace_db-0.0.18.post1/delta_trace_db/query/query.py +102 -0
  25. delta_trace_db-0.0.18.post1/delta_trace_db/query/query_builder.py +212 -0
  26. delta_trace_db-0.0.18.post1/delta_trace_db/query/query_execution_result.py +24 -0
  27. delta_trace_db-0.0.18.post1/delta_trace_db/query/query_result.py +59 -0
  28. delta_trace_db-0.0.18.post1/delta_trace_db/query/raw_query_builder.py +234 -0
  29. delta_trace_db-0.0.18.post1/delta_trace_db/query/sort/__init__.py +0 -0
  30. delta_trace_db-0.0.18.post1/delta_trace_db/query/sort/abstract_sort.py +35 -0
  31. delta_trace_db-0.0.18.post1/delta_trace_db/query/sort/multi_sort.py +32 -0
  32. delta_trace_db-0.0.18.post1/delta_trace_db/query/sort/single_sort.py +57 -0
  33. delta_trace_db-0.0.18.post1/delta_trace_db/query/transaction_query.py +47 -0
  34. delta_trace_db-0.0.18.post1/delta_trace_db/query/transaction_query_result.py +47 -0
  35. delta_trace_db-0.0.18.post1/delta_trace_db/query/util_field.py +30 -0
  36. delta_trace_db-0.0.18.post1/delta_trace_db.egg-info/PKG-INFO +120 -0
  37. delta_trace_db-0.0.18.post1/delta_trace_db.egg-info/SOURCES.txt +44 -0
  38. delta_trace_db-0.0.18.post1/delta_trace_db.egg-info/dependency_links.txt +1 -0
  39. delta_trace_db-0.0.18.post1/delta_trace_db.egg-info/requires.txt +1 -0
  40. delta_trace_db-0.0.18.post1/delta_trace_db.egg-info/top_level.txt +1 -0
  41. delta_trace_db-0.0.18.post1/pyproject.toml +20 -0
  42. delta_trace_db-0.0.18.post1/setup.cfg +4 -0
  43. delta_trace_db-0.0.18.post1/tests/test_delta_trace_db.py +390 -0
  44. delta_trace_db-0.0.18.post1/tests/test_node.py +154 -0
  45. delta_trace_db-0.0.18.post1/tests/test_speed.py +243 -0
  46. delta_trace_db-0.0.18.post1/tests/test_transaction.py +97 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,120 @@
1
+ Metadata-Version: 2.4
2
+ Name: delta_trace_db
3
+ Version: 0.0.18.post1
4
+ Summary: The NoSQL in-memory database with class-based functionality and detailed operation history tracking.
5
+ Author: Masahide Mori
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/MasahideMori-SimpleAppli/delta_trace_db_py
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: file-state-manager>=2.0.0
12
+ Dynamic: license-file
13
+
14
+ # delta_trace_db_py
15
+
16
+ Python implementation of DeltaTraceDB.
17
+
18
+ ## Usage
19
+
20
+ Please refer to the [Dart version](https://github.com/MasahideMori-SimpleAppli/delta_trace_db).
21
+ There are some differences,
22
+ such as arguments being snake case by default and an underscore being added after the variable name
23
+ if there is a namespace conflict with a standard function, but the usage is the same.
24
+
25
+ ## Speed
26
+
27
+ This package is an in-memory database, so it is generally fast.
28
+ Currently, there is no mechanism to speed it up, but it works almost the same as a for loop in a program,
29
+ so there is usually no problem with around 100,000 records.
30
+ I recommend that you test it in an actual environment using speed_test.dart in the test folder.
31
+ However, since it consumes RAM capacity according to the amount of data,
32
+ if you need an extremely large database, consider using a general database.
33
+ For reference, below are the results of a speed test (test/speed_test.dart) run on a slightly
34
+ older PC equipped with a Ryzen 3600 CPU.
35
+ The test conditions were chosen to take a sufficiently long time, but I think it will rarely
36
+ cause
37
+ any problems in practical use.
38
+ Please note that speeds also depend on the amount of data, so if you have a lot of large data, it will be slower.
39
+
40
+ ```text
41
+ tests/test_speed.py speed test for 100000 records
42
+ start add
43
+ end add: 339 ms
44
+ start getAll (with object convert)
45
+ end getAll: 659 ms
46
+ returnsLength: 100000
47
+ start save (with json string convert)
48
+ end save: 467 ms
49
+ start load (with json string convert)
50
+ end load: 557 ms
51
+ start search (with object convert)
52
+ end search: 818 ms
53
+ returnsLength: 100000
54
+ start search paging, half limit pre search (with object convert)
55
+ end search paging: 379 ms
56
+ returnsLength: 50000
57
+ start update at half index and last index object
58
+ end update: 96 ms
59
+ start updateOne of half index object
60
+ end updateOne: 31 ms
61
+ start conformToTemplate
62
+ end conformToTemplate: 79 ms
63
+ start delete half object (with object convert)
64
+ end delete: 562 ms
65
+ returnsLength: 50000
66
+ start deleteOne for last object (with object convert)
67
+ end deleteOne: 22 ms
68
+ returnsLength: 1
69
+ ```
70
+
71
+ ## Future plans
72
+
73
+ It is possible to speed up the database, but this is a low priority, so I think that improving
74
+ usability and creating peripheral tools will take priority.
75
+
76
+ ## Support
77
+
78
+ There is essentially no support at this time, but bugs will likely be fixed.
79
+ If you find any issues, please open an issue on Github.
80
+
81
+ ## About version control
82
+
83
+ The C part will be changed at the time of version upgrade.
84
+ However, versions less than 1.0.0 may change the file structure regardless of the following rules.
85
+
86
+ - Changes such as adding variables, structure change that cause problems when reading previous
87
+ files.
88
+ - C.X.X
89
+ - Adding methods, etc.
90
+ - X.C.X
91
+ - Minor changes and bug fixes.
92
+ - X.X.C
93
+
94
+ ## License
95
+
96
+ This software is released under the Apache-2.0 License, see LICENSE file.
97
+
98
+ Copyright 2025 Masahide Mori
99
+
100
+ Licensed under the Apache License, Version 2.0 (the "License");
101
+ you may not use this file except in compliance with the License.
102
+ You may obtain a copy of the License at
103
+
104
+ http://www.apache.org/licenses/LICENSE-2.0
105
+
106
+ Unless required by applicable law or agreed to in writing, software
107
+ distributed under the License is distributed on an "AS IS" BASIS,
108
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
109
+ See the License for the specific language governing permissions and
110
+ limitations under the License.
111
+
112
+ ## Copyright notice
113
+
114
+ The “Dart” name and “Flutter” name are trademarks of Google LLC.
115
+ *The developer of this package is not Google LLC.
116
+
117
+ ## Trademarks
118
+
119
+ The "Python" name is a trademark of the Python Software Foundation.
120
+ *The developer of this package is not affiliated with the Python Software Foundation.
@@ -0,0 +1,107 @@
1
+ # delta_trace_db_py
2
+
3
+ Python implementation of DeltaTraceDB.
4
+
5
+ ## Usage
6
+
7
+ Please refer to the [Dart version](https://github.com/MasahideMori-SimpleAppli/delta_trace_db).
8
+ There are some differences,
9
+ such as arguments being snake case by default and an underscore being added after the variable name
10
+ if there is a namespace conflict with a standard function, but the usage is the same.
11
+
12
+ ## Speed
13
+
14
+ This package is an in-memory database, so it is generally fast.
15
+ Currently, there is no mechanism to speed it up, but it works almost the same as a for loop in a program,
16
+ so there is usually no problem with around 100,000 records.
17
+ I recommend that you test it in an actual environment using speed_test.dart in the test folder.
18
+ However, since it consumes RAM capacity according to the amount of data,
19
+ if you need an extremely large database, consider using a general database.
20
+ For reference, below are the results of a speed test (test/speed_test.dart) run on a slightly
21
+ older PC equipped with a Ryzen 3600 CPU.
22
+ The test conditions were chosen to take a sufficiently long time, but I think it will rarely
23
+ cause
24
+ any problems in practical use.
25
+ Please note that speeds also depend on the amount of data, so if you have a lot of large data, it will be slower.
26
+
27
+ ```text
28
+ tests/test_speed.py speed test for 100000 records
29
+ start add
30
+ end add: 339 ms
31
+ start getAll (with object convert)
32
+ end getAll: 659 ms
33
+ returnsLength: 100000
34
+ start save (with json string convert)
35
+ end save: 467 ms
36
+ start load (with json string convert)
37
+ end load: 557 ms
38
+ start search (with object convert)
39
+ end search: 818 ms
40
+ returnsLength: 100000
41
+ start search paging, half limit pre search (with object convert)
42
+ end search paging: 379 ms
43
+ returnsLength: 50000
44
+ start update at half index and last index object
45
+ end update: 96 ms
46
+ start updateOne of half index object
47
+ end updateOne: 31 ms
48
+ start conformToTemplate
49
+ end conformToTemplate: 79 ms
50
+ start delete half object (with object convert)
51
+ end delete: 562 ms
52
+ returnsLength: 50000
53
+ start deleteOne for last object (with object convert)
54
+ end deleteOne: 22 ms
55
+ returnsLength: 1
56
+ ```
57
+
58
+ ## Future plans
59
+
60
+ It is possible to speed up the database, but this is a low priority, so I think that improving
61
+ usability and creating peripheral tools will take priority.
62
+
63
+ ## Support
64
+
65
+ There is essentially no support at this time, but bugs will likely be fixed.
66
+ If you find any issues, please open an issue on Github.
67
+
68
+ ## About version control
69
+
70
+ The C part will be changed at the time of version upgrade.
71
+ However, versions less than 1.0.0 may change the file structure regardless of the following rules.
72
+
73
+ - Changes such as adding variables, structure change that cause problems when reading previous
74
+ files.
75
+ - C.X.X
76
+ - Adding methods, etc.
77
+ - X.C.X
78
+ - Minor changes and bug fixes.
79
+ - X.X.C
80
+
81
+ ## License
82
+
83
+ This software is released under the Apache-2.0 License, see LICENSE file.
84
+
85
+ Copyright 2025 Masahide Mori
86
+
87
+ Licensed under the Apache License, Version 2.0 (the "License");
88
+ you may not use this file except in compliance with the License.
89
+ You may obtain a copy of the License at
90
+
91
+ http://www.apache.org/licenses/LICENSE-2.0
92
+
93
+ Unless required by applicable law or agreed to in writing, software
94
+ distributed under the License is distributed on an "AS IS" BASIS,
95
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
96
+ See the License for the specific language governing permissions and
97
+ limitations under the License.
98
+
99
+ ## Copyright notice
100
+
101
+ The “Dart” name and “Flutter” name are trademarks of Google LLC.
102
+ *The developer of this package is not Google LLC.
103
+
104
+ ## Trademarks
105
+
106
+ The "Python" name is a trademark of the Python Software Foundation.
107
+ *The developer of this package is not affiliated with the Python Software Foundation.
@@ -0,0 +1,222 @@
1
+ # coding: utf-8
2
+ import functools
3
+ from typing import Any, Callable, Dict, List, Set
4
+ from file_state_manager.cloneable_file import CloneableFile
5
+ from delta_trace_db.db.util_copy import UtilCopy
6
+ from delta_trace_db.query.nodes.query_node import QueryNode
7
+ from delta_trace_db.query.query import Query
8
+ from delta_trace_db.query.query_result import QueryResult
9
+
10
+
11
+ class Collection(CloneableFile):
12
+ class_name = "Collection"
13
+ version = "7"
14
+
15
+ def __init__(self):
16
+ super().__init__()
17
+ self._data: List[Dict[str, Any]] = []
18
+ self.listeners: Set[Callable[[], None]] = set()
19
+ self._is_transaction_mode: bool = False
20
+ self.run_notify_listeners_in_transaction: bool = False
21
+
22
+ def change_transaction_mode(self, is_transaction_mode: bool):
23
+ self._is_transaction_mode = is_transaction_mode
24
+ self.run_notify_listeners_in_transaction = False
25
+
26
+ @classmethod
27
+ def from_dict(cls, src: Dict[str, Any]) -> "Collection":
28
+ instance = cls()
29
+ instance._data = UtilCopy.jsonable_deep_copy(src.get("data", []))
30
+ return instance
31
+
32
+ def to_dict(self) -> Dict[str, Any]:
33
+ return {
34
+ "className": self.class_name,
35
+ "version": self.version,
36
+ "data": UtilCopy.jsonable_deep_copy(self._data)
37
+ }
38
+
39
+ def clone(self) -> "Collection":
40
+ return Collection.from_dict(self.to_dict())
41
+
42
+ @property
43
+ def raw(self) -> List[Dict[str, Any]]:
44
+ return self._data
45
+
46
+ @property
47
+ def length(self) -> int:
48
+ return len(self._data)
49
+
50
+ def add_listener(self, cb: Callable[[], None]):
51
+ self.listeners.add(cb)
52
+
53
+ def remove_listener(self, cb: Callable[[], None]):
54
+ self.listeners.discard(cb)
55
+
56
+ def notify_listeners(self):
57
+ if not self._is_transaction_mode:
58
+ for cb in self.listeners:
59
+ cb()
60
+ else:
61
+ self.run_notify_listeners_in_transaction = True
62
+
63
+ def _evaluate(self, record: Dict[str, Any], node: QueryNode) -> bool:
64
+ return node.evaluate(record)
65
+
66
+ def add_all(self, q: Query) -> QueryResult:
67
+ add_data = UtilCopy.jsonable_deep_copy(q.add_data)
68
+ self._data.extend(add_data)
69
+ self.notify_listeners()
70
+ return QueryResult(True, q.type, [], len(self._data), len(add_data), 0)
71
+
72
+ def update(self, q: Query, is_single_target: bool) -> QueryResult:
73
+ if q.return_data:
74
+ r = []
75
+ for item in self._data:
76
+ if self._evaluate(item, q.query_node):
77
+ item.update(UtilCopy.jsonable_deep_copy(q.override_data))
78
+ r.append(item)
79
+ if is_single_target:
80
+ break
81
+ if r:
82
+ # 要素が空ではないなら通知を発行。
83
+ self.notify_listeners()
84
+ return QueryResult(True, q.type, UtilCopy.jsonable_deep_copy(r), len(self._data), len(r), len(r))
85
+ else:
86
+ count = 0
87
+ for item in self._data:
88
+ if self._evaluate(item, q.query_node):
89
+ item.update(UtilCopy.jsonable_deep_copy(q.override_data))
90
+ count += 1
91
+ if is_single_target:
92
+ break
93
+ if count > 0:
94
+ self.notify_listeners()
95
+ return QueryResult(True, q.type, [], len(self._data), count, count)
96
+
97
+ def delete(self, q: Query) -> QueryResult:
98
+ if q.return_data:
99
+ deleted_items = [item for item in self._data if self._evaluate(item, q.query_node)]
100
+ self._data = [item for item in self._data if not self._evaluate(item, q.query_node)]
101
+ if deleted_items:
102
+ self.notify_listeners()
103
+ return QueryResult(True, q.type, UtilCopy.jsonable_deep_copy(deleted_items), len(self._data),
104
+ len(deleted_items),
105
+ len(deleted_items))
106
+ else:
107
+ count = sum(1 for item in self._data if self._evaluate(item, q.query_node))
108
+ self._data = [item for item in self._data if not self._evaluate(item, q.query_node)]
109
+ if count > 0:
110
+ self.notify_listeners()
111
+ return QueryResult(True, q.type, [], len(self._data), count, count)
112
+
113
+ def delete_one(self, q: Query) -> QueryResult:
114
+ deleted_items = []
115
+ for i, item in enumerate(self._data):
116
+ if self._evaluate(item, q.query_node):
117
+ deleted_items.append(item)
118
+ del self._data[i]
119
+ break
120
+ if deleted_items:
121
+ self.notify_listeners()
122
+ return QueryResult(True, q.type, UtilCopy.jsonable_deep_copy(deleted_items), len(self._data),
123
+ len(deleted_items),
124
+ len(deleted_items))
125
+
126
+ def search(self, q: Query) -> QueryResult:
127
+ r: List[Dict[str, Any]] = []
128
+ # 検索
129
+ for item in self._data:
130
+ if self._evaluate(item, q.query_node):
131
+ r.append(item)
132
+ hit_count = len(r)
133
+ # ソートやページングのオプション
134
+ if q.sort_obj is not None:
135
+ sorted_list = list(r)
136
+ sorted_list.sort(key=functools.cmp_to_key(q.sort_obj.get_comparator()))
137
+ r = sorted_list
138
+ if q.offset is not None:
139
+ if q.offset > 0:
140
+ r = r[q.offset:]
141
+ else:
142
+ if q.start_after is not None:
143
+ try:
144
+ index = r.index(q.start_after)
145
+ if index != -1 and index + 1 < len(r):
146
+ r = r[index + 1:]
147
+ elif index != -1 and index + 1 >= len(r):
148
+ r = []
149
+ except ValueError:
150
+ pass
151
+ elif q.end_before is not None:
152
+ try:
153
+ index = r.index(q.end_before)
154
+ if index != -1:
155
+ r = r[:index]
156
+ except ValueError:
157
+ pass
158
+ if q.limit is not None:
159
+ if q.offset is None and q.start_after is None and q.end_before is not None:
160
+ r = r[-q.limit:] if len(r) > q.limit else r
161
+ else:
162
+ r = r[:q.limit]
163
+ return QueryResult(
164
+ is_success=True,
165
+ type_=q.type,
166
+ result=UtilCopy.jsonable_deep_copy(r),
167
+ db_length=len(self._data),
168
+ update_count=0,
169
+ hit_count=hit_count,
170
+ )
171
+
172
+ def get_all(self, q: Query) -> QueryResult:
173
+ r = UtilCopy.jsonable_deep_copy(self._data)
174
+ if q.sort_obj:
175
+ r.sort(key=q.sort_obj.get_comparator())
176
+ return QueryResult(True, q.type, r, len(self._data), 0, len(r))
177
+
178
+ def conform_to_template(self, q: Query) -> QueryResult:
179
+ for item in self._data:
180
+ keys_to_remove = [k for k in item.keys() if k not in q.template]
181
+ for k in keys_to_remove:
182
+ item.pop(k)
183
+ for k, v in q.template.items():
184
+ if k not in item:
185
+ item[k] = UtilCopy.jsonable_deep_copy(v)
186
+ self.notify_listeners()
187
+ return QueryResult(True, q.type, [], len(self._data), len(self._data), len(self._data))
188
+
189
+ def rename_field(self, q: Query) -> QueryResult:
190
+ r = []
191
+ for item in self._data:
192
+ if q.rename_before not in item:
193
+ return QueryResult(False, q.type, [], len(self._data), 0, 0,
194
+ f'The target key does not exist. key:{q.rename_before}')
195
+ if q.rename_after in item:
196
+ return QueryResult(False, q.type, [], len(self._data), 0, 0,
197
+ f'An existing key was specified as the new key. key:{q.rename_after}')
198
+ update_count = 0
199
+ for item in self._data:
200
+ item[q.rename_after] = item[q.rename_before]
201
+ del item[q.rename_before]
202
+ update_count += 1
203
+ if q.return_data:
204
+ r.append(item)
205
+ self.notify_listeners()
206
+ return QueryResult(True, q.type, UtilCopy.jsonable_deep_copy(r), len(self._data), update_count, update_count)
207
+
208
+ def count(self, q: Query) -> QueryResult:
209
+ return QueryResult(True, q.type, [], len(self._data), 0, len(self._data))
210
+
211
+ def clear(self, q: Query) -> QueryResult:
212
+ pre_len = len(self._data)
213
+ self._data.clear()
214
+ self.notify_listeners()
215
+ return QueryResult(True, q.type, [], 0, pre_len, pre_len)
216
+
217
+ def clear_add(self, q: Query) -> QueryResult:
218
+ pre_len = len(self._data)
219
+ self._data.clear()
220
+ self._data.extend(UtilCopy.jsonable_deep_copy(q.add_data))
221
+ self.notify_listeners()
222
+ return QueryResult(True, q.type, [], len(self._data), pre_len, pre_len)