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
//! Top level abstraction for buffer objects use crate::pointer::{NP_Cursor_Value, NP_Scalar}; use crate::{pointer::NP_Cursor_Parent, collection::map::NP_Map, utils::print_path}; use crate::{schema::NP_TypeKeys, pointer::NP_Value}; use crate::pointer::NP_Cursor; use alloc::string::String; use crate::{collection::{tuple::NP_Tuple}}; use crate::{schema::NP_Parsed_Schema, collection::table::NP_Table}; use alloc::vec::Vec; use crate::{collection::{list::NP_List}}; use crate::error::NP_Error; use crate::memory::{NP_Size, NP_Memory}; use crate::{json_flex::NP_JSON}; use crate::alloc::borrow::ToOwned; use alloc::string::ToString; /// The address location of the root pointer. #[doc(hidden)] pub const ROOT_PTR_ADDR: usize = 2; /// Maximum size of list collections #[doc(hidden)] pub const LIST_MAX_SIZE: usize = core::u16::MAX as usize; /// Buffers contain the bytes of each object and allow you to perform reads, updates, deletes and compaction. /// /// #[derive(Debug)] pub struct NP_Buffer<'buffer> { /// Schema data used by this buffer memory: NP_Memory<'buffer>, cursor: NP_Cursor } /// When calling `maybe_compact` on a buffer, this struct is provided to help make a choice on wether to compact or not. #[derive(Debug, Eq, PartialEq)] pub struct NP_Size_Data { /// The size of the existing buffer pub current_buffer: usize, /// The estimated size of buffer after compaction pub after_compaction: usize, /// How many known wasted bytes in existing buffer pub wasted_bytes: usize } impl<'buffer> NP_Buffer<'buffer> { #[doc(hidden)] pub fn _new(memory: NP_Memory<'buffer>) -> Self { // make new buffer let root_cursor = NP_Cursor::new(ROOT_PTR_ADDR, 0, &memory, NP_Cursor_Parent::None); NP_Buffer { cursor: root_cursor, memory: memory } } /// Copy an object at the provided path and all it's children into JSON. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "table", /// "columns": [ /// ["age", {"type": "uint8"}], /// ["name", {"type": "string"}] /// ] /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// new_buffer.set(&["name"], "Jeb Kermin"); /// new_buffer.set(&["age"], 30u8); /// /// assert_eq!("{\"name\":\"Jeb Kermin\",\"age\":30}", new_buffer.json_encode(&[])?.stringify()); /// assert_eq!("\"Jeb Kermin\"", new_buffer.json_encode(&["name"])?.stringify()); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn json_encode(&self, path: &'buffer [&str]) -> Result<NP_JSON, NP_Error> { let value_cursor = self.select(self.cursor.clone(), false, path, 0)?; if let Some(x) = value_cursor { Ok(NP_Cursor::json_encode(&x, &self.memory)) } else { Ok(NP_JSON::Null) } } /// Moves the underlying bytes out of the buffer, consuming the buffer in the process. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set initial value /// new_buffer.set(&[], "hello")?; /// // close buffer and get bytes /// let bytes: Vec<u8> = new_buffer.close(); /// assert_eq!([1, 1, 0, 4, 0, 5, 104, 101, 108, 108, 111].to_vec(), bytes); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn close(self) -> Vec<u8> { self.memory.dump() } /// Read the bytes of the buffer immutably. No touching! /// pub fn read_bytes(&self) -> &Vec<u8> { self.memory.read_bytes() } /// Move buffer cursor to new location. Cursors can only be moved into children. If you need to move up reset the cursor to root, then move back down to the desired level. /// pub fn move_cursor(&mut self, path: &[&str]) -> Result<bool, NP_Error> { let value_cursor = self.select(self.cursor.clone(), true, path, 0)?; let cursor = if let Some(x) = value_cursor { x } else { return Ok(false); }; self.cursor = cursor; Ok(true) } /// Reset cursor position to root of buffer /// pub fn cursor_to_root(&mut self) { self.cursor = NP_Cursor::new(ROOT_PTR_ADDR, 0, &self.memory, NP_Cursor_Parent::None); } /// Used to set scalar values inside the buffer, the path only works with dot notation. /// /// The type that you cast the request to will be compared to the schema, if it doesn't match the schema the request will fail. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// // a list where each item is a map where each key has a value containing a list of strings /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "map", "value": { /// "type": "list", "of": {"type": "string"} /// }} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // third item in the top level list -> key "alpha" of map at 3rd element -> 9th element of list at "alpha" key /// // /// new_buffer.set(&["3", "alpha", "9"], "look at all this nesting madness")?; /// /// // get the same item we just set /// let message = new_buffer.get::<&str>(&["3", "alpha", "9"])?; /// /// assert_eq!(message, Some("look at all this nesting madness")); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn set<X>(&mut self, path: &[&str], value: X) -> Result<bool, NP_Error> where X: NP_Value<'buffer> + NP_Scalar { let value_cursor = self.select(self.cursor.clone(), true, path, 0)?; match value_cursor { Some(x) => { if path.len() == 0 { self.cursor = X::set_value(x, &self.memory, value)?; } else { X::set_value(x, &self.memory, value)?; } Ok(true) } None => Ok(false) } } /// Get an iterator for a collection /// /// /// ## List Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set value at 1 index /// new_buffer.set(&["1"], "hello")?; /// // set value at 4 index /// new_buffer.set(&["4"], "world")?; /// // push value onto the end /// new_buffer.list_push(&[], "!")?; /// /// // get iterator of root (list item) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// match item.index { /// 0 => assert_eq!(item.get::<&str>().unwrap(), None), /// 1 => assert_eq!(item.get::<&str>().unwrap(), Some("hello")), /// 2 => assert_eq!(item.get::<&str>().unwrap(), None), /// 3 => assert_eq!(item.get::<&str>().unwrap(), None), /// 4 => assert_eq!(item.get::<&str>().unwrap(), Some("world")), /// 5 => assert_eq!(item.get::<&str>().unwrap(), Some("!")), /// _ => panic!() /// }; /// }); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Table Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "table", /// "columns": [ /// ["age", {"type": "uint8"}], /// ["name", {"type": "string"}], /// ["job", {"type": "string"}], /// ["tags", {"type": "list", "of": {"type": "string"}}] /// ] /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set value of age /// new_buffer.set(&["age"], 20u8)?; /// // set value of name /// new_buffer.set(&["name"], "Bill Kerman")?; /// // push value onto tags list /// new_buffer.list_push(&["tags"], "rocket")?; /// /// // get iterator of root (table) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// /// match item.get_key() { /// "name" => assert_eq!(item.get::<&str>().unwrap(), Some("Bill Kerman")), /// "age" => assert_eq!(item.get::<u8>().unwrap(), Some(20)), /// "job" => assert_eq!(item.get::<&str>().unwrap(), None), /// "tags" => { /* tags column is list, can't do anything with it here */ }, /// _ => { panic!() } /// }; /// }); /// /// // we can also loop through items of the tags list /// new_buffer.get_iter(&["tags"])?.unwrap().into_iter().for_each(|item| { /// assert_eq!(item.index, 0); /// assert_eq!(item.get::<&str>().unwrap(), Some("rocket")); /// }); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Map Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "map", /// "value": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set value of color key /// new_buffer.set(&["color"], "blue")?; /// // set value of sport key /// new_buffer.set(&["sport"], "soccor")?; /// /// // get iterator of root (map) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// println!("hello"); /// match item.get_key() { /// "color" => assert_eq!(item.get::<&str>().unwrap(), Some("blue")), /// "sport" => assert_eq!(item.get::<&str>().unwrap(), Some("soccor")), /// _ => panic!() /// } /// }); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Tuple Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "tuple", /// "values": [ /// {"type": "string"}, /// {"type": "u8"}, /// {"type": "bool"} /// ] /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set value at 0 index /// new_buffer.set(&["0"], "hello")?; /// // set value at 2 index /// new_buffer.set(&["2"], false)?; /// /// // get iterator of root (tuple item) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// match item.index { /// 0 => assert_eq!(item.get::<&str>().unwrap(), Some("hello")), /// 1 => assert_eq!(item.get::<u8>().unwrap(), None), /// 2 => assert_eq!(item.get::<bool>().unwrap(), Some(false)), /// _ => panic!() /// }; /// }); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn get_iter<'iter>(&'iter self, path: &'iter [&str]) -> Result<Option<NP_Generic_Iterator<'iter>>, NP_Error> { let value = self.select(self.cursor.clone(), false, path, 0)?; let mut value = if let Some(x) = value { x } else { return Ok(None); }; let value_addr = if value.buff_addr != 0 { self.memory.read_address(value.buff_addr) } else { 0 }; value.value = value.value.update_value_address(value_addr); // value doesn't exist if value.value.get_value_address() == 0 { return Ok(None); } match self.memory.schema[value.schema_addr] { NP_Parsed_Schema::Table { i: _, sortable: _, columns: _} => { Ok(Some(NP_Generic_Iterator { index: 0, iterator: NP_Iterator_Collection::Table(NP_Table::new(value, &self.memory)) })) }, NP_Parsed_Schema::Map { i: _, sortable: _, value: _} => { Ok(Some(NP_Generic_Iterator { index: 0, iterator: NP_Iterator_Collection::Map(NP_Map::new(value, &self.memory)) })) }, NP_Parsed_Schema::List { i: _, sortable: _, of: _} => { Ok(Some(NP_Generic_Iterator { index: 0, iterator: NP_Iterator_Collection::List(NP_List::new(value, &self.memory, false)) })) }, NP_Parsed_Schema::Tuple { i: _, sortable: _, values: _} => { Ok(Some(NP_Generic_Iterator { index: 0, iterator: NP_Iterator_Collection::Tuple(NP_Tuple::new(value, &self.memory)) })) }, _ => { Err(NP_Error::new("Attempted to ierate on non collection!")) } } } /// Allows quick and efficient inserting into lists, maps and tables. /// CAREFUL. Unlike the `set` method, **this one does not check for existing records with the provided key**. This works for `Lists`, `Maps` and `Tables`. /// /// You get a very fast insert into the buffer at the desired key, but **you must gaurantee that you're not inserting a key that's already been inserted.**. /// /// This method will let you insert duplicate keys all day long, then when you go to compact the buffer the **duplicates will not be deleted**. /// /// This is best used to quickly fill data into a new buffer. /// /// pub fn fast_insert<X>(&mut self, key: &str, value: X) -> Result<bool, NP_Error> where X: NP_Value<'buffer> + NP_Scalar { let collection_cursor = self.cursor.clone(); match self.memory.schema[collection_cursor.schema_addr] { NP_Parsed_Schema::List { of, .. } => { // list push let of_schema = &self.memory.schema[of]; // type does not match schema if X::type_idx().1 != *of_schema.get_type_key() { let mut err = "TypeError: Attempted to set value for type (".to_owned(); err.push_str(X::type_idx().0); err.push_str(") into schema of type ("); err.push_str(of_schema.get_type_data().0); err.push_str(")\n"); return Err(NP_Error::new(err)); } let (_new_index, new_cursor) = NP_List::push(collection_cursor, &self.memory, None)?; X::set_value(new_cursor, &self.memory, value)?; Ok(true) }, NP_Parsed_Schema::Map { .. } => { let new_cursor = NP_Map::select_into(collection_cursor, &self.memory, key, true, true)?; X::set_value(new_cursor, &self.memory, value)?; Ok(true) }, NP_Parsed_Schema::Table { .. } => { let new_cursor = NP_Table::select_into(collection_cursor, &self.memory, key, true, true)?; match new_cursor { Some(x) => { X::set_value(x, &self.memory, value)?; Ok(true) }, None => Ok(false) } }, _ => Ok(false) } } /// Push a value onto the end of a list. /// The path provided must resolve to a list type, and the type being pushed must match the schema /// /// This is the most efficient way to add values to a list type. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// new_buffer.set(&["3"], "launch")?; /// new_buffer.list_push(&[], "this")?; /// new_buffer.list_push(&[], "rocket")?; /// /// // get iterator of root (list item) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// match item.index { /// 0 => assert_eq!(item.get::<&str>().unwrap(), None), /// 1 => assert_eq!(item.get::<&str>().unwrap(), None), /// 2 => assert_eq!(item.get::<&str>().unwrap(), None), /// 3 => assert_eq!(item.get::<&str>().unwrap(), Some("launch")), /// 4 => assert_eq!(item.get::<&str>().unwrap(), Some("this")), /// 5 => assert_eq!(item.get::<&str>().unwrap(), Some("rocket")), /// _ => panic!() /// }; /// }); /// /// let mut new_buffer = factory.empty_buffer(None, None); /// new_buffer.list_push(&[], "launch")?; /// new_buffer.list_push(&[], "this")?; /// new_buffer.list_push(&[], "rocket")?; /// /// // get iterator of root (list item) /// new_buffer.get_iter(&[])?.unwrap().into_iter().for_each(|item| { /// match item.index { /// 0 => assert_eq!(item.get::<&str>().unwrap(), Some("launch")), /// 1 => assert_eq!(item.get::<&str>().unwrap(), Some("this")), /// 2 => assert_eq!(item.get::<&str>().unwrap(), Some("rocket")), /// _ => panic!() /// }; /// }); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn list_push<X>(&mut self, path: &[&str], value: X) -> Result<Option<u16>, NP_Error> where X: NP_Value<'buffer> + NP_Scalar { let list_cursor = if path.len() == 0 { self.cursor.clone() } else { match self.select(self.cursor.clone(), true, path, 0)? { Some(x) => x, None => return Ok(None) }}; match self.memory.schema[list_cursor.schema_addr] { NP_Parsed_Schema::List { i: _, sortable: _, of} => { let of_schema = &self.memory.schema[of]; // type does not match schema if X::type_idx().1 != *of_schema.get_type_key() { let mut err = "TypeError: Attempted to set value for type (".to_owned(); err.push_str(X::type_idx().0); err.push_str(") into schema of type ("); err.push_str(of_schema.get_type_data().0); err.push_str(")\n"); return Err(NP_Error::new(err)); } let (new_index, new_cursor) = NP_List::push(list_cursor, &self.memory, None)?; X::set_value(new_cursor, &self.memory, value)?; Ok(Some(new_index as u16)) }, _ => Ok(None) } } /// Get the schema info at a specific path, works for an type /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::schema::NP_TypeKeys; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // get schema of root /// let type_key = new_buffer.get_type(&[])?.unwrap(); /// /// let is_string = match type_key { /// NP_TypeKeys::UTF8String => { /// true /// }, /// _ => false /// }; /// /// assert!(is_string); /// /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn get_type(&self, path: &[&str]) -> Result<Option<&'buffer NP_TypeKeys>, NP_Error> { let value_cursor = self.select(self.cursor.clone(), false, path, 0)?; let found_cursor = if let Some(x) = value_cursor { x } else { return Ok(None) }; Ok(Some(self.memory.schema[found_cursor.schema_addr].get_type_key())) } /// Get length of String, Bytes, Table, Tuple, List or Map Type /// /// If the type found at the path provided does not support length operations, you'll get `None`. /// /// If the length of the item is zero, you can expect `Some(0)`. /// /// ## String Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set initial value /// new_buffer.set(&[], "hello")?; /// // get length of value at root (String) /// assert_eq!(new_buffer.length(&[])?, Some(5)); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Collection (List) Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set value at 9th index /// new_buffer.set(&["9"], "hello")?; /// // get length of value at root (List) /// assert_eq!(new_buffer.length(&[])?, Some(10)); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Collection (Table) Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "table", /// "columns": [ /// ["age", {"type": "u8"}], /// ["name", {"type": "string"}] /// ] /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // get length of value at root (Table) /// assert_eq!(new_buffer.length(&[])?, Some(2)); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Collection (Map) Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "map", /// "value": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set values /// new_buffer.set(&["foo"], "bar")?; /// new_buffer.set(&["foo2"], "bar2")?; /// // get length of value at root (Map) /// assert_eq!(new_buffer.length(&[])?, Some(2)); /// /// # Ok::<(), NP_Error>(()) /// ``` /// /// ## Collection (Tuple) Example /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "tuple", /// "values": [ /// {"type": "string"}, /// {"type": "string"} /// ] /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // get length of value at root (Tuple) /// assert_eq!(new_buffer.length(&[])?, Some(2)); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn length(&self, path: &[&str]) -> Result<Option<usize>, NP_Error> { let value_cursor = self.select(self.cursor.clone(), false, path, 0)?; let found_cursor = if let Some(x) = value_cursor { x } else { return Ok(None) }; if found_cursor.buff_addr == 0 { return Ok(None); } match &self.memory.schema[found_cursor.schema_addr] { NP_Parsed_Schema::List { i: _, sortable: _, of: _} => { Ok(Some(NP_List::new(found_cursor.clone(), &self.memory, false).into_iter().count())) }, NP_Parsed_Schema::Map { i: _, sortable: _, value: _} => { Ok(Some(NP_Map::new(found_cursor.clone(), &self.memory).into_iter().count())) }, NP_Parsed_Schema::Table { i: _, sortable: _, columns} => { Ok(Some(columns.len())) }, NP_Parsed_Schema::Tuple { i: _, sortable: _, values } => { Ok(Some(values.len())) }, NP_Parsed_Schema::Bytes { i: _, sortable: _, default: _, size} => { if *size > 0 { Ok(Some(*size as usize)) } else { Ok(Some(self.memory.read_address(found_cursor.value.get_value_address()))) } }, NP_Parsed_Schema::UTF8String { i: _, sortable: _, default: _, size} => { if *size > 0 { Ok(Some(*size as usize)) } else { Ok(Some(self.memory.read_address(found_cursor.value.get_value_address()))) } }, _ => { Ok(None) } } } /// Clear an inner value from the buffer. The path only works with dot notation. /// This can also be used to clear deeply nested collection objects or scalar objects. /// /// Returns `true` if it deleted a value, `false` otherwise. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "string"} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set index 0 /// new_buffer.set(&["0"], "hello")?; /// // del index 0 /// new_buffer.del(&["0"])?; /// // value is gone now! /// assert_eq!(None, new_buffer.get::<&str>(&["0"])?); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn del(&mut self, path: &[&str]) -> Result<bool, NP_Error> { let value_cursor = self.select(self.cursor.clone(), false, path, 0)?; match value_cursor { Some(x) => { // clear value address in buffer self.memory.write_address(x.buff_addr, 0); if path.len() == 0 { self.cursor.value = self.cursor.value.update_value_address(0); } Ok(true) } None => Ok(false) } } /// Retrieve an inner value from the buffer. The path only works with dot notation. /// /// The type that you cast the request to will be compared to the schema, if it doesn't match the schema the request will fail. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// // a list where each item is a map where each key has a value containing a list of strings /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "list", /// "of": {"type": "map", "value": { /// "type": "list", "of": {"type": "string"} /// }} /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // third item in the top level list -> key "alpha" of map at 3rd element -> 9th element of list at "alpha" key /// // /// new_buffer.set(&["3", "alpha", "9"], "who would build a schema like this")?; /// /// // get the same item we just set /// let message = new_buffer.get::<&str>(&["3", "alpha", "9"])?; /// /// assert_eq!(message, Some("who would build a schema like this")); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn get<'get, X: 'get>(&'get self, path: &'get [&str]) -> Result<Option<X>, NP_Error> where X: NP_Value<'get> + NP_Scalar { let value_cursor = self.select(self.cursor.clone(), false, path, 0)?; match value_cursor { Some(x) => { match X::into_value(x, &self.memory)? { Some(x) => { Ok(Some(x)) }, None => { match X::schema_default(&self.memory.schema[x.schema_addr]) { Some(y) => { Ok(Some(y)) }, None => { Ok(None) } } } } } None => Ok(None) } } /// This performs a compaction if the closure provided as the third argument returns `true`. /// Compaction is a pretty expensive operation (requires full copy of the whole buffer) so should be done sparingly. /// The closure is provided an argument that contains the original size of the buffer, how many bytes could be saved by compaction, and how large the new buffer would be after compaction. /// /// The first argument, new_capacity, is the capacity of the underlying Vec<u8> that we'll be copying the data into. The default is the size of the old buffer. /// /// The second argument, new_size, can be used to change the size of the address space in the new buffer. Default behavior is to copy the address size of the old buffer. Be careful, if you're going from a larg address space down to a smaller one the data might not fit in the new buffer. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set initial value /// new_buffer.set(&[], "hello")?; /// // using 11 bytes /// assert_eq!(NP_Size_Data { /// current_buffer: 11, /// after_compaction: 11, /// wasted_bytes: 0 /// }, new_buffer.calc_bytes()?); /// // update the value /// new_buffer.set(&[], "hello, world")?; /// // now using 25 bytes, with 7 bytes of wasted space /// assert_eq!(NP_Size_Data { /// current_buffer: 25, /// after_compaction: 18, /// wasted_bytes: 7 /// }, new_buffer.calc_bytes()?); /// // compact to save space /// new_buffer.maybe_compact(None, None, |compact_data| { /// // only compact if wasted bytes are greater than 5 /// if compact_data.wasted_bytes > 5 { /// true /// } else { /// false /// } /// })?; /// // back down to 18 bytes with no wasted bytes /// assert_eq!(NP_Size_Data { /// current_buffer: 18, /// after_compaction: 18, /// wasted_bytes: 0 /// }, new_buffer.calc_bytes()?); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn maybe_compact<F>(&mut self, new_capacity: Option<u32>, new_size: Option<NP_Size>, mut callback: F) -> Result<(), NP_Error> where F: FnMut(NP_Size_Data) -> bool { let bytes_data = self.calc_bytes()?; if callback(bytes_data) { self.compact(new_capacity, new_size)?; } return Ok(()); } /// Compacts a buffer to remove an unused bytes or free space after a mutation. /// This is a pretty expensive operation (requires full copy of the whole buffer) so should be done sparingly. /// /// The first argument, new_capacity, is the capacity of the underlying Vec<u8> that we'll be copying the data into. The default is the size of the old buffer. /// /// The second argument, new_size, can be used to change the size of the address space in the new buffer. Default behavior is to copy the address size of the old buffer. Be careful, if you're going from a larg address space down to a smaller one the data might not fit in the new buffer. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// // set initial value /// new_buffer.set(&[], "hello")?; /// // using 11 bytes /// assert_eq!(NP_Size_Data { /// current_buffer: 11, /// after_compaction: 11, /// wasted_bytes: 0 /// }, new_buffer.calc_bytes()?); /// // update the value /// new_buffer.set(&[], "hello, world")?; /// // now using 25 bytes, with 7 bytes of wasted bytes /// assert_eq!(NP_Size_Data { /// current_buffer: 25, /// after_compaction: 18, /// wasted_bytes: 7 /// }, new_buffer.calc_bytes()?); /// // compact to save space /// new_buffer.compact(None, None)?; /// // back down to 18 bytes with no wasted bytes /// assert_eq!(NP_Size_Data { /// current_buffer: 18, /// after_compaction: 18, /// wasted_bytes: 0 /// }, new_buffer.calc_bytes()?); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn compact(&mut self, new_capacity: Option<u32>, new_size: Option<NP_Size>) -> Result<(), NP_Error> { let capacity = match new_capacity { Some(x) => { x as usize }, None => self.memory.read_bytes().len() }; let size = match new_size { None => self.memory.size, Some(x) => { x } }; let old_root = NP_Cursor::new(ROOT_PTR_ADDR, 0, &self.memory, NP_Cursor_Parent::None); let new_bytes = NP_Memory::new(Some(capacity), size, self.memory.schema); let new_root = NP_Cursor::new(ROOT_PTR_ADDR, 0, &new_bytes, NP_Cursor_Parent::None); self.cursor = NP_Cursor::compact(&old_root, &self.memory, new_root, &new_bytes)?; self.memory = new_bytes; Ok(()) } /// Recursively measures how many bytes each element in the buffer is using. /// This will let you know how many bytes can be saved from a compaction. /// /// ``` /// use no_proto::error::NP_Error; /// use no_proto::NP_Factory; /// use no_proto::buffer::NP_Size_Data; /// /// let factory: NP_Factory = NP_Factory::new(r#"{ /// "type": "string" /// }"#)?; /// /// let mut new_buffer = factory.empty_buffer(None, None); /// new_buffer.set(&[], "hello")?; /// assert_eq!(NP_Size_Data { /// current_buffer: 11, /// after_compaction: 11, /// wasted_bytes: 0 /// }, new_buffer.calc_bytes()?); /// /// # Ok::<(), NP_Error>(()) /// ``` /// pub fn calc_bytes(&self) -> Result<NP_Size_Data, NP_Error> { let root = NP_Cursor::new(ROOT_PTR_ADDR, 0, &self.memory, NP_Cursor_Parent::None); let real_bytes = NP_Cursor::calc_size(root, &self.memory)? + ROOT_PTR_ADDR; let total_size = self.memory.read_bytes().len(); if total_size >= real_bytes { return Ok(NP_Size_Data { current_buffer: total_size, after_compaction: real_bytes, wasted_bytes: total_size - real_bytes }); } else { return Err(NP_Error::new("Error calculating bytes!")); } } fn select(&self, cursor: NP_Cursor, create_path: bool, path: &[&str], mut path_index: usize) -> Result<Option<NP_Cursor>, NP_Error> { let mut loop_cursor = cursor; loop { if path.len() == path_index { return Ok(Some(loop_cursor)); } match self.memory.schema[loop_cursor.schema_addr].get_type_key() { NP_TypeKeys::Table => { let new_cursor = NP_Table::select_into(loop_cursor, &self.memory, &path[path_index], create_path, false)?; match new_cursor { Some(x) => { loop_cursor = x; path_index += 1; }, None => return Ok(None) }; }, NP_TypeKeys::Map => { loop_cursor = NP_Map::select_into(loop_cursor, &self.memory, &path[path_index], create_path, false)?; path_index += 1; }, NP_TypeKeys::List => { let list_key = &path[path_index]; let list_key_int = list_key.parse::<usize>(); match list_key_int { Ok(x) => { if x >= LIST_MAX_SIZE { return Err(NP_Error::new("Lists cannot have more than 2^16 items!")); } loop_cursor = NP_List::select_into(loop_cursor, &self.memory, create_path, x)?; path_index += 1; }, Err(_e) => { let mut err = String::from("Can't query list with string, need number! Path: \n"); err.push_str(print_path(&path, path_index).as_str()); return Err(NP_Error::new(err)) } } }, NP_TypeKeys::Tuple => { let list_key = &path[path_index]; let list_key_int = list_key.parse::<usize>(); match list_key_int { Ok(x) => { let new_cursor = NP_Tuple::select_into(cursor, &self.memory, x, create_path)?; match new_cursor { Some(x) => { loop_cursor = x; path_index += 1; }, None => return Ok(None) } }, Err(_e) => { let mut err = String::from("Can't query list with string, need number! Path: \n"); err.push_str(print_path(&path, path_index).as_str()); return Err(NP_Error::new(err)) } } }, _ => { // we've reached a scalar value but not at the end of the path return Ok(None); } } } } } /// NP Item #[derive(Debug)] pub struct NP_Item<'item> { /// index of this value pub index: usize, /// Key at this index pub key: String, memory: &'item NP_Memory<'item>, cursor: NP_Cursor } impl<'item> NP_Item<'item> { /// get key pub fn get_key<'key>(&'key self) -> &'key str { &self.key } /// If this item has a value pub fn has_value(&self) -> bool { self.cursor.value.get_value_address() != 0 } /// Get value at this pointer pub fn get<X>(&'item self) -> Result<Option<X>, NP_Error> where X: NP_Value<'item> + NP_Scalar { match X::into_value(self.cursor, &self.memory)? { Some(x) => { Ok(Some(x)) }, None => { match X::schema_default(&self.memory.schema[self.cursor.schema_addr]) { Some(y) => { Ok(Some(y)) }, None => { Ok(None) } } } } } /// Set value at this pointer pub fn set<X>(&'item mut self, value: X) -> Result<(), NP_Error> where X: NP_Value<'item> + NP_Scalar { // virtual cursor if self.cursor.buff_addr == 0 { match self.cursor.value { NP_Cursor_Value::ListItem { .. } => { self.cursor = NP_List::commit_virtual_cursor(self.cursor, &self.memory)?; }, NP_Cursor_Value::TableItem { .. } => { self.cursor = NP_Table::commit_virtual_cursor(self.cursor, &self.memory)?; }, NP_Cursor_Value::TupleItem { .. } => { panic!() } , NP_Cursor_Value::MapItem { .. } => { panic!() } , _ => panic!() } } if self.cursor.buff_addr == 0 { // can't commit to cursor that's still virtual panic!() } self.cursor = X::set_value(self.cursor, &self.memory, value)?; Ok(()) } /// Clear the value at this pointer pub fn del(&'item mut self) -> bool { if self.cursor.value.get_value_address() == 0 { false } else { self.memory.write_address(self.cursor.buff_addr, 0); true } } } /// Iterator Enum #[derive(Debug)] #[doc(hidden)] pub enum NP_Iterator_Collection<'col> { /// None None, /// Map Map(NP_Map<'col>), /// List List(NP_List<'col>), /// Table Table(NP_Table<'col>), /// Tuple Tuple(NP_Tuple<'col>) } /// Generic iterator #[derive(Debug)] #[doc(hidden)] pub struct NP_Generic_Iterator<'coll> { /// The colleciton iterator pub iterator: NP_Iterator_Collection<'coll>, /// what index we're on pub index: usize } impl<'collection> Iterator for NP_Generic_Iterator<'collection> { type Item = NP_Item<'collection>; fn next(&mut self) -> Option<Self::Item> { match &mut self.iterator { NP_Iterator_Collection::Map(x) => { if let Some(p) = x.next() { let item = NP_Item { index: self.index, key: String::from(p.0), cursor: p.1, memory: x.memory }; self.index += 1; Some(item) } else { None } }, NP_Iterator_Collection::List(x) => { if let Some(p) = x.next() { let item = NP_Item { key: p.0.to_string(), index: p.0, cursor: p.1, memory: x.memory }; self.index = p.0; Some(item) } else { None } }, NP_Iterator_Collection::Table(x) => { if let Some(p) = x.next() { let item = NP_Item { index: p.0, cursor: p.2, key: String::from(p.1), memory: x.memory }; Some(item) } else { None } }, NP_Iterator_Collection::Tuple(x) => { if let Some(p) = x.next() { let item = NP_Item { index: p.0, cursor: p.1, memory: x.memory, key: p.0.to_string(), }; self.index = p.0; Some(item) } else { None } }, _ => panic!() } } }