// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionStore
static int CollectionStore(
	unqlite_col *pCol, /* Target collection */
	jx9_value *pValue  /* JSON value to be stored */
	)
{
	SyBlob *pWorker = &pCol->sWorker;
	unqlite_kv_methods *pMethods;
	unqlite_kv_engine *pEngine;
	sxu32 nKeyLen;
	int rc;	
	/* Point to the underlying KV store */
	pEngine = unqlitePagerGetKvEngine(pCol->pVm->pDb);
	pMethods = pEngine->pIo->pMethods;
	if( pCol->nTotRec >= SXI64_HIGH ){
		/* Collection limit reached. No more records */
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Collection '%z': Records limit reached",
				&pCol->sName
			);
		return UNQLITE_LIMIT;
	}
	if( pMethods->xReplace == 0 ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Cannot store record into collection '%z' due to a read-only Key/Value storage engine",
				&pCol->sName
			);
		return UNQLITE_READ_ONLY;
	}
	/* Reset the working buffer */
	SyBlobReset(pWorker);
	if( jx9_value_is_json_object(pValue) ){
		jx9_value sId;
		/* If the given type is a JSON object, then add the special __id field */
		jx9MemObjInitFromInt(pCol->pVm->pJx9Vm,&sId,pCol->nLastid);
		jx9_array_add_strkey_elem(pValue,"__id",&sId);
		jx9MemObjRelease(&sId);
	}
	/* Prepare the unique ID for this record */
	SyBlobFormat(pWorker,"%z_%qd",&pCol->sName,pCol->nLastid);
	nKeyLen = SyBlobLength(pWorker);
	if( nKeyLen < 1 ){
		unqliteGenOutofMem(pCol->pVm->pDb);
		return UNQLITE_NOMEM;
	}
	/* Turn to FastJson */
	rc = FastJsonEncode(pValue,pWorker,0);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Finally perform the insertion */
	rc = pMethods->xReplace(
		pEngine,
		SyBlobData(pWorker),nKeyLen,
		SyBlobDataAt(pWorker,nKeyLen),SyBlobLength(pWorker)-nKeyLen
		);
	if( rc == UNQLITE_OK ){
		/* Save the value in the cache */
		CollectionCacheInstallRecord(pCol,pCol->nLastid,pValue);
		/* Increment the unique __id */
		pCol->nLastid++;
		pCol->nTotRec++;
		/* Reflect the change */
		rc = CollectionSetHeader(0,pCol,pCol->nLastid,pCol->nTotRec,0);
	}
	if( rc != UNQLITE_OK ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"IO error while storing record into collection '%z'",
				&pCol->sName
			);
		return rc;
	}
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq (%rax), %rdi
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, 0xb8(%rbx)
jne 0x45e11
addq $0x8, %rbx
leaq 0x3082d(%rip), %rsi     # 0x76629
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3ac51
movl $0xfffffff9, %r13d      # imm = 0xFFFFFFF9
jmp 0x45fdb
movq 0xd8(%rdi), %rcx
movq 0x10(%rcx), %r15
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
cmpq $0x0, 0x38(%rcx)
je 0x45fa6
movq %rsi, %r14
movq %rcx, -0x38(%rbp)
movl $0x0, 0x78(%rbx)
movl 0x80(%rbx), %ecx
testb $0x4, %cl
je 0x45e5f
movq $0x0, 0x70(%rbx)
movl $0x0, 0x7c(%rbx)
andl $-0x5, %ecx
movl %ecx, 0x80(%rbx)
leaq 0x68(%rbx), %r13
testb $0x40, 0x8(%r14)
je 0x45ed9
movq (%r14), %rcx
testb $0x1, 0x40(%rcx)
je 0x45ed9
movq 0xe0(%rax), %rax
movq 0xa8(%rbx), %rcx
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %r12
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x30(%r12)
movaps %xmm0, (%r12)
movaps %xmm0, 0x20(%r12)
movq %rax, 0x10(%r12)
movq %rax, 0x18(%r12)
movl $0x0, 0x30(%r12)
movq %rcx, (%r12)
movl $0x2, 0x8(%r12)
leaq 0x307df(%rip), %rsi     # 0x766a5
movq %r14, %rdi
movq %r12, %rdx
callq 0x3106a
movq %r12, %rdi
callq 0x309f2
leaq 0x8(%rbx), %rdx
movq 0xa8(%rbx), %rcx
leaq 0x301e5(%rip), %rsi     # 0x760d0
movq %r13, %rdi
movq %rdx, -0x30(%rbp)
xorl %eax, %eax
callq 0x3ec96
movl 0x78(%rbx), %r12d
testq %r12, %r12
je 0x45fc3
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3e822
movl %eax, %r13d
testl %eax, %eax
jne 0x45fdb
movq 0x70(%rbx), %rsi
movq %r12, %rcx
addq %rsi, %rcx
movl 0x78(%rbx), %r8d
subl %r12d, %r8d
movq %r15, %rdi
movl %r12d, %edx
movq -0x38(%rbp), %rax
callq *0x38(%rax)
movl %eax, %r13d
testl %eax, %eax
jne 0x45f87
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x41095
movq 0xa8(%rbx), %rdx
movq 0xb8(%rbx), %rcx
incq %rdx
movq %rdx, 0xa8(%rbx)
incq %rcx
movq %rcx, 0xb8(%rbx)
xorl %edi, %edi
movq %rbx, %rsi
xorl %r8d, %r8d
callq 0x3e465
movl %eax, %r13d
testl %r13d, %r13d
je 0x45fed
movq (%rbx), %rax
movq (%rax), %rdi
leaq 0x30711(%rip), %rsi     # 0x766aa
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x3ac51
jmp 0x45fdb
addq $0x8, %rbx
leaq 0x3069f(%rip), %rsi     # 0x76650
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3ac51
movl $0xffffffb5, %r13d      # imm = 0xFFFFFFB5
jmp 0x45fdb
movq (%rbx), %rax
movq (%rax), %rdi
leaq 0x2f9fe(%rip), %rsi     # 0x759ce
callq 0x31af5
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0x45fdb