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
|
# 20nov25 Software Lab. Alexander Burger
# *Jnl *Blob
### Tree Access ###
(de tree (Var Cls Hook)
(cons Var
(if Hook
(cons Cls Hook)
Cls ) ) )
(de genKey (Var Cls Hook Min Max)
(if (lt0 Max)
(let K (minKey (tree Var Cls Hook) Min Max)
(if (lt0 K) (dec K) (or Max -1)) )
(let K (maxKey (tree Var Cls Hook) Min Max)
(if (gt0 K) (inc K) (or Min 1)) ) ) )
(de useKey (Var Cls Hook)
(let (Tree (tree Var Cls Hook) Max (* 2 (inc (count Tree))) N)
(while (fetch Tree (setq N (rand 1 Max))))
N ) )
(de genStrKey (Str Var Cls Hook)
(while (fetch (tree Var Cls Hook) Str)
(setq Str (pack "# " Str)) )
Str )
(de ubZval (Lst X)
(let (Res 0 P 1 Q 1)
(while (find '((N) (>= N Q)) Lst)
(for N Lst
(and
N
(bit? Q N)
(setq Res (| Res P)) )
(setq P (>> -1 P)) )
(setq Q (>> -1 Q)) )
(cons Res X) ) )
### Relations ###
(class +relation)
(dm T (Var)
(=: cls *Class)
(=: var Var) )
# Type check
(dm mis> (Val Obj)) #> lst
(dm ele> (Val))
# Value present?
(dm has> (Val X) #> flg
(= Val X) )
# Set value
(dm put> (Obj Old New)
New )
# Delete value
(dm del> (Obj Old Val)
(and (<> Old Val) Val) )
# Maintain relations
(dm rel> (Obj Old New))
(dm rel?> (Obj Val)
T )
(dm lose> (Obj Val))
(dm keep> (Obj Val))
# Search
(dm iter> (X Lst)
(cons
(list (: cls Dbf 1))
(let @Cls (: cls)
(curry (@Cls) (P)
(loop
(NIL (and (car P) (set P (seq (car P)))))
(T (and (isa '@Cls @) (not (; @ T)))
(car P) ) ) ) ) ) )
(dm match> (X Val Obj)
(cond
((not X) Val)
((str? X) (pre? X Val))
((atom X) (and (= X Val) Val))
((>= (cdr X) (car X))
(and (>= (cdr X) Val (car X)) Val) )
((>= (car X) Val (cdr X)) Val) ) )
# Finalizer
(dm zap> (Obj Val))
(class +Any +relation)
# (+Bag) (cls ..) (..) (..)
(class +Bag +relation)
(dm T (Var Lst)
(=: bag
(mapcar
'((L)
(prog1
(new (car L) Var (cdr L))
(and (get @ 'hook) (=: hook T)) ) )
Lst ) )
(super Var) )
(dm mis> (Val Obj)
(ifn (lst? Val)
"Not a Bag"
(pick
'((This V)
(mis> This V Obj
(when (: hook)
(get (if (sym? @) Obj Val) (: hook)) ) ) )
(: bag)
Val ) ) )
(dm ele> (Val)
(and Val
(or
(atom Val)
(find 'ele> (: bag) Val) ) ) )
(dm has> (Val X)
(when Val
(if (atom Val)
(find 'has> (: bag) Val X)
(fully 'has> (: bag) Val X) ) ) )
(dm put> (Obj Old New)
(trim
(mapcar
'((X O N) (put> X Obj O N))
(: bag)
Old
New ) ) )
(dm rel> (Obj Old New)
(when Old
(mapc
'((This O)
(rel> This Obj O NIL
(when (: hook)
(get (if (sym? @) Obj Old) (: hook)) ) ) )
(: bag)
Old ) )
(when New
(mapc
'((This N)
(rel> This Obj NIL N
(when (: hook)
(get (if (sym? @) Obj New) (: hook)) ) ) )
(: bag)
New ) ) )
(dm rel?> (Obj Val)
(fully
'((This V)
(or
(not V)
(rel?> This Obj V
(when (: hook)
(get (if (sym? @) Obj Val) (: hook)) ) ) ) )
(: bag)
Val ) )
(dm lose> (Obj Val)
(mapc
'((This V)
(lose> This Obj V
(when (: hook)
(get (if (sym? @) Obj Val) (: hook)) ) ) )
(: bag)
Val ) )
(dm keep> (Obj Val)
(mapc
'((This V)
(keep> This Obj V
(when (: hook)
(get (if (sym? @) Obj Val) (: hook)) ) ) )
(: bag)
Val ) )
(dm iter> (X Lst)
(if
(find
'((B) (isa '+index B))
(: bag) )
(iter> @ X Lst)
(super X Lst) ) )
(dm match> (X Val Obj)
(pick 'match> (: bag)
(circ X)
Val
(circ Obj) ) )
(class +Bool +relation)
(dm mis> (Val Obj)
(and Val (nT Val) ,"Boolean input expected") )
# (+Number) [num]
(class +Number +relation)
(dm T (Var Lst)
(=: scl (car Lst))
(super Var) )
(dm mis> (Val Obj)
(and Val (not (num? Val)) ,"Numeric input expected") )
# (+Date)
(class +Date +Number)
(dm T (Var Lst)
(super Var (cons NIL Lst)) )
# (+Time)
(class +Time +Number)
(dm T (Var Lst)
(super Var (cons NIL Lst)) )
# (+Symbol)
(class +Symbol +relation)
(dm mis> (Val Obj)
(unless (sym? Val)
,"Symbolic type expected" ) )
# (+String)
(class +String +Symbol)
(dm mis> (Val Obj)
(and Val (not (str? Val)) ,"String type expected") )
(private) canQuery
# (+Link) typ
(class +Link +relation)
(dm T (Var Lst)
(unless (=: type (car Lst))
(quit "No Link" Var) )
(super Var) )
(de canQuery (Val)
(and
(pair Val)
(pair (car Val))
(fully
'((L)
(find
'((Cls)
(get Cls
((if (lst? (car L)) cadr car) L) ) )
(: type) ) )
Val ) ) )
(dm mis> (Val Obj)
(and
Val
(nor
(isa (: type) Val)
(canQuery Val) )
,"Type error" ) )
# (+Joint) var typ [put get]
(class +Joint +Link)
(dm T (Var Lst)
(=: slot (car Lst))
(=: put (caddr Lst))
(=: get (cadddr Lst))
(super Var (cdr Lst)) )
(dm mis> (Val Obj)
(and
Val
(nor
(canQuery Val)
(and
(isa (: type) Val)
(with (meta Val (: slot))
(or
(isa '+Link This)
(find
'((B) (isa '+Link B))
(: bag) ) ) ) ) )
,"Type error" ) )
(dm rel> (Obj Old New)
(and Old
(del> Old (: slot)
(if (: get)
(@ Obj (get Old (: slot)))
Obj ) ) )
(and New
(not (get Obj T))
(not (has> New (: slot) Obj))
(put> New (: slot)
(if (: put) (@ Obj) Obj) ) ) )
(dm rel?> (Obj Val)
(let X (get Val (: slot))
(cond
((atom X) (== Obj X))
((: get) (@ Obj X))
(T (memq Obj X)) ) ) )
(dm lose> (Obj Val)
(when Val
(put Val (: slot)
(del> (meta Val (: slot))
Obj
(get Val (: slot))
(if (: put) (@ Obj) Obj) ) ) ) )
(dm keep> (Obj Val)
(when Val
(put Val (: slot)
(put> (meta Val (: slot))
Obj
(get Val (: slot))
(if (: put) (@ Obj) Obj) ) ) ) )
# +Link or +Joint prefix
(class +Hook)
(dm rel> (Obj Old New Hook)
(let L
(extract
'((X)
(and (atom X) (setq X (cons T X)))
(and
(or
(== (: var) (meta Obj (cdr X) 'hook))
(find
'((B) (== (: var) (get B 'hook)))
(meta Obj (cdr X) 'bag) ) )
X ) )
(getl Obj) )
(for X L
(rel> (meta Obj (cdr X)) Obj (car X) NIL (or Old *DB))
(rel> (meta Obj (cdr X)) Obj NIL (car X) (or New *DB)) ) )
(extra Obj Old New Hook) )
# +Index prefix
(class +Hook2)
(dm rel> (Obj Old New Hook)
(extra Obj Old New *DB)
(when (or (and Hook (n== Hook *DB)) (and (: hook) (get Obj @)))
(extra Obj Old New Hook) ) )
(dm lose> (Obj Val Hook)
(extra Obj Val *DB)
(when (or (and Hook (n== Hook *DB)) (and (: hook) (get Obj @)))
(extra Obj Val Hook) ) )
(dm keep> (Obj Val Hook)
(extra Obj Val *DB)
(when (or (and Hook (n== Hook *DB)) (and (: hook) (get Obj @)))
(extra Obj Val Hook) ) )
# (+Blob)
(class +Blob +relation)
(de blob (Obj Var)
(pack *Blob (glue "/" (chop Obj)) "." Var) )
(dm put> (Obj Old New)
(and
New
(dirname (blob Obj))
(call "mkdir" "-p" @) )
(if (flg? New)
New
(in New (out (blob Obj (: var)) (echo)))
T ) )
(dm zap> (Obj Val)
(and Val (%@ "unlink" NIL (blob Obj (: var)))) )
### Index classes ###
(private) (idxRel? relAux)
(class +index)
(dm T (Var Lst)
(=: hook (car Lst))
(extra Var (cdr Lst)) )
(dm rel?> (Obj Val Hook))
# (+Key +relation) [hook]
(class +Key +index)
(dm mis> (Val Obj Hook)
(or
(extra Val Obj Hook)
(and
Val
(not (has> Obj (: var) Val))
(fetch
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Val )
,"Not unique" ) ) )
(dm rel> (Obj Old New Hook)
(let Tree (tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(and Old
(= Obj (fetch Tree Old))
(store Tree Old NIL (: dbf)) )
(and New
(not (get Obj T))
(not (fetch Tree New))
(store Tree New Obj (: dbf)) ) )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(== Obj
(fetch
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Val ) ) )
(dm lose> (Obj Val Hook)
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Val NIL (: dbf) )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Val Obj (: dbf) )
(extra Obj Val Hook) )
(dm iter> (X Lst)
(let Tree (tree (: var) (: cls) (caddr Lst))
(cons
(nond
((atom X)
(nond
((str? (car X))
(init Tree (car X) (cdr X)) )
((>= (cdr X) (car X))
(init Tree (pack (car X) `(char T)) (cdr X)) )
(NIL
(init Tree (car X) (pack (cdr X) `(char T))) ) ) )
((str? X) (init Tree X X))
(NIL (init Tree X (pack X `(char T)))) )
(let @Cls (cadr Lst)
(curry (@Cls) (Q)
(loop
(NIL (step Q))
(T (isa '@Cls @) @) ) ) ) ) ) )
# (+Ref +relation) [hook]
(class +Ref +index)
(dm rel> (Obj Old New Hook)
(let
(Tree (tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Aux (mapcar '((S) (get Obj S)) (: aux)) )
(when Old
(let Key (cons Old Aux)
(store Tree
(if (: ub)
(ubZval Key Obj)
(append Key Obj) )
NIL
(: dbf) ) ) )
(and New
(not (get Obj T))
(let Key (cons New Aux)
(store Tree
(if (: ub)
(ubZval Key Obj)
(conc Key Obj) )
Obj
(: dbf) ) ) ) )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(let Key (cons Val (mapcar '((S) (get Obj S)) (: aux)))
(== Obj
(fetch
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(if (: ub)
(ubZval Key Obj)
(append Key Obj) ) ) ) ) )
(dm lose> (Obj Val Hook)
(let Key (cons Val (mapcar '((S) (get Obj S)) (: aux)))
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(if (: ub)
(ubZval Key Obj)
(conc Key Obj) )
NIL
(: dbf) ) )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(let Key (cons Val (mapcar '((S) (get Obj S)) (: aux)))
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(if (: ub)
(ubZval Key Obj)
(conc Key Obj) )
Obj
(: dbf) ) )
(extra Obj Val Hook) )
(dm iter> (X Lst @Flg)
(let Tree (tree (: var) (: cls) (caddr Lst))
(cons
(nond
(X (init Tree NIL T))
((atom X)
(nond
((str? (car X))
(if (>= (cdr X) (car X))
(init Tree
(cons (car X))
(cons (cdr X) T) )
(init Tree
(cons (car X) T)
(cons (cdr X)) ) ) )
((>= (cdr X) (car X))
(init Tree
(cons (pack (car X) `(char T)) T)
(cons (cdr X)) ) )
(NIL
(init Tree
(cons (car X))
(cons (pack (cdr X) `(char T)) T) ) ) ) )
((str? X)
(init Tree (cons X) (cons X T)) )
(NIL
(init Tree (cons X) (cons (pack X `(char T)) T)) ) )
(let @Cls (cadr Lst)
(curry (@Flg @Cls) (Q)
(loop
(NIL (step Q @Flg))
(T (isa '@Cls @) @) ) ) ) ) ) )
# Backing index prefix
(class +Ref2)
(dm T (Var Lst)
(unless (meta *Class Var)
(quit "No Ref2" Var) )
(extra Var Lst) )
(dm rel> (Obj Old New Hook)
(with (meta (: cls) (: var))
(let Tree (tree (: var) (: cls))
(when Old
(store Tree (cons Old Obj) NIL (: dbf)) )
(and New
(not (get Obj T))
(store Tree (cons New Obj) Obj (: dbf)) ) ) )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(and
(with (meta (: cls) (: var))
(== Obj
(fetch
(tree (: var) (: cls))
(cons Val Obj) ) ) )
(extra Obj Val Hook) ) )
(dm lose> (Obj Val Hook)
(with (meta (: cls) (: var))
(store (tree (: var) (: cls)) (cons Val Obj) NIL (: dbf)) )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(with (meta (: cls) (: var))
(store (tree (: var) (: cls)) (cons Val Obj) Obj (: dbf)) )
(extra Obj Val Hook) )
# (+Idx +relation) [cnt [hook]]
(class +Idx +Ref)
(dm T (Var Lst)
(=: min (or (car Lst) 3))
(super Var (cdr Lst)) )
(de idxRel (Obj Old Old2 Olds New New2 News Hook)
(let
(Tree (tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Aux (mapcar '((S) (get Obj S)) (: aux))
Aux2 (append Aux (cons Obj)) )
(setq Aux (conc Aux Obj))
(and Old (store Tree (cons @ Aux) NIL (: dbf)))
(and Old2 (store Tree (cons @ Aux2) NIL (: dbf)))
(for S Olds
(while (nth S (: min))
(store Tree (cons (pack S) Aux2) NIL (: dbf))
(++ S) ) )
(unless (get Obj T)
(and New (store Tree (cons @ Aux) Obj (: dbf)))
(and New2 (store Tree (cons @ Aux2) Obj (: dbf)))
(for S News
(while (nth S (: min))
(store Tree (cons (pack S) Aux2) Obj (: dbf))
(++ S) ) ) ) ) )
(de idxRel? (Obj Val Val2 Vals Hook)
(let
(Tree (tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
Aux (mapcar '((S) (get Obj S)) (: aux))
Aux2 (append Aux (cons Obj)) )
(setq Aux (conc Aux Obj))
(and
(== Obj (fetch Tree (cons Val Aux)))
(or (not Val2) (== Obj (fetch Tree (cons Val2 Aux2))))
(fully
'((S)
(loop
(NIL (nth S (: min)) T)
(NIL (== Obj (fetch Tree (cons (pack S) Aux2))))
(++ S) ) )
Vals ) ) ) )
(dm rel> (Obj Old New Hook)
(idxRel Obj
Old NIL (split (cdr (chop Old)) " " "\n")
New NIL (split (cdr (chop New)) " " "\n")
Hook )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(and
(idxRel? Obj
Val NIL (split (cdr (chop Val)) " " "\n")
Hook )
(extra Obj Val Hook) ) )
(dm lose> (Obj Val Hook)
(idxRel Obj
Val NIL (split (cdr (chop Val)) " " "\n")
NIL NIL NIL
Hook )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(idxRel Obj
NIL NIL NIL
Val NIL (split (cdr (chop Val)) " " "\n")
Hook )
(extra Obj Val Hook) )
(dm iter> (X Lst)
(on *Iter+)
(super X Lst (not X)) )
(dm match> (X Val Obj)
(sub? X Val) )
# (+Sn +index) [hook]
(class +Sn)
(dm rel> (Obj Old New Hook)
(let Tree (tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(and Old
(ext:Snx Old)
(store Tree (cons @ Obj T) NIL (: dbf)) )
(and New
(not (get Obj T))
(ext:Snx New)
(store Tree (cons @ Obj T) Obj (: dbf)) ) )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(and
(let S (ext:Snx Val)
(or
(not S)
(== Obj
(fetch
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(cons S Obj T) ) ) ) )
(extra Obj Val Hook) ) )
(dm lose> (Obj Val Hook)
(let? S (ext:Snx Val)
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(cons S Obj T)
NIL (: dbf) ) )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(let? S (ext:Snx Val)
(store
(tree (: var) (: cls) (or Hook (and (: hook) (get Obj @))))
(cons S Obj T)
Obj (: dbf) ) )
(extra Obj Val Hook) )
(dm iter> (X Lst)
(on *Iter+)
(cons
(list
(list
(car (extra X Lst))
(init (tree (: var) (: cls) (caddr Lst))
(cons (setq X (ext:Snx X)))
(cons (pack X `(char T)) T) ) ) )
(let @Cls (cadr Lst)
(curry (@Cls) (Q)
(loop
(NIL
(or
(step (caar Q))
(and (cdar Q) (shift Q) (step (caar Q))) ) )
(T (isa '@Cls @) @) ) ) ) ) )
(dm match> (X Val Obj)
(or
(extra X Val Obj)
(and (pre? (ext:Snx X) (ext:Snx Val)) Val) ) )
# (+Fold +index) [hook]
(class +Fold)
(dm has> (Val X)
(extra Val
(if (= Val (fold Val)) (fold X) X) ) )
(dm rel> (Obj Old New Hook)
(extra Obj (fold Old) (fold New) Hook) )
(dm rel?> (Obj Val Hook)
(let V (fold Val)
(or (not V) (extra Obj V Hook)) ) )
(dm lose> (Obj Val Hook)
(extra Obj (fold Val) Hook) )
(dm keep> (Obj Val Hook)
(extra Obj (fold Val) Hook) )
(dm iter> (X Lst)
(extra
(if (pair X)
(cons (fold (car X)) (fold (cdr X)))
(fold X) )
Lst ) )
(dm match> (X Val Obj)
(when
(extra
(if (pair X)
(cons (fold (car X)) (fold (cdr X)))
(fold X) )
(fold Val)
Obj )
Val ) )
# (+IdxFold +relation) [cnt [hook]]
(class +IdxFold +Ref)
(dm T (Var Lst)
(=: min (or (car Lst) 3))
(super Var (cdr Lst)) )
(dm rel> (Obj Old New Hook)
(idxRel Obj
Old (fold Old)
(extract '((L) (extract fold L))
(split (cdr (chop Old)) " " "\n") )
New (fold New)
(extract '((L) (extract fold L))
(split (cdr (chop New)) " " "\n") )
Hook )
(extra Obj Old New Hook) )
(dm rel?> (Obj Val Hook)
(and
(let V (fold Val)
(or (not V)
(idxRel? Obj
Val V
(extract '((L) (extract fold L))
(split (cdr (chop Val)) " " "\n") )
Hook ) ) )
(extra Obj Val Hook) ) )
(dm lose> (Obj Val Hook)
(idxRel Obj
Val (fold Val)
(extract '((L) (extract fold L))
(split (cdr (chop Val)) " " "\n") )
NIL NIL NIL
Hook )
(extra Obj Val Hook) )
(dm keep> (Obj Val Hook)
(idxRel Obj
NIL NIL NIL
Val (fold Val)
(extract '((L) (extract fold L))
(split (cdr (chop Val)) " " "\n") )
Hook )
(extra Obj Val Hook) )
(dm iter> (X Lst)
(on *Iter+)
(super (fold X) Lst (not X)) )
(dm match> (X Val Obj)
(and (sub? (fold X) (fold Val)) Val) )
# (+Aux) lst
(class +Aux)
(dm T (Var Lst)
(=: aux (car Lst))
(with *Class
(for A (car Lst)
(if (asoq A (: Aux))
(queue '@ Var)
(queue (:: Aux) (list A Var)) ) ) )
(extra Var (cdr Lst)) )
(de relAux (Obj Var Old Lst)
(let New (get Obj Var)
(put Obj Var Old)
(for A Lst
(rel> (meta Obj A) Obj (get Obj A) NIL) )
(put Obj Var New)
(for A Lst
(rel> (meta Obj A) Obj NIL (get Obj A)) ) ) )
(dm iter> (X Lst)
(if (or (atom X) (atom (car X)))
(extra X Lst)
(let Tree (tree (: var) (: cls) (caddr Lst))
(cons
(if (>= (cdr X) (car X))
(init Tree
(car X)
(append (cdr X) T) )
(init Tree
(append (car X) T)
(cdr X) ) )
(let @Cls (cadr Lst)
(curry (@Cls) (Q)
(loop
(NIL (step Q))
(T (isa '@Cls @) @) ) ) ) ) ) ) )
(dm match> (X Val Obj)
(if (or (atom X) (atom (car X)))
(extra X Val Obj)
(setq Val
(cons Val
(mapcar '((S) (get Obj S)) (: aux)) ) )
(when
(if (>= (cdr X) (car X))
(>= (append (cdr X) T) Val (car X))
(>= (append (car X) T) Val (cdr X)) )
Val ) ) )
# UB-Tree (+Aux prefix)
(class +UB)
(dm T (Var Lst)
(=: ub T)
(extra Var Lst) )
(dm has> (Val X)
(and Val
(or
(extra Val X)
(extra
(let (N (inc (length (: aux))) M 1 V 0)
(while (gt0 Val)
(and (bit? 1 Val) (inc 'V M))
(setq M (>> -1 M) Val (>> N Val)) )
V )
X ) ) ) )
(dm iter> (X Lst)
(cons
(init (tree (: var) (: cls) (caddr Lst))
(ubZval (car X))
(ubZval (cdr X) T) )
(let @Cls (cadr Lst)
(curry (@Cls) (Q)
(loop
(NIL (step Q))
(T (isa '@Cls @) @) ) ) ) ) )
### Relation prefix classes ###
(class +Dep)
(dm T (Var Lst)
(=: dep (car Lst))
(extra Var (cdr Lst)) )
(dm rel> (Obj Old New Hook)
(unless New
(for Var (: dep)
(let? V (get Obj Var)
(rel> (meta Obj Var) Obj V
(put Obj Var (put> (meta Obj Var) Obj V NIL)) )
(when (asoq Var (meta Obj 'Aux))
(relAux Obj Var V (cdr @)) )
(upd> Obj Var V) ) ) )
(extra Obj Old New Hook) )
(class +List)
(dm mis> (Val Obj)
(ifn (lst? Val)
"Not a List"
(pick '((V) (extra V Obj)) Val) ) )
(dm ele> (Val)
(and Val (or (atom Val) (find extra Val))) )
(dm has> (Val X)
(when Val
(or
(= Val X)
(find '((X) (extra Val X)) X)
(loop
(NIL
(let (V (++ Val) Y (++ X))
(or (= V Y) (extra V Y)) ) )
(NIL (or Val X) T)
(T (xor Val X)) ) ) ) )
(dm put> (Obj Old New)
(if (ele> This New)
(cons (extra Obj Old New) Old)
(mapcar
'((N O) (extra Obj O N))
New
Old ) ) )
(dm del> (Obj Old Val)
(and
(<> Old Val)
(delete Val Old T) ) )
(dm rel> (Obj Old New Hook)
(if (or (ele> This Old) (ele> This New))
(extra Obj Old New Hook)
(for O Old
(if (: bag)
(for (I . This) @
(let V (get O I)
(unless (find '((L) (= V (get L I))) New)
(rel> This Obj V NIL
(when (: hook)
(get (if (sym? @) Obj O) (: hook)) ) ) ) ) )
(unless (member O New)
(extra Obj O NIL Hook) ) ) )
(for N New
(if (: bag)
(for (I . This) @
(let V (get N I)
(unless (find '((L) (= V (get L I))) Old)
(rel> This Obj NIL V
(when (: hook)
(get (if (sym? @) Obj N) (: hook)) ) ) ) ) )
(unless (member N Old)
(extra Obj NIL N Hook) ) ) ) ) )
(dm rel?> (Obj Val Hook)
(for V Val
(NIL (or (not V) (extra Obj V Hook)))
T ) )
(dm lose> (Obj Val Hook)
(if (ele> This Val)
(extra Obj Val Hook)
(for V Val
(extra Obj V Hook) ) ) )
(dm keep> (Obj Val Hook)
(if (ele> This Val)
(extra Obj Val Hook)
(for V Val
(extra Obj V Hook) ) ) )
(dm match> (X Val Obj)
(pick '((Y) (extra X Y Obj)) Val) )
(class +Need)
(dm mis> (Val Obj)
(ifn Val
,"Input required"
(extra Val Obj) ) )
(class +Mis)
(dm T (Var Lst)
(=: mis (car Lst))
(extra Var (cdr Lst)) )
(dm mis> (Val Obj)
(or ((: mis) Val Obj) (extra Val Obj)) )
(class +Alt)
(dm T (Var Lst)
(extra Var (cdr Lst))
(=: cls (car Lst)) )
(class +Swap)
(dm mis> (Val Obj)
(extra (if (ext? Val) (val Val) Val) Obj) )
(dm has> (Val X)
(if (ext? Val)
(== Val X)
(extra Val (val X)) ) )
(dm put> (Obj Old New)
(let N
(extra
Obj
(val Old)
(if (ext? New) (val @) New) )
(cond
((ext? Old)
(if (ext? New)
New
(set Old N)
Old ) )
(N
(prog1
(new (or (: dbf 1) 1))
(set @ N) ) ) ) ) )
(dm del> (Obj Old Val)
(ifn (ext? Old)
(extra Obj Old Val)
(set @ (extra Obj (val Old) Val))
@ ) )
(dm rel> (Obj Old New Hook)
(extra
Obj
(if (ext? Old) (val @) Old)
(if (ext? New) (val @) New)
Hook ) )
(dm rel?> (Obj Val Hook)
(if (ext? Val)
(if (val @)
(extra Obj @ Hook)
T )
(extra Obj Val Hook) ) )
(dm lose> (Obj Val Hook)
(extra Obj (if (ext? Val) (val @) Val) Hook) )
(dm keep> (Obj Val Hook)
(extra Obj (if (ext? Val) (val @) Val) Hook) )
### Entities ###
(de dbSync (Obj)
(let *Run NIL
(while (lock (or Obj *DB))
(wait 40) )
(sync) ) )
(class +Entity)
(var Dbf)
(var Aux)
(de incECnt (Obj)
(let M NIL
(for Cls (type Obj)
(recur (Cls)
(or
(== '+Entity Cls)
(memq Cls M)
(when (isa '+Entity (push 'M Cls))
(for C (type @)
(recurse C) )
(if (get *DB Cls)
(inc @)
(put *DB Cls (new T 1)) ) ) ) ) ) ) )
(de decECnt (Obj)
(let M NIL
(for Cls (type Obj)
(recur (Cls)
(or
(== '+Entity Cls)
(memq Cls M)
(when (isa '+Entity (push 'M Cls))
(for C (type @)
(recurse C) )
(and (get *DB Cls) (dec @)) ) ) ) ) ) )
(private) (cloneKey cloneAny)
(dm T @
(incECnt This)
(while (args)
(let A (next)
(cond
((=T A) (put This T T))
((atom A) (put> This A (next)))
(T (put> This (car A) (eval (cdr A)))) ) ) )
(upd> This (val This)) )
(dm zap> ()
(for X (getl This)
(let V (or (atom X) (++ X))
(and (meta This X) (zap> @ This V)) ) )
(unless (: T) (decECnt This)) )
(dm url> (Tab Fld))
(dm url1> (Tab Fld) (url> This 1 Fld))
(dm url2> (Tab Fld) (url> This 2 Fld))
(dm url3> (Tab Fld) (url> This 3 Fld))
(dm url4> (Tab Fld) (url> This 4 Fld))
(dm url5> (Tab Fld) (url> This 5 Fld))
(dm url6> (Tab Fld) (url> This 6 Fld))
(dm url7> (Tab Fld) (url> This 7 Fld))
(dm url8> (Tab Fld) (url> This 8 Fld))
(dm url9> (Tab Fld) (url> This 9 Fld))
(dm gui> ())
(dm upd> (X Old))
(dm has> (Var Val)
(or
(nor
Val
(if2 (get This Var) (ext? @) (val @) @) )
(has> (meta This Var) Val (get This Var)) ) )
(dm rel?> (Var Val)
(nond
(Val T)
((meta This Var) T)
(NIL (rel?> @ This Val)) ) )
(dm put> (Var Val)
(unless (has> This Var Val)
(let Old (get This Var)
(rel> (meta This Var) This Old
(put This Var (put> (meta This Var) This Old Val)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old) ) )
Val )
(dm put!> (Var Val)
(unless (has> This Var Val)
(dbSync)
(let Old (get This Var)
(rel> (meta This Var) This Old
(put This Var (put> (meta This Var) This Old Val)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old)
(commit 'upd) ) )
Val )
(dm del> (Var Val)
(when (and Val (has> (meta This Var) Val (get This Var)))
(let Old (get This Var)
(rel> (meta This Var) This Old
(put This Var (del> (meta This Var) This Old Val)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old) ) ) )
(dm del!> (Var Val)
(when (and Val (has> (meta This Var) Val (get This Var)))
(dbSync)
(let Old (get This Var)
(rel> (meta This Var) This Old
(put This Var (del> (meta This Var) This Old Val)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old)
(commit 'upd) ) ) )
(dm inc> (Var Val)
(let P (prop This Var)
(when (num? (car P))
(let Old @
(rel> (meta This Var) This Old
(inc P (or Val 1)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old) )
(car P) ) ) )
(dm inc!> (Var Val)
(when (num? (get This Var))
(dbSync)
(let (P (prop This Var) Old (car P))
(rel> (meta This Var) This Old
(inc P (or Val 1)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old)
(commit 'upd)
(car P) ) ) )
(dm dec> (Var Val)
(let P (prop This Var)
(when (num? (car P))
(let Old @
(rel> (meta This Var) This Old
(dec P (or Val 1)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old) )
(car P) ) ) )
(dm dec!> (Var Val)
(when (num? (get This Var))
(dbSync)
(let (P (prop This Var) Old (car P))
(rel> (meta This Var) This Old
(dec P (or Val 1)) )
(when (asoq Var (meta This 'Aux))
(relAux This Var Old (cdr @)) )
(upd> This Var Old)
(commit 'upd)
(car P) ) ) )
(dm mis> (Var Val)
(mis> (meta This Var) Val This) )
(dm lose1> (Var)
(when (meta This Var)
(lose> @ This (get This Var)) ) )
(dm lose> (Lst)
(unless (: T)
(for X (getl This)
(let V (or (atom X) (++ X))
(and
(not (memq X Lst))
(meta This X)
(lose> @ This V) ) ) )
(decECnt This)
(=: T T)
(upd> This) ) )
(dm lose!> (Lst)
(dbSync)
(lose> This Lst)
(commit 'upd) )
(de lose "Prg"
(let "Flg" (: T)
(=: T T)
(run "Prg")
(=: T "Flg") ) )
(dm keep1> (Var)
(when (meta This Var)
(keep> @ This (get This Var)) ) )
(dm keep> (Lst)
(when (: T)
(=: T)
(incECnt This)
(for X (getl This)
(let V (or (atom X) (++ X))
(and
(not (memq X Lst))
(meta This X)
(keep> @ This V) ) ) )
(upd> This T) ) )
(dm keep?> (Lst)
(extract
'((X)
(with (and (pair X) (meta This (cdr X)))
(and
(isa '+Key This)
(fetch (tree (: var) (: cls) (and (: hook) (get (up This) @))) (car X))
(cons (car X) ,"Not unique") ) ) )
(getl This) ) )
(dm keep!> (Lst)
(dbSync)
(keep> This Lst)
(commit 'upd) )
(de keep "Prg"
(let "Flg" (: T)
(=: T)
(run "Prg")
(=: T "Flg") ) )
(dm set> (Val)
(unless (= Val (val This))
(decECnt This)
(let Lst (make (maps '((X) (link (fin X))) This))
(for Var Lst
(let? Rel (meta This Var)
(unless (== Rel (meta Val Var))
(let V (get This Var)
(and (isa '+Swap Rel) (setq V (val V)))
(rel> Rel This V (put> Rel This V NIL)) ) ) ) )
(xchg This 'Val)
(for Var Lst
(let? Rel (meta This Var)
(unless (== Rel (meta Val Var))
(let V (get This Var)
(rel> Rel This NIL
(put> Rel This NIL
(if (isa '+Swap Rel) (val V) V) ) ) ) ) ) ) )
(incECnt This)
(upd> This (val This) Val) )
(val This) )
(dm set!> (Val)
(unless (= Val (val This))
(dbSync)
(decECnt This)
(let Lst (make (maps '((X) (link (fin X))) This))
(for Var Lst
(let? Rel (meta This Var)
(unless (== Rel (meta Val Var))
(let V (get This Var)
(and (isa '+Swap Rel) (setq V (val V)))
(rel> Rel This V (put> Rel This V NIL)) ) ) ) )
(xchg This 'Val)
(for Var Lst
(let? Rel (meta This Var)
(unless (== Rel (meta Val Var))
(let V (get This Var)
(rel> Rel This NIL
(put> Rel This NIL
(if (isa '+Swap Rel) (val V) V) ) ) ) ) ) ) )
(incECnt This)
(upd> This (val This) Val)
(commit 'upd) )
(val This) )
(dm clone> (Lst)
(let Obj (new (or (var: Dbf 1) 1) (val This))
(for X
(by
'((X)
(nand
(pair X)
(isa '+Hook (meta This (cdr X))) ) )
sort
(getl This) )
(unless (memq (fin X) Lst)
(if (atom X)
(ifn (meta This X)
(put Obj X T)
(let Rel @
(put> Obj X T)
(when (isa '+Blob Rel)
(in (blob This X)
(out (blob Obj X) (echo)) )
(blob+ Obj X) ) ) )
(ifn (meta This (cdr X))
(put Obj (cdr X) (car X))
(let Rel @
(cond
((find '((B) (isa '+Key B)) (get Rel 'bag))
(let (K @ H (get K 'hook))
(put> Obj (cdr X)
(mapcar
'((Lst)
(mapcar
'((B Val)
(if (== B K)
(cloneKey B (cdr X) Val
(and H (get (if (sym? H) This Lst) H)) )
Val ) )
(get Rel 'bag)
Lst ) )
(car X) ) ) ) )
((isa '+Key Rel)
(put> Obj (cdr X)
(cloneKey Rel (cdr X) (car X)
(and (get Rel 'hook) (get This @)) ) ) )
((or (not (isa '+Joint Rel)) (isa '+List (meta Obj (cdr X))))
(put> Obj (cdr X) (cloneAny (car X) Rel)) ) ) ) ) ) ) )
Obj ) )
(de cloneKey (Rel Var Val Hook)
(cond
((isa '+Number Rel)
(genKey Var (get Rel 'cls) Hook) )
((isa '+String Rel)
(genStrKey (pack "# " Val) Var (get Rel 'cls) Hook) ) ) )
(de cloneAny (Val Rel)
(cond
((isa '+Swap Rel) (val Val))
((isa '+Bag Rel)
(if (isa '+List Rel)
(mapcar
'((B) (mapcar cloneAny B (; Rel bag)))
Val )
(mapcar cloneAny Val (; Rel bag)) ) )
(T Val) ) )
(dm clone!> ()
(prog2
(dbSync)
(clone> This)
(commit 'upd) ) )
(de new! ("Typ" . @)
(prog2
(dbSync)
(pass new (or (meta "Typ" 'Dbf 1) 1) "Typ")
(commit 'upd) ) )
(de set! (Obj Val)
(unless (= Val (val Obj))
(dbSync)
(set Obj Val)
(commit 'upd) )
Val )
(de put! (Obj Var Val)
(unless (= Val (get Obj Var))
(dbSync)
(put Obj Var Val)
(commit 'upd) )
Val )
(de inc! (Obj Var Val)
(when (num? (get Obj Var))
(prog2
(dbSync)
(inc (prop Obj Var) (or Val 1))
(commit 'upd) ) ) )
(de blob! (Obj Var File)
(put!> Obj Var File)
(blob+ Obj Var)
File )
(de blob+ (Obj Var)
(when *Jnl
(chdir *Blob
(%@ "symlink" 'I
(pack (glue "/" (chop Obj)) "." Var)
(pack "=" (name Obj) "." Var) ) ) ) )
# Remote entities
(class +Remote)
(dm zap> ())
(dm has> ~(method 'has> '+Entity))
(dm url> (Tab Fld))
(dm url1> ~(method 'url1> '+Entity))
(dm url2> ~(method 'url2> '+Entity))
(dm url3> ~(method 'url3> '+Entity))
(dm url4> ~(method 'url4> '+Entity))
(dm url5> ~(method 'url5> '+Entity))
(dm url6> ~(method 'url6> '+Entity))
(dm url7> ~(method 'url7> '+Entity))
(dm url8> ~(method 'url8> '+Entity))
(dm url9> ~(method 'url9> '+Entity))
(dm put> (Var Val))
(dm put!> (Var Val))
(dm del> (Var Val))
(dm del!> (Var Val))
(dm inc> (Var Val))
(dm inc!> (Var Val))
(dm dec> (Var Val))
(dm dec!> (Var Val))
(dm mis> (Var Val))
(dm lose1> (Var))
(dm lose> (Lst))
(dm lose!> (Lst))
(dm keep1> (Var))
(dm keep> (Lst))
(dm keep?> (Lst))
(dm keep!> (Lst))
(dm set> (Val))
(dm set!> (Val))
# Default syncronization function
(de upd Lst
(wipe Lst) )
### DB Sizes ###
(de dbs Lst
(setq *Dbs
(make
(for (N . L) Lst
(let Dbf (cons N (>> (- (link (car L))) 64))
(for Cls (cdr L)
(if (atom Cls)
(put Cls 'Dbf Dbf)
(for Var (cdr Cls)
(let Rel (get Cls 1 Var)
(unless Rel
(quit "Bad relation" (cons Var (car Cls))) )
(when (or (isa '+index Rel) (isa '+Swap Rel))
(put @ 'dbf Dbf) )
(for B (; Rel bag)
(when (or (isa '+index B) (isa '+Swap B))
(put @ 'dbf Dbf)) ) ) ) ) ) ) ) ) ) )
(de db: Typ
(or (meta Typ 'Dbf 1) 1) )
### Utilities ###
(private) _db
(de treeRel (Var Cls)
(with (or (get Cls Var) (meta Cls Var))
(or
(find '((B) (isa '+index B)) (: bag))
This ) ) )
# (db 'var 'cls ['hook] 'any ['var 'any ..]) -> sym
(de db (Var Cls . @)
(with (treeRel Var Cls)
(let (Tree (tree (: var) (: cls) (and (: hook) (next))) Val (next))
(if (isa '+Key This)
(if (args)
(and (fetch Tree Val) (pass _db @))
(fetch Tree Val) )
(let Key (cons (if (isa '+Fold This) (fold Val) Val))
(let? A (: aux)
(while (and (args) (== (++ A) (arg 1)))
(next)
(queue 'Key (next)) )
(and (: ub) (setq Key (ubZval Key))) )
(let Q (init Tree Key (append Key T))
(loop
(NIL (step Q T))
(T (pass _db @ Var Val) @) ) ) ) ) ) ) )
(de _db (Obj . @)
(when (isa Cls Obj)
(loop
(NIL (next) Obj)
(NIL (has> Obj @ (next))) ) ) )
# (aux 'var 'cls ['hook] 'any ..) -> sym
(de aux (Var Cls . @)
(with (treeRel Var Cls)
(let Key (if (: ub) (ubZval (rest)) (rest))
(step
(init (tree (: var) (: cls) (and (: hook) (next)))
Key
(append Key T) ) ) ) ) )
# (collect 'var 'cls ['hook] ['any|beg ['end [var ..]]]) -> lst
(de collect (Var Cls . @)
(with (treeRel Var Cls)
(let
(Tree (tree (: var) (: cls) (and (: hook) (next)))
X1 (next)
X2 (if (args) (next) (or X1 T)) )
(make
(cond
((isa '+Key This)
(iter Tree
'((X) (and (isa Cls X) (link (pass get X))))
X1 X2 ) )
((: ub)
(if X1
(ubIter Tree (inc (length (: aux)))
'((X) (and (isa Cls X) (link (pass get X))))
X1 X2 )
(iter Tree
'((X) (and (isa Cls X) (link (pass get X)))) ) ) )
(T
(when (isa '+Fold This)
(setq X1 (fold X1) X2 (or (=T X2) (fold X2))) )
(if (>= X2 X1)
(if (pair X1)
(setq X2 (append X2 T))
(setq X1 (cons X1) X2 (cons X2 T)) )
(if (pair X1)
(setq X1 (append X1 T))
(setq X1 (cons X1 T) X2 (cons X2)) ) )
(iter Tree
'((X)
(and (isa Cls X) (link (pass get X))) )
X1 X2
(or (isa '+Idx This) (isa '+IdxFold This)) ) ) ) ) ) ) )
# Combined 'search' function
(private) (*SX *SY search1)
(de relQ (X Lst)
(iter> (meta (cdr Lst) (car Lst)) X Lst) )
(de search1 (Val Lst) # ((Q . stepFun) . lst)
(let X (++ Lst)
(cons
(cond
((pair (cdr X)) (relQ Val X))
((pair (setq X (get Val (cdr X))))
(cons (list X) pop) )
(T (cons (list (list X)) pop)) )
Lst ) ) )
(de search (*SX *SY . @)
(ifn *SY
(for (P (cddr *SX) P (or (cdr P) (cddr *SX))) # Next search result
(NIL (setq *SY ((cadar P) (caaar P))))
(T
(and
(fully # Filter
'((L) ((cddr L) *SY (cdar L)))
(cddr *SX) )
(nand # Deduplicate
(val *SX)
(idx *SX (cons (hash *SY) *SY) T) )
((cadr *SX) *SY) ) # Extract
(setq P
(rot (cddr *SX) (offset P (cddr *SX))) )
@ ) )
# Init search
(let
(*Iter+ NIL
Lst
(make
(link prog) # idx and extract
(loop
(when (or *SX (nor (args) (cdr (made))))
(link # ((Q . V) genFun . filterFun)
(cons
(cons
(list
(if (pair (caar *SY))
(cons ((car *SY) *SX) (cddr *SY)) # (init . step)
(search1 *SX *SY) ) )
*SX )
'((Q) # Generator
(use Obj
(loop
(T
(nor # Done
(setq Obj ((cdaar Q) (caaar Q)))
(cdr Q) ) )
(T (unless (cdar Q) Obj) Obj) # Result
(if Obj
(let L (cdar Q)
(con Q (cons (car Q) (cdr Q)))
(or
(val *SX) # 'idx'
(not
(isa '+List
(if (pair (cdar L))
(meta @ (caar L)) # (+Ref +Link)
(with (meta Obj (cdar L)) # +Joint
(meta (: type) (: slot)) ) ) ) )
(set *SX T) )
(set Q (search1 Obj L)) )
(set Q (cadr Q))
(con Q (cddr Q)) ) ) ) )
(let # Filter
(@Exe
(if (pair (caar *SY))
(cons
(lit (car (shift '*SY)))
'(X V) )
(list 'match>
(lit
(if (atom (cdar *SY))
(with (meta *SX (cdar *SY)) # +Joint
(meta (: type) (: slot)) )
(meta (cdar *SY) (caar *SY)) ) ) # +index
'V
(list '; 'X (caar *SY))
'X ) )
@Lst (flip (mapcar car (cdr *SY))) )
(curry (@Lst @Exe) (X V)
(let L '@Lst
(recur (X L)
(if L
(pick recurse
(fish ext? (get X (++ L)))
(circ L) )
@Exe ) ) ) ) ) ) ) )
(setq *SX (next))
(NIL (setq *SY (next))
(and *SX (set (made) @)) ) ) ) )
(cons
(or *Iter+ (bool (cddr Lst))) # 'idx'
Lst ) ) ) )
# Multiple indexes
(de relQs (@Lst . @)
(cons
(curry (@Lst) (X)
(on *Iter+)
(cons
(list
(mapcar '((Y) (relQ X Y)) '@Lst) )
'((Q)
(or
((cdaar Q) (caaar Q))
(and
(cdar Q)
(shift Q)
((cdaar Q) (caaar Q)) ) ) ) ) )
(let
(@L
(mapcar
'((Y) (meta (cdr Y) (car Y)))
@Lst )
@V (mapcar car @Lst) )
(curry (@L @V) (X Val)
(find
'((R V) (match> R Val (get X V) X))
'@L
'@V ) ) )
(rest) ) )
# Iterate all objects of given class or search query
(private) (X Prg)
(de forall (X . Prg)
(cond
((or (atom X) (num? (car X))) # 'seq'
(for
(This
(seq
(or
(and (pair X) (++ X))
(; X Dbf 1)
(meta X 'Dbf 1)
1 ) )
This
(seq This) )
(and (isa X This) (run Prg 1)) ) )
((flg? (car X)) # 'search'
(while (search X)
(with @ (run Prg 1)) ) )
(T # 'init'
(while (step X)
(with @ (run Prg 1)) ) ) ) )
# Define object variables as relations
(de rel Lst
(def *Class
(car Lst)
(new (cadr Lst) (car Lst) (cddr Lst)) ) )
# Find or create object
(de request (Typ Var . @)
(let Dbf (or (meta Typ 'Dbf 1) 1)
(ifn Var
(new Dbf Typ)
(with (meta Typ Var)
(or
(pass db Var (: cls))
(if (: hook)
(pass new Dbf Typ @ (next) Var)
(pass new Dbf Typ Var) ) ) ) ) ) )
(de request! (Typ Var . @)
(prog2
(dbSync)
(pass request Typ Var)
(commit 'upd) ) )
# Create or update object
(private) *ObjIdx
(de obj Lst
(let Obj
(let L (++ Lst)
(if (pair (car L))
(apply request L)
(cache '*ObjIdx (++ Lst)
(new (or (meta L 'Dbf 1) 1) L) ) ) )
(while Lst
(let (K (++ Lst) V (++ Lst))
(if (=T K)
(lose> Obj)
(put> Obj K V) ) ) )
Obj ) )
# Create or update lots of objects
(de create (Typ Key Vars . Prg)
(prune 0)
(setq Vars # ((var fd lst cnt . cnt) ..)
(mapcar
'((Var)
(if (isa '+index (meta Typ Var))
(cons Var
(open (tmp (pack "create-" Var)))
NIL 0 1000000 )
Var ) )
Vars ) )
(while (run Prg) # (val ..)
(let (Lst @ Obj (or (fin Lst) (new (meta Typ 'Dbf 1) Typ)))
(and Key (++ Lst) (put> Obj Key @))
(let store '((Tree Key Val Dbf) (link Key))
(mapc
'((V Val)
(when (or Val (fin Lst))
(if (atom V)
(put> Obj V Val)
(out (cadr V)
(for Key (make (put> Obj (car V) Val))
(at (cdddr V) (push (cddr V) Key))
(pr Key Obj) ) ) ) ) )
Vars
Lst ) ) )
(at (0 . 1000000) (commit) (prune 2)) )
(commit)
(prune 0)
(let Lst
(extract
'((V)
(unless (atom V)
(close (cadr V))
(let
(Var (car V)
File (tmp (pack "create-" Var))
N 0 )
(setq V
(mapcar
'((Key)
(let F (tmp (pack "create-" (inc 'N)))
(cons Key F
(or (open F) (quit "Too many files")) ) ) )
(cons NIL (sort (caddr V))) ) )
(in File
(while (setq Key (rd))
(out (cddr (rank Key V))
(pr Key (rd)) ) ) )
(%@ "unlink" NIL File)
(let (Dbf (meta Typ Var 'dbf) Tree (cons Var (new T)))
(for R V
(close (cddr R))
(for X
(sort
(make
(in (cadr R)
(while (rd)
(link (cons @ (rd))) ) ) ) )
(store Tree (car X) (cdr X) Dbf)
(at (0 . 1000) (prune 2)) )
(commit)
(prune 2)
(%@ "unlink" NIL (cadr R)) )
(commit)
Tree ) ) ) )
Vars )
(prune)
(for Tree Lst
(let
(Base (get *DB (meta Typ (car Tree) 'cls))
Root (get (cdr Tree) (car Tree)) )
(ifn (get Base (car Tree))
(put Base (car Tree) Root)
(touch Base)
(inc @ (car Root)) ) )
(zap (cdr Tree)) )
(commit) ) )
### Debug ###
`*Dbg
(noLint 'create 'store)
(load "@lib/sq.l")
|