summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components/cache/partitioner.cpp
blob: 609d42f494849fca75835b53fd43b274e246536a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

/**
 * A partitioner component implementation which forwards data access requests to a
 * dynamically selected data store component. The selection is externalized, performed
 * by a selector component, responsible for selecting the target data store given the
 * data access request key and a list of references to available data store components.
 * This pattern can be used for sharding or load balancing for example.
 */

#define WANT_HTTPD_LOG 1
#include "string.hpp"
#include "function.hpp"
#include "list.hpp"
#include "value.hpp"
#include "monad.hpp"

namespace tuscany {
namespace partitioner {

/**
 * Return the target partition for a key.
 */
const failable<lambda<value(const list<value>&)> > partition(const value& key, const lambda<value(const list<value>&)> selector, const list<value>& partitions) {

    // Call the selector component to convert the given key to a partition number
    const value p = selector(mklist<value>("get", key, partitions));
    if (isNil(p)) {
        ostringstream os;
        os << "Couldn't get partition number: " << key;
        return mkfailure<lambda<value(const list<value>&)> >(str(os), false);
    }
    return (const lambda<value(const list<value>&)>)p;
}

/**
 * Get an item from a partition.
 */
const failable<value> get(const value& key, const lambda<value(const list<value>&)> selector, const list<value>& partitions) {

    // Select partition
    const failable<lambda<value(const list<value>&)> > p = partition(key, selector, partitions);
    if (!hasContent(p))
        return mkfailure<value>(reason(p));

    // Get from selected partition
    const value val = content(p)(mklist<value>("get", key));
    if (isNil(val)) {
        ostringstream os;
        os << "Couldn't get entry from partition: " << key;
        return mkfailure<value>(str(os), false);
    }

    return val;
}

/**
 * Post an item to a partition.
 */
const failable<value> post(const value& key, const value& val, const lambda<value(const list<value>&)> selector, const list<value>& partitions) {
    const value id = append<value>(key, mklist(mkuuid()));

    // Select partition
    const failable<lambda<value(const list<value>&)> > p = partition(id, selector, partitions);
    if (!hasContent(p))
        return mkfailure<value>(reason(p));

    // Put into select partition
    content(p)(mklist<value>("put", id, val));

    return id;
}

/**
 * Put an item into a partition.
 */
const failable<value> put(const value& key, const value& val, const lambda<value(const list<value>&)> selector, const list<value>& partitions) {

    // Select partition
    const failable<lambda<value(const list<value>&)> > p = partition(key, selector, partitions);
    if (!hasContent(p))
        return mkfailure<value>(reason(p));

    // Put into selected partition
    content(p)(mklist<value>("put", key, val));

    return value(true);
}

/**
 * Delete an item from a partition.
 */
const failable<value> del(const value& key, const lambda<value(const list<value>&)> selector, const list<value>& partitions) {

    // Select partition
    const failable<lambda<value(const list<value>&)> > p = partition(key, selector, partitions);
    if (!hasContent(p))
        return mkfailure<value>(reason(p));

    // Delete from selected partition
    content(p)(mklist<value>("delete", key));

    return value(true);
}

}
}

extern "C" {

const tuscany::value apply(const tuscany::list<tuscany::value>& params) {
    const tuscany::value func(car(params));
    if (func == "get")
        return tuscany::partitioner::get(cadr(params), caddr(params), cdddr(params));
    if (func == "post")
        return tuscany::partitioner::post(cadr(params), caddr(params), cadddr(params), cddddr(params));
    if (func == "put")
        return tuscany::partitioner::put(cadr(params), caddr(params), cadddr(params), cddddr(params));
    if (func == "delete")
        return tuscany::partitioner::del(cadr(params), caddr(params), cdddr(params));
    return tuscany::mkfailure<tuscany::value>();
}

}