code wiki / (root) / nx_galx_thumb_migrate.nx

nx_galx_thumb_migrate.nx source

↩ module page · 104 lines · 5095 B

1// nx_galx_thumb_migrate.nx -- SOVEREIGN move of the thumbnail cache from local disk to the NAS, so the 2// thumbs live on the NAS (159TB) instead of eating local C:. For each CID in the view index it copies 3// knowledge/store/galx-thumb/<cid>.png -> /mnt/nas_ai/_galx_thumbs/<cid>.png then unlinks the local copy. 4// Idempotent: a CID already on the NAS is skipped. Uses ONE reused read buffer (no per-file mmap -> no 5// leak, no fork needed). Run several instances over disjoint ranges (launcher) for parallel throughput; 6// CIDs never overlap so there is no contention. Usage: nx_galx_thumb_migrate <start> <count>. 7// license_tier: ORIGINAL 8import "nx_syscalls.nx" 9const M_MAGIC_2048: i64 = 2048 10const M_MAGIC_2000: i64 = 2000 11 12const M_LOCAL: *u8 = "knowledge/store/galx-thumb/" as *u8 13const M_NAS: *u8 = "/mnt/nas_ai/_galx_thumbs/" as *u8 14const M_BUF: i64 = 2097152 // 2MB reused buffer (thumbs are ~150KB) 15 16func m_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func m_n(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 18func m_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ if s[i]>=(48 as u8){ if s[i]<=(57 as u8){ v=v*10+((s[i]-(48 as u8)) as i64) } } i=i+1 } return v } 19func m_unlink(path: *u8) -> i64 { return __syscall(263, 0-100, path as i64, 0, 0, 0, 0) } // unlinkat(AT_FDCWD,path,0) 20func m_mkdir(path: *u8) -> i64 { return __syscall(258, 0-100, path as i64, 0x1ff, 0, 0, 0) } 21func m_path(dir: *u8, cid: *u8, out: *u8) -> i64 { 22 var po: i64 = 0 23 var i: i64 = 0 24 while dir[i] != (0 as u8) { out[po] = dir[i]; po = po + 1; i = i + 1 } 25 var c: i64 = 0 26 while c < 69 { out[po] = cid[c]; po = po + 1; c = c + 1 } 27 out[po] = 46 as u8; po = po + 1; out[po] = 112 as u8; po = po + 1; out[po] = 110 as u8; po = po + 1; out[po] = 103 as u8; po = po + 1 28 out[po] = 0 as u8 29 return 0 30} 31func m_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 } 32 33// copy local thumb -> NAS, unlink local. returns 1=moved, 0=skipped(already on NAS / no local), -1=error. 34func m_one(cid: *u8, buf: *u8, lp: *u8, np: *u8) -> i64 { 35 m_path(M_NAS, cid, np) 36 if m_exists(np) == 1 { 37 m_path(M_LOCAL, cid, lp) 38 if m_exists(lp) == 1 { m_unlink(lp) } // already on NAS -> just drop the local dup 39 return 0 40 } 41 m_path(M_LOCAL, cid, lp) 42 let fd: i64 = sys_openat_rd(lp) 43 if fd < 0 { return 0 } // nothing local to move 44 var n: i64 = 0 45 var go: i64 = 1 46 while go == 1 { 47 if n >= M_BUF { go = 0 } else { 48 let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, M_BUF - n) 49 if r <= 0 { go = 0 } else { n = n + r } 50 } 51 } 52 sys_close(fd) 53 if n <= 0 { return 0 - 1 } 54 let ofd: i64 = sys_openat_wr(np, 0x1a4) 55 if ofd < 0 { return 0 - 1 } 56 var off: i64 = 0 57 while off < n { let w: i64 = sys_write(ofd, ((buf as i64) + off) as *u8, n - off); if w <= 0 { off = n } else { off = off + w } } 58 sys_close(ofd) 59 m_unlink(lp) 60 return 1 61} 62 63func main(argc: i64, argv: *i64) -> i64 { 64 var start: i64 = 0 65 var count: i64 = 0 // no-arg default = no-op (safe: this organ DELETES local files) 66 if argc >= 3 { start = m_atoi(argv[1] as *u8); count = m_atoi(argv[2] as *u8) } 67 let endl: i64 = start + count 68 m_mkdir(M_NAS) 69 let szp: *i64 = sys_mmap(16) as *i64 70 let buf2: *u8 = sys_read_file("knowledge/status/galx_view_index.txt" as *u8, szp) 71 let sz: i64 = szp[0] 72 if (buf2 as i64) == 0 { m_p("MIGRATE FAIL: no view index\n" as *u8); sys_exit(1); return 1 } 73 let copybuf: *u8 = sys_mmap(M_BUF) 74 let cid: *u8 = sys_mmap(96) 75 let lp: *u8 = sys_mmap(M_MAGIC_2048) 76 let np: *u8 = sys_mmap(M_MAGIC_2048) 77 var ls: i64 = 0 78 var i: i64 = 0 79 var lineno: i64 = 0 80 var moved: i64 = 0 81 var skip: i64 = 0 82 while i <= sz { 83 var isnl: i64 = 0 84 if i == sz { isnl = 1 } else { if buf2[i] == (10 as u8) { isnl = 1 } } 85 if isnl == 1 { 86 let llen: i64 = i - ls 87 if llen >= 69 { 88 if lineno >= start { if lineno < endl { 89 var c: i64 = 0 90 while c < 69 { cid[c] = buf2[ls + c]; c = c + 1 } cid[69] = 0 as u8 91 let r: i64 = m_one(cid, copybuf, lp, np) 92 if r == 1 { moved = moved + 1 } else { skip = skip + 1 } 93 if ((moved + skip) % M_MAGIC_2000) == 0 { m_p(" migrated=" as *u8); m_n(moved); m_p(" skip=" as *u8); m_n(skip); m_p("\n" as *u8) } 94 }} 95 lineno = lineno + 1 96 } 97 ls = i + 1 98 } 99 i = i + 1 100 } 101 m_p("MIGRATE range done moved=" as *u8); m_n(moved); m_p(" skip=" as *u8); m_n(skip); m_p("\n" as *u8) 102 sys_exit(0) 103 return 0 104}