Class: ChunkIO::Chunk

Inherits:
Object
  • Object
show all
Defined in:
ext/chunkio/chunkio_chunk.c

Instance Method Summary collapse

Constructor Details

#initialize(context, stream, name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'ext/chunkio/chunkio_chunk.c', line 23

static VALUE chunkio_chunk_initialize(VALUE self, VALUE context, VALUE stream, VALUE name)
{
    struct cio_ctx *ctx = UnwrapChunkIOContext(context);
    struct cio_stream *st = UnwrapChunkIOStream(stream);
    const char *c_name = RSTRING_PTR(name);

    struct cio_chunk *chunk = cio_chunk_open(ctx, st, c_name, CIO_OPEN, 1000);

    ((chunkio_chunk*)DATA_PTR(self))->inner = chunk;
    return self;
}

Instance Method Details

#bytesizeObject

}



100
101
102
103
104
105
106
107
# File 'ext/chunkio/chunkio_chunk.c', line 100

static VALUE chunkio_chunk_bytesize(VALUE self)
{
    chunkio_chunk *chunk = NULL;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    size_t size = cio_chunk_get_content_size(chunk->inner);

    return INT2NUM(size);
}

#closeObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/chunkio/chunkio_chunk.c', line 51

static VALUE chunkio_chunk_close(VALUE self)
{
    chunkio_chunk *chunk = NULL;
    int type;
    struct cio_file *cf;

    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (!chunk->closed) {
        cio_chunk_sync(chunk->inner);
        type = chunk->inner->st->type;
        if (type == CIO_STORE_FS) {
            /* COPY form chunkio cio_file.c */
            chunk->inner->st->type;
            cf = (struct cio_file *)chunk->inner->backend;
            close(cf->fd);
            cf->fd = 0;
        }
        chunk->closed = 1;      /* mark as close */

        return Qnil;
    }

    return Qnil;
}

#dataObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/chunkio/chunkio_chunk.c', line 173

static VALUE chunkio_chunk_get_data(VALUE self)
{
    chunkio_chunk *chunk;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    char *buf = NULL;
    size_t size = 0;
    int ret = cio_chunk_get_content(chunk->inner, &buf, &size);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to get data");
    }

    return rb_str_new(buf, size);
}

#metadataObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/chunkio/chunkio_chunk.c', line 125

static VALUE (VALUE self)
{
    chunkio_chunk *chunk = NULL;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    char *buf = NULL;
    size_t size = 0;

    /* cio_meta_chunk return -1 if size is 0... */
    if (cio_meta_size(chunk->inner) == 0) {
        return rb_str_new(0, 0);
    }

    int ret = cio_meta_read(chunk->inner, &buf, &size);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to get metadata");
    }

    return rb_str_new(buf, size);
}

#set_metadata(buf) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/chunkio/chunkio_chunk.c', line 109

static VALUE (VALUE self, VALUE buf)
{
    chunkio_chunk *chunk = NULL;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    ssize_t len = RSTRING_LEN(buf);
    int ret = cio_meta_write(chunk->inner, (void *)RSTRING_PTR(buf), len);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to set metadata");
    }
    return INT2NUM(len);
}

#syncObject

}



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/chunkio/chunkio_chunk.c', line 158

static VALUE chunkio_chunk_sync(VALUE self)
{
    chunkio_chunk *chunk;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    size_t ret = cio_chunk_sync(chunk->inner);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to sync");
    }
    return Qnil;
}

#tx_beginObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/chunkio/chunkio_chunk.c', line 191

static VALUE chunkio_chunk_tx_begin(VALUE self)
{
    chunkio_chunk *chunk;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    int ret = cio_chunk_tx_begin(chunk->inner);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to begin transaction");
    }

    return Qnil;
}

#tx_commitObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'ext/chunkio/chunkio_chunk.c', line 207

static VALUE chunkio_chunk_tx_commit(VALUE self)
{
    chunkio_chunk *chunk;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    int ret = cio_chunk_tx_commit(chunk->inner);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to commit transaction");
    }
    return Qnil;
}

#tx_rollbackObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'ext/chunkio/chunkio_chunk.c', line 222

static VALUE chunkio_chunk_tx_rollback(VALUE self)
{
    chunkio_chunk *chunk;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    int ret = cio_chunk_tx_rollback(chunk->inner);
    if (ret == -1) {
        rb_raise(rb_eStandardError, "failed to rollback transaction");
    }
    return Qnil;
}


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/chunkio/chunkio_chunk.c', line 35

static VALUE chunkio_chunk_unlink(VALUE self)
{
    chunkio_chunk *chunk = NULL;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    if (cio_chunk_unlink(chunk->inner) == -1) {
        rb_raise(rb_eIOError, "Failed to unlink");
    }

    chunk->closed = 1;          /* mark as closed */
    return Qnil;
}

#write(buf) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/chunkio/chunkio_chunk.c', line 76

static VALUE chunkio_chunk_write(VALUE self, VALUE buf)
{
    chunkio_chunk *chunk = NULL;
    TypedData_Get_Struct(self, chunkio_chunk, &chunkio_chunk_type, chunk);
    ssize_t len = RSTRING_LEN(buf);
    if (chunk->closed) {
        rb_raise(rb_eIOError, "IO was already closed");
    }

    cio_chunk_write(chunk->inner, (void *)RSTRING_PTR(buf), len);
    return INT2NUM(len);
}