Source

CodernityDB / CodernityDB / tree_index.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011-2013 Codernity (http://codernity.com)
#
# 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 index import Index, IndexException, DocIdNotFound, ElemNotFound
import struct
import marshal
import os
import io
import shutil
from storage import IU_Storage
# from ipdb import set_trace

from CodernityDB.env import cdb_environment
from CodernityDB.index import TryReindexException

if cdb_environment.get('rlock_obj'):
    from CodernityDB import patch
    patch.patch_cache_rr(cdb_environment['rlock_obj'])

from CodernityDB.rr_cache import cache1lvl, cache2lvl

tree_buffer_size = io.DEFAULT_BUFFER_SIZE

cdb_environment['tree_buffer_size'] = tree_buffer_size


MODE_FIRST = 0
MODE_LAST = 1

MOVE_BUFFER_PREV = 0
MOVE_BUFFER_NEXT = 1


class NodeCapacityException(IndexException):
    pass


class IU_TreeBasedIndex(Index):

    custom_header = 'from CodernityDB.tree_index import TreeBasedIndex'

    def __init__(self, db_path, name, key_format='32s', pointer_format='I',
                 meta_format='32sIIc', node_capacity=10, storage_class=None):
        if node_capacity < 3:
            raise NodeCapacityException
        super(IU_TreeBasedIndex, self).__init__(db_path, name)
        self.data_start = self._start_ind + 1
        self.node_capacity = node_capacity
        self.flag_format = 'c'
        self.elements_counter_format = 'h'
        self.pointer_format = pointer_format
        self.key_format = key_format
        self.meta_format = meta_format
        self._count_props()
        if not storage_class:
            storage_class = IU_Storage
        if storage_class and not isinstance(storage_class, basestring):
            storage_class = storage_class.__name__
        self.storage_class = storage_class
        self.storage = None
        cache = cache1lvl(100)
        twolvl_cache = cache2lvl(150)
        self._find_key = cache(self._find_key)
        self._match_doc_id = cache(self._match_doc_id)
# self._read_single_leaf_record =
# twolvl_cache(self._read_single_leaf_record)
        self._find_key_in_leaf = twolvl_cache(self._find_key_in_leaf)
        self._read_single_node_key = twolvl_cache(self._read_single_node_key)
        self._find_first_key_occurence_in_node = twolvl_cache(
            self._find_first_key_occurence_in_node)
        self._find_last_key_occurence_in_node = twolvl_cache(
            self._find_last_key_occurence_in_node)
        self._read_leaf_nr_of_elements = cache(self._read_leaf_nr_of_elements)
        self._read_leaf_neighbours = cache(self._read_leaf_neighbours)
        self._read_leaf_nr_of_elements_and_neighbours = cache(
            self._read_leaf_nr_of_elements_and_neighbours)
        self._read_node_nr_of_elements_and_children_flag = cache(
            self._read_node_nr_of_elements_and_children_flag)

    def _count_props(self):
        """
        Counts dynamic properties for tree, such as all complex formats
        """
        self.single_leaf_record_format = self.key_format + self.meta_format
        self.single_node_record_format = self.pointer_format + \
            self.key_format + self.pointer_format
        self.node_format = self.elements_counter_format + self.flag_format\
            + self.pointer_format + (self.key_format +
                                     self.pointer_format) * self.node_capacity
        self.leaf_format = self.elements_counter_format + self.pointer_format * 2\
            + (self.single_leaf_record_format) * self.node_capacity
        self.leaf_heading_format = self.elements_counter_format + \
            self.pointer_format * 2
        self.node_heading_format = self.elements_counter_format + \
            self.flag_format
        self.key_size = struct.calcsize('<' + self.key_format)
        self.meta_size = struct.calcsize('<' + self.meta_format)
        self.single_leaf_record_size = struct.calcsize('<' + self.
                                                       single_leaf_record_format)
        self.single_node_record_size = struct.calcsize('<' + self.
                                                       single_node_record_format)
        self.node_size = struct.calcsize('<' + self.node_format)
        self.leaf_size = struct.calcsize('<' + self.leaf_format)
        self.flag_size = struct.calcsize('<' + self.flag_format)
        self.elements_counter_size = struct.calcsize('<' + self.
                                                     elements_counter_format)
        self.pointer_size = struct.calcsize('<' + self.pointer_format)
        self.leaf_heading_size = struct.calcsize(
            '<' + self.leaf_heading_format)
        self.node_heading_size = struct.calcsize(
            '<' + self.node_heading_format)

    def create_index(self):
        if os.path.isfile(os.path.join(self.db_path, self.name + '_buck')):
            raise IndexException('Already exists')
        with io.open(os.path.join(self.db_path, self.name + "_buck"), 'w+b') as f:
            props = dict(name=self.name,
                         flag_format=self.flag_format,
                         pointer_format=self.pointer_format,
                         elements_counter_format=self.elements_counter_format,
                         node_capacity=self.node_capacity,
                         key_format=self.key_format,
                         meta_format=self.meta_format,
                         version=self.__version__,
                         storage_class=self.storage_class)
            f.write(marshal.dumps(props))
        self.buckets = io.open(os.path.join(self.db_path, self.name +
                                            "_buck"), 'r+b', buffering=0)
        self._create_storage()
        self.buckets.seek(self._start_ind)
        self.buckets.write(struct.pack('<c', 'l'))
        self._insert_empty_root()
        self.root_flag = 'l'

    def destroy(self):
        super(IU_TreeBasedIndex, self).destroy()
        self._clear_cache()

    def open_index(self):
        if not os.path.isfile(os.path.join(self.db_path, self.name + '_buck')):
            raise IndexException("Doesn't exists")
        self.buckets = io.open(
            os.path.join(self.db_path, self.name + "_buck"), 'r+b', buffering=0)
        self.buckets.seek(self._start_ind)
        self.root_flag = struct.unpack('<c', self.buckets.read(1))[0]
        self._fix_params()
        self._open_storage()

    def _insert_empty_root(self):
        self.buckets.seek(self.data_start)
        root = struct.pack('<' + self.leaf_heading_format,
                           0,
                           0,
                           0)
        root += self.single_leaf_record_size * self.node_capacity * '\x00'
        self.buckets.write(root)
        self.flush()

    def insert(self, doc_id, key, start, size, status='o'):
        nodes_stack, indexes = self._find_leaf_to_insert(key)
        self._insert_new_record_into_leaf(nodes_stack.pop(),
                                          key,
                                          doc_id,
                                          start,
                                          size,
                                          status,
                                          nodes_stack,
                                          indexes)

        self._match_doc_id.delete(doc_id)

    def _read_leaf_nr_of_elements_and_neighbours(self, leaf_start):
        self.buckets.seek(leaf_start)
        data = self.buckets.read(
            self.elements_counter_size + 2 * self.pointer_size)
        nr_of_elements, prev_l, next_l = struct.unpack(
            '<' + self.elements_counter_format + 2 * self.pointer_format,
            data)
        return nr_of_elements, prev_l, next_l

    def _read_node_nr_of_elements_and_children_flag(self, start):
        self.buckets.seek(start)
        data = self.buckets.read(self.elements_counter_size + self.flag_size)
        nr_of_elements, children_flag = struct.unpack(
            '<' + self.elements_counter_format + self.flag_format,
            data)
        return nr_of_elements, children_flag

    def _read_leaf_nr_of_elements(self, start):
        self.buckets.seek(start)
        data = self.buckets.read(self.elements_counter_size)
        nr_of_elements = struct.unpack(
            '<' + self.elements_counter_format, data)
        return nr_of_elements[0]

    def _read_single_node_key(self, node_start, key_index):
        self.buckets.seek(self._calculate_key_position(
            node_start, key_index, 'n'))
        data = self.buckets.read(self.single_node_record_size)
        flag_left, key, pointer_right = struct.unpack(
            '<' + self.single_node_record_format, data)
        return flag_left, key, pointer_right

    def _read_single_leaf_record(self, leaf_start, key_index):
        self.buckets.seek(self._calculate_key_position(
            leaf_start, key_index, 'l'))
        data = self.buckets.read(self.single_leaf_record_size)
        key, doc_id, start, size, status = struct.unpack('<' + self.
                                                         single_leaf_record_format, data)
        return key, doc_id, start, size, status

    def _calculate_key_position(self, start, key_index, flag):
        """
        Calculates position of key in buckets file
        """
        if flag == 'l':
            return start + self.leaf_heading_size + key_index * self.single_leaf_record_size
        elif flag == 'n':
#            returns start position of flag before key[key_index]
            return start + self.node_heading_size + key_index * (self.pointer_size + self.key_size)

    def _match_doc_id(self, doc_id, key, element_index, leaf_start, nr_of_elements):
        curr_key_index = element_index + 1
        curr_leaf_start = leaf_start
        next_leaf = self._read_leaf_neighbours(leaf_start)[1]
        while True:
            if curr_key_index < nr_of_elements:
                curr_key, curr_doc_id, curr_start, curr_size,\
                    curr_status = self._read_single_leaf_record(
                        curr_leaf_start, curr_key_index)
                if key != curr_key:
#                    should't happen, crashes earlier on id index
                    raise DocIdNotFound
                elif doc_id == curr_doc_id and curr_status != 'd':
                    return curr_leaf_start, nr_of_elements, curr_key_index
                else:
                    curr_key_index = curr_key_index + 1
            else:  # there are no more elements in current leaf, must jump to next
                if not next_leaf:  # end of leaf linked list
#                    should't happen, crashes earlier on id index
                    raise DocIdNotFound
                else:
                    curr_leaf_start = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                    curr_key_index = 0

    def _find_existing(self, key, element_index, leaf_start, nr_of_elements):
        curr_key_index = element_index + 1
        curr_leaf_start = leaf_start
        next_leaf = self._read_leaf_neighbours(leaf_start)[1]
        while True:
            if curr_key_index < nr_of_elements:
                curr_key, curr_doc_id, curr_start, curr_size,\
                    curr_status = self._read_single_leaf_record(
                        curr_leaf_start, curr_key_index)
                if key != curr_key:
                    raise ElemNotFound
                elif curr_status != 'd':
                    return curr_leaf_start, nr_of_elements, curr_key_index
                else:
                    curr_key_index = curr_key_index + 1
            else:  # there are no more elements in current leaf, must jump to next
                if not next_leaf:  # end of leaf linked list
                    raise ElemNotFound
                else:
                    curr_leaf_start = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                    curr_key_index = 0

    def _update_element(self, leaf_start, key_index, new_data):
        self.buckets.seek(self._calculate_key_position(leaf_start, key_index, 'l')
                          + self.key_size)
        self.buckets.write(struct.pack('<' + self.meta_format,
                                       *new_data))

#        self._read_single_leaf_record.delete(leaf_start_position, key_index)

    def _delete_element(self, leaf_start, key_index):
        self.buckets.seek(self._calculate_key_position(leaf_start, key_index, 'l')
                          + self.single_leaf_record_size - 1)
        self.buckets.write(struct.pack('<c', 'd'))

#        self._read_single_leaf_record.delete(leaf_start_position, key_index)

    def _leaf_linear_key_search(self, key, start, start_index, end_index):
        self.buckets.seek(start)
        data = self.buckets.read(
            (end_index - start_index + 1) * self.single_leaf_record_size)
        curr_key = struct.unpack(
            '<' + self.key_format, data[:self.key_size])[0]
        data = data[self.single_leaf_record_size:]
        curr_index = 0
        while curr_key != key:
            curr_index += 1
            curr_key = struct.unpack(
                '<' + self.key_format, data[:self.key_size])[0]
            data = data[self.single_leaf_record_size:]
        return start_index + curr_index

    def _node_linear_key_search(self, key, start, start_index, end_index):
        self.buckets.seek(start + self.pointer_size)
        data = self.buckets.read((end_index - start_index + 1) * (
            self.key_size + self.pointer_size))
        curr_key = struct.unpack(
            '<' + self.key_format, data[:self.key_size])[0]
        data = data[self.key_size + self.pointer_size:]
        curr_index = 0
        while curr_key != key:
            curr_index += 1
            curr_key = struct.unpack(
                '<' + self.key_format, data[:self.key_size])[0]
            data = data[self.key_size + self.pointer_size:]
        return start_index + curr_index

    def _next_buffer(self, buffer_start, buffer_end):
            return buffer_end, buffer_end + tree_buffer_size

    def _prev_buffer(self, buffer_start, buffer_end):
            return buffer_start - tree_buffer_size, buffer_start

    def _choose_next_candidate_index_in_leaf(self, leaf_start, candidate_start, buffer_start, buffer_end, imin, imax):
        if buffer_start > candidate_start:
            move_buffer = MOVE_BUFFER_PREV
        elif buffer_end < candidate_start + self.single_leaf_record_size:
            move_buffer = MOVE_BUFFER_NEXT
        else:
            move_buffer = None
        return self._calculate_key_position(leaf_start, (imin + imax) / 2, 'l'), (imin + imax) / 2, move_buffer

    def _choose_next_candidate_index_in_node(self, node_start, candidate_start, buffer_start, buffer_end, imin, imax):
        if buffer_start > candidate_start:
            move_buffer = MOVE_BUFFER_PREV
        elif buffer_end < candidate_start + self.single_node_record_size:
            (self.pointer_size + self.key_size) - 1
            move_buffer = MOVE_BUFFER_NEXT
        else:
            move_buffer = None
        return self._calculate_key_position(node_start, (imin + imax) / 2, 'n'), (imin + imax) / 2, move_buffer

    def _find_key_in_leaf(self, leaf_start, key, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_leaf_with_one_element(key, leaf_start)[-5:]
        else:
            return self._find_key_in_leaf_using_binary_search(key, leaf_start, nr_of_elements)[-5:]

    def _find_key_in_leaf_for_update(self, key, doc_id, leaf_start, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_leaf_with_one_element(key, leaf_start, doc_id=doc_id)
        else:
            return self._find_key_in_leaf_using_binary_search(key, leaf_start, nr_of_elements, mode=MODE_FIRST, doc_id=doc_id)

    def _find_index_of_first_key_equal_or_smaller_key(self, key, leaf_start, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_leaf_with_one_element(key, leaf_start, mode=MODE_FIRST, return_closest=True)[:2]
        else:
            return self._find_key_in_leaf_using_binary_search(key, leaf_start, nr_of_elements, mode=MODE_FIRST, return_closest=True)[:2]

    def _find_index_of_last_key_equal_or_smaller_key(self, key, leaf_start, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_leaf_with_one_element(key, leaf_start, mode=MODE_LAST, return_closest=True)[:2]
        else:
            return self._find_key_in_leaf_using_binary_search(key, leaf_start, nr_of_elements, mode=MODE_LAST, return_closest=True)[:2]

    def _find_index_of_first_key_equal(self, key, leaf_start, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_leaf_with_one_element(key, leaf_start, mode=MODE_FIRST)[:2]
        else:
            return self._find_key_in_leaf_using_binary_search(key, leaf_start, nr_of_elements, mode=MODE_FIRST)[:2]

    def _find_key_in_leaf_with_one_element(self, key, leaf_start, doc_id=None, mode=None, return_closest=False):
        curr_key, curr_doc_id, curr_start, curr_size,\
            curr_status = self._read_single_leaf_record(leaf_start, 0)
        if key != curr_key:
            if return_closest and curr_status != 'd':
                return leaf_start, 0
            else:
                raise ElemNotFound
        else:
            if curr_status == 'd':
                raise ElemNotFound
            elif doc_id is not None and doc_id != curr_doc_id:
#                    should't happen, crashes earlier on id index
                raise DocIdNotFound
            else:
                return leaf_start, 0, curr_doc_id, curr_key, curr_start, curr_size, curr_status

    def _find_key_in_leaf_using_binary_search(self, key, leaf_start, nr_of_elements, doc_id=None, mode=None, return_closest=False):
        """
        Binary search implementation used in all get functions
        """
        imin, imax = 0, nr_of_elements - 1
        buffer_start, buffer_end = self._set_buffer_limits()
        candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                  self._calculate_key_position(leaf_start,
                                                                                                                               (imin + imax) / 2,
                                                                                                                               'l'),
                                                                                                  buffer_start,
                                                                                                  buffer_end,
                                                                                                  imin, imax)
        while imax != imin and imax > imin:
            curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                      candidate_index)
            candidate_start = self._calculate_key_position(
                leaf_start, candidate_index, 'l')
            if key < curr_key:
                if move_buffer == MOVE_BUFFER_PREV:
                    buffer_start, buffer_end = self._prev_buffer(
                        buffer_start, buffer_end)
                else:  # if next chosen element is in current buffer, abort moving to other
                    move_buffer is None
                imax = candidate_index - 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)
            elif key == curr_key:
                if mode == MODE_LAST:
                    if move_buffer == MOVE_BUFFER_NEXT:
                        buffer_start, buffer_end = self._next_buffer(
                            buffer_start, buffer_end)
                    else:
                        move_buffer is None
                    imin = candidate_index + 1
                    candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                              candidate_start,
                                                                                                              buffer_start,
                                                                                                              buffer_end,
                                                                                                              imin, imax)
                else:
                    if curr_status == 'o':
                        break
                    else:
                        if move_buffer == MOVE_BUFFER_PREV:
                            buffer_start, buffer_end = self._prev_buffer(
                                buffer_start, buffer_end)
                        else:
                            move_buffer is None
                        imax = candidate_index
                        candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                                  candidate_start,
                                                                                                                  buffer_start,
                                                                                                                  buffer_end,
                                                                                                                  imin, imax)
            else:
                if move_buffer == MOVE_BUFFER_NEXT:
                    buffer_start, buffer_end = self._next_buffer(
                        buffer_start, buffer_end)
                else:
                    move_buffer is None
                imin = candidate_index + 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)

        if imax > imin:
            chosen_key_position = candidate_index
        else:
            chosen_key_position = imax
        curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                  chosen_key_position)
        if key != curr_key:
            if return_closest:  # useful for find all bigger/smaller methods
                return leaf_start, chosen_key_position
            else:
                raise ElemNotFound
        if doc_id and doc_id == curr_doc_id and curr_status == 'o':
            return leaf_start, chosen_key_position, curr_doc_id, curr_key, curr_start, curr_size, curr_status
        else:
            if mode == MODE_FIRST and imin < chosen_key_position:  # check if there isn't any element with equal key before chosen one
                matching_record_index = self._leaf_linear_key_search(key,
                                                                     self._calculate_key_position(leaf_start,
                                                                                                  imin,
                                                                                                  'l'),
                                                                     imin,
                                                                     chosen_key_position)
            else:
                matching_record_index = chosen_key_position
            curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                      matching_record_index)
            if curr_status == 'd' and not return_closest:
                leaf_start, nr_of_elements, matching_record_index = self._find_existing(key,
                                                                                        matching_record_index,
                                                                                        leaf_start,
                                                                                        nr_of_elements)
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                          matching_record_index)
            if doc_id is not None and doc_id != curr_doc_id:
                leaf_start, nr_of_elements, matching_record_index = self._match_doc_id(doc_id,
                                                                                       key,
                                                                                       matching_record_index,
                                                                                       leaf_start,
                                                                                       nr_of_elements)
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                          matching_record_index)
            return leaf_start, matching_record_index, curr_doc_id, curr_key, curr_start, curr_size, curr_status

    def _find_place_in_leaf(self, key, leaf_start, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_place_in_leaf_with_one_element(key, leaf_start)
        else:
            return self._find_place_in_leaf_using_binary_search(key, leaf_start, nr_of_elements)

    def _find_place_in_leaf_with_one_element(self, key, leaf_start):
        curr_key, curr_doc_id, curr_start, curr_size,\
            curr_status = self._read_single_leaf_record(leaf_start, 0)
        if curr_status == 'd':
                return leaf_start, 0, 0, False, True  # leaf start, index of new key position, nr of rec to rewrite, full_leaf flag, on_deleted flag
        else:
            if key < curr_key:
                return leaf_start, 0, 1, False, False
            else:
                return leaf_start, 1, 0, False, False

    def _find_place_in_leaf_using_binary_search(self, key, leaf_start, nr_of_elements):
        """
        Binary search implementation used in insert function
        """
        imin, imax = 0, nr_of_elements - 1
        buffer_start, buffer_end = self._set_buffer_limits()
        candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                  self._calculate_key_position(leaf_start,
                                                                                                                               (imin + imax) / 2,
                                                                                                                               'l'),
                                                                                                  buffer_start,
                                                                                                  buffer_end,
                                                                                                  imin, imax)
        while imax != imin and imax > imin:
            curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                      candidate_index)
            candidate_start = self._calculate_key_position(
                leaf_start, candidate_index, 'l')
            if key < curr_key:
                if move_buffer == MOVE_BUFFER_PREV:
                    buffer_start, buffer_end = self._prev_buffer(
                        buffer_start, buffer_end)
                else:  # if next chosen element is in current buffer, abort moving to other
                    move_buffer is None
                imax = candidate_index - 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)
            else:
                if move_buffer == MOVE_BUFFER_NEXT:
                    buffer_start, buffer_end = self._next_buffer(
                        buffer_start, buffer_end)
                else:
                    move_buffer is None
                imin = candidate_index + 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_leaf(leaf_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)
        if imax < imin and imin < nr_of_elements:
            chosen_key_position = imin
        else:
            chosen_key_position = imax
        curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                  chosen_key_position)
        if curr_status == 'd':
            return leaf_start, chosen_key_position, 0, False, True
        elif key < curr_key:
            if chosen_key_position > 0:
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                          chosen_key_position - 1)
                if curr_start == 'd':
                    return leaf_start, chosen_key_position - 1, 0, False, True
                else:
                    return leaf_start, chosen_key_position, nr_of_elements - chosen_key_position, (nr_of_elements == self.node_capacity), False
            else:
                return leaf_start, chosen_key_position, nr_of_elements - chosen_key_position, (nr_of_elements == self.node_capacity), False
        else:
            if chosen_key_position < nr_of_elements - 1:
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_start,
                                                                                                          chosen_key_position + 1)
                if curr_start == 'd':
                    return leaf_start, chosen_key_position + 1, 0, False, True
                else:
                    return leaf_start, chosen_key_position + 1, nr_of_elements - chosen_key_position - 1, (nr_of_elements == self.node_capacity), False
            else:
                return leaf_start, chosen_key_position + 1, nr_of_elements - chosen_key_position - 1, (nr_of_elements == self.node_capacity), False

    def _set_buffer_limits(self):
        pos = self.buckets.tell()
        buffer_start = pos - (pos % tree_buffer_size)
        return buffer_start, (buffer_start + tree_buffer_size)

    def _find_first_key_occurence_in_node(self, node_start, key, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_node_with_one_element(key, node_start, mode=MODE_FIRST)
        else:
            return self._find_key_in_node_using_binary_search(key, node_start, nr_of_elements, mode=MODE_FIRST)

    def _find_last_key_occurence_in_node(self, node_start, key, nr_of_elements):
        if nr_of_elements == 1:
            return self._find_key_in_node_with_one_element(key, node_start, mode=MODE_LAST)
        else:
            return self._find_key_in_node_using_binary_search(key, node_start, nr_of_elements, mode=MODE_LAST)

    def _find_key_in_node_with_one_element(self, key, node_start, mode=None):
        l_pointer, curr_key, r_pointer = self._read_single_node_key(
            node_start, 0)
        if key < curr_key:
            return 0, l_pointer
        elif key > curr_key:
            return 0, r_pointer
        else:
            if mode == MODE_FIRST:
                return 0, l_pointer
            elif mode == MODE_LAST:
                return 0, r_pointer
            else:
                raise Exception('Invalid mode declared: set first/last')

    def _find_key_in_node_using_binary_search(self, key, node_start, nr_of_elements, mode=None):
        imin, imax = 0, nr_of_elements - 1
        buffer_start, buffer_end = self._set_buffer_limits()
        candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_node(node_start,
                                                                                                  self._calculate_key_position(node_start,
                                                                                                                               (imin + imax) / 2,
                                                                                                                               'n'),
                                                                                                  buffer_start,
                                                                                                  buffer_end,
                                                                                                  imin, imax)
        while imax != imin and imax > imin:
            l_pointer, curr_key, r_pointer = self._read_single_node_key(
                node_start, candidate_index)
            candidate_start = self._calculate_key_position(
                node_start, candidate_index, 'n')
            if key < curr_key:
                if move_buffer == MOVE_BUFFER_PREV:
                    buffer_start, buffer_end = self._prev_buffer(
                        buffer_start, buffer_end)
                else:  # if next chosen element is in current buffer, abort moving to other
                    move_buffer is None
                imax = candidate_index - 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_node(node_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)
            elif key == curr_key:
                if mode == MODE_LAST:
                    if move_buffer == MOVE_BUFFER_NEXT:
                        buffer_start, buffer_end = self._next_buffer(
                            buffer_start, buffer_end)
                    else:
                        move_buffer is None
                    imin = candidate_index + 1
                    candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_node(node_start,
                                                                                                              candidate_start,
                                                                                                              buffer_start,
                                                                                                              buffer_end,
                                                                                                              imin, imax)
                else:
                    break
            else:
                if move_buffer == MOVE_BUFFER_NEXT:
                        buffer_start, buffer_end = self._next_buffer(
                            buffer_start, buffer_end)
                else:
                    move_buffer is None
                imin = candidate_index + 1
                candidate_start, candidate_index, move_buffer = self._choose_next_candidate_index_in_node(node_start,
                                                                                                          candidate_start,
                                                                                                          buffer_start,
                                                                                                          buffer_end,
                                                                                                          imin, imax)

        if imax > imin:
            chosen_key_position = candidate_index
        elif imax < imin and imin < nr_of_elements:
            chosen_key_position = imin
        else:
            chosen_key_position = imax
        l_pointer, curr_key, r_pointer = self._read_single_node_key(
            node_start, chosen_key_position)
        if mode == MODE_FIRST and imin < chosen_key_position:  # check if there is no elements with equal key before chosen one
            matching_record_index = self._node_linear_key_search(key,
                                                                 self._calculate_key_position(node_start,
                                                                                              imin,
                                                                                              'n'),
                                                                 imin,
                                                                 chosen_key_position)
        else:
            matching_record_index = chosen_key_position
        l_pointer, curr_key, r_pointer = self._read_single_node_key(
            node_start, matching_record_index)
        if key < curr_key:
            return matching_record_index, l_pointer
        elif key > curr_key:
            return matching_record_index, r_pointer
        else:
            if mode == MODE_FIRST:
                return matching_record_index, l_pointer
            elif mode == MODE_LAST:
                return matching_record_index, r_pointer
            else:
                raise Exception('Invalid mode declared: first/last')

    def _update_leaf_ready_data(self, leaf_start, start_index, new_nr_of_elements, records_to_rewrite):
        self.buckets.seek(leaf_start)
        self.buckets.write(struct.pack('<h', new_nr_of_elements))
        start_position = self._calculate_key_position(
            leaf_start, start_index, 'l')
        self.buckets.seek(start_position)
        self.buckets.write(
            struct.pack(
                '<' + (new_nr_of_elements - start_index) *
                self.single_leaf_record_format,
                *records_to_rewrite))

#        self._read_single_leaf_record.delete(leaf_start)
        self._read_leaf_nr_of_elements.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)

    def _update_leaf(self, leaf_start, new_record_position, nr_of_elements,
                     nr_of_records_to_rewrite, on_deleted, new_key,
                     new_doc_id, new_start, new_size, new_status):
        if nr_of_records_to_rewrite == 0:  # just write at set position
            self.buckets.seek(self._calculate_key_position(
                leaf_start, new_record_position, 'l'))
            self.buckets.write(
                struct.pack('<' + self.single_leaf_record_format,
                            new_key,
                            new_doc_id,
                            new_start,
                            new_size,
                            new_status))
            self.flush()
        else:  # must read all elems after new one, and rewrite them after new
            start = self._calculate_key_position(
                leaf_start, new_record_position, 'l')
            self.buckets.seek(start)
            data = self.buckets.read(nr_of_records_to_rewrite *
                                     self.single_leaf_record_size)
            records_to_rewrite = struct.unpack('<' + nr_of_records_to_rewrite *
                                               self.single_leaf_record_format, data)
            curr_index = 0
            records_to_rewrite = list(records_to_rewrite)
            for status in records_to_rewrite[4::5]:  # don't write back deleted records, deleting them from list
                if status != 'o':
                    del records_to_rewrite[curr_index * 5:curr_index * 5 + 5]
                    nr_of_records_to_rewrite -= 1
                    nr_of_elements -= 1
                else:
                    curr_index += 1

            self.buckets.seek(start)
            self.buckets.write(
                struct.pack(
                    '<' + (nr_of_records_to_rewrite +
                           1) * self.single_leaf_record_format,
                    new_key,
                    new_doc_id,
                    new_start,
                    new_size,
                    new_status,
                    *tuple(records_to_rewrite)))
            self.flush()
        self.buckets.seek(leaf_start)
        if not on_deleted:  # when new record replaced deleted one, nr of leaf elements stays the same
            self.buckets.write(struct.pack('<h', nr_of_elements + 1))

        self._read_leaf_nr_of_elements.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)
        self._find_key_in_leaf.delete(leaf_start)
#        self._read_single_leaf_record.delete(leaf_start)

    def _read_leaf_neighbours(self, leaf_start):
        self.buckets.seek(leaf_start + self.elements_counter_size)
        neihbours_data = self.buckets.read(2 * self.pointer_size)
        prev_l, next_l = struct.unpack(
            '<' + 2 * self.pointer_format, neihbours_data)
        return prev_l, next_l

    def _update_leaf_size_and_pointers(self, leaf_start, new_size, new_prev, new_next):
        self.buckets.seek(leaf_start)
        self.buckets.write(
            struct.pack(
                '<' + self.elements_counter_format + 2 * self.pointer_format,
                new_size,
                new_prev,
                new_next))

        self._read_leaf_nr_of_elements.delete(leaf_start)
        self._read_leaf_neighbours.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)

    def _update_leaf_prev_pointer(self, leaf_start, pointer):
        self.buckets.seek(leaf_start + self.elements_counter_size)
        self.buckets.write(struct.pack('<' + self.pointer_format,
                                       pointer))

        self._read_leaf_neighbours.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)

    def _update_size(self, start, new_size):
        self.buckets.seek(start)
        self.buckets.write(struct.pack('<' + self.elements_counter_format,
                                       new_size))

        self._read_leaf_nr_of_elements.delete(start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(start)

    def _create_new_root_from_leaf(self, leaf_start, nr_of_records_to_rewrite, new_leaf_size, old_leaf_size, half_size, new_data):
        blanks = (self.node_capacity - new_leaf_size) * \
            self.single_leaf_record_size * '\x00'
        left_leaf_start_position = self.data_start + self.node_size
        right_leaf_start_position = self.data_start + \
            self.node_size + self.leaf_size
        self.buckets.seek(self.data_start + self.leaf_heading_size)
        # read old root
        data = self.buckets.read(
            self.single_leaf_record_size * self.node_capacity)
        leaf_data = struct.unpack('<' + self.
                                  single_leaf_record_format * self.node_capacity, data)
        # remove deleted records, if succeded abort spliting
        if self._update_if_has_deleted(self.data_start, leaf_data, 0, new_data):
            return None
        # find out key which goes to parent node
        if nr_of_records_to_rewrite > new_leaf_size - 1:
            key_moved_to_parent_node = leaf_data[(old_leaf_size - 1) * 5]
        elif nr_of_records_to_rewrite == new_leaf_size - 1:
            key_moved_to_parent_node = new_data[0]
        else:
            key_moved_to_parent_node = leaf_data[old_leaf_size * 5]
        data_to_write = self._prepare_new_root_data(key_moved_to_parent_node,
                                                    left_leaf_start_position,
                                                    right_leaf_start_position,
                                                    'l')
        if nr_of_records_to_rewrite > half_size:
                # key goes to first half
                # prepare left leaf data
            left_leaf_data = struct.pack('<' + self.leaf_heading_format + self.single_leaf_record_format
                                         * (self.node_capacity - nr_of_records_to_rewrite),
                                         old_leaf_size,
                                         0,
                                         right_leaf_start_position,
                                         *leaf_data[:-nr_of_records_to_rewrite * 5])
            left_leaf_data += struct.pack(
                '<' + self.single_leaf_record_format * (
                    nr_of_records_to_rewrite - new_leaf_size + 1),
                new_data[0],
                new_data[1],
                new_data[2],
                new_data[3],
                new_data[4],
                *leaf_data[-nr_of_records_to_rewrite * 5:(old_leaf_size - 1) * 5])
                # prepare right leaf_data
            right_leaf_data = struct.pack('<' + self.elements_counter_format + 2 * self.pointer_format +
                                          self.single_leaf_record_format *
                                          new_leaf_size,
                                          new_leaf_size,
                                          left_leaf_start_position,
                                          0,
                                          *leaf_data[-new_leaf_size * 5:])
        else:
                # key goes to second half
            if nr_of_records_to_rewrite:
                records_before = leaf_data[old_leaf_size *
                                           5:-nr_of_records_to_rewrite * 5]
                records_after = leaf_data[-nr_of_records_to_rewrite * 5:]
            else:
                records_before = leaf_data[old_leaf_size * 5:]
                records_after = []

            left_leaf_data = struct.pack(
                '<' + self.leaf_heading_format +
                self.single_leaf_record_format * old_leaf_size,
                old_leaf_size,
                0,
                right_leaf_start_position,
                *leaf_data[:old_leaf_size * 5])
                # prepare right leaf_data
            right_leaf_data = struct.pack('<' + self.elements_counter_format + 2 * self.pointer_format +
                                          self.single_leaf_record_format * (new_leaf_size -
                                                                            nr_of_records_to_rewrite - 1),
                                          new_leaf_size,
                                          left_leaf_start_position,
                                          0,
                                          *records_before)
            right_leaf_data += struct.pack(
                '<' + self.single_leaf_record_format * (
                    nr_of_records_to_rewrite + 1),
                new_data[0],
                new_data[1],
                new_data[2],
                new_data[3],
                new_data[4],
                *records_after)
        left_leaf_data += (self.node_capacity -
                           old_leaf_size) * self.single_leaf_record_size * '\x00'
        right_leaf_data += blanks
        data_to_write += left_leaf_data
        data_to_write += right_leaf_data
        self.buckets.seek(self._start_ind)
        self.buckets.write(struct.pack('<c', 'n') + data_to_write)
        self.root_flag = 'n'

#            self._read_single_leaf_record.delete(leaf_start)
        self._find_key_in_leaf.delete(leaf_start)
        self._read_leaf_nr_of_elements.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)
        self._read_leaf_neighbours.delete(leaf_start)
        return None

    def _split_leaf(
        self, leaf_start, nr_of_records_to_rewrite, new_key, new_doc_id, new_start, new_size, new_status,
            create_new_root=False):
        """
        Splits full leaf in two separate ones, first half of records stays on old position,
        second half is written as new leaf at the end of file.
        """
        half_size = self.node_capacity / 2
        if self.node_capacity % 2 == 0:
            old_leaf_size = half_size
            new_leaf_size = half_size + 1
        else:
            old_leaf_size = new_leaf_size = half_size + 1
        if create_new_root:  # leaf is a root
            new_data = [new_key, new_doc_id, new_start, new_size, new_status]
            self._create_new_root_from_leaf(leaf_start, nr_of_records_to_rewrite, new_leaf_size, old_leaf_size, half_size, new_data)
        else:
            blanks = (self.node_capacity - new_leaf_size) * \
                self.single_leaf_record_size * '\x00'
            prev_l, next_l = self._read_leaf_neighbours(leaf_start)
            if nr_of_records_to_rewrite > half_size:  # insert key into first half of leaf
                self.buckets.seek(self._calculate_key_position(leaf_start,
                                                               self.node_capacity - nr_of_records_to_rewrite,
                                                               'l'))
                # read all records with key>new_key
                data = self.buckets.read(
                    nr_of_records_to_rewrite * self.single_leaf_record_size)
                records_to_rewrite = struct.unpack(
                    '<' + nr_of_records_to_rewrite * self.single_leaf_record_format, data)
                # remove deleted records, if succeded abort spliting
                if self._update_if_has_deleted(leaf_start,
                                               records_to_rewrite,
                                               self.node_capacity -
                                               nr_of_records_to_rewrite,
                                               [new_key, new_doc_id, new_start, new_size, new_status]):
                    return None
                key_moved_to_parent_node = records_to_rewrite[
                    -new_leaf_size * 5]
                # write new leaf at end of file
                self.buckets.seek(0, 2)  # end of file
                new_leaf_start = self.buckets.tell()
                # prepare new leaf_data
                new_leaf = struct.pack('<' + self.elements_counter_format + 2 * self.pointer_format +
                                       self.single_leaf_record_format *
                                       new_leaf_size,
                                       new_leaf_size,
                                       leaf_start,
                                       next_l,
                                       *records_to_rewrite[-new_leaf_size * 5:])
                new_leaf += blanks
                # write new leaf
                self.buckets.write(new_leaf)
                # update old leaf heading
                self._update_leaf_size_and_pointers(leaf_start,
                                                    old_leaf_size,
                                                    prev_l,
                                                    new_leaf_start)
                # seek position of new key in first half
                self.buckets.seek(self._calculate_key_position(leaf_start,
                                                               self.node_capacity - nr_of_records_to_rewrite,
                                                               'l'))
                # write new key and keys after
                self.buckets.write(
                    struct.pack(
                        '<' + self.single_leaf_record_format *
                        (nr_of_records_to_rewrite - new_leaf_size + 1),
                        new_key,
                        new_doc_id,
                        new_start,
                        new_size,
                        'o',
                        *records_to_rewrite[:-new_leaf_size * 5]))

                if next_l:  # when next_l is 0 there is no next leaf to update, avoids writing data at 0 position of file
                    self._update_leaf_prev_pointer(
                        next_l, new_leaf_start)

#                self._read_single_leaf_record.delete(leaf_start)
                self._find_key_in_leaf.delete(leaf_start)

                return new_leaf_start, key_moved_to_parent_node
            else:  # key goes into second half of leaf     '
                # seek half of the leaf
                self.buckets.seek(self._calculate_key_position(
                    leaf_start, old_leaf_size, 'l'))
                data = self.buckets.read(
                    self.single_leaf_record_size * (new_leaf_size - 1))
                records_to_rewrite = struct.unpack('<' + (new_leaf_size - 1) *
                                                   self.single_leaf_record_format, data)
                # remove deleted records, if succeded abort spliting
                if self._update_if_has_deleted(leaf_start,
                                               records_to_rewrite,
                                               old_leaf_size,
                                               [new_key, new_doc_id, new_start, new_size, new_status]):
                    return None
                key_moved_to_parent_node = records_to_rewrite[
                    -(new_leaf_size - 1) * 5]
                if key_moved_to_parent_node > new_key:
                    key_moved_to_parent_node = new_key
                self.buckets.seek(0, 2)  # end of file
                new_leaf_start = self.buckets.tell()
                # prepare new leaf data
                index_of_records_split = nr_of_records_to_rewrite * 5
                if index_of_records_split:
                    records_before = records_to_rewrite[
                        :-index_of_records_split]
                    records_after = records_to_rewrite[
                        -index_of_records_split:]
                else:
                    records_before = records_to_rewrite
                    records_after = []
                new_leaf = struct.pack('<' + self.elements_counter_format + 2 * self.pointer_format
                                       + self.single_leaf_record_format * (new_leaf_size -
                                                                           nr_of_records_to_rewrite - 1),
                                       new_leaf_size,
                                       leaf_start,
                                       next_l,
                                       *records_before)
                new_leaf += struct.pack(
                    '<' + self.single_leaf_record_format *
                    (nr_of_records_to_rewrite + 1),
                    new_key,
                    new_doc_id,
                    new_start,
                    new_size,
                    'o',
                    *records_after)
                new_leaf += blanks
                self.buckets.write(new_leaf)
                self._update_leaf_size_and_pointers(leaf_start,
                                                    old_leaf_size,
                                                    prev_l,
                                                    new_leaf_start)
                if next_l:  # pren next_l is 0 there is no next leaf to update, avoids writing data at 0 position of file
                    self._update_leaf_prev_pointer(
                        next_l, new_leaf_start)

#                self._read_single_leaf_record.delete(leaf_start)
                self._find_key_in_leaf.delete(leaf_start)

                return new_leaf_start, key_moved_to_parent_node

    def _update_if_has_deleted(self, leaf_start, records_to_rewrite, start_position, new_record_data):
        """
        Checks if there are any deleted elements in data to rewrite and prevent from writing then back.
        """
        curr_index = 0
        nr_of_elements = self.node_capacity
        records_to_rewrite = list(records_to_rewrite)
        for status in records_to_rewrite[4::5]:  # remove deleted from list
            if status != 'o':
                del records_to_rewrite[curr_index * 5:curr_index * 5 + 5]
                nr_of_elements -= 1
            else:
                curr_index += 1
        # if were deleted dont have to split, just update leaf
        if nr_of_elements < self.node_capacity:
            data_split_index = 0
            for key in records_to_rewrite[0::5]:
                if key > new_record_data[0]:
                    break
                else:
                    data_split_index += 1
            records_to_rewrite = records_to_rewrite[:data_split_index * 5]\
                + new_record_data\
                + records_to_rewrite[data_split_index * 5:]
            self._update_leaf_ready_data(leaf_start,
                                         start_position,
                                         nr_of_elements + 1,
                                         records_to_rewrite),
            return True
        else:  # did not found any deleted records in leaf
            return False

    def _prepare_new_root_data(self, root_key, left_pointer, right_pointer, children_flag='n'):
        new_root = struct.pack(
            '<' + self.node_heading_format + self.single_node_record_format,
            1,
            children_flag,
            left_pointer,
            root_key,
            right_pointer)
        new_root += (self.key_size + self.pointer_size) * (self.
                                                           node_capacity - 1) * '\x00'
        return new_root

    def _create_new_root_from_node(self, node_start, children_flag, nr_of_keys_to_rewrite, new_node_size, old_node_size, new_key, new_pointer):
            # reading second half of node
            self.buckets.seek(self.data_start + self.node_heading_size)
            # read all keys with key>new_key
            data = self.buckets.read(self.pointer_size + self.
                                     node_capacity * (self.key_size + self.pointer_size))
            old_node_data = struct.unpack('<' + self.pointer_format + self.node_capacity *
                                          (self.key_format + self.pointer_format), data)
            self.buckets.seek(0, 2)  # end of file
            new_node_start = self.buckets.tell()
            if nr_of_keys_to_rewrite == new_node_size:
                key_moved_to_root = new_key
                # prepare new nodes data
                left_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                        old_node_size * (self.
                                                         key_format + self.pointer_format),
                                        old_node_size,
                                        children_flag,
                                        *old_node_data[:old_node_size * 2 + 1])

                right_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                         new_node_size * (self.
                                                          key_format + self.pointer_format),
                                         new_node_size,
                                         children_flag,
                                         new_pointer,
                                         *old_node_data[old_node_size * 2 + 1:])
            elif nr_of_keys_to_rewrite > new_node_size:
                key_moved_to_root = old_node_data[old_node_size * 2 - 1]
                # prepare new nodes data
                if nr_of_keys_to_rewrite == self.node_capacity:
                    keys_before = old_node_data[:1]
                    keys_after = old_node_data[1:old_node_size * 2 - 1]
                else:
                    keys_before = old_node_data[:-nr_of_keys_to_rewrite * 2]
                    keys_after = old_node_data[-(
                        nr_of_keys_to_rewrite) * 2:old_node_size * 2 - 1]
                left_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                        (self.node_capacity - nr_of_keys_to_rewrite) * (self.
                                                                                        key_format + self.pointer_format),
                                        old_node_size,
                                        children_flag,
                                        *keys_before)
                left_node += struct.pack(
                    '<' + (self.key_format + self.pointer_format) *
                    (nr_of_keys_to_rewrite - new_node_size),
                    new_key,
                    new_pointer,
                    *keys_after)

                right_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                         new_node_size * (self.
                                                          key_format + self.pointer_format),
                                         new_node_size,
                                         children_flag,
                                         *old_node_data[old_node_size * 2:])
            else:
#               'inserting key into second half of node and creating new root'
                key_moved_to_root = old_node_data[old_node_size * 2 + 1]
                # prepare new nodes data
                left_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                        old_node_size * (self.
                                                         key_format + self.pointer_format),
                                        old_node_size,
                                        children_flag,
                                        *old_node_data[:old_node_size * 2 + 1])
                if nr_of_keys_to_rewrite:
                    keys_before = old_node_data[(old_node_size +
                                                 1) * 2:-nr_of_keys_to_rewrite * 2]
                    keys_after = old_node_data[-nr_of_keys_to_rewrite * 2:]
                else:
                    keys_before = old_node_data[(old_node_size + 1) * 2:]
                    keys_after = []
                right_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                         (new_node_size - nr_of_keys_to_rewrite - 1) * (self.
                                                                                        key_format + self.pointer_format),
                                         new_node_size,
                                         children_flag,
                                         *keys_before)
                right_node += struct.pack(
                    '<' + (nr_of_keys_to_rewrite + 1) *
                    (self.key_format + self.pointer_format),
                    new_key,
                    new_pointer,
                    *keys_after)
            new_root = self._prepare_new_root_data(key_moved_to_root,
                                                   new_node_start,
                                                   new_node_start + self.node_size)
            left_node += (self.node_capacity - old_node_size) * \
                (self.key_size + self.pointer_size) * '\x00'
            # adding blanks after new node
            right_node += (self.node_capacity - new_node_size) * \
                (self.key_size + self.pointer_size) * '\x00'
            self.buckets.seek(0, 2)
            self.buckets.write(left_node + right_node)
            self.buckets.seek(self.data_start)
            self.buckets.write(new_root)

            self._read_single_node_key.delete(node_start)
            self._read_node_nr_of_elements_and_children_flag.delete(node_start)
            return None

    def _split_node(self, node_start, nr_of_keys_to_rewrite, new_key, new_pointer, children_flag, create_new_root=False):
        """
        Splits full node in two separate ones, first half of records stays on old position,
        second half is written as new leaf at the end of file.
        """
        half_size = self.node_capacity / 2
        if self.node_capacity % 2 == 0:
            old_node_size = new_node_size = half_size
        else:
            old_node_size = half_size
            new_node_size = half_size + 1
        if create_new_root:
            self._create_new_root_from_node(node_start, children_flag, nr_of_keys_to_rewrite, new_node_size, old_node_size, new_key, new_pointer)
        else:
            blanks = (self.node_capacity - new_node_size) * (
                self.key_size + self.pointer_size) * '\x00'
            if nr_of_keys_to_rewrite == new_node_size:  # insert key into first half of node
                # reading second half of node
                self.buckets.seek(self._calculate_key_position(node_start,
                                                               old_node_size,
                                                               'n') + self.pointer_size)
                # read all keys with key>new_key
                data = self.buckets.read(nr_of_keys_to_rewrite *
                                         (self.key_size + self.pointer_size))
                old_node_data = struct.unpack('<' + nr_of_keys_to_rewrite *
                                              (self.key_format + self.pointer_format), data)
                # write new node at end of file
                self.buckets.seek(0, 2)
                new_node_start = self.buckets.tell()
                # prepare new node_data
                new_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                       (self.key_format +
                                        self.pointer_format) * new_node_size,
                                       new_node_size,
                                       children_flag,
                                       new_pointer,
                                       *old_node_data)
                new_node += blanks
                # write new node
                self.buckets.write(new_node)
                # update old node data
                self._update_size(
                    node_start, old_node_size)

                self._read_single_node_key.delete(node_start)
                self._read_node_nr_of_elements_and_children_flag.delete(
                    node_start)

                return new_node_start, new_key
            elif nr_of_keys_to_rewrite > half_size:  # insert key into first half of node
                # seek for first key to rewrite
                self.buckets.seek(self._calculate_key_position(node_start, self.node_capacity - nr_of_keys_to_rewrite, 'n')
                                  + self.pointer_size)
                # read all keys with key>new_key
                data = self.buckets.read(
                    nr_of_keys_to_rewrite * (self.key_size + self.pointer_size))
                old_node_data = struct.unpack(
                    '<' + nr_of_keys_to_rewrite * (self.key_format + self.pointer_format), data)
                key_moved_to_parent_node = old_node_data[-(
                    new_node_size + 1) * 2]
                self.buckets.seek(0, 2)
                new_node_start = self.buckets.tell()
                # prepare new node_data
                new_node = struct.pack('<' + self.node_heading_format +
                                       self.pointer_format + (self.key_format +
                                                              self.pointer_format) * new_node_size,
                                       new_node_size,
                                       children_flag,
                                       old_node_data[-new_node_size * 2 - 1],
                                       *old_node_data[-new_node_size * 2:])
                new_node += blanks
                # write new node
                self.buckets.write(new_node)
                self._update_size(
                    node_start, old_node_size)
                # seek position of new key in first half
                self.buckets.seek(self._calculate_key_position(node_start, self.node_capacity - nr_of_keys_to_rewrite, 'n')
                                  + self.pointer_size)
                # write new key and keys after
                self.buckets.write(
                    struct.pack(
                        '<' + (self.key_format + self.pointer_format) *
                        (nr_of_keys_to_rewrite - new_node_size),
                        new_key,
                        new_pointer,
                        *old_node_data[:-(new_node_size + 1) * 2]))

                self._read_single_node_key.delete(node_start)
                self._read_node_nr_of_elements_and_children_flag.delete(
                    node_start)

                return new_node_start, key_moved_to_parent_node
            else:  # key goes into second half
                # reading second half of node
                self.buckets.seek(self._calculate_key_position(node_start,
                                                               old_node_size,
                                                               'n')
                                  + self.pointer_size)
                data = self.buckets.read(
                    new_node_size * (self.key_size + self.pointer_size))
                old_node_data = struct.unpack('<' + new_node_size *
                                              (self.key_format + self.pointer_format), data)
                # find key which goes to parent node
                key_moved_to_parent_node = old_node_data[0]
                self.buckets.seek(0, 2)  # end of file
                new_node_start = self.buckets.tell()
                index_of_records_split = nr_of_keys_to_rewrite * 2
                # prepare new node_data
                first_leaf_pointer = old_node_data[1]
                old_node_data = old_node_data[2:]
                if index_of_records_split:
                    keys_before = old_node_data[:-index_of_records_split]
                    keys_after = old_node_data[-index_of_records_split:]
                else:
                    keys_before = old_node_data
                    keys_after = []
                new_node = struct.pack('<' + self.node_heading_format + self.pointer_format +
                                       (self.key_format + self.pointer_format) *
                                       (new_node_size -
                                        nr_of_keys_to_rewrite - 1),
                                       new_node_size,
                                       children_flag,
                                       first_leaf_pointer,
                                       *keys_before)
                new_node += struct.pack('<' + (self.key_format + self.pointer_format) *
                                        (nr_of_keys_to_rewrite + 1),
                                        new_key,
                                        new_pointer,
                                        *keys_after)
                new_node += blanks
                # write new node
                self.buckets.write(new_node)
                self._update_size(node_start, old_node_size)

                self._read_single_node_key.delete(node_start)
                self._read_node_nr_of_elements_and_children_flag.delete(
                    node_start)

                return new_node_start, key_moved_to_parent_node

    def insert_first_record_into_leaf(self, leaf_start, key, doc_id, start, size, status):
        self.buckets.seek(leaf_start)
        self.buckets.write(struct.pack('<' + self.elements_counter_format,
                                       1))
        self.buckets.seek(leaf_start + self.leaf_heading_size)
        self.buckets.write(struct.pack('<' + self.single_leaf_record_format,
                                       key,
                                       doc_id,
                                       start,
                                       size,
                                       status))

#            self._read_single_leaf_record.delete(leaf_start)
        self._find_key_in_leaf.delete(leaf_start)
        self._read_leaf_nr_of_elements.delete(leaf_start)
        self._read_leaf_nr_of_elements_and_neighbours.delete(leaf_start)

    def _insert_new_record_into_leaf(self, leaf_start, key, doc_id, start, size, status, nodes_stack, indexes):
        nr_of_elements = self._read_leaf_nr_of_elements(leaf_start)
        if nr_of_elements == 0:
            self.insert_first_record_into_leaf(
                leaf_start, key, doc_id, start, size, status)
            return
        leaf_start, new_record_position, nr_of_records_to_rewrite, full_leaf, on_deleted\
            = self._find_place_in_leaf(key, leaf_start, nr_of_elements)
        if full_leaf:
            try:  # check if leaf has parent node
                leaf_parent_pointer = nodes_stack.pop()
            except IndexError:  # leaf is a root
                leaf_parent_pointer = 0
            split_data = self._split_leaf(leaf_start,
                                          nr_of_records_to_rewrite,
                                          key,
                                          doc_id,
                                          start,
                                          size,
                                          status,
                                          create_new_root=(False if leaf_parent_pointer else True))
            if split_data is not None:  # means that split created new root or replaced split with update_if_has_deleted
                new_leaf_start_position, key_moved_to_parent_node = split_data
                self._insert_new_key_into_node(leaf_parent_pointer,
                                               key_moved_to_parent_node,
                                               leaf_start,
                                               new_leaf_start_position,
                                               nodes_stack,
                                               indexes)
        else:  # there is a place for record in leaf
            self.buckets.seek(leaf_start)
            self._update_leaf(
                leaf_start, new_record_position, nr_of_elements, nr_of_records_to_rewrite,
                on_deleted, key, doc_id, start, size, status)

    def _update_node(self, new_key_position, nr_of_keys_to_rewrite, new_key, new_pointer):
        if nr_of_keys_to_rewrite == 0:
            self.buckets.seek(new_key_position)
            self.buckets.write(
                struct.pack('<' + self.key_format + self.pointer_format,
                            new_key,
                            new_pointer))
            self.flush()
        else:
            self.buckets.seek(new_key_position)
            data = self.buckets.read(nr_of_keys_to_rewrite * (
                                     self.key_size + self.pointer_size))
            keys_to_rewrite = struct.unpack(
                '<' + nr_of_keys_to_rewrite * (self.key_format + self.pointer_format), data)
            self.buckets.seek(new_key_position)
            self.buckets.write(
                struct.pack(
                    '<' + (nr_of_keys_to_rewrite + 1) *
                    (self.key_format + self.pointer_format),
                    new_key,
                    new_pointer,
                    *keys_to_rewrite))
            self.flush()

    def _insert_new_key_into_node(self, node_start, new_key, old_half_start, new_half_start, nodes_stack, indexes):
        parent_key_index = indexes.pop()
        nr_of_elements, children_flag = self._read_node_nr_of_elements_and_children_flag(node_start)
        parent_prev_pointer = self._read_single_node_key(
            node_start, parent_key_index)[0]
        if parent_prev_pointer == old_half_start:  # splited child was on the left side of his parent key, must write new key before it
            new_key_position = self.pointer_size + self._calculate_key_position(node_start, parent_key_index, 'n')
            nr_of_keys_to_rewrite = nr_of_elements - parent_key_index
        else:  # splited child was on the right side of his parent key, must write new key after it
            new_key_position = self.pointer_size + self._calculate_key_position(node_start, parent_key_index + 1, 'n')
            nr_of_keys_to_rewrite = nr_of_elements - (parent_key_index + 1)
        if nr_of_elements == self.node_capacity:
            try:  # check if node has parent
                node_parent_pointer = nodes_stack.pop()
            except IndexError:  # node is a root
                node_parent_pointer = 0
            new_data = self._split_node(node_start,
                                        nr_of_keys_to_rewrite,
                                        new_key,
                                        new_half_start,
                                        children_flag,
                                        create_new_root=(False if node_parent_pointer else True))
            if new_data:  # if not new_data, new root has been created
                new_node_start_position, key_moved_to_parent_node = new_data
                self._insert_new_key_into_node(node_parent_pointer,
                                               key_moved_to_parent_node,
                                               node_start,
                                               new_node_start_position,
                                               nodes_stack,
                                               indexes)

            self._find_first_key_occurence_in_node.delete(node_start)
            self._find_last_key_occurence_in_node.delete(node_start)
        else:  # there is a empty slot for new key in node
            self._update_size(node_start, nr_of_elements + 1)
            self._update_node(new_key_position,
                              nr_of_keys_to_rewrite,
                              new_key,
                              new_half_start)

            self._find_first_key_occurence_in_node.delete(node_start)
            self._find_last_key_occurence_in_node.delete(node_start)
            self._read_single_node_key.delete(node_start)
            self._read_node_nr_of_elements_and_children_flag.delete(node_start)

    def _find_leaf_to_insert(self, key):
        """
        Traverses tree in search for leaf for insert, remembering parent nodes in path,
        looks for last occurence of key if already in tree.
        """
        nodes_stack = [self.data_start]
        if self.root_flag == 'l':
            return nodes_stack, []
        else:
            nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(self.data_start)
            curr_index, curr_pointer = self._find_last_key_occurence_in_node(
                self.data_start, key, nr_of_elements)
            nodes_stack.append(curr_pointer)
            indexes = [curr_index]
            while(curr_child_flag == 'n'):
                nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(curr_pointer)
                curr_index, curr_pointer = self._find_last_key_occurence_in_node(curr_pointer, key, nr_of_elements)
                nodes_stack.append(curr_pointer)
                indexes.append(curr_index)
            return nodes_stack, indexes
        # nodes stack contains start addreses of nodes directly above leaf with key, indexes match keys adjacent nodes_stack values (as pointers)
        # required when inserting new keys in upper tree levels

    def _find_leaf_with_last_key_occurence(self, key):
        if self.root_flag == 'l':
            return self.data_start
        else:
            nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(self.data_start)
            curr_position = self._find_last_key_occurence_in_node(
                self.data_start, key, nr_of_elements)[1]
            while(curr_child_flag == 'n'):
                nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(curr_position)
                curr_position = self._find_last_key_occurence_in_node(
                    curr_position, key, nr_of_elements)[1]
            return curr_position

    def _find_leaf_with_first_key_occurence(self, key):
        if self.root_flag == 'l':
            return self.data_start
        else:
            nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(self.data_start)
            curr_position = self._find_first_key_occurence_in_node(
                self.data_start, key, nr_of_elements)[1]
            while(curr_child_flag == 'n'):
                nr_of_elements, curr_child_flag = self._read_node_nr_of_elements_and_children_flag(curr_position)
                curr_position = self._find_first_key_occurence_in_node(
                    curr_position, key, nr_of_elements)[1]
            return curr_position

    def _find_key(self, key):
        containing_leaf_start = self._find_leaf_with_first_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(containing_leaf_start)
        try:
            doc_id, l_key, start, size, status = self._find_key_in_leaf(
                containing_leaf_start, key, nr_of_elements)
        except ElemNotFound:
            if next_leaf:
                nr_of_elements = self._read_leaf_nr_of_elements(next_leaf)
            else:
                raise ElemNotFound
            doc_id, l_key, start, size, status = self._find_key_in_leaf(
                next_leaf, key, nr_of_elements)
        return doc_id, l_key, start, size, status

    def _find_key_to_update(self, key, doc_id):
        """
        Search tree for key that matches not only given key but also doc_id.
        """
        containing_leaf_start = self._find_leaf_with_first_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(containing_leaf_start)
        try:
            leaf_start, record_index, doc_id, l_key, start, size, status = self._find_key_in_leaf_for_update(key,
                                                                                                             doc_id,
                                                                                                             containing_leaf_start,
                                                                                                             nr_of_elements)
        except ElemNotFound:
            if next_leaf:
                nr_of_elements = self._read_leaf_nr_of_elements(next_leaf)
            else:
                raise TryReindexException()
            try:
                leaf_start, record_index, doc_id, l_key, start, size, status = self._find_key_in_leaf_for_update(key,
                                                                                                                 doc_id,
                                                                                                                 next_leaf,
                                                                                                                 nr_of_elements)
            except ElemNotFound:
                raise TryReindexException()
        return leaf_start, record_index, doc_id, l_key, start, size, status

    def update(self, doc_id, key, u_start=0, u_size=0, u_status='o'):
        containing_leaf_start, element_index, old_doc_id, old_key, old_start, old_size, old_status = self._find_key_to_update(key, doc_id)
        new_data = (old_doc_id, old_start, old_size, old_status)
        if not u_start:
            new_data[1] = u_start
        if not u_size:
            new_data[2] = u_size
        if not u_status:
            new_data[3] = u_status
        self._update_element(containing_leaf_start, element_index, new_data)

        self._find_key.delete(key)
        self._match_doc_id.delete(doc_id)
        self._find_key_in_leaf.delete(containing_leaf_start, key)
        return True

    def delete(self, doc_id, key, start=0, size=0):
        containing_leaf_start, element_index = self._find_key_to_update(
            key, doc_id)[:2]
        self._delete_element(containing_leaf_start, element_index)

        self._find_key.delete(key)
        self._match_doc_id.delete(doc_id)
        self._find_key_in_leaf.delete(containing_leaf_start, key)
        return True

    def _find_key_many(self, key, limit=1, offset=0):
        leaf_with_key = self._find_leaf_with_first_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        try:
            leaf_with_key, key_index = self._find_index_of_first_key_equal(
                key, leaf_with_key, nr_of_elements)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        except ElemNotFound:
            leaf_with_key = next_leaf
            key_index = 0
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        while offset:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if key == curr_key:
                    if status != 'd':
                        offset -= 1
                    key_index += 1
                else:
                    return
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return
        while limit:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if key == curr_key:
                    if status != 'd':
                        yield doc_id, start, size, status
                        limit -= 1
                    key_index += 1
                else:
                    return
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return

    def _find_key_smaller(self, key, limit=1, offset=0):
        leaf_with_key = self._find_leaf_with_first_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        leaf_with_key, key_index = self._find_index_of_first_key_equal_or_smaller_key(key, leaf_with_key, nr_of_elements)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        curr_key = self._read_single_leaf_record(leaf_with_key, key_index)[0]
        if curr_key >= key:
            key_index -= 1
        while offset:
            if key_index >= 0:
                key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    offset -= 1
                key_index -= 1
            else:
                if prev_leaf:
                    leaf_with_key = prev_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(prev_leaf)
                    key_index = nr_of_elements - 1
                else:
                    return
        while limit:
            if key_index >= 0:
                key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    yield doc_id, key, start, size, status
                    limit -= 1
                key_index -= 1
            else:
                if prev_leaf:
                    leaf_with_key = prev_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(prev_leaf)
                    key_index = nr_of_elements - 1
                else:
                    return

    def _find_key_equal_and_smaller(self, key, limit=1, offset=0):
        leaf_with_key = self._find_leaf_with_last_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        try:
            leaf_with_key, key_index = self._find_index_of_last_key_equal_or_smaller_key(key, leaf_with_key, nr_of_elements)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        except ElemNotFound:
            leaf_with_key = prev_leaf
            key_index = self._read_leaf_nr_of_elements_and_neighbours(
                leaf_with_key)[0]
        curr_key = self._read_single_leaf_record(leaf_with_key, key_index)[0]
        if curr_key > key:
            key_index -= 1
        while offset:
            if key_index >= 0:
                key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    offset -= 1
                key_index -= 1
            else:
                if prev_leaf:
                    leaf_with_key = prev_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(prev_leaf)
                    key_index = nr_of_elements - 1
                else:
                    return
        while limit:
            if key_index >= 0:
                key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    yield doc_id, key, start, size, status
                    limit -= 1
                key_index -= 1
            else:
                if prev_leaf:
                    leaf_with_key = prev_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(prev_leaf)
                    key_index = nr_of_elements - 1
                else:
                    return

    def _find_key_bigger(self, key, limit=1, offset=0):
        leaf_with_key = self._find_leaf_with_last_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        try:
            leaf_with_key, key_index = self._find_index_of_last_key_equal_or_smaller_key(key, leaf_with_key, nr_of_elements)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        except ElemNotFound:
            key_index = 0
        curr_key = self._read_single_leaf_record(leaf_with_key, key_index)[0]
        if curr_key <= key:
            key_index += 1
        while offset:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    offset -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return
        while limit:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    yield doc_id, curr_key, start, size, status
                    limit -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return

    def _find_key_equal_and_bigger(self, key, limit=1, offset=0):
        leaf_with_key = self._find_leaf_with_first_key_occurence(key)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        leaf_with_key, key_index = self._find_index_of_first_key_equal_or_smaller_key(key, leaf_with_key, nr_of_elements)
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
        curr_key = self._read_single_leaf_record(leaf_with_key, key_index)[0]
        if curr_key < key:
            key_index += 1
        while offset:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    offset -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return
        while limit:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_with_key, key_index)
                if status != 'd':
                    yield doc_id, curr_key, start, size, status
                    limit -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return

    def _find_key_between(self, start, end, limit, offset, inclusive_start, inclusive_end):
        """
        Returns generator containing all keys withing given interval.
        """
        if inclusive_start:
            leaf_with_key = self._find_leaf_with_first_key_occurence(start)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
            leaf_with_key, key_index = self._find_index_of_first_key_equal_or_smaller_key(start, leaf_with_key, nr_of_elements)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
            curr_key = self._read_single_leaf_record(
                leaf_with_key, key_index)[0]
            if curr_key < start:
                key_index += 1
        else:
            leaf_with_key = self._find_leaf_with_last_key_occurence(start)
            nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_with_key)
            leaf_with_key, key_index = self._find_index_of_last_key_equal_or_smaller_key(start, leaf_with_key, nr_of_elements)
            curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_with_key, key_index)
            if curr_key <= start:
                key_index += 1
        while offset:
            if key_index < nr_of_elements:
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_with_key, key_index)
                if curr_status != 'd':
                    offset -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return
        while limit:
            if key_index < nr_of_elements:
                curr_key, curr_doc_id, curr_start, curr_size, curr_status = self._read_single_leaf_record(leaf_with_key, key_index)
                if curr_key > end or (curr_key == end and not inclusive_end):
                    return
                elif curr_status != 'd':
                    yield curr_doc_id, curr_key, curr_start, curr_size, curr_status
                    limit -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_with_key = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return

    def get(self, key):
        return self._find_key(self.make_key(key))

    def get_many(self, key, limit=1, offset=0):
        return self._find_key_many(self.make_key(key), limit, offset)

    def get_between(self, start, end, limit=1, offset=0, inclusive_start=True, inclusive_end=True):
        if start is None:
            end = self.make_key(end)
            if inclusive_end:
                return self._find_key_equal_and_smaller(end, limit, offset)
            else:
                return self._find_key_smaller(end, limit, offset)
        elif end is None:
            start = self.make_key(start)
            if inclusive_start:
                return self._find_key_equal_and_bigger(start, limit, offset)
            else:
                return self._find_key_bigger(start, limit, offset)
        else:
            start = self.make_key(start)
            end = self.make_key(end)
            return self._find_key_between(start, end, limit, offset, inclusive_start, inclusive_end)

    def all(self, limit=-1, offset=0):
        """
        Traverses linked list of all tree leaves and returns generator containing all elements stored in index.
        """
        if self.root_flag == 'n':
            leaf_start = self.data_start + self.node_size
        else:
            leaf_start = self.data_start
        nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(leaf_start)
        key_index = 0
        while offset:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_start, key_index)
                if status != 'd':
                    offset -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_start = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return
        while limit:
            if key_index < nr_of_elements:
                curr_key, doc_id, start, size, status = self._read_single_leaf_record(
                    leaf_start, key_index)
                if status != 'd':
                    yield doc_id, curr_key, start, size, status
                    limit -= 1
                key_index += 1
            else:
                key_index = 0
                if next_leaf:
                    leaf_start = next_leaf
                    nr_of_elements, prev_leaf, next_leaf = self._read_leaf_nr_of_elements_and_neighbours(next_leaf)
                else:
                    return

    def make_key(self, key):
        raise NotImplementedError()

    def make_key_value(self, data):
        raise NotImplementedError()

    def _open_storage(self):
        s = globals()[self.storage_class]
        if not self.storage:
            self.storage = s(self.db_path, self.name)
        self.storage.open()

    def _create_storage(self):
        s = globals()[self.storage_class]
        if not self.storage:
            self.storage = s(self.db_path, self.name)
        self.storage.create()

    def compact(self, node_capacity=0):
        if not node_capacity:
            node_capacity = self.node_capacity

        compact_ind = self.__class__(
            self.db_path, self.name + '_compact', node_capacity=node_capacity)
        compact_ind.create_index()

        gen = self.all()
        while True:
            try:
                doc_id, key, start, size, status = gen.next()
            except StopIteration:
                break
            self.storage._f.seek(start)
            value = self.storage._f.read(size)
            start_ = compact_ind.storage._f.tell()
            compact_ind.storage._f.write(value)
            compact_ind.insert(doc_id, key, start_, size, status)

        compact_ind.close_index()
        original_name = self.name
        # os.unlink(os.path.join(self.db_path, self.name + "_buck"))
        self.close_index()
        shutil.move(os.path.join(compact_ind.db_path, compact_ind.
                                 name + "_buck"), os.path.join(self.db_path, self.name + "_buck"))
        shutil.move(os.path.join(compact_ind.db_path, compact_ind.
                                 name + "_stor"), os.path.join(self.db_path, self.name + "_stor"))
        # self.name = original_name
        self.open_index()  # reload...
        self.name = original_name
        self._save_params(dict(name=original_name))
        self._fix_params()
        self._clear_cache()
        return True

    def _fix_params(self):
        super(IU_TreeBasedIndex, self)._fix_params()
        self._count_props()

    def _clear_cache(self):
        self._find_key.clear()
        self._match_doc_id.clear()
#        self._read_single_leaf_record.clear()
        self._find_key_in_leaf.clear()
        self._read_single_node_key.clear()
        self._find_first_key_occurence_in_node.clear()
        self._find_last_key_occurence_in_node.clear()
        self._read_leaf_nr_of_elements.clear()
        self._read_leaf_neighbours.clear()
        self._read_leaf_nr_of_elements_and_neighbours.clear()
        self._read_node_nr_of_elements_and_children_flag.clear()

    def close_index(self):
        super(IU_TreeBasedIndex, self).close_index()
        self._clear_cache()


class IU_MultiTreeBasedIndex(IU_TreeBasedIndex):
    """
    Class that allows to index more than one key per database record.

    It operates very well on GET/INSERT. It's not optimized for
    UPDATE operations (will always readd everything)
    """

    def __init__(self, *args, **kwargs):
        super(IU_MultiTreeBasedIndex, self).__init__(*args, **kwargs)

    def insert(self, doc_id, key, start, size, status='o'):
        if isinstance(key, (list, tuple)):
            key = set(key)
        elif not isinstance(key, set):
            key = set([key])
        ins = super(IU_MultiTreeBasedIndex, self).insert
        for curr_key in key:
            ins(doc_id, curr_key, start, size, status)
        return True

    def update(self, doc_id, key, u_start, u_size, u_status='o'):
        if isinstance(key, (list, tuple)):
            key = set(key)
        elif not isinstance(key, set):
            key = set([key])
        upd = super(IU_MultiTreeBasedIndex, self).update
        for curr_key in key:
            upd(doc_id, curr_key, u_start, u_size, u_status)

    def delete(self, doc_id, key, start=0, size=0):
        if isinstance(key, (list, tuple)):
            key = set(key)
        elif not isinstance(key, set):
            key = set([key])
        delete = super(IU_MultiTreeBasedIndex, self).delete
        for curr_key in key:
            delete(doc_id, curr_key, start, size)

    def get(self, key):
        return super(IU_MultiTreeBasedIndex, self).get(key)

    def make_key_value(self, data):
        raise NotImplementedError()


# classes for public use, done in this way because of
# generation static files with indexes (_index directory)


class TreeBasedIndex(IU_TreeBasedIndex):
    pass


class MultiTreeBasedIndex(IU_MultiTreeBasedIndex):
    """
    It allows to index more than one key for record. (ie. prefix/infix/suffix search mechanizms)
    That class is designed to be used in custom indexes.
    """
    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.