Class: JIO::Transaction

Inherits:
Object
  • Object
show all
Defined in:
ext/jio/transaction.c

Instance Method Summary collapse

Instance Method Details

#commitBoolean

Reads / writes all operations for this transaction to / from disk, in the order they were added. After this function returns successfully, all the data can be trusted to be on the disk. The commit is atomic with regards to other processes using libjio, but not accessing directly to the file.

Examples

transaction.commit    =>  boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
# File 'ext/jio/transaction.c', line 125

static VALUE rb_jio_transaction_commit(VALUE obj)
{
    ssize_t ret;
    JioGetTransaction(obj);
    TRAP_BEG;
    ret = jtrans_commit(trans->trans);
    TRAP_END;
    return rb_jio_transaction_result(ret, "commit");
}

#committed?Boolean

Determines if this transaction has been committed.

Examples

transaction.committed?    =>  boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
# File 'ext/jio/transaction.c', line 193

static VALUE rb_jio_transaction_committed_p(VALUE obj)
{
    jtrans_t *t = NULL;
    JioGetTransaction(obj);
    t = trans->trans;
    return (t->flags & J_COMMITTED) ? Qtrue : Qfalse;
}

#read(2, 2) ⇒ Boolean

Reads X bytes from Y offset into a transaction view. The buffered data is only visible once the transaction has been committed.

Examples

transaction.read(2, 2)    =>  boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/jio/transaction.c', line 45

static VALUE rb_jio_transaction_read(VALUE obj, VALUE length, VALUE offset)
{
    int ret;
    VALUE buf;
    ssize_t len;
    JioGetTransaction(obj);
    AssertLength(length);
    AssertOffset(offset);
    len = (ssize_t)FIX2LONG(length);
    buf = rb_str_new(0, len);
    TRAP_BEG;
    ret = jtrans_add_r(trans->trans, RSTRING_PTR(buf), len, (off_t)NUM2OFFT(offset));
    TRAP_END;
    if (ret == -1) rb_sys_fail("jtrans_add_r");
    if (NIL_P(trans->views)) trans->views = rb_ary_new();
    rb_ary_push(trans->views, JioEncode(buf));
    return Qtrue;
}

#releasenil

Free all transaction state and operation buffers

Examples

transaction.release    =>  nil

Returns:

  • (nil)


172
173
174
175
176
177
178
179
180
# File 'ext/jio/transaction.c', line 172

static VALUE rb_jio_transaction_release(VALUE obj)
{
    JioGetTransaction(obj);
    TRAP_BEG;
    jtrans_free(trans->trans);
    TRAP_END;
    trans->flags |= JIO_TRANSACTION_RELEASED;
    return Qnil;
}

#rollbackBoolean

This function atomically undoes a previous committed transaction. After its successful return, the data can be trusted to be on disk. The read operations will be ignored as we only care about on disk consistency.

Examples

transaction.rollback    =>  boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/jio/transaction.c', line 148

static VALUE rb_jio_transaction_rollback(VALUE obj)
{
    ssize_t ret;
    VALUE res;
    JioGetTransaction(obj);
    TRAP_BEG;
    ret = jtrans_rollback(trans->trans);
    TRAP_END;
    res = rb_jio_transaction_result(ret, "rollback");
    if (!NIL_P(trans->views)) rb_ary_clear(trans->views);
    return res;
}

#rollbacked?Boolean

Determines if this transaction has been rolled back.

Examples

transaction.rollbacked?    =>  boolean

Returns:

  • (Boolean)


212
213
214
215
216
217
218
# File 'ext/jio/transaction.c', line 212

static VALUE rb_jio_transaction_rollbacked_p(VALUE obj)
{
    jtrans_t *t = NULL;
    JioGetTransaction(obj);
    t = trans->trans;
    return (t->flags & J_ROLLBACKED) ? Qtrue : Qfalse;
}

#rollbacking?Boolean

Determines if this transaction is in the process of being rolled back.

Examples

transaction.rollbacking?    =>  boolean

Returns:

  • (Boolean)


231
232
233
234
235
236
237
# File 'ext/jio/transaction.c', line 231

static VALUE rb_jio_transaction_rollbacking_p(VALUE obj)
{
    jtrans_t *t = NULL;
    JioGetTransaction(obj);
    t = trans->trans;
    return (t->flags & J_ROLLBACKING) ? Qtrue : Qfalse;
}

#viewsArray

Returns a sequence of buffers representing previous transactional read operations. The result is always an empty Array if the transaction hasn’t been committed yet. Operations will be applied in order, and overlapping operations are permitted, in which case the latest one will prevail.

Examples

transaction.views    =>  Array

Returns:

  • (Array)


77
78
79
80
81
82
83
84
# File 'ext/jio/transaction.c', line 77

static VALUE rb_jio_transaction_views(VALUE obj)
{
    jtrans_t *t = NULL;
    JioGetTransaction(obj);
    t = trans->trans;
    if ((t->flags & J_COMMITTED) && !NIL_P(trans->views)) return trans->views;
    return jio_empty_view;
}

#write("data", 2) ⇒ Boolean

Spawns a write operation from a given buffer to X offset for this transaction. Only written to disk when the transaction has been committed. Operations will be applied in order, and overlapping operations are permitted, in which case the latest one will prevail.

Examples

transaction.write("data", 2)    =>  boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/jio/transaction.c', line 99

static VALUE rb_jio_transaction_write(VALUE obj, VALUE buf, VALUE offset)
{
    int ret;
    JioGetTransaction(obj);
    Check_Type(buf, T_STRING);
    AssertOffset(offset);
    TRAP_BEG;
    ret = jtrans_add_w(trans->trans, RSTRING_PTR(buf), (size_t)RSTRING_LEN(buf), (off_t)NUM2OFFT(offset));
    TRAP_END;
    if (ret == -1) rb_sys_fail("jtrans_add_w");
    return Qtrue;
}