Puts (adds or replaces) resources in the specified database. @param data database @param bin binary file (can be null) @param path target path @return success flag
(final Data data, final IOFile bin, final String path)
| 69 | * @return success flag |
| 70 | */ |
| 71 | private boolean put(final Data data, final IOFile bin, final String path) { |
| 72 | final IntList docs = data.resources.docs(path); |
| 73 | final int ds = docs.size(); |
| 74 | int bs = 0; |
| 75 | |
| 76 | if(options.get(MainOptions.REPLACE) || docs.isEmpty() && (bin == null || !bin.exists())) { |
| 77 | final AtomicUpdateCache auc = new AtomicUpdateCache(data); |
| 78 | final IntConsumer exec = start -> { |
| 79 | for(int d = start; d < ds; d++) auc.addDelete(docs.get(d)); |
| 80 | auc.execute(false); |
| 81 | }; |
| 82 | |
| 83 | if(bin != null && bin.exists()) { |
| 84 | // replace binary file if it already exists |
| 85 | final BinaryPut put = new BinaryPut(path); |
| 86 | put.setInput(in); |
| 87 | put.lock = false; |
| 88 | if(!put.run(context)) return error(put.info()); |
| 89 | bs = 1; |
| 90 | exec.accept(0); |
| 91 | } else { |
| 92 | // otherwise, add new document as xml |
| 93 | add = new Add(path); |
| 94 | try { |
| 95 | add.setInput(in); |
| 96 | add.init(context, out); |
| 97 | if(!add.build()) return error(add.info()); |
| 98 | |
| 99 | final DataClip clip = new DataClip(add.tmpData); |
| 100 | int d = 0; |
| 101 | if(docs.isEmpty()) { |
| 102 | auc.addInsert(data.meta.size, -1, clip); |
| 103 | } else { |
| 104 | auc.addReplace(docs.get(d++), clip); |
| 105 | } |
| 106 | exec.accept(d); |
| 107 | } finally { |
| 108 | add.finish(); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return info(RES_REPLACED_X_X, ds + bs, jc().performance); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void build(final CmdBuilder cb) { |
no test coverage detected