Class: KBEngine
- Includes:
- DRb::DRbUndumped, KBEncryptionMixin, KBTypeConversionsMixin
- Defined in:
- lib/kirbybase.rb
Overview
KBEngine
Constant Summary
Constants included from KBEncryptionMixin
KBEncryptionMixin::EN_KEY, KBEncryptionMixin::EN_KEY1, KBEncryptionMixin::EN_KEY_LEN, KBEncryptionMixin::EN_STR, KBEncryptionMixin::EN_STR_LEN
Constants included from KBTypeConversionsMixin
KBTypeConversionsMixin::ENCODE_RE, KBTypeConversionsMixin::KB_NIL, KBTypeConversionsMixin::UNENCODE_RE
Class Method Summary collapse
-
.create_called_from_database_instance(db) ⇒ Object
———————————————————————– KBEngine.create_called_from_database_instance ———————————————————————–.
Instance Method Summary collapse
-
#add_column(table, field_def, after) ⇒ Object
———————————————————————– add_column ———————————————————————–.
-
#add_index(table, col_names, index_no) ⇒ Object
———————————————————————– add_index ———————————————————————–.
-
#add_to_indexes(table, rec, fpos) ⇒ Object
———————————————————————– add_to_indexes ———————————————————————–.
-
#change_column_default_value(table, col_name, value) ⇒ Object
———————————————————————– change_column_default_value ———————————————————————–.
-
#change_column_required(table, col_name, required) ⇒ Object
———————————————————————– change_column_required ———————————————————————–.
-
#change_column_type(table, col_name, col_type) ⇒ Object
———————————————————————– change_column_type ———————————————————————–.
-
#delete_from_indexes(table, rec, fpos) ⇒ Object
———————————————————————– delete_from_indexes ———————————————————————–.
-
#delete_records(table, recs) ⇒ Object
———————————————————————– delete_records ———————————————————————–.
-
#delete_table(tablename) ⇒ Object
———————————————————————– delete_table ———————————————————————–.
-
#drop_column(table, col_name) ⇒ Object
———————————————————————– drop_column ———————————————————————–.
-
#drop_index(table, col_names) ⇒ Object
———————————————————————– drop_index ———————————————————————–.
-
#get_header_vars(table) ⇒ Object
———————————————————————– get_header_vars ———————————————————————–.
-
#get_index(table, index_name) ⇒ Object
———————————————————————– get_index ———————————————————————–.
-
#get_index_timestamp(table, index_name) ⇒ Object
———————————————————————– get_index_timestamp ———————————————————————–.
-
#get_rec_by_recno(table, recno) ⇒ Object
———————————————————————– get_rec_by_recno ———————————————————————–.
-
#get_recno_index(table) ⇒ Object
———————————————————————– get_recno_index ———————————————————————–.
-
#get_recs(table) ⇒ Object
———————————————————————– get_recs ———————————————————————–.
-
#get_recs_by_recno(table, recnos) ⇒ Object
———————————————————————– get_recs_by_recno ———————————————————————–.
-
#get_total_recs(table) ⇒ Object
———————————————————————- get_total_recs ———————————————————————-.
-
#index_exists?(table, index_fields) ⇒ Boolean
———————————————————————– index_exists? ———————————————————————–.
-
#init_index(table, index_fields) ⇒ Object
———————————————————————– init_index ———————————————————————–.
-
#init_recno_index(table) ⇒ Object
———————————————————————– init_recno_index ———————————————————————–.
-
#initialize(db) ⇒ KBEngine
constructor
———————————————————————– initialize ———————————————————————–.
-
#insert_record(table, rec) ⇒ Object
———————————————————————– insert_record ———————————————————————–.
-
#line_to_rec(line, encrypted) ⇒ Object
———————————————————————– line_to_rec ———————————————————————–.
-
#new_table(name, field_defs, encrypt, record_class) ⇒ Object
———————————————————————– new_table ———————————————————————– ++ Create physical file holding table.
-
#pack_table(table) ⇒ Object
———————————————————————– pack_table ———————————————————————–.
-
#read_blob_file(filepath) ⇒ Object
———————————————————————– read_blob_file ———————————————————————–.
-
#read_memo_file(filepath) ⇒ Object
———————————————————————– read_memo_file ———————————————————————–.
-
#rebuild_index(table, index_fields) ⇒ Object
———————————————————————– rebuild_index ———————————————————————–.
-
#rebuild_recno_index(table) ⇒ Object
———————————————————————– rebuild_recno_index ———————————————————————–.
-
#recno_index_exists?(table) ⇒ Boolean
———————————————————————– recno_index_exists? ———————————————————————–.
-
#remove_indexes(tablename) ⇒ Object
———————————————————————– remove_indexes ———————————————————————–.
-
#remove_recno_index(tablename) ⇒ Object
———————————————————————– remove_recno_index ———————————————————————–.
-
#rename_column(table, old_col_name, new_col_name) ⇒ Object
———————————————————————– rename_column ———————————————————————–.
-
#rename_table(old_tablename, new_tablename) ⇒ Object
———————————————————————– rename_table ———————————————————————–.
-
#reset_recno_ctr(table) ⇒ Object
———————————————————————– reset_recno_ctr ———————————————————————–.
-
#table_exists?(tablename) ⇒ Boolean
———————————————————————– table_exists? ———————————————————————–.
-
#tables ⇒ Object
———————————————————————– tables ———————————————————————–.
-
#update_recno_index(table, recno, fpos) ⇒ Object
———————————————————————– update_recno_index ———————————————————————–.
-
#update_records(table, recs) ⇒ Object
———————————————————————– update_records ———————————————————————–.
-
#update_to_indexes(table, rec) ⇒ Object
———————————————————————– update_to_indexes ———————————————————————–.
-
#write_blob_file(filepath, contents) ⇒ Object
———————————————————————– write_blob_file ———————————————————————–.
-
#write_memo_file(filepath, contents) ⇒ Object
———————————————————————– write_memo_file ———————————————————————–.
Methods included from KBEncryptionMixin
Methods included from KBTypeConversionsMixin
#convert_to_encoded_string, #convert_to_native_type
Constructor Details
#initialize(db) ⇒ KBEngine
initialize
768 769 770 771 772 773 774 775 |
# File 'lib/kirbybase.rb', line 768 def initialize(db) @db = db @recno_indexes = {} @indexes = {} # This hash will hold the table locks if in client/server mode. @mutex_hash = {} if @db.server? end |
Class Method Details
Instance Method Details
#add_column(table, field_def, after) ⇒ Object
add_column
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 |
# File 'lib/kirbybase.rb', line 1296 def add_column(table, field_def, after) # Find the index position of where to insert the column, either at # the end (-1) or after the field specified. if after.nil? or table.field_names.last == after insert_after = -1 else insert_after = table.field_names.index(after)+1 end with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end if insert_after == -1 header_rec.insert(insert_after, field_def) else # Need to account for recno ctr, delete ctr, record class. header_rec.insert(insert_after+3, field_def) end if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true line = fptr.readline.chomp if table.encrypted? temp_line = unencrypt_str(line) else temp_line = line end rec = temp_line.split('|', -1) rec.insert(insert_after, '') if table.encrypted? new_fptr.write(encrypt_str(rec.join('|')) + "\n") else new_fptr.write(rec.join('|') + "\n") end end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#add_index(table, col_names, index_no) ⇒ Object
add_index
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 |
# File 'lib/kirbybase.rb', line 1427 def add_index(table, col_names, index_no) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end col_names.each do |c| header_rec[table.field_names.index(c)+3] += \ ':Index->%d' % index_no end if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#add_to_indexes(table, rec, fpos) ⇒ Object
add_to_indexes
861 862 863 864 865 866 867 868 |
# File 'lib/kirbybase.rb', line 861 def add_to_indexes(table, rec, fpos) @recno_indexes[table.name].add_index_rec(rec.first, fpos) re_table_name = Regexp.new(table.name.to_s) @indexes.each_pair do |key, index| index.add_index_rec(rec) if key.to_s =~ re_table_name end end |
#change_column_default_value(table, col_name, value) ⇒ Object
change_column_default_value
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 |
# File 'lib/kirbybase.rb', line 1520 def change_column_default_value(table, col_name, value) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end if header_rec[table.field_names.index(col_name)+3] =~ \ /Default->/ hr_chunks = \ header_rec[table.field_names.index(col_name)+3].split(':') if value.nil? hr_chunks = hr_chunks.delete_if { |x| x =~ /Default->/ } header_rec[table.field_names.index(col_name)+3] = \ hr_chunks.join(':') else hr_chunks.collect! do |x| if x =~ /Default->/ 'Default->%s' % value else x end end header_rec[table.field_names.index(col_name)+3] = \ hr_chunks.join(':') end else if value.nil? else header_rec[table.field_names.index(col_name)+3] += \ ':Default->%s' % value end end if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#change_column_required(table, col_name, required) ⇒ Object
change_column_required
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 |
# File 'lib/kirbybase.rb', line 1587 def change_column_required(table, col_name, required) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end if header_rec[table.field_names.index(col_name)+3 ] =~ /Required->/ hr_chunks = \ header_rec[table.field_names.index(col_name)+3].split(':') if not required hr_chunks = hr_chunks.delete_if {|x| x =~ /Required->/} header_rec[table.field_names.index(col_name)+3] = \ hr_chunks.join(':') else hr_chunks.collect! do |x| if x =~ /Required->/ 'Default->%s' % required else x end end header_rec[table.field_names.index(col_name)+3] = \ hr_chunks.join(':') end else if not required else header_rec[table.field_names.index(col_name)+3] += \ ':Required->%s' % required end end if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#change_column_type(table, col_name, col_type) ⇒ Object
change_column_type
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 |
# File 'lib/kirbybase.rb', line 1208 def change_column_type(table, col_name, col_type) col_index = table.field_names.index(col_name) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end temp_fields = header_rec[col_index+3].split(':') temp_fields[1] = col_type.to_s header_rec[col_index+3] = temp_fields.join(':') if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#delete_from_indexes(table, rec, fpos) ⇒ Object
delete_from_indexes
873 874 875 876 877 878 879 880 |
# File 'lib/kirbybase.rb', line 873 def delete_from_indexes(table, rec, fpos) @recno_indexes[table.name].delete_index_rec(rec.recno) re_table_name = Regexp.new(table.name.to_s) @indexes.each_pair do |key, index| index.delete_index_rec(rec.recno) if key.to_s =~ re_table_name end end |
#delete_records(table, recs) ⇒ Object
delete_records
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 |
# File 'lib/kirbybase.rb', line 1189 def delete_records(table, recs) with_write_locked_table(table) do |fptr| recs.each do |rec| # Go to offset within the file where the record is and # replace it with all spaces. write_record(table, fptr, rec.fpos, ' ' * rec.line_length) incr_del_ctr(table, fptr) delete_from_indexes(table, rec, rec.fpos) end # Return the number of records deleted. return recs.size end end |
#delete_table(tablename) ⇒ Object
delete_table
959 960 961 962 963 964 965 966 |
# File 'lib/kirbybase.rb', line 959 def delete_table(tablename) with_write_lock(tablename) do File.delete(File.join(@db.path, tablename.to_s + @db.ext)) remove_indexes(tablename) remove_recno_index(tablename) return true end end |
#drop_column(table, col_name) ⇒ Object
drop_column
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 |
# File 'lib/kirbybase.rb', line 1365 def drop_column(table, col_name) col_index = table.field_names.index(col_name) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') header_rec.delete_at(col_index+3) new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else header_rec = line.split('|') header_rec.delete_at(col_index+3) new_fptr.write(header_rec.join('|') + "\n") end begin while true line = fptr.readline.chomp if table.encrypted? temp_line = unencrypt_str(line) else temp_line = line end rec = temp_line.split('|', -1) rec.delete_at(col_index) if table.encrypted? new_fptr.write(encrypt_str(rec.join('|')) + "\n") else new_fptr.write(rec.join('|') + "\n") end end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#drop_index(table, col_names) ⇒ Object
drop_index
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 |
# File 'lib/kirbybase.rb', line 1471 def drop_index(table, col_names) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end col_names.each do |c| temp_field_def = \ header_rec[table.field_names.index(c)+3].split(':') temp_field_def = temp_field_def.delete_if {|x| x =~ /Index->/ } header_rec[table.field_names.index(c)+3] = \ temp_field_def.join(':') end if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#get_header_vars(table) ⇒ Object
get_header_vars
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 |
# File 'lib/kirbybase.rb', line 991 def get_header_vars(table) with_table(table) do |fptr| encrypted, line = get_header_record(table, fptr) last_rec_no, del_ctr, record_class, *flds = line.split('|') field_names = flds.collect { |x| x.split(':')[0].to_sym } field_types = flds.collect { |x| x.split(':')[1].to_sym } field_indexes = [nil] * field_names.size field_defaults = [nil] * field_names.size field_requireds = [false] * field_names.size field_extras = [nil] * field_names.size flds.each_with_index do |x,i| field_extras[i] = {} if x.split(':').size > 2 x.split(':')[2..-1].each do |y| if y =~ /Index/ field_indexes[i] = y elsif y =~ /Default/ field_defaults[i] = \ convert_to_native_type(field_types[i], y.split('->')[1]) elsif y =~ /Required/ field_requireds[i] = \ convert_to_native_type(:Boolean, y.split('->')[1]) else field_extras[i][y.split('->')[0]] = \ y.split('->')[1] end end end end return [encrypted, last_rec_no.to_i, del_ctr.to_i, record_class, field_names, field_types, field_indexes, field_defaults, field_requireds, field_extras] end end |
#get_index(table, index_name) ⇒ Object
get_index
902 903 904 |
# File 'lib/kirbybase.rb', line 902 def get_index(table, index_name) return @indexes["#{table.name}_#{index_name}".to_sym].get_idx end |
#get_index_timestamp(table, index_name) ⇒ Object
get_index_timestamp
909 910 911 |
# File 'lib/kirbybase.rb', line 909 def (table, index_name) return @indexes["#{table.name}_#{index_name}".to_sym]. end |
#get_rec_by_recno(table, recno) ⇒ Object
get_rec_by_recno
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/kirbybase.rb', line 1093 def get_rec_by_recno(table, recno) encrypted = table.encrypted? recno_idx = get_recno_index(table) return nil unless recno_idx.has_key?(recno) with_table(table) do |fptr| fptr.seek(recno_idx[recno]) rec, line_length = line_to_rec(fptr.readline, encrypted) raise "Recno Index Corrupt for table %s!" % table.name if \ rec.empty? raise "Recno Index Corrupt for table %s!" % table.name unless \ rec[0].to_i == recno rec << recno_idx[recno] << line_length return rec end end |
#get_recno_index(table) ⇒ Object
get_recno_index
822 823 824 |
# File 'lib/kirbybase.rb', line 822 def get_recno_index(table) return @recno_indexes[table.name].get_idx end |
#get_recs(table) ⇒ Object
get_recs
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 |
# File 'lib/kirbybase.rb', line 1033 def get_recs(table) encrypted = table.encrypted? recs = [] with_table(table) do |fptr| begin # Skip header rec. fptr.readline # Loop through table. while true # Record current position in table. fpos = fptr.tell rec, line_length = line_to_rec(fptr.readline, encrypted) next if rec.empty? rec << fpos << line_length recs << rec end # Here's how we break out of the loop... rescue EOFError end return recs end end |
#get_recs_by_recno(table, recnos) ⇒ Object
get_recs_by_recno
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 |
# File 'lib/kirbybase.rb', line 1063 def get_recs_by_recno(table, recnos) encrypted = table.encrypted? recs = [] recno_idx = get_recno_index(table) with_table(table) do |fptr| # Skip header rec. fptr.readline # Take all the recnos you want to get, add the file positions # to them, and sort by file position, so that when we seek # through the physical file we are going in ascending file # position order, which should be fastest. recnos.collect { |r| [recno_idx[r], r] }.sort.each do |r| fptr.seek(r[0]) rec, line_length = line_to_rec(fptr.readline, encrypted) next if rec.empty? raise "Index Corrupt!" unless rec[0].to_i == r[1] rec << r[0] << line_length recs << rec end return recs end end |
#get_total_recs(table) ⇒ Object
get_total_recs
971 972 973 |
# File 'lib/kirbybase.rb', line 971 def get_total_recs(table) return get_recs(table).size end |
#index_exists?(table, index_fields) ⇒ Boolean
index_exists?
895 896 897 |
# File 'lib/kirbybase.rb', line 895 def index_exists?(table, index_fields) @indexes.include?("#{table.name}_#{index_fields.join('_')}".to_sym) end |
#init_index(table, index_fields) ⇒ Object
init_index
829 830 831 832 833 834 835 836 837 838 |
# File 'lib/kirbybase.rb', line 829 def init_index(table, index_fields) return if index_exists?(table, index_fields) with_write_locked_table(table) do |fptr| @indexes["#{table.name}_#{index_fields.join('_')}".to_sym] = \ KBIndex.new(table, index_fields) @indexes["#{table.name}_#{index_fields.join('_')}".to_sym ].rebuild(fptr) end end |
#init_recno_index(table) ⇒ Object
init_recno_index
780 781 782 783 784 785 786 787 |
# File 'lib/kirbybase.rb', line 780 def init_recno_index(table) return if recno_index_exists?(table) with_write_locked_table(table) do |fptr| @recno_indexes[table.name] = KBRecnoIndex.new(table) @recno_indexes[table.name].rebuild(fptr) end end |
#insert_record(table, rec) ⇒ Object
insert_record
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 |
# File 'lib/kirbybase.rb', line 1130 def insert_record(table, rec) with_write_locked_table(table) do |fptr| # Auto-increment the record number field. rec_no = incr_rec_no_ctr(table, fptr) # Insert the newly created record number value at the beginning # of the field values. rec[0] = rec_no fptr.seek(0, IO::SEEK_END) fpos = fptr.tell write_record(table, fptr, 'end', rec.join('|')) add_to_indexes(table, rec, fpos) # Return the record number of the newly created record. return rec_no end end |
#line_to_rec(line, encrypted) ⇒ Object
line_to_rec
1117 1118 1119 1120 1121 1122 1123 1124 1125 |
# File 'lib/kirbybase.rb', line 1117 def line_to_rec(line, encrypted) line.chomp! line_length = line.length line = unencrypt_str(line) if encrypted line.strip! # Convert line to rec and return rec and line length. return line.split('|', -1), line_length end |
#new_table(name, field_defs, encrypt, record_class) ⇒ Object
new_table
++ Create physical file holding table. This table should not be directly called in your application, but only called by #create_table.
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 |
# File 'lib/kirbybase.rb', line 939 def new_table(name, field_defs, encrypt, record_class) # Header rec consists of last record no. used, delete count, and # all field names/types. Here, I am inserting the 'recno' field # at the beginning of the fields. header_rec = ['000000', '000000', record_class, 'recno:Integer', field_defs].join('|') header_rec = 'Z' + encrypt_str(header_rec) if encrypt begin fptr = open(File.join(@db.path, name.to_s + @db.ext), 'w') fptr.write(header_rec + "\n") ensure fptr.close end end |
#pack_table(table) ⇒ Object
pack_table
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 |
# File 'lib/kirbybase.rb', line 1653 def pack_table(table) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp # Reset the delete counter in the header rec to 0. if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') header_rec[1] = '000000' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else header_rec = line.split('|') header_rec[1] = '000000' new_fptr.write(header_rec.join('|') + "\n") end lines_deleted = 0 begin while true line = fptr.readline if table.encrypted? temp_line = unencrypt_str(line) else temp_line = line end if temp_line.strip == '' lines_deleted += 1 else new_fptr.write(line) end end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) # Return the number of deleted records that were removed. return lines_deleted end end |
#read_blob_file(filepath) ⇒ Object
read_blob_file
1731 1732 1733 1734 1735 1736 1737 1738 |
# File 'lib/kirbybase.rb', line 1731 def read_blob_file(filepath) begin f = File.new(File.join(@db.memo_blob_path, filepath), 'rb') return f.read ensure f.close end end |
#read_memo_file(filepath) ⇒ Object
read_memo_file
1707 1708 1709 1710 1711 1712 1713 1714 |
# File 'lib/kirbybase.rb', line 1707 def read_memo_file(filepath) begin f = File.new(File.join(@db.memo_blob_path, filepath)) return f.read ensure f.close end end |
#rebuild_index(table, index_fields) ⇒ Object
rebuild_index
843 844 845 846 847 848 |
# File 'lib/kirbybase.rb', line 843 def rebuild_index(table, index_fields) with_write_locked_table(table) do |fptr| @indexes["#{table.name}_#{index_fields.join('_')}".to_sym ].rebuild(fptr) end end |
#rebuild_recno_index(table) ⇒ Object
rebuild_recno_index
792 793 794 795 796 |
# File 'lib/kirbybase.rb', line 792 def rebuild_recno_index(table) with_write_locked_table(table) do |fptr| @recno_indexes[table.name].rebuild(fptr) end end |
#recno_index_exists?(table) ⇒ Boolean
recno_index_exists?
815 816 817 |
# File 'lib/kirbybase.rb', line 815 def recno_index_exists?(table) @recno_indexes.include?(table.name) end |
#remove_indexes(tablename) ⇒ Object
remove_indexes
853 854 855 856 |
# File 'lib/kirbybase.rb', line 853 def remove_indexes(tablename) re_table_name = Regexp.new(tablename.to_s) @indexes.delete_if { |k,v| k.to_s =~ re_table_name } end |
#remove_recno_index(tablename) ⇒ Object
remove_recno_index
801 802 803 |
# File 'lib/kirbybase.rb', line 801 def remove_recno_index(tablename) @recno_indexes.delete(tablename) end |
#rename_column(table, old_col_name, new_col_name) ⇒ Object
rename_column
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 |
# File 'lib/kirbybase.rb', line 1252 def rename_column(table, old_col_name, new_col_name) col_index = table.field_names.index(old_col_name) with_write_lock(table.name) do fptr = open(table.filename, 'r') new_fptr = open(table.filename+'temp', 'w') line = fptr.readline.chomp if line[0..0] == 'Z' header_rec = unencrypt_str(line[1..-1]).split('|') else header_rec = line.split('|') end temp_fields = header_rec[col_index+3].split(':') temp_fields[0] = new_col_name.to_s header_rec[col_index+3] = temp_fields.join(':') if line[0..0] == 'Z' new_fptr.write('Z' + encrypt_str(header_rec.join('|')) + "\n") else new_fptr.write(header_rec.join('|') + "\n") end begin while true new_fptr.write(fptr.readline) end # Here's how we break out of the loop... rescue EOFError end # Close the table and release the write lock. fptr.close new_fptr.close File.delete(table.filename) FileUtils.mv(table.filename+'temp', table.filename) end end |
#rename_table(old_tablename, new_tablename) ⇒ Object
rename_table
1418 1419 1420 1421 1422 |
# File 'lib/kirbybase.rb', line 1418 def rename_table(old_tablename, new_tablename) old_full_path = File.join(@db.path, old_tablename.to_s + @db.ext) new_full_path = File.join(@db.path, new_tablename.to_s + @db.ext) File.rename(old_full_path, new_full_path) end |
#reset_recno_ctr(table) ⇒ Object
reset_recno_ctr
978 979 980 981 982 983 984 985 986 |
# File 'lib/kirbybase.rb', line 978 def reset_recno_ctr(table) with_write_locked_table(table) do |fptr| encrypted, header_line = get_header_record(table, fptr) last_rec_no, rest_of_line = header_line.split('|', 2) write_header_record(table, fptr, ['%06d' % 0, rest_of_line].join('|')) return true end end |
#table_exists?(tablename) ⇒ Boolean
table_exists?
916 917 918 |
# File 'lib/kirbybase.rb', line 916 def table_exists?(tablename) return File.exists?(File.join(@db.path, tablename.to_s + @db.ext)) end |
#tables ⇒ Object
tables
923 924 925 926 927 928 929 930 |
# File 'lib/kirbybase.rb', line 923 def tables list = [] Dir.foreach(@db.path) { |filename| list << File.basename(filename, '.*').to_sym if \ File.extname(filename) == @db.ext } return list end |
#update_recno_index(table, recno, fpos) ⇒ Object
update_recno_index
808 809 810 |
# File 'lib/kirbybase.rb', line 808 def update_recno_index(table, recno, fpos) @recno_indexes[table.name].update_index_rec(recno, fpos) end |
#update_records(table, recs) ⇒ Object
update_records
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 |
# File 'lib/kirbybase.rb', line 1154 def update_records(table, recs) with_write_locked_table(table) do |fptr| recs.each do |rec| line = rec[:rec].join('|') # This doesn't actually 'delete' the line, it just # makes it all spaces. That way, if the updated # record is the same or less length than the old # record, we can write the record back into the # same spot. If the updated record is greater than # the old record, we will leave the now spaced-out # line and write the updated record at the end of # the file. write_record(table, fptr, rec[:fpos], ' ' * rec[:line_length]) if line.length > rec[:line_length] fptr.seek(0, IO::SEEK_END) new_fpos = fptr.tell write_record(table, fptr, 'end', line) incr_del_ctr(table, fptr) update_recno_index(table, rec[:rec].first, new_fpos) else write_record(table, fptr, rec[:fpos], line) end update_to_indexes(table, rec[:rec]) end # Return the number of records updated. return recs.size end end |
#update_to_indexes(table, rec) ⇒ Object
update_to_indexes
885 886 887 888 889 890 |
# File 'lib/kirbybase.rb', line 885 def update_to_indexes(table, rec) re_table_name = Regexp.new(table.name.to_s) @indexes.each_pair do |key, index| index.update_index_rec(rec) if key.to_s =~ re_table_name end end |
#write_blob_file(filepath, contents) ⇒ Object
write_blob_file
1743 1744 1745 1746 1747 1748 1749 1750 |
# File 'lib/kirbybase.rb', line 1743 def write_blob_file(filepath, contents) begin f = File.new(File.join(@db.memo_blob_path, filepath), 'wb') f.write(contents) ensure f.close end end |
#write_memo_file(filepath, contents) ⇒ Object
write_memo_file
1719 1720 1721 1722 1723 1724 1725 1726 |
# File 'lib/kirbybase.rb', line 1719 def write_memo_file(filepath, contents) begin f = File.new(File.join(@db.memo_blob_path, filepath), 'w') f.write(contents) ensure f.close end end |